-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAddSection.cpp
More file actions
166 lines (124 loc) · 3.57 KB
/
AddSection.cpp
File metadata and controls
166 lines (124 loc) · 3.57 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
#include "AddSection.h"
#include "puPEinfoData.h"
// x64 asm
#ifdef _WIN64
extern "C" void __stdcall AsmCountTemp(PVOID dwdata);
extern "C" void __stdcall AsmCountTemp1(PVOID dwdata);
#else
#endif
char* AddSection::newlpBase = nullptr;
AddSection::AddSection()
{
PuPEInfo obj_PuPE;
pFileBaseData = obj_PuPE.puGetImageBase();
pNtHeadre = obj_PuPE.puGetNtHeadre();
pSectionHeadre = obj_PuPE.puGetSection();
FileSize = obj_PuPE.puFileSize();
FileHandle = obj_PuPE.puFileHandle();
OldOep = obj_PuPE.puOldOep();
}
AddSection::~AddSection()
{
}
BOOL AddSection::ModifySectionNumber()
{
PIMAGE_NT_HEADERS pNtHeaders = (PIMAGE_NT_HEADERS)this->pNtHeadre;
DWORD temp = pNtHeaders->FileHeader.NumberOfSections;
SectionSizeof = temp * 0x28;
pNtHeaders->FileHeader.NumberOfSections += 0x1;
return TRUE;
}
BOOL AddSection::ModifySectionInfo(const BYTE* Name, const DWORD & size)
{
#ifdef _WIN64
DWORD64 pSectionAddress = (DWORD64)pSectionHeadre;
#else
DWORD pSectionAddress = (DWORD)pSectionHeadre;
#endif
pSectionAddress = pSectionAddress + SectionSizeof - 0x28;
PIMAGE_SECTION_HEADER PtrpSection = (PIMAGE_SECTION_HEADER)pSectionAddress;
pSectionAddress += 0x28;
NewpSection = (PIMAGE_SECTION_HEADER)pSectionAddress;
memcpy(NewpSection->Name, Name, sizeof(Name));
DWORD dwtemps = PtrpSection->VirtualAddress + PtrpSection->SizeOfRawData;
DWORD Temp = 0;
#ifdef _WIN64
// x64下使用,因为不涉及__int64类型,所以汇编使用同一套即可
AsmCountTemp(&dwtemps);
NewpSection->VirtualAddress = dwtemps;
Temp = PtrpSection->SizeOfRawData + PtrpSection->PointerToRawData;
AsmCountTemp1(&Temp);
// check arg
if (!dwtemps || !Temp)
return 0;
#else
__asm{
pushad;
mov esi, dwtemps;
mov eax, dwtemps;
mov edx, 0x1;
mov cx, 0x1000;
div cx;
test dx, dx;
jz MemSucess
shr dx, 12;
inc dx;
shl dx, 12;
add esi, edx;
shr esi, 12;
shl esi, 12;
mov dwtemps, esi;
MemSucess:
popad
}
NewpSection->VirtualAddress = dwtemps;
Temp = PtrpSection->SizeOfRawData + PtrpSection->PointerToRawData;
__asm{
pushad;
mov esi, Temp;
mov edx, 0x1;
mov eax, Temp;
mov ecx, 0x200;
div cx;
test dx, dx;
jz FileSucess
xor eax, eax
mov ax, 0x200;
sub ax, dx;
add esi, eax;
mov Temp, esi;
FileSucess:
popad
}
#endif // _WIN64
NewpSection->PointerToRawData = Temp;
NewpSection->SizeOfRawData = size;
NewpSection->Misc.VirtualSize = NewpSection->SizeOfRawData;
NewpSection->Characteristics = 0xE00000E0;
return TRUE;
}
BOOL AddSection::ModifyProgramEntryPoint()
{
PIMAGE_NT_HEADERS pNt = (PIMAGE_NT_HEADERS)pNtHeadre;
pNt->OptionalHeader.AddressOfEntryPoint = NewpSection->VirtualAddress;
return TRUE;
}
BOOL AddSection::ModifySizeofImage()
{
PIMAGE_NT_HEADERS pNt = (PIMAGE_NT_HEADERS)pNtHeadre;
pNt->OptionalHeader.SizeOfImage = NewpSection->VirtualAddress + NewpSection->SizeOfRawData;
pNt->OptionalHeader.DllCharacteristics = 0x8000;
return TRUE;
}
BOOL AddSection::AddNewSectionByteData(const DWORD & size)
{
newlpBase = (char *)malloc(FileSize + size);
memset(newlpBase, 0, (FileSize + size));
memcpy(newlpBase, pFileBaseData, FileSize);
free(pFileBaseData);
DWORD dWriteSize = 0; OVERLAPPED OverLapped = { 0 };
int nRetCode = WriteFile(FileHandle, newlpBase, (FileSize + size), &dWriteSize, &OverLapped);
free(newlpBase);
if (dWriteSize == 0){ AfxMessageBox(L"CreateSection WriteFIle faliuer"); return FALSE; }
return TRUE;
}