/
shell.py
executable file
·343 lines (279 loc) · 10.2 KB
/
shell.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/python
import os
import sys
import cmd
import time
from optparse import OptionParser
from binject.utils import userHasRoot
from binject.inject import AutoInjector
class InjectShell(cmd.Cmd):
# Color Escape Sequences
# http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
prompt = "\x1b[1;34m(binject)\x1b[0m " # highlight color
def __init__(self):
cmd.Cmd.__init__(self)
self.injector = AutoInjector()
if userHasRoot():
self.stdout.write("Mode: process\n")
self.injector.setEditMode("process")
else:
self.stdout.write("Mode: binary\n")
self.injector.setEditMode("binary")
self.doPrintResult = True
def convert(self, arg, typ):
try:
if typ == "int":
return int(arg)
if typ == "hex":
return int(arg, 16)
if typ == "str":
return arg
except Exception, e:
return None
return arg
def parseArgs(self, arg, types):
args = arg.split(" ")
conv = []
for j, typ in enumerate(types):
if j < len(args):
conv.append(self.convert(args[j], typ))
else:
conv.append(None)
return conv
#
# Objdump Analysis
#
def do_loadAnalysis(self, arg):
[path] = self.parseArgs(arg, ["str"])
return self.injector.loadAnalysis(path)
def do_analyzeBinary(self, arg):
[path] = self.parseArgs(arg, ["str"])
return self.injector.analyze(path)
def do_saveAnalysis(self, arg):
[path] = self.parseArgs(arg, ["str"])
return self.injector.saveAnalysis(path)
def do_instruction(self, arg):
[address] = self.parseArgs(arg, ["hex"])
inst = self.injector.objdump.getInstruction(address)
print inst
return inst is not None
#
# Source Code Hooks
#
def do_extractHooks(self, arg):
"Usage: extractHooks path/to/src/"
[path] = self.parseArgs(arg, ["str"])
if path and os.path.exists(path):
if self.injector.objdump:
self.injector.setSourcePath(path)
self.hooks = self.injector.extractHooks()
self.do_hooks("")
return True
else:
self.stdout.write("Needs to analyze a binary first\n")
return False
else:
self.do_help("extractHooks")
return False
def do_hooks(self, arg):
for i, hook in enumerate(self.hooks):
lineObj, typ, lineText = hook
self.stdout.write("%.2d. %s:%d\t%s\n" % (i+1, lineObj["file"], lineObj["lineno"], lineText.strip()))
return True
def do_hook(self, arg):
[hookIndex] = self.parseArgs(arg, ["int"])
hook = self.hooks[hookIndex-1]
line = hook[0]
instructions = self.injector.objdump.getInstructionsOfRange(line["instruction_first"], line["instruction_last"])
for i, inst in enumerate(instructions):
print inst
return True
def do_source(self, arg):
[fname, lineno] = self.parseArgs(arg, ["str", "int"])
if fname:
sources = self.injector.objdump.getSources()
filedata = None
for path in sources:
if fname == os.path.basename(path):
filedata = sources[path]
break
if lineno:
if lineno in filedata:
for i, inst in enumerate(self.injector.objdump.getInstructionsOfLine(filedata[lineno])):
print inst
else:
self.stdout.write("No instructions.\n")
return False
else:
items = filedata.iteritems()
items = sorted(items, key=lambda x: x[0])
for lineno, line in items:
print "%s:%d" % (line["file"], line["lineno"])
return True
#
# Editor
#
def do_setEditMode(self, arg):
[path] = self.parseArgs(arg, ["str"])
self.stdout.write("Mode: %s\n" % (path))
return self.injector.setEditMode(path)
def do_setTarget(self, arg):
[path] = self.parseArgs(arg, ["str"])
return self.injector.setTarget(path)
def do_injectFaultAt(self, arg):
"""injects a fault at the given instruction or hook
e.g. injectFaultAt hook 1
e.g. injectFaultAt inst 4008cf
"""
[typ, address] = self.parseArgs(arg, ["str", "hex"])
if typ == "inst":
inst = self.injector.objdump.getInstruction(address)
if inst:
self.injector.injectFaultAtInstruction(inst)
return True
elif typ == "hook":
hook = self.hooks[address-1]
line = hook[0]
self.injector.injectFaultAtLine(line)
return True
return False
def do_injectSkipAt(self, arg):
"""injects a skip at the given instruction or hook
e.g. injectSkipAt hook 1
e.g. injectSkipAt inst 4008cf
"""
[typ, address] = self.parseArgs(arg, ["str", "hex"])
if typ == "inst":
inst = self.injector.objdump.getInstruction(address)
if inst:
self.injector.injectSkipAtInstruction(inst)
return True
elif typ == "hook":
hook = self.hooks[address-1]
line = hook[0]
self.injector.injectSkipAtLine(line)
return True
return False
def do_open(self, arg):
return self.injector.openEditor()
def do_close(self, arg):
return self.injector.closeEditor()
def do_saveBinary(self, arg):
[path] = self.parseArgs(arg, ["str"])
return self.injector.writeBinary(path)
"Customization of the Shell"
def onecmd(self, line):
"""Interpret the argument as though it had been typed in response
to the prompt.
This may be overridden, but should not normally need to be;
see the precmd() and postcmd() methods for useful execution hooks.
The return value is a flag indicating whether interpretation of
commands by the interpreter should stop.
"""
cmd, arg, line = self.parseline(line)
if not line:
return self.emptyline()
if cmd is None:
return self.default(line)
self.lastcmd = line
if line == 'EOF' :
self.lastcmd = ''
if cmd == '':
return self.default(line)
else:
try:
func = getattr(self, 'do_' + cmd)
except AttributeError:
return self.default(line)
t1 = time.time()
try:
success = func(arg)
except Exception, e:
success = False
self.stdout.write("An error occurred: %s\n" % (e))
t2 = time.time()
if self.doPrintResult:
if success:
# green highlight
self.stdout.write("\x1b[1;32mdone\x1b[0m (%.2fms)\n" % (1000*(t2-t1)))
else:
# red highlight
self.stdout.write("\x1b[1;31mfailed\x1b[0m (%.2fms)\n" % (1000*(t2-t1)))
return False
def completenames(self, text, *ignored):
dotext = 'do_'+text
options = [a[3:] for a in self.get_names() if a.startswith(dotext)]
return options
def get_names(self):
# This method used to pull in base class attributes
# at a time dir() didn't do it yet.
return dir(self.__class__)
def do_help(self, arg):
cmd.Cmd.do_help(self, arg)
return True
def cmdloop(self, intro=None):
"""Repeatedly issue a prompt, accept input, parse an initial prefix
off the received input, and dispatch to action methods, passing them
the remainder of the line as argument.
"""
self.preloop()
if self.use_rawinput and self.completekey:
try:
import readline
self.old_completer = readline.get_completer()
readline.set_completer(self.complete)
readline.parse_and_bind(self.completekey+": complete")
except ImportError:
pass
try:
if intro is not None:
self.intro = intro
if self.intro:
self.stdout.write(str(self.intro)+"\n")
stop = None
while not stop:
if self.cmdqueue:
line = self.cmdqueue.pop(0)
else:
if self.use_rawinput:
try:
line = raw_input(self.prompt)
except EOFError:
line = 'EOF'
else:
self.stdout.write(self.prompt)
self.stdout.flush()
line = self.stdin.readline()
if not len(line):
line = 'EOF'
else:
line = line.rstrip('\r\n')
cmds = [a.strip() for a in line.split("&")]
for cmd in cmds:
line = self.precmd(cmd)
stop = self.onecmd(cmd)
stop = self.postcmd(stop, cmd)
self.postloop()
finally:
if self.use_rawinput and self.completekey:
try:
import readline
readline.set_completer(self.old_completer)
except ImportError:
pass
if __name__ == '__main__':
parser = OptionParser()
parser.add_option("-f", type="string", dest="file")
(options, args) = parser.parse_args()
shell = InjectShell()
if options.file:
with open(options.file, "r") as fh:
cmds = fh.readlines()
for cmd in [cmd.strip() for cmd in cmds]:
if len(cmd) > 0:
print "%s%s" % (shell.prompt, cmd)
shell.onecmd(cmd)
for cmd in args:
print "%s%s" % (shell.prompt, cmd)
shell.onecmd(cmd)
shell.cmdloop()