forked from Rorschach123/DexScanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DexFormDetect.py
159 lines (136 loc) · 8.03 KB
/
DexFormDetect.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
__author__ = 'Rorschach'
import DexFormAnalyzer
class DexDetect:
className = ""
dexLoader = None
def __init__(self):
self.className = "Dex Detect"
self.dexLoader = None
def SetDexLoaderObj(self,dexLoader):
self.dexLoader = dexLoader
def CheckingInsAndTypeValue(self,insNum,ins,dexHeader,insValue,strDes):
str = ""
if insNum % 2 == 0 and ins[insNum] == insValue:
# print self.dexLoader.dexClassName[codeNum] + "--" + self.dexLoader.dexMethodName[codeNum]
typeNum = (ins[insNum+2] & 0xff) | ((ins[insNum+3] << 8) & 0xff00)
if typeNum > dexHeader.typeIdxSize:
return 0
typeIdxOffset = typeNum * 4 + dexHeader.typeIdxOff
stringIdx = DexFormAnalyzer.endianToNormal(DexFormAnalyzer.stringListToIntList(self.dexLoader.mapfile[typeIdxOffset:typeIdxOffset+4],4),4) * 4 + dexHeader.stringIdxOff
stringIdxImp = DexFormAnalyzer.endianToNormal(DexFormAnalyzer.stringListToIntList(self.dexLoader.mapfile[stringIdx:stringIdx+4],4),4)
stringlen = ord(self.dexLoader.mapfile[stringIdxImp])
for strNum in range(0,stringlen):
str += self.dexLoader.mapfile[stringIdxImp+strNum+1]
if str == strDes:
return 1
else:
return 0
def CheckingInsAndClassValue(self,insNum,ins,dexHeader,insValue,methodName,className):
str = ""
classStr = ""
if insNum % 2 == 0 and ins[insNum] == insValue:
methodNum = (ins[insNum+2] & 0xff) | ((ins[insNum+3] << 8) & 0xff00)
if methodNum > dexHeader.methodIdsSize:
return 0
methodIdxOffset = methodNum * 8 + dexHeader.methodIdsOff
stringIdx = DexFormAnalyzer.endianToNormal(DexFormAnalyzer.stringListToIntList(self.dexLoader.mapfile[methodIdxOffset+4:methodIdxOffset+8],4),4) * 4 + dexHeader.stringIdxOff
stringIdxImp = DexFormAnalyzer.endianToNormal(DexFormAnalyzer.stringListToIntList(self.dexLoader.mapfile[stringIdx:stringIdx+4],4),4)
# if stringIdxImp > len(self.dexLoader.mapfile):
# return 0
stringlen = ord(self.dexLoader.mapfile[stringIdxImp])
for strNum in range(0,stringlen):
str += self.dexLoader.mapfile[stringIdxImp+strNum+1]
if str != methodName:
return 0
#type idx
classStr = ""
typeNum = DexFormAnalyzer.endianToNormal(DexFormAnalyzer.stringListToIntList(self.dexLoader.mapfile[methodIdxOffset+0:methodIdxOffset+2],2),2)
classNameOffset = DexFormAnalyzer.endianToNormal(DexFormAnalyzer.stringListToIntList(self.dexLoader.mapfile[dexHeader.typeIdxOff + 4 * typeNum:dexHeader.typeIdxOff + 4 * typeNum + 2],2),2)
classNameStrOffset = classNameOffset * 4 + dexHeader.stringIdxOff
stringIdxImp = DexFormAnalyzer.endianToNormal(DexFormAnalyzer.stringListToIntList(self.dexLoader.mapfile[classNameStrOffset:classNameStrOffset+4],4),4)
stringlen = ord(self.dexLoader.mapfile[stringIdxImp])
for strNum in range(0,stringlen):
classStr += self.dexLoader.mapfile[stringIdxImp+strNum+1]
if classStr == className:
return 1
else:
return 0
def CheckingInsAndMethodValue(self,insNum,ins,dexHeader,insValue,strDes):
str = ""
if insNum % 2 == 0 and ins[insNum] == insValue:
methodNum = (ins[insNum+2] & 0xff) | ((ins[insNum+3] << 8) & 0xff00)
if methodNum > dexHeader.methodIdsSize:
return 0
methodIdxOffset = methodNum * 8 + dexHeader.methodIdsOff
stringIdx = DexFormAnalyzer.endianToNormal(DexFormAnalyzer.stringListToIntList(self.dexLoader.mapfile[methodIdxOffset+4:methodIdxOffset+8],4),4) * 4 + dexHeader.stringIdxOff
stringIdxImp = DexFormAnalyzer.endianToNormal(DexFormAnalyzer.stringListToIntList(self.dexLoader.mapfile[stringIdx:stringIdx+4],4),4)
# if stringIdxImp > len(self.dexLoader.mapfile):
# return 0
stringlen = ord(self.dexLoader.mapfile[stringIdxImp])
for strNum in range(0,stringlen):
str += self.dexLoader.mapfile[stringIdxImp+strNum+1]
if str == strDes:
return 1
else:
return 0
def CheckingInsAndIncludeStrValue(self,insNum,ins,dexHeader,insValue,strDes):
str = ""
if insNum % 2 == 0 and ins[insNum] == insValue:
# print self.dexLoader.dexClassName[codeNum] + "--" + self.dexLoader.dexMethodName[codeNum]
strNum = (ins[insNum+2] & 0xff) | ((ins[insNum+3] << 8) & 0xff00)
if strNum > dexHeader.stringIdxSize:
return 0
stringIdx = strNum * 4 + dexHeader.stringIdxOff
stringIdxImp = DexFormAnalyzer.endianToNormal(DexFormAnalyzer.stringListToIntList(self.dexLoader.mapfile[stringIdx:stringIdx+4],4),4)
stringlen = ord(self.dexLoader.mapfile[stringIdxImp])
for strNum in range(0,stringlen):
str += self.dexLoader.mapfile[stringIdxImp+strNum+1]
if strDes in str:
return 1
else:
return 0
def CheckingInsAndStrValue(self,insNum,ins,dexHeader,insValue,strDes):
str = ""
if insNum % 2 == 0 and ins[insNum] == insValue:
# print self.dexLoader.dexClassName[codeNum] + "--" + self.dexLoader.dexMethodName[codeNum]
strNum = (ins[insNum+2] & 0xff) | ((ins[insNum+3] << 8) & 0xff00)
if strNum > dexHeader.stringIdxSize:
return 0
stringIdx = strNum * 4 + dexHeader.stringIdxOff
stringIdxImp = DexFormAnalyzer.endianToNormal(DexFormAnalyzer.stringListToIntList(self.dexLoader.mapfile[stringIdx:stringIdx+4],4),4)
stringlen = ord(self.dexLoader.mapfile[stringIdxImp])
for strNum in range(0,stringlen):
str += self.dexLoader.mapfile[stringIdxImp+strNum+1]
if str == strDes:
return 1
else:
return 0
def DetectApkApi(self,dexHeader):
#Loop All method code to check
print "--Detc-- Start Checking if has Some Api"
countFindDexClassLoader = 0
countFindDexClassLoaderPossible = 0
for codeNum in range(0,len(self.dexLoader.dexMethodCodes)):
ins = self.dexLoader.dexMethodCodes[codeNum].insns
resultDexClassLoader = [0,0]
for insNum in range(0,len(ins) - 2):
resultDexClassLoader = self.DetectDexClassLoader(dexHeader,insNum,ins,resultDexClassLoader)
if resultDexClassLoader[0] > 0 and resultDexClassLoader[1] > 0:
countFindDexClassLoader += 1
print "--Detc-- DexClassLoader : ClassName = " + self.dexLoader.dexClassName[codeNum] + " MethodName = " + self.dexLoader.dexMethodName[codeNum]
else:
if resultDexClassLoader[0] > 0:
countFindDexClassLoaderPossible += 1
print "--Detc-- Possible_DexClassLoader : ClassName = " + self.dexLoader.dexClassName[codeNum] + " MethodName = " + self.dexLoader.dexMethodName[codeNum]
if countFindDexClassLoader != 0:
print "--Detc-- Find DexClassLoader Api,Count number = %d" %(countFindDexClassLoader)
else:
print "--Detc-- Not Found DexClassLoader Api"
if countFindDexClassLoaderPossible != 0:
print "--Detc-- Find Possible DexClassLoader Api,Count number = %d" %(countFindDexClassLoaderPossible)
def DetectDexClassLoader(self,dexHeader,insNum,ins,resultDexClassLoader):
#init dexclassloader
#loadClass
resultDexClassLoader[0] += self.CheckingInsAndClassValue(insNum,ins,dexHeader,0x70,"<init>","Ldalvik/system/DexClassLoader;")
resultDexClassLoader[1] += self.CheckingInsAndMethodValue(insNum,ins,dexHeader,0x6E,"loadClass")
return resultDexClassLoader