forked from TimelifeCzy/Shell_Protect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddSection.cpp
199 lines (179 loc) · 4.57 KB
/
AddSection.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
#include "stdafx.h"
#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
AddSection::AddSection()
{
}
AddSection::~AddSection()
{
if (pFileBaseData) {
free(pFileBaseData);
pFileBaseData = nullptr;
}
if (FileHandle) {
CloseHandle(FileHandle);
FileHandle = nullptr;
}
}
BOOL AddSection::Init() {
Free();
SinglePuPEInfo::instance()->puOpenFileLoadEx(m_FilePath);
pFileBaseData = SinglePuPEInfo::instance()->puGetImageBase();
pNtHeadre = SinglePuPEInfo::instance()->puGetNtHeadre();
pSectionHeadre = SinglePuPEInfo::instance()->puGetSection();
FileSize = SinglePuPEInfo::instance()->puFileSize();
FileHandle = SinglePuPEInfo::instance()->puFileHandle();
OldOep = SinglePuPEInfo::instance()->puGetOEP();
return true;
}
BOOL AddSection::Free() {
SinglePuPEInfo::instance()->puClearPeData();
if (pFileBaseData) {
pFileBaseData = nullptr;
}
if (FileHandle) {
FileHandle = nullptr;
}
if (m_newlpBase) {
free(m_newlpBase);
m_newlpBase = nullptr;
}
pNtHeadre = nullptr;
pSectionHeadre = nullptr;
SectionSizeof = 0;
FileSize = 0;
return true;
}
BOOL AddSection::ModifySectionNumber()
{
PIMAGE_NT_HEADERS pNtHeaders = (PIMAGE_NT_HEADERS)this->pNtHeadre;
if (pNtHeaders) {
DWORD temp = pNtHeaders->FileHeader.NumberOfSections;
SectionSizeof = temp * 0x28;
pNtHeaders->FileHeader.NumberOfSections += 0x1;
return TRUE;
}
return false;
}
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;
if (!PtrpSection)
return false;
pSectionAddress += 0x28;
NewpSection = (PIMAGE_SECTION_HEADER)pSectionAddress;
memcpy(NewpSection->Name, Name, sizeof(Name));
DWORD dwtemps = PtrpSection->VirtualAddress + PtrpSection->SizeOfRawData;
if (!dwtemps)
return false;
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;
if (pNt) {
pNt->OptionalHeader.AddressOfEntryPoint = NewpSection->VirtualAddress;
return TRUE;
}
return false;
}
BOOL AddSection::ModifySizeofImage()
{
PIMAGE_NT_HEADERS pNt = (PIMAGE_NT_HEADERS)pNtHeadre;
if (pNt) {
pNt->OptionalHeader.SizeOfImage = NewpSection->VirtualAddress + NewpSection->SizeOfRawData;
pNt->OptionalHeader.DllCharacteristics = 0x8000;
return TRUE;
}
return FALSE;
}
BOOL AddSection::AddNewSectionByteData(const DWORD & size)
{
const int newFileSize = FileSize + size;
m_newlpBase = (char *)malloc(newFileSize);
if (!m_newlpBase || (nullptr == m_newlpBase))
return false;
memset(m_newlpBase, 0, newFileSize);
if (pFileBaseData) {
memcpy(m_newlpBase, pFileBaseData, FileSize);
}
else
return false;
DWORD dWriteSize = 0; OVERLAPPED OverLapped = { 0 };
int nRetCode = WriteFile(FileHandle, m_newlpBase, (FileSize + size), &dWriteSize, &OverLapped);
if (m_newlpBase) {
free(m_newlpBase);
m_newlpBase = nullptr;
}
if (dWriteSize == 0){
AfxMessageBox(L"CreateSection WriteFIle faliuer");
return FALSE;
}
return TRUE;
}