Skip to content

Commit 7339743

Browse files
author
czy
committed
sources code optimization.
1 parent 68120ec commit 7339743

16 files changed

Lines changed: 103 additions & 85 deletions

AddSection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ BOOL AddSection::Init() {
3434
pSectionHeadre = SinglePuPEInfo::instance()->puGetSection();
3535
FileSize = SinglePuPEInfo::instance()->puFileSize();
3636
FileHandle = SinglePuPEInfo::instance()->puFileHandle();
37-
OldOep = SinglePuPEInfo::instance()->puOldOep();
37+
OldOep = SinglePuPEInfo::instance()->puGetOEP();
3838
return true;
3939
}
4040

CombatShell/CombatShell.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ void RepairTheIAT()
432432

433433
#else
434434
// IAT
435-
byte OpCode[] = { 0xe8, 0x01, 0x00, 0x00,
435+
BYTE OpCode[] = { 0xe8, 0x01, 0x00, 0x00,
436436
0x00, 0xe9, 0x58, 0xeb,
437437
0x01, 0xe8, 0xb8, 0x8d,
438438
0xe4, 0xd8, 0x62, 0xeb,
@@ -568,7 +568,7 @@ DWORD ProcessCallBack(LPVOID lpThreadParameter)
568568
MyFindWindowExW = (FnFindWindowExW)puGetProcAddress(g_stud.s_User32, 0x4818F71E);
569569
MyPostMessageW = (FnPostMessage)puGetProcAddress(g_stud.s_User32, 0x386047E);
570570

571-
HWND hCalc, hbutton;
571+
HWND hCalc = nullptr, hbutton = nullptr;
572572
HWND* hWnd = (HWND*)lpThreadParameter;
573573
static int i = 10;
574574
MySleep(10000);

CombatShell/CombatShell.rc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ END
5151
//
5252

5353
VS_VERSION_INFO VERSIONINFO
54-
FILEVERSION 24,7,3,1
54+
FILEVERSION 24,7,6,1
5555
PRODUCTVERSION 1,0,0,1
5656
FILEFLAGSMASK 0x3fL
5757
#ifdef _DEBUG
@@ -67,7 +67,7 @@ BEGIN
6767
BEGIN
6868
BLOCK "080404b0"
6969
BEGIN
70-
VALUE "FileVersion", "24.7.3.1"
70+
VALUE "FileVersion", "24.7.6.1"
7171
VALUE "InternalName", "CombatSh.dll"
7272
VALUE "LegalCopyright", "Copyright (C) 2024"
7373
VALUE "OriginalFilename", "CombatSh.dll"

CombatShell/CombatShell.vcxproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@
3131
<UseDebugLibraries>true</UseDebugLibraries>
3232
<PlatformToolset>v142</PlatformToolset>
3333
<CharacterSet>Unicode</CharacterSet>
34+
<SpectreMitigation>false</SpectreMitigation>
3435
</PropertyGroup>
3536
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
3637
<ConfigurationType>DynamicLibrary</ConfigurationType>
3738
<UseDebugLibraries>false</UseDebugLibraries>
3839
<PlatformToolset>v142</PlatformToolset>
3940
<WholeProgramOptimization>true</WholeProgramOptimization>
4041
<CharacterSet>Unicode</CharacterSet>
42+
<SpectreMitigation>false</SpectreMitigation>
4143
</PropertyGroup>
4244
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
4345
<ConfigurationType>DynamicLibrary</ConfigurationType>
@@ -116,13 +118,16 @@
116118
<PrecompiledHeader>Use</PrecompiledHeader>
117119
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
118120
<Optimization>Disabled</Optimization>
121+
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
119122
</ClCompile>
120123
<Link>
121124
<SubSystem>Windows</SubSystem>
122125
<EnableCOMDATFolding>true</EnableCOMDATFolding>
123126
<OptimizeReferences>true</OptimizeReferences>
124127
<GenerateDebugInformation>true</GenerateDebugInformation>
125128
<EnableUAC>false</EnableUAC>
129+
<AdditionalDependencies>liblz4_static.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
130+
<AdditionalLibraryDirectories>../lz4/static/</AdditionalLibraryDirectories>
126131
</Link>
127132
</ItemDefinitionGroup>
128133
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@@ -173,6 +178,10 @@
173178
<ClInclude Include="resource.h" />
174179
</ItemGroup>
175180
<ItemGroup>
181+
<ClCompile Include="..\lz4\include\lz4.c">
182+
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">NotUsing</PrecompiledHeader>
183+
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
184+
</ClCompile>
176185
<ClCompile Include="..\quick\quicklz.c">
177186
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotUsing</PrecompiledHeader>
178187
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>

CombatShell/CombatShell.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
<ClCompile Include="..\quick\quicklz.c">
6666
<Filter>quick</Filter>
6767
</ClCompile>
68+
<ClCompile Include="..\lz4\include\lz4.c">
69+
<Filter>lz4</Filter>
70+
</ClCompile>
6871
</ItemGroup>
6972
<ItemGroup>
7073
<ResourceCompile Include="CombatShell.rc">

CombatShellManage.rc

0 Bytes
Binary file not shown.

CompressionData.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,6 @@ BOOL CompressionData::EncryptionSectionData(
123123
// 压缩PE区段数据
124124
BOOL CompressionData::CompressSectionData()
125125
{
126-
CString csTmep;
127-
std::string sTagetDirectory = "";
128-
std::wstring wsTagetDirectory = L"";
129-
{
130-
csTmep = m_MasterStaticTextStr;
131-
int n = csTmep.ReverseFind('\\') + 1;
132-
int m = csTmep.GetLength() - n;
133-
wsTagetDirectory = csTmep.Left(n);
134-
csTmep = csTmep.Right(m);
135-
}
136-
sTagetDirectory = g_CombatShellDataLocalFile;
137-
138126
std::string sDriectory = "";
139127
std::string sCombatShellPath = "";
140128
CodeTool::CGetCurrentDirectory(sDriectory);
@@ -208,7 +196,7 @@ BOOL CompressionData::CompressSectionData()
208196
DWORD ComressTotalSize = 0;
209197

210198
// 注意修复-改为程序Name_FileData.txt - 保存本地数据记录,脱壳使用.
211-
if ((fpFile = fopen(sTagetDirectory.c_str(), "wb+")) == NULL)
199+
if ((fpFile = fopen(g_CombatShellDataLocalFile, "wb+")) == NULL)
212200
{
213201
AfxMessageBox(L"CombatShell 打开创建失败.");
214202
return false;
@@ -352,6 +340,14 @@ BOOL CompressionData::CompressSectionData()
352340
CleanDirectData(ComressNewBase, ComressTotalSize, Size);
353341

354342
// Create File
343+
std::wstring wsTagetDirectory = L"";
344+
{
345+
CString csTmep = m_MasterStaticTextStr;
346+
int n = csTmep.ReverseFind('\\') + 1;
347+
int m = csTmep.GetLength() - n;
348+
wsTagetDirectory = csTmep.Left(n);
349+
csTmep = csTmep.Right(m);
350+
}
355351
const std::wstring wsMaskCompre = (wsTagetDirectory + L"CompressionMask.exe").c_str();
356352
HANDLE HandComprele = CreateFile(wsMaskCompre.c_str(), GENERIC_READ | GENERIC_WRITE, FALSE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
357353

MasterWindows.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ BOOL MasterWindows::NewSection()
102102
return false;
103103

104104
SingleAddSection::instance()->puInti(m_MasterStaticTextStr);
105+
m_dwOldOEP = SinglePuPEInfo::instance()->puGetOEP();
105106
SingleAddSection::instance()->puModifySectioNumber();
106107
nRet = SingleAddSection::instance()->puModifySectionInfo(Name, SectionSize);
107108
if (!nRet) {
@@ -274,7 +275,7 @@ void MasterWindows::OnBnClickedButton1()
274275

275276
// 3. CombatShell 数据拷贝/操作
276277
m_MasterStaticTextStr = (csTargetDirectory + "CompressionMask.exe").GetBSTR();
277-
if (!SingleStudData::instance()->puInit(m_MasterStaticTextStr)) {
278+
if (!SingleStudData::instance()->puInit(m_MasterStaticTextStr, m_dwOldOEP)) {
278279
AfxMessageBox(L"studData failuer!");
279280
return;
280281
}
@@ -292,10 +293,10 @@ void MasterWindows::OnBnClickedButton1()
292293
m_MasterStaticTextStr = nStr;
293294
}
294295
ShowPEInfoData(m_MasterStaticTextStr);
295-
AfxMessageBox(m_MasterStaticTextStr + L" Success!");
296+
AfxMessageBox((m_MasterStaticTextStr + L" Success!").GetString());
296297
}
297298
else
298-
AfxMessageBox(m_MasterStaticTextStr + L" Failure!");
299+
AfxMessageBox((m_MasterStaticTextStr + L" Failure!").GetString());
299300
}
300301

301302
// PE View
@@ -362,38 +363,39 @@ void MasterWindows::OnBnClickedButton2()
362363
UpdateData(TRUE);
363364
if (m_MasterStaticTextStr.IsEmpty())
364365
{
365-
AfxMessageBox(L"请先拖入文件");
366+
MessageBox(L"请先拖入文件");
366367
return;
367368
}
368369

369-
// 判断是否我们的壳,否则不给脱壳
370+
// 判断是否我们的壳,否则不给脱壳(未检测.)
370371
UnShllerProcPath = m_MasterStaticTextStr;
371372

372373
UnShell obj_Unshell;
373374
if (!obj_Unshell.puUnShell()) {
374-
AfxMessageBox(L"puUnShell error");
375+
MessageBox(L"puUnShell error");
375376
return;
376377
}
377378
if (!obj_Unshell.puRepCompressionData()) {
378-
AfxMessageBox(L"puRepCompressionData error.");
379+
MessageBox(L"puRepCompressionData error.");
379380
return;
380381
}
381382
if(!obj_Unshell.puDeleteSectionInfo()) {
382-
AfxMessageBox(L"puDeleteSectionInfo error.");
383+
MessageBox(L"puDeleteSectionInfo error.");
383384
return;
384385
}
385386

386387
if (obj_Unshell.puSaveUnShell())
387388
{
388-
DeleteFile(m_MasterStaticTextStr);
389389
const std::wstring sUnShellPath = CodeTool::string2wstring(obj_Unshell.puGetUnShellPath().c_str()).c_str();
390+
// Clear
391+
obj_Unshell.puClose();
392+
DeleteFile(m_MasterStaticTextStr);
390393
int nRet = CopyFile(sUnShellPath.c_str(), m_MasterStaticTextStr, FALSE);
391394
if (nRet) {
392-
m_MasterStaticTextStr = sUnShellPath.c_str();
393395
DeleteFile(sUnShellPath.c_str());
394396
DeleteFileA(g_CombatShellDataLocalFile);
395397
}
396-
AfxMessageBox(L"puSaveUnShell_Success");
398+
MessageBox(L"puSaveUnShell_Success.");
397399
}
398400
ShowPEInfoData(m_MasterStaticTextStr);
399401
}

MasterWindows.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ class MasterWindows : public CDialogEx
4545
afx_msg void OnPaint();
4646
CStatic m_bitmapZionloab;
4747

48+
DWORD m_dwOldOEP = 0;
4849
};

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ PE虚拟壳框架,项目意图给学习软件保护加壳的初学者提供一
1010

1111
![image](https://github.com/TimelifeCzy/Shell_Protect/blob/main/readmepng/1.png)
1212

13-
2. 重点说一下虚拟机:虚拟壳目前仅支持x64加代码片段(需要稍作修改),这只是一个示例和思路,如何自己编写虚拟机,运行态保存上下文环境及维护堆栈。
13+
2. 一键加壳成功后, 被加壳程序同级别会生成FileName_CombatShellData.dat文件,文件本身是原PE数据,仅脱壳使用,计划是保存新增节表中,简单方便化就本地存储.
14+
```
15+
压缩节表数据 | 清理的数据目录16个) + 清理的记录节表数据 | 原始的OEP
16+
```
17+
18+
3. 虚拟机:虚拟壳目前仅支持x64加代码片段(需要稍作修改),这只是一个示例和思路,如何自己编写虚拟机,运行态保存上下文环境及维护堆栈。
1419

1520
- 对需要加密代码段进行Vmcode,简单点说加密代码,dll中壳代码执行vmentry进入虚拟机,默认x32直接进入壳main函数未Vm。
1621

@@ -28,18 +33,18 @@ PE虚拟壳框架,项目意图给学习软件保护加壳的初学者提供一
2833

2934
项目中的虚拟机没有高级算法,只是简单加密用来阐述过程,Vmcode分析引擎因为能力/精力有限,没有去构造Vmcode代码分析引擎,只是构造了解密后代码分析引擎挂钩handle处理。再加壳时候记录了加密汇编大小/长度/基于该代码段的起始偏移,用来做为Vmcode分析引擎使用,快速解密和处理分发。
3035

36+
工程中有一处硬编码,void CompressionData::VmcodeEntry(), 可以做反汇编获取行数优化.
3137
**注意:本项目仅支持加壳器中的Main函数,只对Main汇编映射指令进行了处理,未处理其它指令。**
3238

3339
造轮子的意义在于学习理解:理解虚拟机和指令集映射,理解虚拟机结构和协同工作。
34-
3540
推荐专业的虚拟机分析引擎(看雪版主玩命): https://github.com/devilogic/cerberus.git ,加密解密书籍有配套虚拟壳代码(没有看过),也可以参考。
3641

3742
软件保护技术还可应用于免杀,当然可以扩展更深层次了解杀软检测及反检测。
3843

3944
![image](https://github.com/TimelifeCzy/Shell_Protect/blob/main/readmepng/5.png)
4045
![image](https://github.com/TimelifeCzy/Shell_Protect/blob/main/readmepng/6.png)
4146

42-
项目仅用于学习,请大家重视
47+
项目不提供Release版本,仅用于学习和研究
4348

4449
## Stargazers over time
4550

0 commit comments

Comments
 (0)