内存写入 隐藏模块注入

啦啦啦啦啦啦

思路:
1.获取自身SizeOfImage ImageBase
2.远程申请注入进程SizeOfImage大小的内存 内存位置随机 获得内存位置:ProcessAddr
3.申请一段SizeOfImage大小缓冲区,写入自身程序 内存位置:ImageBase
4.修复Image重定位表(参数为:ProcessAddr)
5.写入缓冲区ProcessAddr,SizeOfImage
6.远程线程申请入口点为DWORD WINAPI(LPVOID lparameter)
7.入口点修复导入表,因为有的dll没有在进程里加载,有的dll入口点不对
//main下面的代码是自己写的函数,如果要复制粘贴记得整理
//注释写的不好后期加的不然里面都没有注释😓

#include "MyTools.h"
#include "MyTools2.h"
//IATHook测试是否注入成功 ImportTable修复是否成功
LPVOID pOldFunAddr = GetProcAddress(LoadLibrary("user32.dll"), "MessageBoxA");

INT WINAPI MyMessageBox(HWND hwnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType) {
	typedef INT(WINAPI *MESSAGEBOX) (HWND hwnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType);
	DWORD dwRet = ((MESSAGEBOX)pOldFunAddr)(hwnd, "HOOK SUCEESS", "WARNING", uType);
	printf("parameter:%x %x %x %x return:%x\n", hwnd, lpText, lpCaption, uType, dwRet);
	return 0;
}

