This commit is contained in:
Lenheart 2022-03-07 13:50:29 +08:00
parent bd510c2bac
commit 0b2e30605a
5 changed files with 175 additions and 0 deletions

View File

@ -144,10 +144,89 @@ void LenheartThread(void)
#include "inlinehook.h"
inlinehook* TEA = nullptr;
inlinehook* TEB = nullptr;
void JMPre(int address)
{
_asm
{
pop ebp
add esp, 0x00000004
ret
}
}
void qubb(int a1,int a2,int a3)
{
_asm
{
push esi
push edi
pushfd
cld
mov ecx, dword ptr[ebp + 0x10]
mov esi, dword ptr[ebp + 0x0c]
mov edi, dword ptr[ebp + 0x08]
rep movsb
popfd
pop edi
pop esi
}
}
void datec()
{
int ptrd;
int packtype;
int packlen;
ptrd = 0;
_asm
{
mov dword ptr[ebp - 0x04], ebx
}
//packtype = *(int*)(ptrd + 1);
qubb(packtype,ptrd + 1, 2);
qubb(packlen,ptrd + 3, 2);
//packlen = *(int*)(ptrd + 3);
}
void hookadd()
{
_asm
{
pushad
pushfd
}
datec();
_asm
{
popfd
popad
}
_asm
{
mov ecx, dword ptr[ebp - 0x0000012c]
}
JMPre(7333970);
}
DWORD WINAPI MyThreadProc2(LPVOID pParam)
{
std::cout << u8"开始HOOK" << std::endl;
TEA = new inlinehook(0x6FE84C, (int)hookadd);
//修改地址
TEA->Motify_address();
return 0;

36
test/inlinehook.cpp Normal file
View File

@ -0,0 +1,36 @@
#include "pch.h"
#include "inlinehook.h"
#include <Windows.h>
DWORD inlinehook::Motify_memory_attributes(int address, DWORD attributes)
{
DWORD Old_attributes;
VirtualProtect(reinterpret_cast<void*>(address), Byte_Length, attributes, &Old_attributes);
return Old_attributes;
}
void inlinehook::Motify_address()
{
DWORD attributes = Motify_memory_attributes(m_original_address);
//写入我们构造的BYTE 实现 hook
memcpy(reinterpret_cast<void*>(m_original_address), m_self_byte, Byte_Length);
//恢复内存属性
Motify_memory_attributes(m_original_address, attributes);
}
void inlinehook::Restore_address()
{
DWORD attributes = Motify_memory_attributes(m_original_address);
//写入原始的BYTE 实现 hook
memcpy(reinterpret_cast<void*>(m_original_address), m_original_byte, Byte_Length);
//恢复内存属性
Motify_memory_attributes(m_original_address, attributes);
}

52
test/inlinehook.h Normal file
View File

@ -0,0 +1,52 @@
#pragma once
constexpr int Byte_Length = 5;
class inlinehook
{
private:
using uchar = unsigned char;
//原始函数处的汇编指令
uchar m_original_byte[Byte_Length];
//我们构造的汇编指令
uchar m_self_byte[Byte_Length];
//原始函数地址
int m_original_address;
//我们函数地址
int m_self_address;
DWORD Motify_memory_attributes(int address, DWORD attributes = PAGE_EXECUTE_READWRITE);
public:
inlinehook(int original_address, int self_address):m_original_address(original_address), m_self_address(self_address)
{
//构造jmp
m_self_byte[0] = '\xe9';
//计算偏移
int offset = self_address - (original_address + Byte_Length);
//构造跳转到我们的函数BYTE
memcpy(&m_self_byte[1], &offset, Byte_Length - 1);
//修改内存属性
DWORD attributes = Motify_memory_attributes(original_address);
//保存原始的函数地址的BYTE
memcpy(&m_original_byte, reinterpret_cast<void*>(original_address), Byte_Length);
//恢复内存属性
Motify_memory_attributes(original_address, attributes);
}
//修改地址
void Motify_address();
//还原地址
void Restore_address();
};

View File

@ -171,11 +171,13 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="framework.h" />
<ClInclude Include="inlinehook.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="RSAC.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp" />
<ClCompile Include="inlinehook.cpp" />
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>

View File

@ -24,6 +24,9 @@
<ClInclude Include="RSAC.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="inlinehook.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp">
@ -38,5 +41,8 @@
<ClCompile Include="RSAC.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="inlinehook.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>