-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeatureextraction.py
More file actions
83 lines (68 loc) · 2.35 KB
/
featureextraction.py
File metadata and controls
83 lines (68 loc) · 2.35 KB
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
import os
import zipfile
import subprocess
import pkg_resources
from xml.etree import ElementTree as ET
PERMISSIONS_LIST = []
def ObtainPermissionList():
resource_package = __name__
resource_path = '/'.join(('', 'PERMISSION_LIST'))
Xall_path = pkg_resources.resource_filename(resource_package, resource_path)
file = open(Xall_path, "r")
global PERMISSIONS_LIST
for line in file :
line = line.split('\n')[0]
a = line.split('.')
PERMISSIONS_LIST.append(a[2])
def ExtractingManifest(name) :
try :
file_zip = zipfile.ZipFile(name)
except :
print(name)
file_zip.extract('AndroidManifest.xml', '.')
file_zip.close()
def GetManifestXML():
resource_package = __name__
resource_path = '/'.join(('', 'AXMLPrinter2.jar'))
Xall_path = pkg_resources.resource_filename(resource_package, resource_path)
result = subprocess.run(["java", "-jar",Xall_path,"AndroidManifest.xml"], stdout=subprocess.PIPE)
os.remove("AndroidManifest.xml")
return result.stdout.decode("utf-8", 'replace')
def permissionExtract(AndroidManifest):
permission = []
t = 0
try :
root = ET.fromstring(AndroidManifest)
except :
f = open("error.txt", "w+")
f.write(AndroidManifest)
permissions = root.findall("uses-permission")
icons = root.findall("android:icon")
for ic in icons :
t++
for perm in permissions :
for att in perm.attrib:
value = str(perm.attrib[att]).split('.')
permission.append(value[len(value)-1])
return permission
def CreatePermissionVector(permission):
permissionVector = [0] * 399 # 395 permissions, 1 id, 1 app size, 1 icons, 1 benign/malicious
print("**************************************" + str((len(PERMISSIONS_LIST) + 1)) + "****************************")
for perm in permission :
try :
permissionVector[(PERMISSIONS_LIST.index(perm))+1] = 1
except :
print('OK')
permissionVector[len(permissionVector)-2] = t
return permissionVector
def Extract(filename):
ExtractingManifest(filename)
return CreatePermissionVector(permissionExtract(GetManifestXML()))
def AppSizeExtract(path):
t=0
for e in os.scandir(path):
if e.is_dir():
t+=AppSizeExtract(e.path)
else:
t+=e.stat().st_size
return t/1000000 # bytes to mb