-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPE.cpp
150 lines (122 loc) · 2.98 KB
/
PE.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
#include "stdafx.h"
#include <locale.h>
#include <vector>
using namespace std;
INT32 g_bitmapBase = 0;
BOOL g_bitmapBaseSet = FALSE;
class CAddress {
public:
INT32 realAddress;
BOOL valid;
};
class CSegment {
private:
INT32 addressBase;
INT32 bitmapAddress;
DWORD bitmap;
vector<CAddress> vectorAddress;
vector<CAddress>::iterator pd;
CAddress tmpAddress;
CHAR outputBuffer[256];
ULONG64 displacement;
ULONG cb;
public:
CSegment(INT32 argAddressBase):displacement(0),cb(0)
{
if (g_bitmapBaseSet == FALSE)
{
return;
}
addressBase = argAddressBase;
bitmapAddress = g_bitmapBase+addressBase*4; //edx + eax *4
ReadMemory(bitmapAddress, &bitmap, 4, &cb); //mov edx,[ ]
if(bitmap!=0)
dprintf("Bitmap:0x%p\n", bitmap);
for (int i = 0; i < 16; i++)
{
tmpAddress.realAddress = addressBase * 0x100 + 0x10 * i;
tmpAddress.valid = FALSE;
if(i!=0)
bitmap = bitmap >> 2;
if((bitmap & 1) == 1)
{
tmpAddress.valid = true;
}
vectorAddress.push_back(tmpAddress);
}//end of for input
for (pd = vectorAddress.begin(); pd != vectorAddress.end(); pd++)
{
if ((*pd).valid == 1)
{
GetSymbol((*pd).realAddress, outputBuffer, &displacement);
dprintf("Valid Target:%p",(*pd).realAddress);
for(int i=0;i<256;i++)
dprintf("%c", outputBuffer[i]);
dprintf("\n");
//dprintf("+0x%p\n", displacement);
}
}//end of for output
}
};
INT32 parseHexFromString(PCSTR args)
{
PCSTR pointer = args;
INT32 result;
if ((args[0] == '0') && (args[1] == 'x'))
{
pointer = &(args[3]);
}
sscanf(pointer, "%x", &result);
return result;
}
HRESULT CALLBACK setbitmapbase(PDEBUG_CLIENT4 Client, PCSTR args)
{
if (!args || !*args)
{
dprintf("No argument found. A base for bitmap should be given.\n");
return S_FALSE;
}
INIT_API();
g_bitmapBase = parseHexFromString(args);
g_bitmapBaseSet = TRUE;
dprintf("bitmapBase:%p\n", g_bitmapBase);
EXIT_API();
return S_OK;
}
HRESULT CALLBACK getvalidenum(PDEBUG_CLIENT4 Client, PCSTR args)
{
INT32 addressBase = 0;
INT32 tryAddress = 0;
INT32 tmpMemory[1] = {0,};
ULONG cb = 0;
INIT_API();
for (addressBase = 0x000001; addressBase <= 0x7FFFFF; addressBase++)
{
try
{
tryAddress = addressBase * 0x100;
//dprintf("tryAddress:%p\n", tryAddress);
ReadMemory(tryAddress,&tmpMemory,4,&cb);
CSegment * validateSegment = new CSegment(addressBase);
delete validateSegment;
}
catch (exception ex)
{
dprintf("exception\n");
}
}
EXIT_API();
return S_OK;
}
HRESULT CALLBACK help(PDEBUG_CLIENT4 Client, PCSTR args)
{
INIT_API();
dprintf("Help for CFGValidEnum.dll\n"
">help = Shows this help.\n"
">setbitmapbase = Set base of bitmap.Necessary before getvalidenum.\n"
" i.e: setbitmapbase 04000010\n"
">getvalidenum = Get all of the valid call targets.\n"
);
EXIT_API();
return S_OK;
}