Skip to content

Commit ed063e3

Browse files
author
Bjoern Kerler
committed
Add pull requests pbiernat#15 and pbiernat#16
1 parent 2d998b1 commit ed063e3

File tree

5 files changed

+75
-6
lines changed

5 files changed

+75
-6
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Installation on Windows typically requires installing PyQt5.
3535
---
3636

3737
#### Packaging a Function
38+
##### Binary Ninja
3839
From within Binary Ninja, right click anywhere inside of a function and select `[ripr] Package Function`.
3940

4041
<img src="https://puu.sh/thLAo/491ac39e58.PNG" width="600">
@@ -43,6 +44,10 @@ After packaging, a table will appear listing all of the "packages" you have crea
4344

4445
<img src="https://puu.sh/tnz8C/d0f5141f43.PNG" width="600">
4546

47+
##### Radare2
48+
If you've followed step 3 in the installation instructions, run `.(ripr 0x1234)` (with 0x1234 replaced by the address of the function).
49+
Otherwise, you can manually invoke ripr with `#!pipe python /absolute/path/to/ripr/r2pipe_run.py 0x1234`.
50+
4651
#### Packaging Specific Basic Blocks
4752
You can also choose to only package specific basic blocks rather than the entire function.
4853

codegen.py

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,24 @@ def __init__(self, name, arch):
2222
def gen_arg_number(self, argno):
2323
pass
2424

25+
def genPointer(self, arg, regs, indent):
26+
pass
27+
28+
def dumpContext(self, indent):
29+
pass
30+
2531
class x64callConv(callConv):
2632
# TODO Stack based arguments
2733
def __init__(self, name, arch):
2834
self.name = name
2935
self.arch = arch
3036
self.platform = ''
31-
37+
self.regs = ["UC_X86_REG_RAX", "UC_X86_REG_RBP", "UC_X86_REG_RBX", "UC_X86_REG_RCX",\
38+
"UC_X86_REG_RDI", "UC_X86_REG_RDX", "UC_X86_REG_RSI", "UC_X86_REG_RSP",\
39+
"UC_X86_REG_RIP", "UC_X86_REG_R8", "UC_X86_REG_R9", "UC_X86_REG_R10",\
40+
"UC_X86_REG_R11", "UC_X86_REG_R12", "UC_X86_REG_R13", "UC_X86_REG_R14",\
41+
"UC_X86_REG_R15"]
42+
3243
def gen_arg_number(self, argno, indent=1):
3344
print ("X64")
3445
if self.platform == "win":
@@ -57,10 +68,19 @@ def systemV(self, arg, indent):
5768
return ' ' * (indent*4) + "self.mu.reg_write(%s, arg_%x)\n" % (regs[arg.num], arg.num)
5869
return self.genPointer(arg, regs, indent)
5970

71+
def dumpContext(self, indent):
72+
ret = ' ' * (indent * 4) + ("print ('[!] Exception occured - Emulator state (x64):')\n")
73+
for r in self.regs:
74+
ret += ' ' * (indent * 4) + ("print (\"%s : %%016X\" %% (self.mu.reg_read(%s)))\n" % (r,r))
75+
return ret
76+
6077
class x86callConv(callConv):
6178
def __init__(self, name, arch):
6279
self.name = name
6380
self.arch = arch
81+
self.regs = ["UC_X86_REG_EAX", "UC_X86_REG_EBP", "UC_X86_REG_EBX", "UC_X86_REG_ECX",\
82+
"UC_X86_REG_EDI", "UC_X86_REG_EDX", "UC_X86_REG_ESI", "UC_X86_REG_ESP",\
83+
"UC_X86_REG_EIP"]
6484

6585
def genPointer(self, arg, indent):
6686
ret = ' ' * (indent * 4) + "argAddr_%x = (%d * 0x1000)\n" % (arg.num, arg.num + 1)
@@ -75,10 +95,20 @@ def gen_arg_number(self, arg, indent):
7595
return ' ' * (indent * 4) + "self.mu.mem_write(self.mu.reg_read(UC_X86_REG_ESP) + %d, struct.pack('<i', arg_%x))\n" % ( (arg.num * 4) + 4, arg.num)
7696
return self.genPointer(arg, indent)
7797

