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
| #include <string> #include <stdio.h> #include <stdlib.h> #include <memory.h> #include <tchar.h> #include <Windows.h> #include <iostream> #include <fstream> #include <TlHelp32.h> #include <comdef.h>
using namespace std;
#define DEF_PROCESS_NAME "notepad.exe"
LPVOID WriteFileAddress = NULL; CREATE_PROCESS_DEBUG_INFO CreateProcessDebugInfomation; BYTE INT3 = 0xCC, OldByte = 0;
BOOL injectDll(int pid, wchar_t* szDllPath) {
HANDLE hProcess = NULL, hThread = NULL; HMODULE hMod = NULL; LPVOID pRemoteBuf = NULL; DWORD dwBufSize = (DWORD)(_tcslen(szDllPath) + 1) * sizeof(TCHAR); LPTHREAD_START_ROUTINE pThreadProc; if (!(hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid))) { _tprintf(L"OpenProcess(%d) failed [%d]\n", pid,GetLastError()); return FALSE; } pRemoteBuf = VirtualAllocEx(hProcess, NULL, dwBufSize, MEM_COMMIT, PAGE_READWRITE); WriteProcessMemory(hProcess, pRemoteBuf, (LPVOID)szDllPath, dwBufSize, NULL); hMod = GetModuleHandle(L"kernel32.dll"); pThreadProc = (LPTHREAD_START_ROUTINE)GetProcAddress(hMod, "LoadLibraryW"); hThread = CreateRemoteThread(hProcess, NULL, 0, pThreadProc, pRemoteBuf, 0, NULL); WaitForSingleObject(hThread, INFINITE); CloseHandle(hThread); CloseHandle(hProcess); return TRUE; }
int traverseProcesses() { PROCESSENTRY32 pe32; pe32.dwSize = sizeof(pe32); HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL); if (hProcessSnap == INVALID_HANDLE_VALUE) { printf("Create snap Error!\n"); return false; } BOOL bResult = Process32First(hProcessSnap, &pe32); int num(0); while (bResult) { _bstr_t b(pe32.szExeFile); if (!strcmp(b, "notepad.exe")) { printf("[*] Find notepad.exe: %d\n", pe32.th32ProcessID); return pe32.th32ProcessID; } bResult = Process32Next(hProcessSnap, &pe32); } return -1; } int _tmain(int argc, TCHAR* argv[]) { wchar_t dllName[] = L"myhackdll.dll"; int pid = traverseProcesses(); if (pid == -1) { printf("[-] Not found\n"); exit(-1); } if (injectDll(pid, dllName)) { _tprintf(L"Inject success\n"); } else { _tprintf(L"Inject failed\n"); } return 0; }
|