Skip to content

Commit 425816e

Browse files
authored
Add Quark Script APIs to detect CWE-94 (#373)
1 parent f7325e3 commit 425816e

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

quark/script/__init__.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,30 @@ def fullName(self) -> str:
8585
"""
8686
return self.innerObj.full_name
8787

88+
@property
89+
def className(self) -> str:
90+
"""Show the class name of the method.
91+
92+
:return: the string of the method class name
93+
"""
94+
return self.innerObj.class_name
95+
96+
@property
97+
def methodName(self) -> str:
98+
"""Show the method name of the method.
99+
100+
:return: the string of the method name
101+
"""
102+
return self.innerObj.name
103+
104+
@property
105+
def descriptor(self) -> str:
106+
"""Show the descriptor of the method.
107+
108+
:return: the string of the method descriptor
109+
"""
110+
return self.innerObj.descriptor
111+
88112

89113
class Behavior:
90114
def __init__(
@@ -218,6 +242,41 @@ def getAllStrings(self) -> List[str]:
218242
apkinfo = self.quark.apkinfo
219243
return apkinfo.get_strings()
220244

245+
def findMethodInCaller(
246+
self,
247+
callerMethod: List[str],
248+
targetMethod: List[str]
249+
) -> bool:
250+
"""
251+
Check if target method is in caller method.
252+
253+
:params callerMethod: python list contains class name,
254+
method name and descriptor of caller method.
255+
:params targetMethod: python list contains class name,
256+
method name and descriptor of target method.
257+
:return: True/False
258+
"""
259+
260+
apkinfo = self.quark.apkinfo
261+
262+
callerMethodObj = apkinfo.find_method(
263+
class_name=callerMethod[0],
264+
method_name=callerMethod[1],
265+
descriptor=callerMethod[2])
266+
267+
if not callerMethodObj:
268+
print("Caller method not Found!")
269+
raise ValueError
270+
271+
callerMethodInstance = Method(self, callerMethodObj)
272+
273+
for calleeMethod, _ in callerMethodInstance.getXrefTo():
274+
if calleeMethod.innerObj.class_name == targetMethod[0] and \
275+
calleeMethod.innerObj.name == targetMethod[1] and \
276+
calleeMethod.innerObj.descriptor == targetMethod[2]:
277+
return True
278+
return False
279+
221280

222281
def runQuarkAnalysis(samplePath: PathLike, ruleInstance: Rule) -> QuarkResult:
223282
"""Given detection rule and target sample, this instance runs the basic

tests/script/test_script.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,22 @@ def testMethodGetXrefFrom(QUARK_ANALYSIS_RESULT):
230230
def testgetAllStrings(QUARK_ANALYSIS_RESULT):
231231
assert len(QUARK_ANALYSIS_RESULT.getAllStrings()) == 1005
232232

233+
@staticmethod
234+
def testfindMethodInCaller(QUARK_ANALYSIS_RESULT):
235+
callerMethod = [
236+
"Lcom/google/progress/WifiCheckTask;",
237+
"checkWifiCanOrNotConnectServer",
238+
"([Ljava/lang/String;)Z",
239+
]
240+
targetMethod = [
241+
"Landroid/util/Log;",
242+
"e",
243+
"(Ljava/lang/String; Ljava/lang/String;)I",
244+
]
245+
246+
assert QUARK_ANALYSIS_RESULT.findMethodInCaller(
247+
callerMethod, targetMethod)
248+
233249

234250
def testRunQuarkAnalysis(SAMPLE_PATH):
235251
ruleset = Ruleset(RULE_FOLDER_PATH)

0 commit comments

Comments
 (0)