Skip to content

Commit c89b0cb

Browse files
committed
add patchkit run
1 parent 8e41825 commit c89b0cb

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

core/patcher.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
from binary import Binary
88

99
class Patcher:
10-
def __init__(self, binary, verbose=False, cflags=None):
10+
def __init__(self, binary, verbose=False, cflags=None, silent=False):
1111
self.bin = Binary(binary)
1212
self.bin.verbose = verbose
1313
self.bin.linker.cflags = cflags
1414
self.patches = []
1515
self.patchfiles = []
1616
self.verbose = verbose
1717
self.cflags = cflags
18+
self.silent = silent
1819

1920
def add(self, path):
2021
if path.endswith('.py'):
@@ -26,12 +27,16 @@ def add(self, path):
2627
continue
2728
self.patchfiles.append((name, os.path.join(base, os.path.basename(name))))
2829

30+
def debug(self, *args):
31+
if not self.silent:
32+
print >>sys.stderr, ' '.join(map(str, args))
33+
2934
def patch(self):
3035
cwd = os.getcwd()
3136
try:
3237
for path, pathname in self.patchfiles:
3338
sys.path.insert(0, os.path.dirname(path))
34-
print '[*]', pathname
39+
self.debug('[*]', pathname)
3540
patchfile = os.path.basename(path).rsplit('.', 1)[0]
3641
patch = __import__(patchfile)
3742
sys.path.pop(0)
@@ -48,8 +53,8 @@ def patch(self):
4853
except AttributeError:
4954
pass
5055
except Exception:
51-
print 'Warning: could not preserve patch function order'
52-
traceback.print_exc()
56+
self.debug('Warning: could not preserve patch function order')
57+
self.debug(traceback.format_exc())
5358
order = vars(patch).values()
5459

5560
for func in order:
@@ -58,19 +63,19 @@ def patch(self):
5863
continue
5964

6065
if hasattr(func, '__call__'):
61-
print ' [+] %s()' % func.__name__
66+
self.debug(' [+] %s()' % func.__name__)
6267
with self.bin.collect() as patchset:
6368
try:
6469
func(patchset)
6570
except Exception as e:
66-
print 'Exception thrown by patch:', path, func.__name__
71+
self.debug('Exception thrown by patch:', path, func.__name__)
6772
traceback.print_exc()
68-
print 'Memory maps:'
73+
self.debug('Memory maps:')
6974
for prog in self.bin.elf.progs:
7075
if prog.isload:
71-
print '0x%x-0x%x' % (prog.vaddr, prog.vaddr + prog.vsize)
76+
self.debug('0x%x-0x%x' % (prog.vaddr, prog.vaddr + prog.vsize))
7277
sys.exit(1)
73-
print
78+
self.debug()
7479
finally:
7580
os.chdir(cwd)
7681

run

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python
2+
3+
import os
4+
import sys
5+
6+
from core import Patcher
7+
from optparse import OptionParser
8+
9+
if __name__ == '__main__':
10+
parser = OptionParser(usage='Usage: run [options] <binary> <patchdir> [patchdir...]')
11+
parser.add_option('-v', '--verbose', dest="verbose", action="store_true", help="verbose output")
12+
13+
options, args = parser.parse_args()
14+
15+
if len(args) < 2:
16+
parser.print_help()
17+
sys.exit(1)
18+
19+
args = map(os.path.abspath, args)
20+
patchdirs = args[1:]
21+
22+
patch = Patcher(args[0], verbose=options.verbose, silent=not options.verbose)
23+
for d in patchdirs:
24+
patch.add(d)
25+
patch.patch()

0 commit comments

Comments
 (0)