X86,X64Shellcode框架

#include<windows.h>

#pragma code_seg("shell")
#pragma comment(linker,"/entry:shellcode")
//#pragma comment (linker,"/merge:.data=shell")
//#pragma comment (linker,"/merge:.rdata=shell")
#pragma comment(linker,"/SECTION:shell,RWE")
DWORD WINAPI GetHash(const char* szBuffer);
FARPROC WINAPI GetProcAddr(LONG_PTR hModule, DWORD dhash);
using PFUNGetProcAddress = FARPROC(WINAPI*)(HMODULE, LPCSTR);
using PFUNLoadLibraryA = HMODULE(WINAPI*)(LPCSTR);
using PFUNMessageBoxA = int(WINAPI*)(_In_opt_ HWND hWnd, _In_opt_ LPCSTR lpText, _In_opt_ LPCSTR lpCaption, _In_ UINT uType);
void shellcode() {
#ifndef _WIN64
	DWORD Kernel32Base;
	__asm {
		mov  eax, fs: [0x30]		//TEB->PEB
		mov  eax, [eax + 0x0c]		//PEB->LDR
		mov  eax, [eax + 0xc]		//LDR->.exe
		mov eax, [eax]				//exe->ntdll.dll
		mov eax, [eax]				//ntdll.dll->kernel32.dll
		mov eax, [eax + 0x18]		//kernel32->kernel32.dll.moudlehandle
		mov Kernel32Base, eax
	}
#else
	LONG_PTR Kernel32Base;
/*	_TEB* pTeb = NtCurrentTeb();*/
	PULONGLONG pPeb = (PULONGLONG)__readgsqword(0x60);
	PULONGLONG pLdr = (PULONGLONG) * (PULONGLONG)((ULONGLONG)pPeb + 0x18);
	PULONGLONG InLoadOrderModuleList = (PULONGLONG)((ULONGLONG)pLdr + 0x10);
	PULONGLONG pModuleExe = (PULONGLONG)*InLoadOrderModuleList;
	PULONGLONG pModuleNtdll = (PULONGLONG)*pModuleExe;
	PULONGLONG pModuleKernel32 = (PULONGLONG)*pModuleNtdll;
	Kernel32Base = pModuleKernel32[6];
#endif
	//	PFUNGetProcAddress pfunGetProcAddr = (PFUNGetProcAddress)GetProcAddr(asmKernel32Moudle,0xb775fbf);
	char szUser32dll[] = { 'u','s','e','r','3','2','.','d','l','l',0 };
	PFUNLoadLibraryA pfunLoadLibraryA = (PFUNLoadLibraryA)GetProcAddr(Kernel32Base, 0x641922E8);
	HMODULE hUser32 = pfunLoadLibraryA(szUser32dll);
	PFUNMessageBoxA pfunMessageBoxA = (PFUNMessageBoxA)GetProcAddr((LONG_PTR)hUser32, 0xd43c7014);
	pfunMessageBoxA(NULL, NULL, NULL, MB_OK);
	return;
}
FARPROC WINAPI GetProcAddr(LONG_PTR hModule, DWORD dhash) {
	PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)hModule;
	PIMAGE_NT_HEADERS pNtHeader = (PIMAGE_NT_HEADERS)(hModule + pDosHeader->e_lfanew);
	PIMAGE_EXPORT_DIRECTORY pExportDir = (PIMAGE_EXPORT_DIRECTORY)(hModule + pNtHeader->OptionalHeader.DataDirectory->VirtualAddress);
	DWORD* pFunName = (DWORD*)(hModule + pExportDir->AddressOfNames);
	DWORD* pFun = (DWORD*)(hModule + pExportDir->AddressOfFunctions);
	WORD* pOder = (WORD*)(hModule + pExportDir->AddressOfNameOrdinals);
	while (GetHash((const char*)hModule + *pFunName) != dhash) {
		pFunName++;
		pOder++;
	}
	return (FARPROC)(pFun[*pOder] + hModule);
}
inline DWORD WINAPI GetHash(const char* szBuffer) {
	DWORD dHash = *szBuffer;
	while (*szBuffer++) {
		dHash = ((dHash << 25) | dHash >> 7);
		dHash += *szBuffer;
	}
	return dHash;
}
#pragma code_seg()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值