Repository navigation
Expand file tree
/
Copy pathdebugger.cpp
More file actions
343 lines (243 loc) · 8.31 KB
/
Copy pathdebugger.cpp
File metadata and controls
343 lines (243 loc) · 8.31 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include <stdio.h>
#include <iostream>
#include <string>
#include <sstream> // std::istringstream
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/user.h>
#include <sys/reg.h>
#include <sys/personality.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <signal.h>
#include <syscall.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/reg.h>
#include <sys/user.h>
#include <unistd.h>
#include <errno.h>
#include <vector>
#include <map>
/*
break point probably should be here 0x565554c0
0x5655552c
0x56555527
Linux only debugger using PTRACE
Run the debugger like ./debugger
when prompted for executable please provide full path.
######### Commands ##############
next : to step
break 0x12345678 : to set break point at 0x12345678
continue : continue or c may be used
exit : exit the program
infobreak : List break points
examined : Video DWORD at location
info registers : show registers
########################################
*/
//std::vector< unsigned int > BreakPoints;
std::map < unsigned int, unsigned int > BreakPoints;
void InfoRegisters( pid_t child )
{
struct user_regs_struct regs;
ptrace(PTRACE_GETREGS, child, 0, ®s);
/*
eax 0x0 0x0
ecx 0x0 0x0
edx 0x0 0x0
ebx 0x0 0x0
esp 0xffffd080 0xffffd080
ebp 0x0 0x0
esi 0x0 0x0
edi 0x0 0x0
eip 0xf7fd6c70 0xf7fd6c70
eflags 0x200 [ IF ]
cs 0x23 0x23
ss 0x2b 0x2b
ds 0x2b 0x2b
es 0x2b 0x2b
fs 0x0 0x0
gs 0x0 0x0
*/
printf("######### Listing Registers #########\n");
printf(" EAX: 0x%08x\n ECX: 0x%08x\n EDX: 0x%08x\n EBX: 0x%08x\n ESP: 0x%08x\n EBP: 0x%08x\n ESI: 0x%08x\n EDI: 0x%08x\n EIP: 0x%08x\n"
, regs.eax,regs.ecx,regs.edx,regs.ebx,regs.esp,regs.ebp,regs.esi,regs.edi,regs.eip ); // exterend here is you want more registers.
printf("######### Finished Registers #########\n");
}
void CheckDWORDMem(pid_t child, unsigned int addr )
{
//unsigned addr = 0x8048096;
unsigned data = ptrace(PTRACE_PEEKTEXT, child, (void*)addr, 0);
printf("[+] Checking BP Location 0x%08x: 0x%08x\n", addr, data);
}
void ListBreakPoints()
{
// Printing values in the map
std::cout << std::endl << "######### Listing BreakPoints #########" << std::endl;
for( std::map< unsigned int, unsigned int >::iterator iter=BreakPoints.begin(); iter!=BreakPoints.end(); ++iter)
{
//std::cout << (*iter).first << ": " << (*iter).second << std::endl;
printf("[+] Listing BPs 0x%08x: 0x%08x\n", iter->first, iter->second);
}
std::cout << std::endl << "######### Finished BreakPoints #########\n\n" << std::endl;
}
void SetBreakPoint(pid_t child, unsigned int addr)
{
/* Write the trap instruction 'int 3' into the address */
unsigned data = ptrace(PTRACE_PEEKTEXT, child, (void*)addr, 0);
unsigned data_with_trap = (data & 0xFFFFFF00) | 0xCC;
ptrace(PTRACE_POKETEXT, child, (void*)addr, (void*)data_with_trap);
printf("[+] Setting BP 0x%08x: 0x%08x\n", addr, data);
BreakPoints.insert ( std::pair< unsigned int, unsigned int>(addr,data) );
}
void execute_debugee (pid_t child, const std::string& prog_name)
{
personality(ADDR_NO_RANDOMIZE); // Disable ALSR
if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
std::cerr << "[!] Error in PTRACE_TRACEME\n";
return;
}
execl(prog_name.c_str(), prog_name.c_str(), nullptr);
}
void Nexti(pid_t child)
{
int wait_status;
/* Make the child execute another instruction */
if (ptrace(PTRACE_SINGLESTEP, child, 0, 0) < 0) {
perror("[!] Error in PTRACE_SINGLESTEP");
return;
}
wait(&wait_status);
}
void Continue ( pid_t child )
{
int wait_status;
struct user_regs_struct regs;
ptrace(PTRACE_CONT, child, 0, 0);
wait(&wait_status);
if (WIFSTOPPED(wait_status)) {
//printf("Child got a signal: %d\n", (WSTOPSIG(wait_status)) );
if( (WSTOPSIG(wait_status)) == 5 )
{
ptrace(PTRACE_GETREGS, child, 0, ®s);
printf("[!] BreakPoint Hit at 0x%08x \n", regs.eip );
// Restore data at breakpoint
std::map<unsigned int , unsigned int>::iterator iter = BreakPoints.find(regs.eip - 1);
if (iter != BreakPoints.end())
{
//std::cout << std::endl<< "[+] Value => " << BreakPoints.find(regs.eip -= 1)->second << '\n';
printf("[+] Address 0x%08x: Data 0x%08x\n", iter->first, iter->second);
}
unsigned int addr = BreakPoints.find(regs.eip - 1)->first;
unsigned int data = BreakPoints.find(regs.eip - 1)->second;
ptrace(PTRACE_POKETEXT, child, (void*)addr, (void*)data);
regs.eip -= 1;
ptrace(PTRACE_SETREGS, child, 0, ®s);
// End restore data
}
}
else {
perror("[-] wait");
return;
}
printf("[+] Child stopped at EIP = 0x%08x\n", regs.eip);
}
std::string lastcommand;
void HandleCommand( pid_t child , std::string command )
{
// printf("COMMAND: %s\n" , command.c_str());
// A better would be comparion string
// std::basic_string_view::starts_with):
if (command == "exit"){exit(0);}
else if (command == "next" || command == "nexti"){Nexti( child ); } // handle next /nexti commands
else if (command.find("break") == 0 )
{
// All this does is get the last string which is hopefully the address
// break 0x56555527
std::istringstream iss(command);
std::size_t LastWord = command.find_last_of(' ');
std::string addr = command.substr(LastWord + 1); // should be 0x########
// convert address which is in string to unsigned int.
unsigned int uiaddr;
std::stringstream hexstring ;
hexstring << std::hex << addr;
hexstring >> uiaddr;
//printf("--- Break Point - 0x%08x \n", uiaddr);
SetBreakPoint(child, uiaddr);
}
else if (command == "continue"|| command == "c")
{
Continue(child);
}
else if (command == "infobreak" || command == "info break")
{
ListBreakPoints();
}
else if (command.find("examined") == 0)
{
//examined --supposed to check a DWORD space in memory
// All this does is get the last string which is hopefully the address
// examined 0x56555527
std::istringstream iss(command);
std::size_t LastWord = command.find_last_of(' ');
std::string addr = command.substr(LastWord + 1); // should be 0x########
// convert address which is in string to unsigned int.
unsigned int uiaddr;
std::stringstream hexstring ;
hexstring << std::hex << addr;
hexstring >> uiaddr;
CheckDWORDMem(child, uiaddr);
}
else if (command == "info registers" || command == "i r")
{
InfoRegisters(child);
}
else if (command.empty()){
//printf("command empty");
lastcommand = command;
Nexti( child );
}
else {
printf("[-] Unknown command entered\n");
command = "";
}
lastcommand = command;
return ;
}
int main()
{
int wait_status;
unsigned icounter = 0;
std::string executable;
std::string command;
std::cout << "[?] Type your name of executable path: ";
std::getline(std::cin, executable);
pid_t child = fork();
if (child == 0) {
execute_debugee(child, "/home/krash/works/Anastasia/build-Anastasia-Desktop-Debug/plugins/garbage/hello");
}
else{
// exit?
}
// set up
// launch exe
printf("[+] Debugger Started\n");
wait(&wait_status);
while(WIFSTOPPED(wait_status)){
//printf("WAIT STATUS %d\n" , wait_status);
icounter++;
struct user_regs_struct regs;
ptrace(PTRACE_GETREGS, child, 0, ®s);
unsigned instr = ptrace(PTRACE_PEEKTEXT, child, regs.eip, 0);
printf("[+] Icounter = %u. EIP = 0x%08x. instr = 0x%08x\n", icounter, regs.eip, instr);
printf(">>> ");
std::getline(std::cin, command);
HandleCommand(child , command);
}
printf("The child executed %u instructions\n", icounter);
}