forked from Phat3/PINdemonium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcInfo.cpp
220 lines (165 loc) · 5.68 KB
/
ProcInfo.cpp
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
#include "ProcInfo.h"
#include "Log.h"
#include "Debug.h"
ProcInfo* ProcInfo::instance = 0;
ProcInfo* ProcInfo::getInstance()
{
if (instance == 0)
instance = new ProcInfo();
return instance;
}
ProcInfo::ProcInfo()
{
this->prev_ip = 0;
this->popad_flag = FALSE;
this->pushad_flag = FALSE;
}
ProcInfo::~ProcInfo(void)
{
}
/* Setter */
/*
Save the initial registers inside the struct
You can fine the macro fo the registers at:
https://software.intel.com/sites/landingpage/pintool/docs/49306/Pin/html/group__REG__CPU__IA32.html
*/
void ProcInfo::setStartRegContext(CONTEXT * ctx){
this->reg_start_context.eax = PIN_GetContextReg(ctx,REG_EAX);
this->reg_start_context.ebx = PIN_GetContextReg(ctx,REG_EBX);
this->reg_start_context.ecx = PIN_GetContextReg(ctx,REG_ECX);
this->reg_start_context.edx = PIN_GetContextReg(ctx,REG_EDX);
this->reg_start_context.esp = PIN_GetContextReg(ctx,REG_ESP);
this->reg_start_context.ebp = PIN_GetContextReg(ctx,REG_EBP);
this->reg_start_context.edi = PIN_GetContextReg(ctx,REG_EDI);
this->reg_start_context.esi = PIN_GetContextReg(ctx,REG_ESI);
}
/*
Save the current registers inside the struct
You can fine the macro fo the registers at:
https://software.intel.com/sites/landingpage/pintool/docs/49306/Pin/html/group__REG__CPU__IA32.html
*/
void ProcInfo::setCurrRegContext(CONTEXT * ctx){
this->reg_curr_context.eax = PIN_GetContextReg(ctx,REG_EAX);
this->reg_curr_context.ebx = PIN_GetContextReg(ctx,REG_EBX);
this->reg_curr_context.ecx = PIN_GetContextReg(ctx,REG_ECX);
this->reg_curr_context.edx = PIN_GetContextReg(ctx,REG_EDX);
this->reg_curr_context.esp = PIN_GetContextReg(ctx,REG_ESP);
this->reg_curr_context.ebp = PIN_GetContextReg(ctx,REG_EBP);
this->reg_curr_context.edi = PIN_GetContextReg(ctx,REG_EDI);
this->reg_curr_context.esi = PIN_GetContextReg(ctx,REG_ESI);
}
void ProcInfo::setFirstINSaddress(ADDRINT address){
this->first_instruction = address;
}
void ProcInfo::setPrevIp(ADDRINT ip){
this->prev_ip = ip;
}
void ProcInfo::setPushadFlag(BOOL flag){
this->pushad_flag = flag;
}
void ProcInfo::setPopadFlag(BOOL flag){
this->popad_flag = flag;
}
/* Getter */
RegContext ProcInfo::getStartRegContext(){
return this->reg_start_context;
}
RegContext ProcInfo::getCurrRegContext(){
return this->reg_curr_context;
}
ADDRINT ProcInfo::getFirstINSaddress(){
return this->first_instruction;
}
ADDRINT ProcInfo::getPrevIp(){
return this->prev_ip;
}
std::vector<Section> ProcInfo::getSections(){
return this->Sections;
}
BOOL ProcInfo::getPushadFlag(){
return this->pushad_flag ;
}
BOOL ProcInfo::getPopadFlag(){
return this->popad_flag;
}
/* Utils + Helper */
void ProcInfo::PrintStartContext(){
MYLOG("======= START REGISTERS ======= \n");
MYLOG("EAX: %08x " , this->reg_start_context.eax);
MYLOG("EBX: %08x " , this->reg_start_context.ebx);
MYLOG("ECX: %08x " , this->reg_start_context.ecx);
MYLOG("EDX: %08x " , this->reg_start_context.edx);
MYLOG("ESP: %08x " , this->reg_start_context.esp);
MYLOG("EBP: %08x " , this->reg_start_context.ebp);
MYLOG("ESI: %08x " , this->reg_start_context.esi);
MYLOG("EDI: %08x " , this->reg_start_context.edi);
MYLOG("============================== \n");
}
void ProcInfo::PrintCurrContext(){
MYLOG("======= CURRENT REGISTERS ======= \n");
MYLOG("EAX: %08x " , this->reg_curr_context.eax);
MYLOG("EBX: %08x " , this->reg_curr_context.ebx);
MYLOG("ECX: %08x " , this->reg_curr_context.ecx);
MYLOG("EDX: %08x " , this->reg_curr_context.edx);
MYLOG("ESP: %08x " , this->reg_curr_context.esp);
MYLOG("EBP: %08x " , this->reg_curr_context.ebp);
MYLOG("ESI: %08x " , this->reg_curr_context.esi);
MYLOG("EDI: %08x " , this->reg_curr_context.edi);
MYLOG("================================= \n");
}
void ProcInfo::PrintSections(){
MYLOG("======= SECTIONS ======= \n");
for(int i = 0; i < this->Sections.size(); i++) {
Section item = this->Sections.at(i);
MYLOG("%s -> begin : %08x end : %08x", item.name.c_str(), item.begin, item.end);
}
MYLOG("================================= \n");
}
//insert a new section in our structure
void ProcInfo::insertSection(Section section){
this->Sections.push_back(section);
}
//return the section's name where the IP resides
string ProcInfo::getSectionNameByIp(ADDRINT ip){
string s = "";
for(int i = 0; i < this->Sections.size(); i++) {
Section item = this->Sections.at(i);
if(ip >= item.begin && ip <= item.end){
s = item.name;
}
}
return s;
}
float ProcInfo::GetEntropy(){
IMG binary_image = APP_ImgHead();
const double d1log2 = 1.4426950408889634073599246810023;
double Entropy = 0.0;
unsigned long Entries[256];
unsigned char* Buffer;
ADDRINT start_address = IMG_LowAddress(binary_image);
ADDRINT end_address = IMG_HighAddress(binary_image);
UINT32 size = end_address - start_address;
Buffer = (unsigned char *)malloc(size);
MYLOG("size to dump is %d" , size);
MYLOG("Start address is %08x" , start_address);
MYLOG("Start address is %08x" , end_address);
MYLOG("IMAGE NAME IS %s" , IMG_Name(binary_image));
PIN_SafeCopy(Buffer , (void const *)start_address , size);
memset(Entries, 0, sizeof(unsigned long) * 256);
for (unsigned long i = 0; i < size; i++)
Entries[Buffer[i]]++;
for (unsigned long i = 0; i < 256; i++)
{
double Temp = (double) Entries[i] / (double) size;
if (Temp > 0)
Entropy += - Temp*(log(Temp)*d1log2);
}
MYLOG("ENTROPY IS %f" , Entropy);
return Entropy;
}
float ProcInfo::getInitialEntropy(){
return this->InitialEntropy;
}
void ProcInfo::setInitialEntropy(float Entropy){
this->InitialEntropy = Entropy;
}