-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbreakpoint.cpp
More file actions
159 lines (140 loc) · 4.97 KB
/
breakpoint.cpp
File metadata and controls
159 lines (140 loc) · 4.97 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
#include <windows.h>
#include <map>
#include <stdio.h>
#include <winnt.h>
#include "breakpoint.hpp"
#include "Display.hpp"
using std::map;
void Breakpoint::setUser(DWORD address){
Userbp[address]=0xCC;
}
bool Breakpoint::setUser(DWORD address, PROCESS_INFORMATION &pi, Disassembler &d){
return this->set(address, pi, d, true);
}
bool Breakpoint::setInvisible(DWORD address, PROCESS_INFORMATION &pi, Disassembler &d){
return this->set(address, pi, d, false);
}
bool Breakpoint::set(DWORD address, PROCESS_INFORMATION &pi, Disassembler &d, bool user){
BYTE cInstruction[15], bp;
DWORD dwReadBytes;
if(!ReadProcessMemory(pi.hProcess, (void*)address, &cInstruction, 15, &dwReadBytes)){
ColorChanger::SetColor(RED, true);
std::cout << "BREAKPOINT ERROR: Not able to put breakpoint at 0x" << std::hex << std::uppercase << address << std::endl;
std::cout << GetLastErrorAsString() << std::endl;
ColorChanger::Reset();
return false;
}
if(!d.isValid(cInstruction, address)){
ColorChanger::SetColor(RED, true);
std::cout << "BREAKPOINT ERROR: Not able to put breakpoint at 0x" << std::hex << std::uppercase << address << std::endl;
std::cout << "This adress isn't an instruction." << std::endl;
ColorChanger::Reset();
return false;
}
if(*cInstruction== 0xCC)
return true;
bp = 0xCC;
if(!WriteProcessMemory(pi.hProcess, (void*)address,&bp, 1, &dwReadBytes)
|| !FlushInstructionCache(pi.hProcess,(void*)address,1)){
ColorChanger::SetColor(RED, true);
std::cout << "BREAKPOINT ERROR: Not able to put breakpoint at 0x" << std::hex << std::uppercase << address << std::endl;
std::cout << GetLastErrorAsString() << std::endl;
ColorChanger::Reset();
return false;
}
if(user)
Userbp[address]=*cInstruction;
else
Bpinvisible = std::make_pair(address,*cInstruction);
return true;
}
void Breakpoint::setAllUser(PROCESS_INFORMATION &pi, Disassembler &d){
for (map<DWORD,BYTE>::iterator x=Userbp.begin(); x!=Userbp.end();){
if(this->set(x->first, pi, d, true)){
Prompt();
printf("Breakpoint at 0x%lX was set.\n",x->first);
++x;
}
else
Userbp.erase(x++);
}
}
bool Breakpoint::addrPresentUser(DWORD address){
return Userbp.count(address);
}
void Breakpoint::deleteUser(DWORD address, PROCESS_INFORMATION &pi){
BYTE cInstruction;
DWORD dwReadBytes;
cInstruction = Userbp[address];
if(!WriteProcessMemory(pi.hProcess, (void*)address,&cInstruction, 1, &dwReadBytes)
|| !FlushInstructionCache(pi.hProcess,(void*)address,1)){
ColorChanger::SetColor(RED, true);
std::cout << "BREAKPOINT ERROR: Not able to delete breakpoint at 0x" << std::hex << std::uppercase << address << std::endl;
std::cout << GetLastErrorAsString() << std::endl;
ColorChanger::Reset();
}
}
void Breakpoint::deleteInvisible(PROCESS_INFORMATION &pi){
if(Bpinvisible.first){
BYTE cInstruction;
DWORD dwReadBytes;
//Decremente eip et place une exeption single step//
DWORD address = Bpinvisible.first;
Bpinvisible.first = 0;
cInstruction = Bpinvisible.second;
WriteProcessMemory(pi.hProcess, (void*)address,&cInstruction, 1, &dwReadBytes);
FlushInstructionCache(pi.hProcess,(void*)address,1);
}
}
void Breakpoint::DeleteAllUserbp(PROCESS_INFORMATION &pi, bool existDebuggee){
BYTE cInstruction;
DWORD dwReadBytes;
DWORD address;
if(Userbp.empty()){
Prompt();
printf("No breakpoint was initialized.\n");
return;
}
if (!existDebuggee){
Userbp.clear();
Prompt();
printf("All breakpoint were deleted.\n");
return;
}
for (map<DWORD,BYTE>::iterator x=Userbp.begin(); x!=Userbp.end();){
address=x->first;
cInstruction = x->second;
if(!WriteProcessMemory(pi.hProcess, (void*)address,&cInstruction, 1, &dwReadBytes)
|| !FlushInstructionCache(pi.hProcess,(void*)address,1)){
ColorChanger::SetColor(RED, true);
printf("BREAKPOINT ERROR: Not able to delete breakpoint at %lX\n%s\n",address,GetLastErrorAsString().c_str());
ColorChanger::Reset();
++x;
continue;
}
Userbp.erase(x++);
Prompt();
printf("Breakpoint at address 0x%lX was deleted.\n",address);
}
}
void Breakpoint::listAllUser(){
int compteur=0;
printf("\n");
ColorChanger::SetColor(MAGENTA, true);
for (map<DWORD,BYTE>::iterator x=Userbp.begin(); x!=Userbp.end();x++,++compteur){
DWORD address=x->first;
printf(" Breakpoint %d at address 0x%lX.\n",compteur,address);
}
if(compteur==0){
ColorChanger::SetColor(MAGENTA);
printf(" No Breakpoint was set yet.\n");
}
printf("\n");
ColorChanger::Reset();
}
const map<DWORD,BYTE> Breakpoint::GetMap() const{
return this->Userbp;
}
const pair<DWORD,BYTE> Breakpoint::GetPair() const{
return this->Bpinvisible;
}