forked from R3dFruitRollUp/gef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ida_gef.py
259 lines (207 loc) · 7.67 KB
/
ida_gef.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
"""
This script is the server-side of the XML-RPC defined for gef for
IDA Pro.
It will spawn a threaded XMLRPC server from your current IDA session
making it possible for gef to interact with IDA.
To run from inside IDA:
- open IDA on your binary and press Alt-F7
- a popup "Run Script" will appear, simply enter the location of this
script
If all went well, you will see something like
[+] Creating new thread for XMLRPC server: Thread-1
[+] Starting XMLRPC server: 0.0.0.0:1337
[+] Registered 10 functions.
which indicates that the server is running.
If you edit HOST/PORT, use `gef config` command to edit them
Ref:
- https://docs.python.org/2/library/simplexmlrpcserver.html
- https://pymotw.com/2/SimpleXMLRPCServer/
@_hugsy_
"""
from __future__ import print_function
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer, list_public_methods
import threading
import string
import inspect
import idautils, idc, idaapi
HOST, PORT = "0.0.0.0", 1337
DEBUG = True
_breakpoints = set()
_current_instruction_color = None
_current_instruction = 0
def expose(f):
"Decorator to set exposed flag on a function."
f.exposed = True
return f
def is_exposed(f):
"Test whether another function should be publicly exposed."
return getattr(f, 'exposed', False)
def ishex(s):
return s.startswith("0x") or s.startswith("0X")
class Gef:
"""
Top level class where exposed methods are declared.
"""
def __init__(self, server, *args, **kwargs):
self.server = server
self._version = ("IDA Pro", str(idaapi.IDA_SDK_VERSION))
return
def _dispatch(self, method, params):
"""
Plugin dispatcher
"""
if DEBUG:
print("Received '%s'" % method)
func = getattr(self, method)
if not is_exposed(func):
raise NotImplementedError('Method "%s" is not exposed' % method)
if DEBUG:
print("Executing %s(%s)" % (method, params))
return func(*params)
def _listMethods(self):
"""
Class method listing (required for introspection API).
"""
m = []
for x in list_public_methods(self):
if x.startswith("_"): continue
if not is_exposed( getattr(self, x) ): continue
m.append(x)
return m
def _methodHelp(self, method):
"""
Method help (required for introspection API).
"""
f = getattr(self, method)
return inspect.getdoc(f)
@expose
def version(self):
""" version() => None
Return a tuple containing the tool used and its version
Example: ida version
"""
return self._version
@expose
def shutdown(self):
""" shutdown() => None
Cleanly shutdown the XML-RPC service.
Example: ida shutdown
"""
self.server.server_close()
print("[+] XMLRPC server stopped")
setattr(self.server, "shutdown", True)
return 0
@expose
def MakeComm(self, address, comment):
""" MakeComm(int addr, string comment) => None
Add a comment to the current IDB at the location `address`.
Example: ida MakeComm 0x40000 "Important call here!"
"""
addr = long(address, 16) if ishex(address) else long(address)
return idc.MakeComm(addr, comment)
@expose
def SetColor(self, address, color="0x005500"):
""" SetColor(int addr [, int color]) => None
Set the location pointed by `address` in the IDB colored with `color`.
Example: ida SetColor 0x40000
"""
addr = long(address, 16) if ishex(address) else long(address)
color = long(color, 16) if ishex(color) else long(color)
return idc.SetColor(addr, CIC_ITEM, color)
@expose
def MakeName(self, address, name):
""" MakeName(int addr, string name]) => None
Set the location pointed by `address` with the name specified as argument.
Example: ida MakeName 0x4049de __entry_point
"""
addr = long(address, 16) if ishex(address) else long(address)
return idc.MakeName(addr, name)
@expose
def Jump(self, address):
""" Jump(int addr) => None
Move the IDA EA pointer to the address pointed by `addr`.
Example: ida Jump 0x4049de
"""
addr = long(address, 16) if ishex(address) else long(address)
return idc.Jump(addr)
def GetStructByName(self, name):
for (struct_idx, struct_sid, struct_name) in Structs():
if struct_name == name:
return struct_sid
return None
@expose
def ImportStruct(self, struct_name):
""" ImportStruct(string name) => dict
Import an IDA structure in GDB which can be used with the `pcustom`
command.
Example: ida ImportStruct struct_1
"""
if self.GetStructByName(struct_name) is None:
return {}
res = {struct_name: [x for x in StructMembers(self.GetStructByName(struct_name))]}
return res
@expose
def ImportStructs(self):
""" ImportStructs() => dict
Import all structures from the current IDB into GDB, to be used with the `pcustom`
command.
Example: ida ImportStructs
"""
res = {}
for s in Structs():
res.update(self.ImportStruct(s[2]))
return res
@expose
def Sync(self, offset, added, removed):
""" Sync(offset, added, removed) => None
Synchronize debug info with gef. This is an internal function. It is
not recommended using it from the command line.
"""
global _breakpoints, _current_instruction, _current_instruction_color
if _current_instruction > 0:
idc.SetColor(_current_instruction, CIC_ITEM, _current_instruction_color)
base_addr = idaapi.get_imagebase()
pc = base_addr + int(offset, 16)
_current_instruction = long(pc)
_current_instruction_color = GetColor(_current_instruction, CIC_ITEM)
idc.SetColor(_current_instruction, CIC_ITEM, 0x00ff00)
print("PC @ " + hex(_current_instruction).strip('L'))
# post it to the ida main thread to prevent race conditions
idaapi.execute_sync(lambda: idc.Jump(_current_instruction), idaapi.MFF_WRITE)
cur_bps = set([ idc.GetBptEA(n)-base_addr for n in range(idc.GetBptQty()) ])
ida_added = cur_bps - _breakpoints
ida_removed = _breakpoints - cur_bps
_breakpoints = cur_bps
# update bp from gdb
for bp in added:
idc.AddBpt(base_addr+bp)
_breakpoints.add(bp)
for bp in removed:
if bp in _breakpoints:
_breakpoints.remove(bp)
idc.DelBpt(base_addr+bp)
return [list(ida_added), list(ida_removed)]
class RequestHandler(SimpleXMLRPCRequestHandler):
rpc_paths = ("/RPC2",)
def start_xmlrpc_server():
"""
Initialize the XMLRPC thread
"""
print("[+] Starting XMLRPC server: {}:{}".format(HOST, PORT))
server = SimpleXMLRPCServer((HOST, PORT),
requestHandler=RequestHandler,
logRequests=False,
allow_none=True)
server.register_introspection_functions()
server.register_instance( Gef(server) )
print("[+] Registered {} functions.".format( len(server.system_listMethods()) ))
while True:
if hasattr(server, "shutdown") and server.shutdown==True:
break
server.handle_request()
return
if __name__ == "__main__":
t = threading.Thread(target=start_xmlrpc_server, args=())
t.daemon = True
print("[+] Creating new thread for XMLRPC server: {}".format(t.name))
t.start()