-
Notifications
You must be signed in to change notification settings - Fork 190
/
pydbg_server.py
196 lines (151 loc) · 5.76 KB
/
pydbg_server.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
#!c:\python\python.exe
#
# PyDBG
# Copyright (C) 2006 Pedram Amini <[email protected]>
#
# $Id: pydbg_server.py 194 2007-04-05 15:31:53Z cameron $
#
'''
@author: Pedram Amini
@contact: [email protected]
@organization: www.openrce.org
'''
import socket
import sys
import threading
import cPickle
import getopt
from pydbg import *
from pydbg.defines import *
# null either of these by setting to lambda x: None
err = lambda msg: sys.stderr.write("[!] " + msg + "\n") or sys.exit(1)
log = lambda msg: sys.stdout.write("[*] " + msg + "\n")
########################################################################################################################
class pydbg_server_thread (threading.Thread):
def __init__ (self, client, client_address):
threading.Thread.__init__(self)
self.client = client
self.client_address = client_address
self.pydbg = pydbg(cs=True)
self.connected = True
def callback_handler_wrapper (self, pydbg):
try:
# non client/server access to dbg/context are done via member variables. in client/server mode however, we
# must excplicity pass these back to the client.
self.pickle_send(("callback", pydbg.dbg, pydbg.context))
except:
return DBG_CONTINUE
# enter a read loop, exiting when the client sends the "DONE" moniker.
while 1:
try:
pickled = self.pickle_recv()
except:
return DBG_CONTINUE
# XXX - this try except block should not be needed. look into the cause of why there is an out of order
# recv at some later point.
try:
(method, (args, kwargs)) = pickled
except:
break
ret_message = False
# client is done handling the exception.
if method == "**DONE**":
return args
else:
# resolve a pointer to the requested method.
method_pointer = None
try:
exec("method_pointer = self.pydbg.%s" % method)
except:
pass
if method_pointer:
try:
ret_message = method_pointer(*args, **kwargs)
except pdx, x:
ret_message = ("exception", x.__str__())
try:
self.pickle_send(ret_message)
except:
return DBG_CTONINUE
def pickle_recv (self):
try:
length = long(self.client.recv(4), 16)
received = self.client.recv(length)
return cPickle.loads(received)
except:
log("connection severed to %s:%d" % (self.client_address[0], self.client_address[1]))
self.connected = False
self.pydbg.set_debugger_active(False)
raise Exception
def pickle_send (self, data):
print "sending", data
data = cPickle.dumps(data)
try:
self.client.send("%04x" % len(data))
self.client.send(data)
except:
log("connection severed to %s:%d" % (self.client_address[0], self.client_address[1]))
self.connected = False
self.pydbg.set_debugger_active(False)
raise Exception
def run (self):
log("connection received from: %s:%d" % (self.client_address[0], self.client_address[1]))
while self.connected:
try:
pickled = self.pickle_recv()
except:
break
# XXX - this try except block should not be needed. look into the cause of why there is an out of order
# recv at some later point.
try:
(method, (args, kwargs)) = pickled
print method, args, kwargs
except:
continue
ret_message = False
# if client requested the set_callback method.
if method == "set_callback":
self.pydbg.set_callback(args, self.callback_handler_wrapper)
ret_message = True
else:
# resolve a pointer to the requested method.
method_pointer = None
try:
exec("method_pointer = self.pydbg.%s" % method)
except:
pass
if method_pointer:
try:
ret_message = method_pointer(*args, **kwargs)
except pdx, x:
ret_message = ("exception", x.__str__())
try:
self.pickle_send(ret_message)
except:
break
########################################################################################################################
# parse command line options.
try:
opts, args = getopt.getopt(sys.argv[1:], "h:p:", ["host=","port="])
except getopt.GetoptError:
err(USAGE)
host = "0.0.0.0"
port = 7373
for o, a in opts:
if o in ("-h", "--host"): host = a
if o in ("-p", "--port"): port = int(a)
try:
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
server.listen(1)
except:
sys.stderr.write("Unable to bind to %s:%d\n" % (host, port))
sys.exit(1)
while 1:
log("waiting for connection")
(client, client_address) = server.accept()
server_thread = pydbg_server_thread(client, client_address)
try:
server_thread.start()
except:
log("client disconnected")