-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdripdbg.py
198 lines (173 loc) · 5.93 KB
/
dripdbg.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
import time
import socket
import matplotlib.pyplot as plt
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("0.0.0.0", 12345))
s.listen(1)
eom_sig = "#$^&e"
def remote_print(conn, str):
try:
conn.send(("p" + str + eom_sig).encode("ASCII")) # Remote debugger command to print stuff
except:
conn.close()
s.close()
quit()
def dripdbg_get_msg(conn):
try:
cur_msg = ""
while True:
cur_msg += (conn.recv(1024).decode("ASCII"))
if eom_sig in cur_msg:
return cur_msg.split(eom_sig)[0]
except:
print("ERROR")
conn.close()
s.close()
quit()
def dripdbg_get_threads(conn):
conn.send(("t" + eom_sig).encode("ASCII"))
return dripdbg_get_msg(conn).split("+=")[1:]
def dripdbg_get_tids(conn):
conn.send(("i" + eom_sig).encode("ASCII"))
elements = dripdbg_get_msg(conn).split("+=")[1:]
ret = []
for e in elements:
ret.append(int(e))
return ret
def dripdbg_get_thread_info(conn, tid):
data = str(tid)
conn.send(("I" + data + eom_sig).encode("ASCII"))
elements = dripdbg_get_msg(conn).split("+=")[1:]
ret = []
for e in elements:
ret.append(int(e))
return ret
def dripdbg_get_cpu_time_info(conn, tid):
data = str(tid)
conn.send(("c" + data + eom_sig).encode("ASCII"))
elements = dripdbg_get_msg(conn).split("+=")[1:]
ret = []
for e in elements:
ret.append(int(e))
return ret
def dripdbg_kill_thread(conn, tid):
data = str(tid)
conn.send(("k" + data + eom_sig).encode("ASCII"))
def get_hello_world(conn):
try:
conn.send(("h" + eom_sig).encode("ASCII"))
return dripdbg_get_msg(conn)
except:
conn.close()
s.close()
quit()
return "bad"
def dripdbg_parse_cmd(conn, cmd, recursive, last_command):
params_etc = cmd.split(" ")
# Commands
if params_etc[0] == "threads":
print("Thread list: ", dripdbg_get_threads(conn))
print("Thread ids: ", dripdbg_get_tids(conn))
elif params_etc[0] == "registers":
# Sanity check
if not len(params_etc) == 2:
print("expected: registers <tid>")
return cmd
if not params_etc[1].isnumeric():
print("expected: registers <tid>")
return cmd
# Actually run command
thread_info = dripdbg_get_thread_info(conn, int(params_etc[1]))
print("rax: " + hex(thread_info[0]))
print("rbx: " + hex(thread_info[1]))
print("rcx: " + hex(thread_info[2]))
print("rdx: " + hex(thread_info[3]))
print("rdi: " + hex(thread_info[4]))
print("rsi: " + hex(thread_info[5]))
print("rbp: " + hex(thread_info[6]))
print("r8: " + hex(thread_info[7]))
print("r9: " + hex(thread_info[8]))
print("r10: " + hex(thread_info[9]))
print("r11: " + hex(thread_info[10]))
print("r12: " + hex(thread_info[11]))
print("r13: " + hex(thread_info[12]))
print("r14: " + hex(thread_info[13]))
print("r15: " + hex(thread_info[14]))
print("rsp: " + hex(thread_info[15]))
print("rip: " + hex(thread_info[16]))
print("rflags: " + hex(thread_info[17]))
print("cs: " + hex(thread_info[18]))
print("ss: " + hex(thread_info[19]))
print("cr3: " + hex(thread_info[20]))
elif params_etc[0] == "print":
if not len(params_etc) == 2:
print("expected: print <message>")
return cmd
remote_print(conn, params_etc[1])
elif params_etc[0] == "cputime":
# Sanity check
if not len(params_etc) == 2:
print("expected: cputime <tid>")
return cmd
if not params_etc[1].isnumeric():
print("expected: cputime <tid>")
return cmd
cpu_time_info = dripdbg_get_cpu_time_info(conn, int(params_etc[1]))
print("Time started: " + str(cpu_time_info[0]))
print("Time stopped: " + str(cpu_time_info[1]))
print("Time total: " + str(cpu_time_info[2]))
elif params_etc[0] == "watchcputime":
# Sanity check
if not len(params_etc) == 3:
print("expected: watchcputime <tid> <times>")
return cmd
if not params_etc[1].isnumeric():
print("expected: watchcputime <tid> <times>")
return cmd
if not params_etc[2].isnumeric():
print("expected: watchcputime <tid> <times>")
return cmd
data = []
base = 0
for i in range(int(params_etc[2])):
cpu_time_info = dripdbg_get_cpu_time_info(conn, int(params_etc[1]))
if len(data) == 0:
data.append(0)
base = cpu_time_info[2]
else:
data.append(cpu_time_info[2] - base)
print(data)
time.sleep(2)
plt.plot(data)
plt.show()
elif params_etc[0] == "killt":
# Sanity check
if not len(params_etc) == 2:
print("expected: killt <tid>")
return cmd
if not params_etc[1].isnumeric():
print("expected: killt <tid>")
return cmd
dripdbg_kill_thread(conn, int(params_etc[1]))
elif params_etc[0] == "quit":
conn.close()
s.close()
quit()
else:
if params_etc[0] == "" and not recursive == True:
return dripdbg_parse_cmd(conn, last_command, True, "")
print("Unknown command: " + params_etc[0])
return cmd
return cmd
while True:
conn,addr = s.accept()
time.sleep(10)
remote_print(conn, "\nHello from debugger!")
print("Message: " + get_hello_world(conn))
print("Thread list: ", dripdbg_get_threads(conn))
print("Thread ids: ", dripdbg_get_tids(conn))
last_command = ""
while True:
# Command parser
command = input("(DripDBG) ")
last_command = dripdbg_parse_cmd(conn, command, False, last_command)