Skip to content

Commit 5bf0dbc

Browse files
committed
Make variable names and docstring more readable
1 parent 236c1b5 commit 5bf0dbc

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

quark/script/frida/__init__.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# This file is part of Quark-Engine - https://github.com/quark-engine/quark-engine
33
# See the file 'LICENSE' for copying permission.
44

5+
from ctypes import Union
56
import functools
67
import json
78
import re
@@ -17,6 +18,8 @@
1718
from frida.core import Device
1819
from frida.core import Session as FridaSession
1920

21+
MethodCallEvent = Dict[str, Union[List[str], str]]
22+
2023

2124
class MethodCallEventDispatcher:
2225
def __init__(self, frida: FridaSession) -> None:
@@ -29,20 +32,20 @@ def _getMethodIdentifier(targetMethod: str, paramType: str):
2932

3033
def startWatchingMethodCall(
3134
self, targetMethod: str, methodParamTypes: str
32-
) -> List["Behavior"]:
35+
) -> List[MethodCallEvent]:
3336
"""Start tracking calls to the target method.
3437
3538
:param targetMethod: the target API
3639
:param methodParamTypes: the parameter types of the target API
3740
:return: python list that holds calls to the target method
3841
"""
39-
messageBuffer = []
42+
eventBuffer = []
4043
methodId = self._getMethodIdentifier(targetMethod, methodParamTypes)
4144

42-
self.watchedMethods[methodId] = messageBuffer
45+
self.watchedMethods[methodId] = eventBuffer
4346
self.script.exports.watch_method_call(targetMethod, methodParamTypes)
4447

45-
return messageBuffer
48+
return eventBuffer
4649

4750
def stopWatchingMethodCall(
4851
self, targetMethod: str, methodParamTypes: str
@@ -80,9 +83,17 @@ def receiveMessage(self, messageFromFridaAgent: dict, _) -> None:
8083

8184

8285
@functools.lru_cache
83-
def _setupFrida(
86+
def _spawnApp(
8487
appPackageName: str, protocol="usb", **kwargs: Any
8588
) -> Tuple[Device, FridaSession, int]:
89+
"""Spawn the target APP with Frida
90+
91+
:param appPackageName: the package name of the target APP
92+
:param protocol: string that holds the protocol to communicate with the
93+
Frida server, defaults to "usb"
94+
:return: tuple containing the device ID, the Frida instance and the process
95+
ID of the APP.
96+
"""
8697
device = None
8798
if protocol == "usb":
8899
device = frida.get_usb_device(**kwargs)
@@ -99,6 +110,12 @@ def _setupFrida(
99110

100111
@functools.lru_cache
101112
def _injectAgent(frida: FridaSession) -> MethodCallEventDispatcher:
113+
"""Inject a Frida agent to help track method calls.
114+
115+
:param frida: Frida instance to be injected
116+
:return: dispatcher that stores the captured calls to the appropriate
117+
buffers
118+
"""
102119
dispatcher = MethodCallEventDispatcher(frida)
103120

104121
pathToFridaAgentSource = pkg_resources.resource_filename(
@@ -116,7 +133,7 @@ def _injectAgent(frida: FridaSession) -> MethodCallEventDispatcher:
116133

117134
@dataclass
118135
class Behavior:
119-
_message: Dict[str, str]
136+
_callEvent: MethodCallEvent
120137

121138
def hasString(self, pattern: str, regex: bool = False) -> List[str]:
122139
"""Check if the behavior contains strings
@@ -153,12 +170,12 @@ def getParamValues(self) -> List[str]:
153170
154171
:return: python list containing parameter values
155172
"""
156-
return self._message["paramValues"]
173+
return self._callEvent["paramValues"]
157174

158175

159176
@dataclass
160177
class FridaResult:
161-
_messageBuffer: List[str]
178+
_eventBuffer: List[MethodCallEvent]
162179

163180
@property
164181
def behaviorOccurList(self) -> List[Behavior]:
@@ -167,7 +184,7 @@ def behaviorOccurList(self) -> List[Behavior]:
167184
168185
:return: detected behavior instance
169186
"""
170-
return [Behavior(message) for message in self._messageBuffer]
187+
return [Behavior(message) for message in self._eventBuffer]
171188

172189

173190
def runFridaHook(
@@ -178,20 +195,22 @@ def runFridaHook(
178195
) -> FridaResult:
179196
"""Track calls to the specified method for given seconds.
180197
181-
:param apkPackageName: the target APK
198+
:param apkPackageName: the package name of the target APP
182199
:param targetMethod: the target API
183200
:param methodParamTypes: string that holds the parameters used by the
184201
target API
185-
:param secondToWait: seconds to wait for method calls
202+
:param secondToWait: seconds to wait for method calls, defaults to 10
186203
:return: FridaResult instance
187204
"""
188-
device, frida, appProcess = _setupFrida(apkPackageName)
205+
device, frida, appProcess = _spawnApp(apkPackageName)
189206
dispatcher = _injectAgent(frida)
190207

191-
buffer = dispatcher.startWatchingMethodCall(targetMethod, methodParamTypes)
208+
eventBuffer = dispatcher.startWatchingMethodCall(
209+
targetMethod, methodParamTypes
210+
)
192211
device.resume(appProcess)
193212

194213
sleep(secondToWait)
195214
dispatcher.stopWatchingMethodCall(targetMethod, methodParamTypes)
196215

197-
return FridaResult(buffer)
216+
return FridaResult(eventBuffer)

0 commit comments

Comments
 (0)