Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added --watch and friends to notify #581

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion agent/src/android/hooking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,36 @@ const getPatternType = (pattern: string): PatternType => {
return PatternType.Klass;
};

export const lazyWatchForPattern = (query: string): void => {
export const lazyWatchForPattern = (query: string, watch: boolean, dargs: boolean, dret: boolean, dbt: boolean): void => {
// TODO: Use param to control interval
let found = false;
const job: IJob = {
identifier: jobs.identifier(),
implementations: [],
type: `notify-class for: ${query}`,
};

// This method loops over all enumerate matches and then calls watch
// with the arguments specified in the parent function
const watchMatches = (matches: Java.EnumerateMethodsMatchGroup[]) => {
matches.forEach(match => {
match.classes.forEach(_class => {
_class.methods.forEach(_method => {
watchMethod(_class.name + "." + _method, job, dargs, dbt, dret);
})
})
})
}

// Check if the pattern is found before starting an interval
javaEnumerate(query).then(matches => {
if (matches.length > 0) {
found = true;
send(`${c.green(query)} is already loaded / available`);
if (watch) {
watchMatches(matches);
jobs.add(job);
}
}
});

Expand All @@ -87,6 +108,10 @@ export const lazyWatchForPattern = (query: string): void => {
if (!found && matches.length > 0) {
send(`${c.green(query)} is now available`);
found = true;
if (watch) {
watchMatches(matches);
jobs.add(job);
}
}

if (found) clearInterval(interval);
Expand Down
2 changes: 1 addition & 1 deletion agent/src/rpc/android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const android = {
androidHookingWatch: (pattern: string, watchArgs: boolean, watchBacktrace: boolean, watchRet: boolean): Promise<void> =>
hooking.watch(pattern, watchArgs, watchBacktrace, watchRet),
androidHookingEnumerate: (query: string): Promise<Java.EnumerateMethodsMatchGroup[]> => hooking.javaEnumerate(query),
androidHookingLazyWatchForPattern: (query: string): void => hooking.lazyWatchForPattern(query),
androidHookingLazyWatchForPattern: (query: string, watch: boolean, dargs: boolean, dret: boolean, dbt: boolean): void => hooking.lazyWatchForPattern(query, watch, dargs, dret, dbt),

// android heap methods
androidHeapEvaluateHandleMethod: (handle: number, js: string): Promise<void> => heap.evaluate(handle, js),
Expand Down
20 changes: 19 additions & 1 deletion objection/commands/android/hooking.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ def _should_dump_backtrace(args: list = None) -> bool:
return '--dump-backtrace' in args


def _should_watch(args: list = None) -> bool:
"""
Check if --dump-args is part of the arguments.

:param args:
:return:
"""

return '--watch' in args


def _should_dump_args(args: list = None) -> bool:
"""
Check if --dump-args is part of the arguments.
Expand Down Expand Up @@ -207,7 +218,14 @@ def notify(args: list = None) -> None:
return

api = state_connection.get_api()
api.android_hooking_lazy_watch_for_pattern(query)
should_watch = _should_watch(args)
dump_arguments = _should_dump_args(args)
dump_backtrace = _should_dump_backtrace(args)
dump_return = _should_dump_return_value(args)
api.android_hooking_lazy_watch_for_pattern(query,
should_watch, dump_arguments,
dump_return,
dump_backtrace)


def watch(args: list = None) -> None:
Expand Down
4 changes: 3 additions & 1 deletion objection/console/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,9 @@
},
'notify': {
'meta': 'Notify when a class becomes available',
'exec': android_hooking.notify
'exec': android_hooking.notify,
'flags': ['--dump-args', '--dump-return', '--dump-backtrace', '--watch']

},
'generate': {
'meta': 'Generate Frida hooks for Android',
Expand Down