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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| #include<stdio.h> #include<stdlib.h> #include<Windows.h>
#include<tlhelp32.h>
#define PROCNAME "Project1.exe"
BOOL loadDll(DWORD dwProcessID, const CHAR *szDllPathName) { HANDLE hProcess; DWORD strLen; LPVOID lpAllocAddr; LPVOID lpLoadAddr; BOOL bRet; HMODULE hModule; HANDLE hThread;
CHAR strBuff[100];
hProcess = OpenProcess(PROCESS_ALL_ACCESS, NULL, dwProcessID); if (hProcess == NULL) { sprintf_s(strBuff, 100, "OpenProcess Error:%d\n", GetLastError()); OutputDebugString(strBuff); return FALSE; }
strLen = strlen(szDllPathName) + 1;
lpAllocAddr = VirtualAllocEx(hProcess, NULL, strLen, MEM_COMMIT, PAGE_READWRITE); if (lpAllocAddr == NULL) { OutputDebugString("VirtualAllocEx Error\n"); CloseHandle(hProcess); return FALSE; }
bRet = WriteProcessMemory(hProcess, lpAllocAddr, szDllPathName, strLen, 0); if (!bRet) { OutputDebugString("WriteProcessMemory Error\n"); VirtualFreeEx(hProcess, lpAllocAddr, strLen, MEM_RELEASE); CloseHandle(hProcess); return FALSE; }
hModule = GetModuleHandle("Kernel32.dll");
if (hModule == NULL) { OutputDebugString("GetModuleHandle Error\n"); VirtualFreeEx(hProcess, lpAllocAddr, strLen, MEM_RELEASE); CloseHandle(hProcess); return FALSE; }
lpLoadAddr = GetProcAddress(hModule, "LoadLibraryA");
if (lpLoadAddr == NULL) { OutputDebugString("GetProcAddress Error\n"); VirtualFreeEx(hProcess, lpAllocAddr, strLen, MEM_RELEASE); CloseHandle(hProcess); return FALSE; }
hThread = CreateRemoteThread( hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)lpLoadAddr, lpAllocAddr, 0, NULL );
if (!hThread) { OutputDebugString("CreateRemoteThread Error\n"); VirtualFreeEx(hProcess, lpAllocAddr, strLen, MEM_RELEASE); CloseHandle(hProcess); return FALSE; }
VirtualFreeEx(hProcess, lpAllocAddr, strLen, MEM_RELEASE); CloseHandle(hProcess); CloseHandle(hThread);
return TRUE; }
DWORD getProcID(CONST CHAR *szProcName) { PROCESSENTRY32 enter; HANDLE hSnapshot;
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (hSnapshot == INVALID_HANDLE_VALUE) { OutputDebugString("CreateToolhelp32Snapshot Error\n"); return 0; }
if (Process32First(hSnapshot, &enter) == TRUE) { do { if (strcmp(enter.szExeFile, szProcName) == 0) { CloseHandle(hSnapshot); return enter.th32ProcessID; } } while (Process32Next(hSnapshot, &enter) == TRUE); }
OutputDebugString("Not found!\n"); return 0; }
int main() {
DWORD procID; procID = getProcID(PROCNAME);
if (!procID) { OutputDebugString("getProcID Error\n"); return 0; }
loadDll(procID, "D:\\absolution\\temporary\\myDll.dll");
getchar();
return 0; }
|