VOID IATHook(LPVOID pOldFunAddr, LPVOID MyMessageBox,LPVOID lparameter) {
	HMODULE imageBase = (HMODULE)lparameter;
	PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)imageBase;
	PIMAGE_NT_HEADERS pNtHeader = (PIMAGE_NT_HEADERS)((DWORD)pDosHeader + pDosHeader->e_lfanew);
	PIMAGE_FILE_HEADER pFileHeader = (PIMAGE_FILE_HEADER)((DWORD)pNtHeader + 4);
	PIMAGE_OPTIONAL_HEADER32 pOptionalHeader = (PIMAGE_OPTIONAL_HEADER32)((DWORD)pFileHeader +
		IMAGE_SIZEOF_FILE_HEADER);
	PIMAGE_IMPORT_DESCRIPTOR pImportDes = (PIMAGE_IMPORT_DESCRIPTOR)(pOptionalHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress + (DWORD)imageBase);
	while (pImportDes->OriginalFirstThunk && pImportDes->FirstThunk) {
		PDWORD pIATthunk = (PDWORD)(pImportDes->FirstThunk + (DWORD)imageBase);
		while (*pIATthunk) {
			if (*pIATthunk == (DWORD)pOldFunAddr) {
				DWORD a = 0;
				if (!VirtualProtect(pIATthunk,10, PAGE_EXECUTE_READWRITE,&a)) {
					printf("VirtualProtectEx faild\n");
					MessageBox(0, "VirtualProtectEx", 0, 0);
					return;
				}
				*pIATthunk = (DWORD)MyMessageBox;
				break;
			}
			pIATthunk++;
		}
		pImportDes++;
	}
}
//入口点
VOID entry(LPVOID lparameter) {
	//修复导入表
	FixMemoryIATDirectory(lparameter, (HANDLE)0xFFFFFFFF);
	IATHook(pOldFunAddr, MyMessageBox, lparameter);
	MessageBox(0, 0, 0, 0);
	//反向HOOK一下就是卸载HOOK
	IATHook(MyMessageBox, pOldFunAddr, lparameter);
	MessageBox(0, 0, 0, 0);
	ExitProcess(0);
	
}
VOID ModuleInject(LPCSTR processName) {
	HANDLE hSelfHandle = GetCurrentProcess();
	HANDLE hSelfThread = GetCurrentThread();
	DWORD SizeOfImage;
	HMODULE imageBase = GetModuleHandle(NULL);
	CHAR selfPath[256] = { 0 };
	//获得所需要的Info
	if (!GetModuleFileName(NULL, selfPath, 255)) {
		printf("GetModuleFileName faild\n");
		return;
	}
	if (!GetProcessInfo(selfPath, &SizeOfImage)) {
		printf("GetProcessInfo faild\n");
		return;
	}
	LPVOID lpImageBuffer = malloc(SizeOfImage);
	//获取自身ImageBuffer
	if (!ReadProcessMemory(hSelfHandle, imageBase, lpImageBuffer, SizeOfImage, 0)) {
		printf("ReadProcessMemory faild\n");
		return;
	}
	//创建进程
	STARTUPINFO si = { 0 };
	si.cb = sizeof(si);
	PROCESS_INFORMATION pi;
	if (!CreateProcess(processName, 0, 0, 0, FALSE, CREATE_NEW_CONSOLE, 0, 0, &si, &pi)) {
		printf("CreateProcess faild\n");
		return;
	}
	LPVOID lpImageBase;
	if (!(lpImageBase = VirtualAllocEx(pi.hProcess, NULL, SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE))) {
		printf("VirtualAllocEx faild\n");
		return;
	}
	//修复重定位
	if (!FixImageBaseRelocation(&lpImageBuffer, (DWORD)lpImageBase)) {
		printf("FixImageBaseRelocation faild\n");
		return;
	}
	if (!WriteProcessMemory(pi.hProcess, lpImageBase, lpImageBuffer, SizeOfImage, 0)) {
		printf("WriteProcessMemory faild\n");
		return;
	}
	//计算entry在进程里的位置
	DWORD entryAddr = (DWORD)entry + (DWORD)lpImageBase - (DWORD)imageBase;
	printf("%x = %x + %x - %x\n", entryAddr, entry, lpImageBase, imageBase);
	//跨进程创建线程执行entry
	HANDLE hThead = CreateRemoteThread(pi.hProcess, 0, 0, (LPTHREAD_START_ROUTINE)entryAddr, lpImageBase, 0, 0);
	if (!hThead) {
		printf("CreateRemoteThread faild\n");
		return;
	}
	WaitForSingleObject(hThead, -1);
	printf("ModuleIndect Sucessed");
	free(lpImageBuffer);
}
int main() {
	ModuleInject("X:\\xxx\\xxx.exe");
}
//功能:获取ImageSize 
//IN LPCSTR processPath,OUT PDWORD imageSize
BOOL GetProcessInfo(LPCSTR processPath, PDWORD imageSize) {
	if (processPath == NULL) {
		return FALSE;
	}

	LPVOID pFileBuffer;
	DWORD dwFileSize = ReadPEFile(processPath, &pFileBuffer);
	if (!dwFileSize) {
		printf("ReadPEFile faild\n");
		return FALSE;
	}
	PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)pFileBuffer;
	PIMAGE_NT_HEADERS pNtHeader = (PIMAGE_NT_HEADERS)((DWORD)pDosHeader + pDosHeader->e_lfanew);
	PIMAGE_FILE_HEADER pFileHeader = (PIMAGE_FILE_HEADER)((DWORD)pNtHeader + 4);
	PIMAGE_OPTIONAL_HEADER32 pOptionalHeader = (PIMAGE_OPTIONAL_HEADER32)((DWORD)pFileHeader +
		IMAGE_SIZEOF_FILE_HEADER);
	*imageSize = pOptionalHeader->SizeOfImage;

	free(pFileBuffer);
	return TRUE;
}
//功能修复Image的重定位表
//无OUT
BOOL FixImageBaseRelocation(LPVOID* pImageBuffer, DWORD newImageBase) {
	PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)*pImageBuffer;
	PIMAGE_NT_HEADERS pNtHeader = (PIMAGE_NT_HEADERS)((DWORD)pDosHeader + pDosHeader->e_lfanew);
	PIMAGE_FILE_HEADER pFileHeader = (PIMAGE_FILE_HEADER)((DWORD)pNtHeader + 4);
	PIMAGE_OPTIONAL_HEADER32 pOptionalHeader = (PIMAGE_OPTIONAL_HEADER32)((DWORD)pFileHeader +
		IMAGE_SIZEOF_FILE_HEADER);
	PIMAGE_DATA_DIRECTORY pDataDirectory = (PIMAGE_DATA_DIRECTORY)pOptionalHeader->DataDirectory;
	if (pDataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress == NULL) {
		printf("没有重定位表\n");
		return TRUE;
	}
	PIMAGE_BASE_RELOCATION pBaseRelocation = (PIMAGE_BASE_RELOCATION)(pDataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress + (DWORD)*pImageBuffer);

	DWORD imageBase = pOptionalHeader->ImageBase;
	pOptionalHeader->ImageBase = newImageBase;
	DWORD imageOffeset = pOptionalHeader->ImageBase - imageBase;

	while (pBaseRelocation->SizeOfBlock != 0 && pBaseRelocation->VirtualAddress != 0) {
		DWORD sizeOfWord = (pBaseRelocation->SizeOfBlock - 8) / 2;
		PWORD pWord = (PWORD)((DWORD)pBaseRelocation + 8);
		for (int i = 0; i < sizeOfWord; i++) {
			if (*pWord >> 12 != 0) {
				PDWORD offsetAddr = (PDWORD)(pBaseRelocation->VirtualAddress + (*pWord & 0xFFF) + (DWORD)*pImageBuffer);
				*offsetAddr = *offsetAddr + imageOffeset;
				pWord++;
				continue;
			}
			pWord++;
		}
		pBaseRelocation = (PIMAGE_BASE_RELOCATION)((DWORD)pBaseRelocation + pBaseRelocation->SizeOfBlock);
	}

	return 1;
}
//功能:修复进程指定位置PE格式的进程
//无OUT
BOOL FixMemoryIATDirectory(LPVOID imageBase,HANDLE hProcess) {
	MessageBox(0, "Hello", 0, 0);
	PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)imageBase;
	PIMAGE_NT_HEADERS pNtHeader = (PIMAGE_NT_HEADERS)((DWORD)pDosHeader + pDosHeader->e_lfanew);
	PIMAGE_FILE_HEADER pFileHeader = (PIMAGE_FILE_HEADER)((DWORD)pNtHeader + 4);
	PIMAGE_OPTIONAL_HEADER32 pOptionalHeader = (PIMAGE_OPTIONAL_HEADER32)((DWORD)pFileHeader + IMAGE_SIZEOF_FILE_HEADER);
	PIMAGE_DATA_DIRECTORY pDataDirectory = (PIMAGE_DATA_DIRECTORY)pOptionalHeader->DataDirectory;
	if (pDataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress == NULL) {
		printf("没有IAT表\n");
		return 0;
	}
	PIMAGE_IMPORT_DESCRIPTOR pImportDes = (PIMAGE_IMPORT_DESCRIPTOR)
		 (pDataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress + (DWORD)imageBase);
	while (pImportDes->OriginalFirstThunk != NULL && pImportDes->FirstThunk != NULL) {
		LPSTR lpDllName = (LPSTR)(pImportDes->Name + (DWORD)imageBase);
		HINSTANCE hModule = GetModuleHandle(lpDllName);
		if (!hModule) {
			hModule = LoadLibraryA(lpDllName);
			if (!hModule) {
				printf("hModule获取失败\n");
				return FALSE;
			}
		}
		PDWORD pIntThunk = (PDWORD)(pImportDes->OriginalFirstThunk + (DWORD)imageBase);
		PIMAGE_THUNK_DATA pIATThunk = (PIMAGE_THUNK_DATA)(pImportDes->FirstThunk + (DWORD)imageBase);
		while (*pIntThunk) {
			LPVOID lpFunAddr;
			if (*pIntThunk & IMAGE_ORDINAL_FLAG) {
				lpFunAddr = GetProcAddress(hModule, (LPCSTR)(*pIntThunk - IMAGE_ORDINAL_FLAG));
			} else {
				PIMAGE_IMPORT_BY_NAME pImportByName = (PIMAGE_IMPORT_BY_NAME)(*pIntThunk + (DWORD)imageBase);
				lpFunAddr = GetProcAddress(hModule, pImportByName->Name);
				WriteProcessMemory(hProcess, pIATThunk, &lpFunAddr, 4, 0);
			}
			pIATThunk->u1.Function = (DWORD)lpFunAddr;
			pIntThunk++;
			pIATThunk++;
		}
		pImportDes++;
	}
	return TRUE;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值