-
Notifications
You must be signed in to change notification settings - Fork 31
/
strongarm-cli.py
322 lines (257 loc) · 11.3 KB
/
strongarm-cli.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import argparse
import logging
import pathlib
import sys
from typing import List, Text
from strongarm.cli.utils import (
disassemble_function,
disassemble_method,
pick_macho_slice,
print_analyzer_classes,
print_analyzer_exported_symbols,
print_analyzer_imported_symbols,
print_analyzer_methods,
print_analyzer_protocols,
print_binary_info,
print_binary_load_commands,
print_binary_sections,
print_binary_segments,
print_raw_strings,
print_selector,
)
from strongarm.logger import strongarm_logger
from strongarm.macho import BinaryEncryptedError, MachoAnalyzer, MachoBinary, MachoParser, VirtualMemoryPointer
def print_header(args: argparse.Namespace) -> None:
header_lines = ["strongarm - Mach-O analyzer", f"{args.binary_path}"]
# find the longest line
longest_line_len = max(map(len, header_lines))
# add a line of hyphens, where the hyphen count matches the longest line
header_lines.append("-" * longest_line_len)
# print header
print()
for line in header_lines:
print(line)
print()
class InfoCommand:
def __init__(self, binary: MachoBinary, analyzer: MachoAnalyzer):
self.binary = binary
self.analyzer = analyzer
self.commands = {
"all": (self.run_all_commands, None),
"metadata": (print_binary_info, self.binary),
"segments": (print_binary_segments, self.binary),
"sections": (print_binary_sections, self.binary),
"loads": (print_binary_load_commands, self.binary),
"classes": (print_analyzer_classes, self.analyzer),
"protocols": (print_analyzer_protocols, self.analyzer),
"methods": (print_analyzer_methods, self.analyzer),
"imports": (print_analyzer_imported_symbols, self.analyzer),
"exports": (print_analyzer_exported_symbols, self.analyzer),
"strings": (print_raw_strings, self.binary),
}
def description(self) -> str:
return "Read binary information. info " + " ".join(f"[{cmd}]" for cmd in self.commands)
def run_all_commands(self) -> None:
for cmd in self.commands:
if cmd == "all":
continue
self.run_command(cmd)
def run_command(self, cmd: Text) -> None:
if cmd == "all":
self.run_all_commands()
return
if cmd not in self.commands:
print(f"Unknown argument supplied to info: {cmd}")
return
func, arg = self.commands[cmd]
func(arg) # type: ignore
class StrongarmShell:
def __init__(self, binary: MachoBinary, analyzer: MachoAnalyzer):
self.binary = binary
self.analyzer = analyzer
self.commands = {
"help": ("Display available commands", self.help),
"exit": ("Exit interactive shell", self.exit),
"info": (InfoCommand(self.binary, self.analyzer).description(), self.info),
"sels": ("List selectors implemented by a class. sels [class]", self.selectors),
"disasm": ("Decompile a given selector. disasm [sel]", self.disasm),
"disasm_f": ("Decompile a given selector. disasm [sel]", self.disasm_f),
"dump": ("Hex dump a memory address. dump [size] [virtual address]", self.dump_memory),
}
print("strongarm interactive shell")
print("Type 'help' for available commands.")
self.active = True
def dump_memory(self, args: List[str]) -> None:
def err() -> None:
print("Usage: dump [size] [virtual address]")
return
if len(args) < 2:
return err()
try:
dump_size = int(args[0], 10)
address = int(args[1], 16)
except ValueError as e:
print(f"Failed to interpret address: {e}")
return err()
binary_data = self.binary.get_content_from_virtual_address(VirtualMemoryPointer(address), dump_size)
# split to 16 byte regions
region_size = 16
current_index = 0
while True:
if current_index >= dump_size:
break
# grab the next grouping of bytes
byte_region = binary_data[current_index : current_index + region_size]
region_start = address + current_index
region_start_str = hex(region_start)
print(region_start_str, end="\t\t")
ascii_rep = "|"
for idx, byte in enumerate(byte_region):
print("{:02x}".format(byte), end=" ")
# indent every 8 bytes
if idx > 0 and (idx + 1) % 8 == 0:
print("\t", end="")
ascii_byte = chr(byte) if 32 <= byte < 127 else "."
ascii_rep += ascii_byte
ascii_rep += "|"
print(ascii_rep)
current_index += region_size
def selectors(self, args: List[str]) -> None:
if not len(args):
print("Usage: sels [class]")
return
class_name = args[0]
objc_classes = [x for x in self.analyzer.objc_classes() if x.name == class_name]
if not len(objc_classes):
print(f"Unknown class '{class_name}'. Run 'info classes' for a list of implemented classes.")
return
objc_class = objc_classes[0]
for sel in objc_class.selectors:
print_selector(objc_class, sel)
def disasm(self, args: List[str]) -> None:
if not len(args):
print("Usage: disasm [sel]")
return
sel_name = args[0]
matching_sels = [x for x in self.analyzer.get_objc_methods() if x.objc_sel.name == sel_name]
if not len(matching_sels):
print(f"Unknown selector '{sel_name}'. Run 'info methods' for a list of selectors.")
return
disassembled_str = disassemble_method(self.binary, matching_sels[0])
print(disassembled_str)
def disasm_f(self, args: List[str]) -> None:
if not len(args):
print("Usage: disasm [sel]")
return
disassembled_str = disassemble_function(self.binary, VirtualMemoryPointer(args[0], 16))
print(disassembled_str)
def help(self, _args: List[str]) -> None:
print("Commands")
print("----------------")
for name, (description, funcptr) in self.commands.items():
print(f"{name}: {description}")
def info(self, args: List[str]) -> None:
info_cmd = InfoCommand(self.binary, self.analyzer)
if not len(args):
print("No option provided")
print(info_cmd.description())
for option in args:
info_cmd.run_command(option)
def exit(self, _args: List[str]) -> None:
print("Quitting...")
self.active = False
def run_command(self, user_input: Text) -> bool:
components = user_input.split(" ")
cmd_name = components[0]
cmd_args = components[1:]
if cmd_name not in self.commands:
print(f"Unknown command: '{cmd_name}'. Type 'help' for available commands.")
return self.active
func = self.commands[cmd_name][1]
func(cmd_args)
return self.active
def process_command(self) -> bool:
user_input = input("strongarm$ ")
return self.run_command(user_input)
def strongarm_script(binary: MachoBinary, analyzer: MachoAnalyzer) -> None:
"""If you want to run a script instead of using the CLI, write it here and change `script` to `True` in main()."""
def main(args: argparse.Namespace) -> None:
# XXX(PT): Change this if you want to run a quick script! Write it in strongarm_script()
script = False
# end of config
def configure_logger() -> None:
root = strongarm_logger.getChild(__file__)
root.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
ch.setFormatter(formatter)
root.addHandler(ch)
configure_logger()
if args.verbose:
strongarm_logger.setLevel(logging.DEBUG)
print_header(args)
parser = MachoParser(pathlib.Path(args.binary_path))
# print slice info
print("Slices:")
for macho_slice in parser.slices:
print(f"\t{macho_slice.cpu_type.name} Mach-O slice")
binary = pick_macho_slice(parser)
print(f"Reading {binary.cpu_type.name} slice")
print()
print("Minimum Deployment Target:", binary.get_minimum_deployment_target())
print("Sdk Deployment Target:", binary.get_sdk_deployment_target())
print("Build Tools:")
for build_tool_name, build_tool_version in binary.get_build_tools().items():
print(f" {build_tool_name}: {build_tool_version}")
try:
analyzer = MachoAnalyzer.get_analyzer(binary)
except BinaryEncryptedError as e:
print("Error reading binary:", e)
return
args_command_list = [
command
for command in (
# fmt: off
args.metadata,
args.segments, args.sections,
args.loads,
args.classes, args.protocols, args.methods,
args.imports, args.exports,
args.strings,
# fmt: on
)
if command
]
if script:
print("Running provided script...\n\n")
strongarm_script(binary, analyzer)
else:
shell = StrongarmShell(binary, analyzer)
if args_command_list:
for command in args_command_list:
shell.run_command(f"info {command}")
else:
autorun_cmd = "info metadata segments sections loads"
print(f"Auto-running '{autorun_cmd}'\n\n")
shell.run_command(autorun_cmd)
# this will return False once the shell exists
while shell.process_command():
pass
print()
print("May your arms be beefy and your binaries unencrypted")
if __name__ == "__main__":
arg_parser = argparse.ArgumentParser(description="Mach-O Analyzer")
arg_parser.add_argument("--verbose", action="store_true", help="Output extra info while analyzing")
arg_parser.add_argument("binary_path", metavar="binary_path", type=str, help="Path to binary to analyze")
arg_parser.add_argument("--metadata", action="store_const", const="metadata", help="Print metadata")
arg_parser.add_argument("--segments", action="store_const", const="segments", help="Print segments")
arg_parser.add_argument("--sections", action="store_const", const="sections", help="Print sections")
arg_parser.add_argument("--loads", action="store_const", const="loads", help="Print load commands")
arg_parser.add_argument("--classes", action="store_const", const="classes", help="Print Objective-C classes")
arg_parser.add_argument("--protocols", action="store_const", const="protocols", help="Print Objective-C protocols")
arg_parser.add_argument("--methods", action="store_const", const="methods", help="Print Objective-C methods")
arg_parser.add_argument("--imports", action="store_const", const="imports", help="Print imported symbols")
arg_parser.add_argument("--exports", action="store_const", const="exports", help="Print exported symbols")
arg_parser.add_argument("--strings", action="store_const", const="strings", help="Print strings")
main(arg_parser.parse_args())