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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
| #include <stdio.h> #include <windows.h> #include <tchar.h> #include <tlhelp32.h> #include <stdbool.h> #pragma warning(disable:4996) #pragma warning(disable:28251) #define IDC_BUTTON 0x100 #define IDC_EDIT_1 0x101 #define IDC_EDIT_2 0x102 LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); HINSTANCE g_hInstance; HWND g_hwnd;
BOOL LoadDll(DWORD dwProcessID, const char* szDllPathName) {
BOOL bRet; HANDLE hProcess; HANDLE hThread; DWORD dwLength; DWORD dwLoadAddr; LPVOID lpAllocAddr; DWORD dwThreadID; HMODULE hModule;
bRet = 0; dwLoadAddr = 0; hProcess = 0;
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessID); if (hProcess == NULL) { MessageBox(g_hwnd, TEXT("OpenProcess failed!"), TEXT("提示"), MB_OK); return FALSE; }
dwLength = strlen(szDllPathName) + 1;
lpAllocAddr = VirtualAllocEx(hProcess, NULL, dwLength, MEM_COMMIT, PAGE_READWRITE); if (lpAllocAddr == NULL) { MessageBox(g_hwnd, TEXT("VirtualAllocEx failed!"), TEXT("提示"), MB_OK); CloseHandle(hProcess); return FALSE; }
bRet = WriteProcessMemory(hProcess, lpAllocAddr, reinterpret_cast<LPCVOID>(szDllPathName), dwLength, NULL); if (!bRet) { MessageBox(g_hwnd, TEXT("WriteProcessMemory failed!"), TEXT("提示"), MB_OK); CloseHandle(hProcess); return FALSE; }
hModule = GetModuleHandle("kernel32.dll"); if (!hModule) { MessageBox(g_hwnd, TEXT("GetModuleHandle failed!"), TEXT("提示"), MB_OK); CloseHandle(hProcess); return FALSE; }
dwLoadAddr = reinterpret_cast<DWORD>(GetProcAddress(hModule, "LoadLibraryA")); if (!dwLoadAddr) { MessageBox(g_hwnd, TEXT("GetProcessAddress failed!"), TEXT("提示"), MB_OK); CloseHandle(hModule); CloseHandle(hProcess); return FALSE; } hThread = CreateRemoteThread(hProcess, NULL, 0, reinterpret_cast<LPTHREAD_START_ROUTINE>(dwLoadAddr), lpAllocAddr, 0, &dwThreadID); if (!hThread) { MessageBox(g_hwnd, TEXT("CreateRemoteThread failed!"), TEXT("提示"), MB_OK); CloseHandle(hModule); CloseHandle(hProcess); return FALSE; } CloseHandle(hThread); CloseHandle(hProcess); return TRUE; }
DWORD GetProcessIdByName(const char* processName) { HANDLE hSnap; PROCESSENTRY32 pe32; DWORD processId = 0; hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hSnap == INVALID_HANDLE_VALUE) { MessageBox(g_hwnd, TEXT("Failed to create process snapshot!"), TEXT("提示"), MB_OK); return 0; } pe32.dwSize = sizeof(PROCESSENTRY32); if (!Process32First(hSnap, &pe32)) { MessageBox(g_hwnd, TEXT("Failed to get the first process!"), TEXT("提示"), MB_OK); CloseHandle(hSnap); return 0; } while (true) { if (strcmp((char const*)pe32.szExeFile, processName) == 0) { processId = pe32.th32ProcessID; break; } if (!Process32Next(hSnap, &pe32)) { break; } } CloseHandle(hSnap); return processId; }
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { char szOutBuff[0x80]; TCHAR className[] = TEXT("DllInjector"); WNDCLASS wndclass = { 0 }; wndclass.hbrBackground = (HBRUSH)COLOR_BACKGROUND; wndclass.lpszClassName = className; wndclass.hInstance = hInstance; wndclass.lpfnWndProc = WindowProc; RegisterClass(&wndclass);
HWND hwnd = CreateWindow(className,TEXT("Dll Injector (CreateRemoteThread)"),WS_OVERLAPPEDWINDOW,300,350,320,180,NULL,NULL,hInstance,NULL); g_hwnd = hwnd; if (hwnd == NULL) { MessageBox(NULL, TEXT("CreateWindow failed!"), TEXT("提示"), MB_OK); return -10; } ShowWindow(hwnd, SW_SHOW); MSG msg; BOOL bRet; while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0) { if (bRet == -1) { MessageBox(NULL, TEXT("GetMessage failed!"), TEXT("提示"), MB_OK); } else { TranslateMessage(&msg); DispatchMessage(&msg); } } return 0; }
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_DESTROY: { PostQuitMessage(0); return 0; } case WM_CREATE: { CreateWindow(TEXT("EDIT"), TEXT(""), WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_MULTILINE, 100, 20, 170, 20, hwnd, (HMENU)IDC_EDIT_1, g_hInstance, NULL); CreateWindow(TEXT("EDIT"), TEXT(""), WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_MULTILINE, 100, 55, 170, 20, hwnd, (HMENU)IDC_EDIT_2, g_hInstance, NULL); CreateWindow(TEXT("STATIC"), TEXT("进程名:"), WS_CHILD | WS_VISIBLE, 15, 20, 75, 20, hwnd, NULL, g_hInstance, NULL); CreateWindow(TEXT("STATIC"), TEXT("DLL路径:"), WS_CHILD | WS_VISIBLE, 15, 55, 75, 20, hwnd, NULL, g_hInstance, NULL); CreateWindow(TEXT("BUTTON"), TEXT("注入DLL"), WS_CHILD | WS_VISIBLE, 100, 100, 100, 30, hwnd, (HMENU)IDC_BUTTON, g_hInstance, NULL); DragAcceptFiles(hwnd, TRUE); break; }
case WM_COMMAND: { switch (LOWORD(wParam)) { case IDC_BUTTON: { TCHAR processName[0x200]; TCHAR dllPath[0x200];
HWND hEdit1 = GetDlgItem(hwnd, IDC_EDIT_1); GetWindowText(hEdit1, processName, sizeof(processName));
HWND hEdit2 = GetDlgItem(hwnd, IDC_EDIT_2); GetWindowText(hEdit2, dllPath, sizeof(dllPath));
DWORD processId = GetProcessIdByName((const char*)processName); if (processId == NULL) { MessageBox(hwnd, TEXT("Failed to find the process"), TEXT("提示"), MB_OK); return NULL; }
if (LoadDll(processId, (const char*)dllPath)) { MessageBox(hwnd, TEXT("DLL injected successfully."), TEXT("提示"), MB_OK); return NULL; } else { MessageBox(hwnd, TEXT("Failed to inject DLL."), TEXT("提示"), MB_OK); return NULL; }
break; }
}
break; } case WM_DROPFILES: { HDROP hDrop = (HDROP)wParam; UINT nFiles = DragQueryFile(hDrop, (UINT)-1, NULL, 0); if (nFiles >= 1) { TCHAR szFilePath[MAX_PATH];
if (DragQueryFile(hDrop, 0, szFilePath, MAX_PATH) > 0) { HWND hEdit2 = GetDlgItem(hwnd, IDC_EDIT_2); SetWindowText(hEdit2, szFilePath); } } DragFinish(hDrop); break; } } return DefWindowProc(hwnd, uMsg, wParam, lParam); }
|