-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd_hook.py
170 lines (133 loc) · 5.62 KB
/
cmd_hook.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
160
161
162
163
164
165
166
167
168
169
170
# !/usr/bin/python3
# -*- coding: utf8 -*-
# author: moyichen
# date: 2020/6/1
from pprint import pformat
from AndroidDevice import AndroidDevice
from HookLog import HookLog
from HookScriptGenerator import genFridaAgentScript
from Hookee import Hookee
from FridaHooker import FridaHooker
from agent import AgentConfig, Agent
from utils import *
from progressbar import *
@click.command()
@click.option('--filename', '-f', help='the log file name.', required=False)
@click.option('--output', '-o', help='the report file name.', required=False)
def report(filename, output):
hooklog = HookLog(filename)
if output is None:
output = filename
report_filename = '{}.html'.format(os.path.splitext(output)[0])
hooklog.gen_report(report_filename, auto_open=True)
json_filename = "{}.speedscope.json".format(os.path.splitext(output)[0])
hooklog.gen_speedscope_json(json_filename, True)
@click.command()
@click.option('--app', '-p', required=True, help='package name')
@click.option('--function', '-i', help='function name. support regex e.g. `-i ".*click.*[#libXXX.so]"`. '
'If not set, it will use --filename.', required=False, multiple=True)
@click.option('--backtrace', '-b', help='catch the call backtrace.', default=False, is_flag=True)
@click.option('--update-so', '-u', help='Update the so from remote device.', default=False, is_flag=True)
@click.option('--auto-start', '-a', help='auto start hook.', default=False, is_flag=True)
@click.option('--engine', help='hook engine xxx or frida.', default='frida')
@click.option('--clip-begin', help='clip begin tag.')
@click.option('--clip-end', help='clip end tag.')
@click.option('--debug', '-d', help='Enable the debug mode.', default=False, is_flag=True)
@click.option('--filename', '-f', help='get config from file.', required=False)
@click.option('--restart', '-r', help='restart app.', default=False, is_flag=True)
def hook(app, function, backtrace, update_so, auto_start, engine, clip_begin, clip_end, debug, filename, restart):
"""
hook functions
"""
device = AndroidDevice.get_device()
hookee = Hookee(app, debug, device)
log_info(function)
log_info(backtrace)
log_info("hook: {} {}".format(app, hookee.version))
if engine == 'frida':
hooker = FridaHooker(hookee, device, auto_start)
else:
log_exit("Do not support {}".format(engine))
hooker.setDebugMode(debug)
if update_so:
hooker.download_miss_so()
if filename:
hooker.gen_config_from_file(filename, debug)
else:
hooker.gen_config(function, backtrace)
if restart:
hooker.stop_app()
hooker.clear()
hooker.prepare()
hooker.start_hook()
hooker.stop_hook()
hooker.clip(clip_begin, clip_end)
hooker.gen_report()
hooker.clear()
@click.command()
@click.option('--app', '-p', help='package name')
@click.option('--update-so', '-u', help='Update the so from remote device.', default=False, is_flag=True)
@click.option('--auto-start', '-a', help='auto start hook.', default=False, is_flag=True)
@click.option('--engine', help='hook engine xxx or frida.', default='frida')
@click.option('--clip-begin', help='clip begin tag.')
@click.option('--clip-end', help='clip end tag.')
@click.option('--debug', '-d', help='Enable the debug mode.', default=False, is_flag=True)
@click.option('--filename', '-f', help='get config from file.', required=False)
@click.option('--restart', '-r', help='restart app.', default=False, is_flag=True)
def fps(app, update_so, auto_start, engine, clip_begin, clip_end, debug, filename, restart):
"""
hook functions
"""
device = AndroidDevice.get_device()
hookee = Hookee(app, debug, device)
if engine == 'frida':
hooker = FridaHooker(hookee, device, auto_start)
else:
log_exit("Do not support {}".format(engine))
hooker.setDebugMode(debug)
if update_so:
hooker.download_miss_so()
if filename:
hooker.gen_config_from_file(filename, debug)
else:
hooker.gen_config(['eglSwapBuffers#libEGL.so'], backtrace=False)
if restart:
hooker.stop_app()
hooker.clear()
hooker.prepare()
hooker.start_hook()
hooker.stop_hook()
hooker.clip(clip_begin, clip_end)
hooker.gen_report(needPie=True, isDuration=False)
hooker.clear()
@click.command()
@click.argument('symbol_dir')
@click.option('--function', '-i', help='function name. support regex e.g. `-i ".*click.*[#libXXX.so]"`. '
'If not set, it will use --filename.', required=False, multiple=True)
@click.option('--backtrace', '-b', help='catch the call backtrace.', default=False, is_flag=True)
@click.option('--filename', '-f', help='get config from file.', default='./frida.js', required=False)
def script(symbol_dir, function, backtrace, filename):
"""
generate frida js
"""
js = genFridaAgentScript(function, symbol_dir, backtrace)
with open(filename, 'w+') as f:
f.write(js)
@click.command()
@click.option('--app', '-p', required=True, help='package name')
@click.option('--filename', '-f', help='get config from file.', required=True)
@click.option('--restart', '-r', help='restart app.', default=False, is_flag=True)
@click.option('--symbol-dir', '-s', help='the symbol directory.', default=None)
def hook2(app, filename, restart, symbol_dir):
"""
hook functions
"""
c = AgentConfig(name=app, spawn=restart, symbol_dir=symbol_dir)
a = Agent(c)
a.run()
a.attach_script_file(filename)
a.resume()
log_info("start tracing. press any key to stop.")
sys.stdin.read(1)
if __name__ == '__main__':
pass