98+
def dumpContext(self, indent):
99+
ret = ' ' * (indent * 4) + ("print ('[!] Exception occured - Emulator state (x86):')\n")
100+
for r in self.regs:
101+
ret += ' ' * (indent * 4) + ("print (\"%s : %%08X\" %% (self.mu.reg_read(%s)))\n" % (r,r))
102+
return ret
103+
78104
class armcallConv(callConv):
79105
def __init__(self, name, arch):
80106
self.name = name
81107
self.arch = arch
108+
self.regs = ["UC_ARM_REG_R0", "UC_ARM_REG_R1", "UC_ARM_REG_R2", "UC_ARM_REG_R3",\
109+
"UC_ARM_REG_R4", "UC_ARM_REG_R5", "UC_ARM_REG_R6", "UC_ARM_REG_R7",\
110+
"UC_ARM_REG_R8", "UC_ARM_REG_R9", "UC_ARM_REG_R10", "UC_ARM_REG_R11",\
111+
"UC_ARM_REG_R12", "UC_ARM_REG_R13", "UC_ARM_REG_R14", "UC_ARM_REG_R15"]
82112

83113
def genPointer(self, arg, regs, indent):
84114
ret = ' ' * (indent * 4) + "argAddr_%x = (%d * 0x1000)\n" % (arg.num, arg.num+1)
@@ -92,7 +122,12 @@ def gen_arg_number(self, arg, indent):
92122
if arg.pointerDepth == 0 or arg.pointerDepth > 1:
93123
return ' ' * (indent *4 ) + "self.mu.reg_write(%s, arg_%x)\n" % (regs[arg.num], arg.num)
94124
return self.genPointer(arg, regs, indent)
95-
125+
126+
def dumpContext(self, indent):
127+
ret = ' ' * (indent * 4) + ("print ('[!] Exception occured - Emulator state (arm):')\n")
128+
for r in self.regs:
129+
ret += ' ' * (indent * 4) + ("print (\"%s : %%X\" %% (self.mu.reg_read(%s)))\n" % (r,r))
130+
return ret
96131

97132
class codeSlice(object):
98133
'''
@@ -155,6 +190,15 @@ def __init__(self, name, isFunc=True):
155190

156191
self.isFunc = isFunc
157192

193+
def setArch(self,a):
194+
self.arch=a
195+
if self.arch == 'x64':
196+
self.callConv = x64callConv("linux", "x64")
197+
if self.arch == 'x86':
198+
self.callConv = x86callConv("linux", "x86")
199+
if self.arch == 'arm':
200+
self.callConv =armcallConv("linux", "arm")
201+
158202
def data_saved(self, addr):
159203
return any(lowaddr <= addr <= highaddr for (lowaddr, highaddr) in self.saved_ranges)
160204

@@ -369,6 +413,8 @@ def generate_return_guard(self, indent=1):
369413

370414
# Raise original exception if PC is not equal to the appropriate marker value or imported call marker
371415
out += ' ' * (indent * 4) + "else:\n"
416+
if self.callConv is not None:
417+
out += self.callConv.dumpContext(indent+1)
372418
out += ' ' * ((indent + 1) * 4) + "raise e"
373419

374420
return out + "\n"
@@ -465,7 +511,8 @@ def generate_default_hookFunc(self, name, indent=1):
465511
The default python hook for imported calls should do nothing.
466512
'''
467513
out = ' ' * (indent * 4) + """def hook_%s(self):
468-
pass\n""" % name
514+
print ("[!] %s hook not implemented!")
515+
pass\n""" % (name, name)
469516
return out
470517

471518
def _build_impCall_hook_dict(self, indent=1):

conScan.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
from binaryninja import *
1+
try:
2+
from binaryninja import *
3+
except:
4+
print ("[!!] Not running in Binary Ninja")
5+
try:
6+
import r2pipe
7+
except:
8+
print ("[!!] Not running in Radare2")
9+
210
from .analysis_engine import *
311

412
class ilVar(object):

dependency.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@
55
'''
66

77
from .analysis_engine import aengine as ae
8-
from binaryninja import *
8+
# Try to import stuff.
9+
try:
10+
from binaryninja import *
11+
except:
12+
print ("[!!] Not running in Binary Ninja")
13+
try:
14+
import r2pipe
15+
except:
16+
print ("[!!] Not running in Radare2")
17+
918

1019
class ImportedCall(object):
1120
'''

packager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def __init__(self, isFunc, address, engine, ui = None, length=None):
2626

2727
self.codeobj = genwrapper('', isFunc)
2828
self.arch = self.engine.get_arch()
29-
self.codeobj.arch = self.arch
29+
self.codeobj.setArch(self.arch)
3030

3131
self.impCallStrategy = None
3232
self.dataStrategy = None

0 commit comments

Comments
 (0)