-
Notifications
You must be signed in to change notification settings - Fork 27
/
ida_gdb_bridge.py
executable file
·193 lines (152 loc) · 4.64 KB
/
ida_gdb_bridge.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
import idautils
import ida_kernwin
import signal
import idc
import idaapi
import socket
import struct
import threading
import time
import Queue
TRACE = False
DEBUG = False
COLOR_CUR = 0x68ff90
# make sure our operations time out so we can actually quit IDA
DEFAULT_TIMEOUT = 3
socket.setdefaulttimeout(DEFAULT_TIMEOUT)
def dprint(s):
if DEBUG == True:
print('IDAbridge: ' + s)
# use idaapi.execute_ui_requests to ensure that SetColor and Jump
# are executed in the main thread to ensure thread safety. if we
# ignore that, we will run into btree and other errors.
# thanks for hex-rays for helping with this!
class color_req_t(object):
def __init__(self, ea, color):
self.ea = ea
self.color = color
def __call__(self):
idc.SetColor(self.ea, idc.CIC_ITEM, self.color)
return False # Don't reschedule
def safe_setcolor(ea, color):
ida_kernwin.execute_ui_requests((color_req_t(ea, color),))
class jump_req_t(object):
def __init__(self, ea):
self.ea = ea
def __call__(self):
idc.Jump(self.ea)
return False # Don't reschedule
def safe_jump(ea):
ida_kernwin.execute_ui_requests((jump_req_t(ea),))
# TODO: in case of any exceptions it may be nice to have
# a list of all locations where color was changed so that on
# exiting the plugin, we could restore these.
class ColorThread(threading.Thread):
def __init__(self, ea = -1):
#print "ColorThread()"
threading.Thread.__init__(self)
self.c_queue = Queue.Queue()
self.ea = ea
self.running = False
def join(self, timeout = None):
self.running = False
threading.Thread.join(self, timeout)
def run(self):
self.running = True
while self.running == True:
event_ea = self.ea
try:
ea = self.c_queue.get(block=True, timeout=DEFAULT_TIMEOUT)
except:
continue
try:
event_ea = int(struct.unpack("<Q", ea)[0])
except Exception as e:
dprint("error pc conversion: %s" %(str(e)))
continue
dprint("current position: %x" %(event_ea))
try:
safe_jump(event_ea)
except:
continue
# TODO: do some sanity checking on received EAs
# e.g. make sure that the address is within the image space
if TRACE == False:
safe_setcolor(self.ea, 0xffffff)
safe_setcolor(event_ea, COLOR_CUR)
self.ea = event_ea
class BridgeThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.server = None
self.running = False
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.c_thread = ColorThread()
def join(self, timeout = None):
self.running = False
self.s.close()
threading.Thread.join(self, timeout)
def bind(self):
try:
self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.s.bind(self.server)
except:
dprint("bind() failed")
self.running = False
return False
self.running = True
return True
def run(self):
self.s.listen(1)
dprint("accepting connections")
if not self.c_thread.running:
self.c_thread.start()
# TODO: make it possible to keep a connection or switch to UDP
while self.running == True:
try:
conn, addr = self.s.accept()
except socket.timeout:
continue
try:
read_ea = conn.recv(8)
except Exception as e:
dprint("closing client/%s connection: %s" % (addr, str(e)))
conn.close()
continue
conn.close()
self.c_thread.c_queue.put(read_ea)
class ida_gdb_debug_bridge_plugin(idaapi.plugin_t):
flags = idaapi.PLUGIN_OK
comment = ""
help = ""
wanted_name = "IDA debug bridge"
wanted_hotkey = "Alt-f"
def init(self):
self.bridge_thread = BridgeThread()
return idaapi.PLUGIN_OK
def run(self, arg):
if self.bridge_thread.running == True:
dprint("server already running: %s:%s\n" % (self.bridge_thread.server[0], self.bridge_thread.server[1]))
return
ask_port = idc.AskLong(2305, "Enter port number for incoming events")
if ask_port < 1 or ask_port <= 1024:
dprint('invalid port number')
return
ask_addr = idc.AskStr('0.0.0.0', "Enter address to bind to")
self.bridge_thread.server = (ask_addr, ask_port)
if self.bridge_thread.running == False:
self.bridge_thread.bind()
if self.bridge_thread.running == False:
dprint("there was an issue starting the server")
else:
self.bridge_thread.start()
return
def term(self):
if self.bridge_thread.c_thread.ea != -1:
safe_setcolor(self.bridge_thread.c_thread.ea, 0xffffff)
self.bridge_thread.running = False
self.bridge_thread.c_thread.running = False
self.bridge_thread.s.close()
pass
def PLUGIN_ENTRY():
return ida_gdb_debug_bridge_plugin()