-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.cpp
231 lines (192 loc) · 6.67 KB
/
functions.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
221
222
223
224
225
226
227
228
229
230
231
#include "functions.h"
#include <windows.h>
#include <cstddef> // offsetof
bool GetNTHeader(void * pMap, DWORD dwSize, IMAGE_DOS_HEADER ** opDosH, IMAGE_NT_HEADERS ** opNTH, IMAGE_SECTION_HEADER ** opSecs, WORD * onSecs)
{
IMAGE_DOS_HEADER * pDosH;
IMAGE_NT_HEADERS * pNTH;
pDosH = (IMAGE_DOS_HEADER *)pMap;
if(dwSize > sizeof(IMAGE_DOS_HEADER) && pDosH->e_magic == IMAGE_DOS_SIGNATURE && pDosH->e_lfanew > 0 && pDosH->e_lfanew < 0x10000000)
{
pNTH = (IMAGE_NT_HEADERS *)((ULONG_PTR)pMap + pDosH->e_lfanew);
if(dwSize >= (pDosH->e_lfanew + sizeof(IMAGE_NT_HEADERS)) && pNTH->Signature == IMAGE_NT_SIGNATURE)
{
if(pNTH->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC ||
pNTH->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
{
if(dwSize >= (pDosH->e_lfanew + SizeOfPEHeader(pNTH)))
{
if(opDosH) *opDosH = pDosH;
if(opNTH) *opNTH = pNTH;
if(opSecs) *opSecs = IMAGE_FIRST_SECTION(pNTH);
if(onSecs) *onSecs = pNTH->FileHeader.NumberOfSections;
return true;
}
}
}
}
return false;
}
IMAGE_NT_HEADERS * GetNTHeader(void * pMap)
{
IMAGE_DOS_HEADER * pDosH;
IMAGE_NT_HEADERS * pNTH;
pDosH = (IMAGE_DOS_HEADER *)pMap;
if(pDosH->e_magic == IMAGE_DOS_SIGNATURE && pDosH->e_lfanew > 0 && pDosH->e_lfanew < 0x10000000)
{
pNTH = (IMAGE_NT_HEADERS *)((ULONG_PTR)pMap + pDosH->e_lfanew);
if(pNTH->Signature == IMAGE_NT_SIGNATURE)
{
if(pNTH->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC ||
pNTH->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
{
return pNTH;
}
}
}
return 0;
}
DWORD SizeOfPEHeader(const IMAGE_NT_HEADERS * pNTH)
{
return (offsetof(IMAGE_NT_HEADERS, OptionalHeader) + pNTH->FileHeader.SizeOfOptionalHeader + (pNTH->FileHeader.NumberOfSections * sizeof(IMAGE_SECTION_HEADER)));
}
bool IsPE64(const IMAGE_NT_HEADERS * pNTH)
{
return (pNTH->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC);
}
DWORD RVAToOffset(DWORD dwRVA, const void * pMap)
{
const IMAGE_NT_HEADERS * pNTH;
const IMAGE_SECTION_HEADER * SectionTable;
DWORD VAddress, VSize, ROffset, RSize, UpperBound;
unsigned int i;
pNTH = (IMAGE_NT_HEADERS *)((ULONG_PTR)pMap + ((IMAGE_DOS_HEADER *)pMap)->e_lfanew);
SectionTable = IMAGE_FIRST_SECTION(pNTH);
if(dwRVA < SectionTable[0].VirtualAddress) // RVA points to header
return dwRVA;
for(i=0; i < pNTH->FileHeader.NumberOfSections; i++)
{
VAddress = SectionTable[i].VirtualAddress;
VSize = AlignUp(SectionTable[i].Misc.VirtualSize, pNTH->OptionalHeader.SectionAlignment);
ROffset = SectionTable[i].PointerToRawData;
RSize = AlignUp(SectionTable[i].SizeOfRawData, pNTH->OptionalHeader.FileAlignment);
UpperBound = (RSize < VSize) ? RSize : VSize;
if((dwRVA >= VAddress) && (dwRVA < (VAddress + UpperBound)))
{
return (dwRVA - VAddress + ROffset);
}
}
return 0;
}
DWORD StripSection(void * pMap, DWORD dwFsize, DWORD nSection)
{
IMAGE_NT_HEADERS * pNTH;
IMAGE_SECTION_HEADER * pSH, * pSHC;
DWORD dwNewFsize;
DWORD Diff;
unsigned int i;
pNTH = GetNTHeader(pMap);
// section doesn't exist / only one section
if(nSection >= pNTH->FileHeader.NumberOfSections || pNTH->FileHeader.NumberOfSections == 1)
return 0;
pSHC = IMAGE_FIRST_SECTION(pNTH); // used in loop
pSH = &pSHC[nSection]; // section to delete
pNTH->FileHeader.NumberOfSections--;
if(nSection == pNTH->FileHeader.NumberOfSections)
{ // last section -> truncate at section start
dwNewFsize = pSH->PointerToRawData; // ????
pSH--;
if(IsPE64(pNTH)){
if(pSH->Misc.PhysicalAddress)
((IMAGE_NT_HEADERS64 *)pNTH)->OptionalHeader.SizeOfImage = pSH->VirtualAddress + pSH->Misc.VirtualSize;
else // WATCOM is always a bit special >:-)
((IMAGE_NT_HEADERS64 *)pNTH)->OptionalHeader.SizeOfImage = pSH->VirtualAddress + pSH->SizeOfRawData;
}
else{
if(pSH->Misc.PhysicalAddress)
((IMAGE_NT_HEADERS32 *)pNTH)->OptionalHeader.SizeOfImage = pSH->VirtualAddress + pSH->Misc.VirtualSize;
else
((IMAGE_NT_HEADERS32 *)pNTH)->OptionalHeader.SizeOfImage = pSH->VirtualAddress + pSH->SizeOfRawData;
}
}
else // not the last section
{
if(pSH->SizeOfRawData != 0)
{
Diff = AlignUp(pSH->SizeOfRawData, HDR3264(pNTH, OptionalHeader.FileAlignment));
dwNewFsize = dwFsize - Diff;
// copy section(s) after this section to the start of this section
memcpy((void *)((ULONG_PTR)pMap + pSH->PointerToRawData), (void *)((ULONG_PTR)pMap + pSH->PointerToRawData + Diff), dwFsize - pSH->PointerToRawData - Diff);
for(i=0; i <= pNTH->FileHeader.NumberOfSections; i++)
{
if(pSHC->PointerToRawData >= (pSH->PointerToRawData + Diff))
pSHC->PointerToRawData -= Diff;
pSHC++;
}
}
// fix section header
if(nSection == 0)
{
pSH->SizeOfRawData = 0;
pNTH->FileHeader.NumberOfSections++;
}
else
{
(pSH-1)->Misc.VirtualSize += pSH->Misc.VirtualSize;
memcpy(pSH, (pSH+1), sizeof(IMAGE_SECTION_HEADER) * (pNTH->FileHeader.NumberOfSections-nSection));
}
}
if(nSection != 0)
{ // Zerofill last section header
pSH = IMAGE_FIRST_SECTION(pNTH);
memset(&pSH[pNTH->FileHeader.NumberOfSections], 0, sizeof(IMAGE_SECTION_HEADER));
}
return dwNewFsize;
}
void FixVSizes(IMAGE_NT_HEADERS * pNTH)
{
IMAGE_SECTION_HEADER * pSH;
// fix virtual parts of the PE Header (a must for win2k)
pSH = IMAGE_FIRST_SECTION(pNTH);
for(WORD i=0; i < pNTH->FileHeader.NumberOfSections-1; i++)
{
pSH->Misc.VirtualSize = (pSH+1)->VirtualAddress - pSH->VirtualAddress;
pSH++;
}
// (pSH -> pointer to last section)
// align last section VSize to Sectionalignment
pSH->Misc.VirtualSize = AlignUp(pSH->Misc.VirtualSize, HDR3264(pNTH, OptionalHeader.SectionAlignment));
if(IsPE64(pNTH)){
if(pSH->Misc.PhysicalAddress)
((IMAGE_NT_HEADERS64 *)pNTH)->OptionalHeader.SizeOfImage = pSH->VirtualAddress + pSH->Misc.VirtualSize;
else
((IMAGE_NT_HEADERS64 *)pNTH)->OptionalHeader.SizeOfImage = pSH->VirtualAddress + pSH->SizeOfRawData;
}
else{
if(pSH->Misc.PhysicalAddress)
((IMAGE_NT_HEADERS32 *)pNTH)->OptionalHeader.SizeOfImage = pSH->VirtualAddress + pSH->Misc.VirtualSize;
else
((IMAGE_NT_HEADERS32 *)pNTH)->OptionalHeader.SizeOfImage = pSH->VirtualAddress + pSH->SizeOfRawData;
}
}
DWORD AlignUp(DWORD dwBadSize, DWORD dwAlignment)
{
dwAlignment--;
return ((dwBadSize+dwAlignment) & ~dwAlignment);
}
DWORD AlignDown(DWORD dwBadSize, DWORD dwAlignment)
{
dwAlignment--;
return (dwBadSize & ~dwAlignment);
}
bool ValidFileAlignment(DWORD dwAlign)
{
// lower and upper limits (pecoff spec)
const DWORD MIN_ALIGN = 0x200;
const DWORD MAX_ALIGN = 0x10000;
if((dwAlign & (dwAlign-1)) != 0) // power of 2 ?
return false;
if(dwAlign < MIN_ALIGN || dwAlign > 0x10000)
return false;
return true;
}