1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
#include "stdafx.h" #include <windows.h> #include "tlhelp32.h" #include "tchar.h"
DWORD addr_OpenProcess; BYTE code[8]={0}; BYTE oldcode[8]={0};
DWORD GetpidByName(char* name) { PROCESSENTRY32 mes; HANDLE hShot; bool exist; DWORD pid = -1; hShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if(hShot == NULL) return -1; mes.dwSize = sizeof(mes); exist = Process32First(hShot,&mes); while(exist) { if(_tcsstr(mes.szExeFile,name) != NULL) { pid = mes.th32ProcessID; break; } exist = Process32Next(hShot,&mes); } CloseHandle(hShot); return pid; }
HANDLE WINAPI MyOpenProcess(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId){ HANDLE handle; char* name = "notepad.exe"; if (GetpidByName(name) == dwProcessId){ SetLastError(5); return NULL; } DWORD old; if (VirtualProtectEx(GetCurrentProcess(), (void*)addr_OpenProcess, 5, PAGE_EXECUTE_READWRITE, &old)){ WriteProcessMemory(GetCurrentProcess(), (void*)addr_OpenProcess, oldcode, 5, NULL); VirtualProtectEx(GetCurrentProcess(), (void*)addr_OpenProcess, 5, old, &old); } handle = OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId); if (VirtualProtectEx(GetCurrentProcess(), (void*)addr_OpenProcess, 5, PAGE_EXECUTE_READWRITE, &old)){ WriteProcessMemory(GetCurrentProcess(), (void*)addr_OpenProcess, code, 5, NULL); VirtualProtectEx(GetCurrentProcess(), (void*)addr_OpenProcess, 5, old, &old); } return handle; }
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: addr_OpenProcess = 0; HMODULE hDll; hDll = LoadLibrary(TEXT("Kernel32.dll")); addr_OpenProcess = (DWORD)GetProcAddress(hDll, "OpenProcess"); if (addr_OpenProcess){ code[0] = 0xe9; DWORD a = (DWORD)MyOpenProcess - (DWORD)addr_OpenProcess - 5; RtlMoveMemory(code + 1, &a, 4); DWORD old; if (VirtualProtectEx(GetCurrentProcess(), (void*)addr_OpenProcess, 5, PAGE_EXECUTE_READWRITE, &old)){ RtlMoveMemory(oldcode, (void*)addr_OpenProcess, 5); WriteProcessMemory(GetCurrentProcess(), (void*)addr_OpenProcess, code, 5, NULL); VirtualProtectEx(GetCurrentProcess(), (void*)addr_OpenProcess, 5, old, &old); } } case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; }
return TRUE; }
|