-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextract_api.py
90 lines (74 loc) · 2.19 KB
/
extract_api.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
#/usr/bin/python
#coding=utf-8
import sys,os
import shutil
import zipfile
from xml.dom import minidom
import MySQLdb
#get all the smali files
def get_all_files(file_dir):
for file in os.listdir(file_dir):
if os.path.isfile(file_dir+"/"+file):
files.append(file_dir+"/"+file)
else:
if file_dir=="apk/smali" or file_dir.find("com")!=-1:
get_all_files(file_dir+"/"+file)
file_name = "1.apk"
file_dir = "apk/smali"
#get MainActivity name
AndroidManifest = minidom.parse("apk/AndroidManifest.xml")
MainActivity = ""
for activity in AndroidManifest.getElementsByTagName("activity"):
activity_name = activity.attributes["android:name"].value
if len(activity.getElementsByTagName("action")) > 0:
for temp in activity.getElementsByTagName("action"):
if temp.attributes["android:name"].value=="android.intent.action.MAIN":
MainActivity = activity_name
#get all smali files path
files = []
get_all_files(file_dir)
methods = {}
for file in files:
f = open(file)
while 1:
lines = f.readlines(100000)
if not lines:
break;
for line in lines:
if line.find("invoke-")!=-1:
method_name = line.split(",")[len(line.split(","))-1].strip("\n")
method_name = method_name.replace("/",".")
method_name = method_name.replace(";->",".")
method_name = method_name[method_name.find('L')+1:]
method_name = method_name[:method_name.find(')')+1]
method_name = method_name.replace("(L","(")
method_name = method_name.replace(";L",",")
method_name = method_name.replace(";)",")")
counts = methods.get(method_name,0)+1
methods.update({method_name:counts})
methods_string = ""
for k,v in methods.items():
methods_string += "%s:%d;" %(k,v)
#update database
#connect MySQL
conn= MySQLdb.connect(
host='localhost',
port = 3306,
user='root',
passwd='root',
db ='Android',
)
cur = conn.cursor()
#is already exist
cur.execute("select count(id) from malware where name='%s'" %(MainActivity))
already_exist = cur.fetchone()
if(already_exist[0]>0):
sql = "update malware set apis='%s' where name='%s'" %(methods_string,MainActivity)
cur.execute(sql)
else:
print("not exist")
exit()
cur.close()
conn.commit()
conn.close()
print("over")