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
| #include <debugapi.h> #include <errhandlingapi.h> #include <handleapi.h> #include <libloaderapi.h> #include <memoryapi.h> #include <minwinbase.h> #include <minwindef.h> #include <processthreadsapi.h> #include <string.h> #include <synchapi.h> #include <windows.h> #include <stdio.h> #include <winnt.h>
#define __MAP__ "Share"
HANDLE g_hMapFile; LPTSTR lpBuff;
BOOL Init() { g_hMapFile = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL,PAGE_READWRITE,0,0x1000,__MAP__); if (g_hMapFile == NULL) { printf("CreateFileMappingA Error\n"); return FALSE; } lpBuff = MapViewOfFile(g_hMapFile,FILE_MAP_ALL_ACCESS,0,0,0x1000); if (lpBuff == NULL) { printf("MapViewOfFile Error\n"); return FALSE; } return TRUE; }
BOOL LoadDll(DWORD dwProcessID,char *szDllPathName){ BOOL bRet; HANDLE hProcess; HANDLE hThread; DWORD dwLength; FARPROC dwLoadAddr; LPVOID lpAllocAddr; HMODULE hModule;
bRet = FALSE; dwLoadAddr = 0; hProcess = 0;
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE,dwProcessID); if (hProcess == NULL) { OutputDebugStringA("OpenProcess Error! \n"); return FALSE; } dwLength = strlen(szDllPathName) + 1;
lpAllocAddr = VirtualAllocEx(hProcess, NULL, dwLength, MEM_COMMIT,PAGE_READWRITE);
if (lpAllocAddr == NULL) { OutputDebugStringA("VirtualAllocEx Error! \n"); CloseHandle(hProcess); return FALSE; } bRet = WriteProcessMemory(hProcess, lpAllocAddr, szDllPathName,dwLength,NULL); if (!bRet) { OutputDebugStringA("WriteProcessMemory Error! \n"); CloseHandle(hProcess); return FALSE; }
hModule = GetModuleHandleA("Kernel32.dll"); if (hModule == NULL) { OutputDebugStringA("GetModuleHandleA Error! \n"); CloseHandle(hProcess); return FALSE; } dwLoadAddr = GetProcAddress(hModule, "LoadLibraryA"); if (dwLoadAddr == NULL) { OutputDebugStringA("GetProcAddress Error! \n"); CloseHandle(hProcess); CloseHandle(hModule); return FALSE; } hThread = CreateRemoteThread(hProcess, NULL, 0,(LPTHREAD_START_ROUTINE)dwLoadAddr,lpAllocAddr,0,NULL); if (hThread == NULL) { OutputDebugStringA("CreateRemoteThread Error! \n"); CloseHandle(hProcess); CloseHandle(hModule); return FALSE; }
CloseHandle(hProcess); CloseHandle(hThread);
return TRUE; }
int main(int argc, char *argv[]) { DWORD dwCtrlCode; DWORD dwOrderList[3];
dwCtrlCode = 0; if (Init()) { LoadDll(111, "Dll1.dll"); }else { return 0; }
dwOrderList[0] = 3; dwOrderList[1] = 3; dwOrderList[2] = 4; for (int i = 0; i < 3; i++) { dwCtrlCode = dwOrderList[i]; CopyMemory(lpBuff,&dwCtrlCode,4); Sleep(2000); } return 0; }
|