-
Notifications
You must be signed in to change notification settings - Fork 3
/
thesis_flow_script.py
executable file
·90 lines (87 loc) · 3.7 KB
/
thesis_flow_script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import zipfile
import os, sys
import shutil
import glob
decompile_path = ("C:\\Users\\islab718A\\Desktop\\PARFORTHESIS\\dex2jar-2.0")
normal_app_path = ("C:\\Users\\islab718A\\Desktop\\PARFORTHESIS\\Malware_APP")
Malware_app_path = ("C:\\Users\\islab718A\\Desktop\\PARFORTHESIS\\Malware_APP")
jarfile_save_path = ("C:\\Users\\islab718A\\Desktop\\PARFORTHESIS\\jarfile")
ExtractionOfZip_path = ("C:\\Users\\islab718A\\Desktop\\PARFORTHESIS\\ExtractionOfZip")
JAVAStoration_path = ("C:\\Users\\islab718A\\Desktop\\PARFORTHESIS\\store_each_java")
ToDoJAD_path = ("C:\\Users\\islab718A\\Desktop\\PARFORTHESIS\\to_do_jad")
NormalFinalJavaPath = ("C:\\Users\\islab718A\\Desktop\\PARFORTHESIS\\final_all_java_malware")
print "copy normal_apk to folder..."
for foldername in os.listdir(normal_app_path):
for filename in os.listdir(normal_app_path+"\\"+foldername):
if os.path.exists(JAVAStoration_path+"\\"+filename.replace(".apk", "")):
continue
else:
#copy to dex2jar part
src = (normal_app_path+"\\"+foldername+"\\"+filename)
dst = (decompile_path)
shutil.copy2(src, dst)
#execute dex2jar part
os.chdir(decompile_path)
os.system("d2j-dex2jar.bat "+filename)
os.remove(filename)
jar_name = filename.replace(".apk", "-dex2jar.jar")
jar2zip_name = jar_name.replace(".jar", ".zip")
try:
os.rename(decompile_path + "\\" + jar_name, jarfile_save_path + "\\" + jar2zip_name)
except WindowsError, e:
continue
#extract all zip to one folder
zip_ref = zipfile.ZipFile(jarfile_save_path + "\\" + jar2zip_name, 'r')
zip_ref.extractall(ExtractionOfZip_path + "\\" + filename.replace(".apk", ""))
zip_ref.close()
os.remove(jarfile_save_path + "\\" + jar2zip_name)
#run through all class in filename folder, then for each using jad
for root, dirs, files in os.walk(ExtractionOfZip_path + "\\" +filename.replace(".apk", "")):#root is class's path
for file in files:
if file.endswith('.class'):
if not os.path.exists(JAVAStoration_path+"\\"+filename.replace(".apk", "")):
os.makedirs(JAVAStoration_path+"\\"+filename.replace(".apk", ""))
o = 0
while True:
iu = 0
if os.path.exists(ToDoJAD_path+"\\"+str(o)+file):
o = o + 1
else:
try:
os.rename(root+"\\"+file, ToDoJAD_path+"\\"+str(o)+file)
break
except WindowsError, e:
iu = 1
break
if iu == 1:
continue
os.chdir(ToDoJAD_path)
g = 0
while True:
if os.path.exists(str(o)+file.replace(".class",str(g) + ".java")):
g = g + 1
else:
os.system("jad -p "+str(o)+file+" > "+str(o)+file.replace(".class",str(g) + ".java"))
break
k=0
while True:
if os.path.exists(JAVAStoration_path + "\\" + filename.replace(".apk", "") + "\\" +str(o)+ file.replace(".class",str(g)+ str(k)+".java")):
k = k + 1
else:
os.rename(str(o)+file.replace(".class",str(g) + ".java"), JAVAStoration_path + "\\" + filename.replace(".apk", "") + "\\" + str(o)+file.replace(".class",str(g)+ str(k)+".java"))
break
#os.remove(file)
os.chdir(decompile_path)##if
#all java code in one file
os.chdir(JAVAStoration_path+"\\"+filename.replace(".apk", ""))
try:
os.remove(NormalFinalJavaPath+"\\"+ filename.replace(".apk", ".txt"))
except OSError:
pass
with open(NormalFinalJavaPath+"\\"+ filename.replace(".apk", ".txt"), 'w') as outfile:
for fname in os.listdir(JAVAStoration_path+"\\"+filename.replace(".apk", "")):
with open(fname) as infile:
for line in infile:
if 'Copyright' not in line and 'Jad' not in line and 'options' not in line and '//' not in line:
outfile.write(line)
print "finish copy normal_apk to folder"