forked from upupupY/Sheller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnShell.cpp
227 lines (164 loc) · 5.76 KB
/
UnShell.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
#include "UnShell.h"
#include "./Stud/Stud.h"
#include <malloc.h>
#include "lz4.h"
#include "puPEinfoData.h"
extern _Stud* g_stu;
UnShell::UnShell()
{
PuPEInfo obj_puPe;
HANDLE tempHandle = obj_puPe.puFileHandle();
CloseHandle(tempHandle);
hFile = CreateFile(L"C:\\Users\\Administrator\\Desktop\\CompressionMask.exe", GENERIC_READ | GENERIC_WRITE, FALSE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwSize = GetFileSize(hFile, NULL);
m_Base = (void *)malloc(dwSize);
memset(m_Base, 0, dwSize);
DWORD dwRead = 0;
OVERLAPPED OverLapped = { 0 };
int nRetCode = ReadFile(hFile, m_Base, dwSize, &dwRead, &OverLapped);
pDosHander = (PIMAGE_DOS_HEADER)m_Base;
pHeadres = (PIMAGE_NT_HEADERS)(pDosHander->e_lfanew + (LONG)m_Base);
pSection = IMAGE_FIRST_SECTION(pHeadres);
m_NtAddress = (void*)pHeadres;
if ((fpFile = fopen("FileData.txt", "r+")) == NULL)
{
AfxMessageBox(L"文件打开失败");
}
}
UnShell::~UnShell()
{
fclose(fpFile);
}
// 恢复压缩的数据
BOOL UnShell::RepCompressionData()
{
// 把压缩的数据都申请空间保存--->文件200对齐拼接
// 加载stub中的数据里面保存了区段数据大小
m_studBase = LoadLibraryEx(L"E:\\VS项目\\加壳器\\Release\\Stud.dll", NULL, DONT_RESOLVE_DLL_REFERENCES);
g_stu = (_Stud*)GetProcAddress((HMODULE)m_studBase, "g_stud");
DWORD SectionCount = pHeadres->FileHeader.NumberOfSections;
/*===================文件中读取必要的数据================================*/
for (DWORD i = 0; i < SectionCount - 3; ++i)
{
fread(&g_stu->s_blen[i], sizeof(DWORD), 1, fpFile);
}
for (DWORD i = 0; i < 16; ++i)
{
fread(&g_stu->s_DataDirectory[i][0], sizeof(DWORD), 1, fpFile);
fread(&g_stu->s_DataDirectory[i][1], sizeof(DWORD), 1, fpFile);
// fscanf(fpFile, "%04x %04x", &g_stu->s_DataDirectory[i][0], &g_stu->s_DataDirectory[i][1]);
}
for (DWORD i = 0; i < SectionCount - 2; ++i)
{
fread(&g_stu->s_SectionOffsetAndSize[i][0], sizeof(DWORD), 1, fpFile);
fread(&g_stu->s_SectionOffsetAndSize[i][1], sizeof(DWORD), 1, fpFile);
// fscanf(fpFile, "%04x %04x", &g_stu->s_SectionOffsetAndSize[i][0], &g_stu->s_SectionOffsetAndSize[i][1]);
}
fread(&g_stu->s_dwOepBase, sizeof(DWORD), 1, fpFile);
/*========================================================================*/
TotaldwSize = 0;
for (DWORD i = 0; i < SectionCount - 2; ++i)
{
// 获取文件偏移及文件大小0是偏移 1是大小
TotaldwSize += g_stu->s_SectionOffsetAndSize[i][0];
}
// 申请
Sectionbuf = (char*)malloc(TotaldwSize);
// 文件中加密数据其实位置
DWORD DataStart = 0x400;
// 保存游标
DWORD Flag = 0;
PuPEInfo obj_pePE;
BYTE Name[] = ".com";
// 区段压缩的数据起始位置
PIMAGE_SECTION_HEADER address = (PIMAGE_SECTION_HEADER)obj_pePE.puGetSectionAddress((char*)m_Base, Name);
int nFlag = 0;
DWORD Address = address->PointerToRawData;
for (DWORD i = 0; i < SectionCount - 2; ++i)
{
if (g_stu->s_blen[nFlag] == 0)
{
nFlag += 1;
continue;
}
// 缓冲区 RVA+加载基址 缓冲区大小 压缩过去的大小
int nRet = LZ4_decompress_safe((char*)(Address + (DWORD)m_Base), &Sectionbuf[Flag], g_stu->s_blen[nFlag], g_stu->s_SectionOffsetAndSize[i][0]);
Address += g_stu->s_blen[i];
Flag += g_stu->s_SectionOffsetAndSize[i][0];
nFlag++;
}
return TRUE;
}
// 删除区段数据
BOOL UnShell::DeleteSectionInfo()
{
DWORD dwSectionCount = pHeadres->FileHeader.NumberOfSections;
PIMAGE_DATA_DIRECTORY pDataDirectory = (PIMAGE_DATA_DIRECTORY)pHeadres->OptionalHeader.DataDirectory;
// 修复数据目录表
for (DWORD i = 0; i < 16; ++i)
{
// 先读取数据
if (0 != g_stu->s_DataDirectory[i][0])
pDataDirectory->VirtualAddress = g_stu->s_DataDirectory[i][0];
if (0 != g_stu->s_DataDirectory[i][1])
pDataDirectory->Size = g_stu->s_DataDirectory[i][1];
++pDataDirectory;
}
// 修复所有区段的数据
for (DWORD i = 0; i < dwSectionCount - 2; ++i)
{
if (0 != g_stu->s_SectionOffsetAndSize[i][0])
pSection->SizeOfRawData = g_stu->s_SectionOffsetAndSize[i][0];
if (0 != g_stu->s_SectionOffsetAndSize[i][1])
pSection->PointerToRawData = g_stu->s_SectionOffsetAndSize[i][1];
++pSection;
}
// 区段个数 - 2;
pHeadres->FileHeader.NumberOfSections -= 2;
// 清空新增区段的PE结构(数据)
PIMAGE_SECTION_HEADER pSection_s = IMAGE_FIRST_SECTION(pHeadres);
DWORD NewdwSectionOfSize = (dwSectionCount - 2) * 0x28;
char* temp = (char*)malloc(80);
memset(temp, 0, 80);
// 清空加载的区段数据 修改不了里面数据
DWORD old = 0;
BYTE Name[] = ".mas";
BYTE Name1[] = ".com";
PuPEInfo pePu;
DWORD masAdd = (DWORD)pePu.puGetSectionAddress((char*)m_Base, Name);
VirtualProtect((char*)masAdd, 40, PAGE_READWRITE, &old);
memcpy((char*)masAdd, temp, 40);
VirtualProtect((char*)masAdd, 40, old, &old);
DWORD comAdd = (DWORD)pePu.puGetSectionAddress((char*)m_Base, Name1);
VirtualProtect((char*)comAdd, 40, PAGE_READWRITE, &old);
memcpy((char*)comAdd, temp, 40);
VirtualProtect((char*)comAdd, 40, old, &old);
free(temp);
temp = nullptr;
--pSection;
// 修复SizeofImage = 最后一个区段.VirtualAddress + 最后一个区段.SizeOfRawData按内存对齐粒度对齐的大小
pHeadres->OptionalHeader.SizeOfImage = pSection->VirtualAddress + pSection->SizeOfRawData;
// 恢复OEP
pHeadres->OptionalHeader.AddressOfEntryPoint = g_stu->s_dwOepBase;
return TRUE;
}
// 保存\收尾工作
BOOL UnShell::SaveUnShell()
{
DWORD Size = 0x400 + TotaldwSize;
// PE头 + 解压后的数据
UnShellNewFile = (char*)malloc(Size);
memcpy(UnShellNewFile, m_Base, 0x400);
memcpy(&UnShellNewFile[0x400], Sectionbuf, TotaldwSize);
// 写入exe程序完成压缩
DWORD dwWrite = 0; OVERLAPPED OverLapped;
// 创建文件
HANDLE Handle = CreateFile(L"C:\\Users\\Administrator\\Desktop\\UnShellNewPro.exe", GENERIC_READ | GENERIC_WRITE, FALSE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
// 写入文件
int nRet = WriteFile(Handle, UnShellNewFile, Size, &dwWrite, NULL);
// 关闭句柄
CloseHandle(Handle);
if (!nRet)
return FALSE;
return TRUE;
}