fork(5) download
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <windows.h>
  4. #include <tlhelp32.h>
  5.  
  6. #define LOGFILE "C:\\injection.log"
  7. #define PROCESSNAME L"explorer.exe"
  8.  
  9. DWORD getProcessID();
  10. int addLogMessage(char* str, int code);
  11. BOOL setPrivilege(HANDLE hToken, LPCTSTR szPrivName, BOOL fEnable);
  12.  
  13. typedef FARPROC (WINAPI *LPMessageBox)(HWND, LPCWSTR, LPCWSTR, UINT);
  14.  
  15. typedef struct _InjectData {
  16. char title[50];
  17. char msg[50];
  18. LPMessageBox MessageB;
  19. } InjectData, *PInjectData;
  20.  
  21. InjectData injectData = {
  22. "Test",
  23. "Привет",
  24. NULL
  25. };
  26.  
  27. static DWORD WINAPI InjectionMain(LPVOID lpParams) {
  28.  
  29. PInjectData info = (PInjectData)lpParams;
  30.  
  31. info->MessageB(NULL, (LPCWSTR)info->msg, (LPCWSTR)info->title, MB_OK);
  32. return 0;
  33. }
  34.  
  35. static void __declspec( naked ) end_proc() {
  36. }
  37.  
  38.  
  39. int _tmain(int argc, _TCHAR* argv[]) {
  40.  
  41. char buffer [50];
  42. HANDLE hToken;
  43. HANDLE processHandel;
  44. HINSTANCE userHinstance;
  45.  
  46. DWORD processID = getProcessID();
  47.  
  48. HANDLE hCurrentProc = GetCurrentProcess();
  49.  
  50. if(!OpenProcessToken(hCurrentProc, TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, &hToken)) {
  51. addLogMessage("OpenProcessToken Error", GetLastError());
  52. return 0;
  53. } else {
  54. if (!setPrivilege(hToken, SE_DEBUG_NAME, TRUE)) {
  55. addLogMessage("SetPrivlegesSE_DEBUG_NAME Error", GetLastError());
  56. return 0;
  57. }
  58. }
  59.  
  60. if(processID == 0) {
  61. MessageBox(NULL, _T("Процесс не найден!"), _T("Error"), MB_OK | MB_ICONERROR);
  62. return 0;
  63. }
  64.  
  65. processHandel = OpenProcess(PROCESS_ALL_ACCESS, false, processID);
  66. if(processHandel == NULL) {
  67. addLogMessage("Open process error", GetLastError());
  68. return 0;
  69. }
  70.  
  71. userHinstance = LoadLibrary(_T("user32.dll"));
  72. injectData.MessageB = (LPMessageBox) GetProcAddress(userHinstance, "MessageBoxA");
  73.  
  74. DWORD ProcSize = (DWORD)end_proc - (DWORD)InjectionMain;
  75. sprintf_s(buffer, "Process size: %u", ProcSize);
  76. addLogMessage(buffer, 0);
  77.  
  78. LPVOID lpProc = VirtualAllocEx(processHandel, NULL, ProcSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
  79. LPVOID lpParams = VirtualAllocEx(processHandel, NULL, 1024, MEM_COMMIT, PAGE_READWRITE );
  80.  
  81. if (!lpProc || !lpParams) {
  82. addLogMessage("Error allocating memory ", 1000);
  83. return 0;
  84. }
  85.  
  86. sprintf_s(buffer, "Memory allocated at 0x%X and 0x%X", lpProc, lpParams );
  87. addLogMessage(buffer, 0);
  88.  
  89. DWORD dwWritten;
  90. if(WriteProcessMemory(processHandel, lpProc, InjectionMain, ProcSize, &dwWritten ) == 0) {
  91. addLogMessage("WriteProcessMemory error", GetLastError());
  92. return 0;
  93. }
  94.  
  95. if(WriteProcessMemory( processHandel, lpParams, &injectData, sizeof(injectData), &dwWritten ) == 0) {
  96. addLogMessage("WriteProcessMemory error", GetLastError());
  97. return 0;
  98. }
  99.  
  100. sprintf_s(buffer, "Чтения памяти", lpProc, lpParams );
  101. addLogMessage(buffer, 0);
  102.  
  103. DWORD ThreadID;
  104. HANDLE hThread = CreateRemoteThread(processHandel, NULL, 0, (LPTHREAD_START_ROUTINE)lpProc, lpParams, 0, &ThreadID);
  105.  
  106. if (hThread == NULL) {
  107. sprintf_s(buffer, "Error creating thread");
  108. addLogMessage(buffer, GetLastError());
  109. return 0;
  110. } else {
  111. WaitForSingleObject( hThread, INFINITE );
  112. }
  113.  
  114. FreeLibrary(userHinstance);
  115.  
  116. VirtualFreeEx(processHandel, lpProc, ProcSize, MEM_DECOMMIT );
  117. VirtualFreeEx(processHandel, lpParams, 1024, MEM_DECOMMIT );
  118. CloseHandle(processHandel);
  119.  
  120. addLogMessage("Success injecting!", 0);
  121.  
  122. return 0;
  123. }
  124.  
  125. DWORD getProcessID() {
  126. DWORD processID = 0;
  127. HANDLE snapHandle;
  128. PROCESSENTRY32 processEntry = {0};
  129.  
  130. if( (snapHandle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)) == INVALID_HANDLE_VALUE ) {
  131. return 0;
  132. }
  133.  
  134. processEntry.dwSize = sizeof(PROCESSENTRY32);
  135. Process32First(snapHandle, &processEntry);
  136. do {
  137. if ( wcscmp(processEntry.szExeFile, PROCESSNAME) == 0 ) {
  138. return processEntry.th32ProcessID;
  139. }
  140. } while (Process32Next(snapHandle,&processEntry));
  141.  
  142. if ( snapHandle != INVALID_HANDLE_VALUE ) {
  143. CloseHandle(snapHandle);
  144. }
  145.  
  146. return 0;
  147. }
  148.  
  149. BOOL setPrivilege(HANDLE hToken, LPCTSTR szPrivName, BOOL fEnable) {
  150. TOKEN_PRIVILEGES tp;
  151. tp.PrivilegeCount = 1;
  152. LookupPrivilegeValue(NULL, szPrivName, &tp.Privileges[0].Luid);
  153. tp.Privileges[0].Attributes = fEnable ? SE_PRIVILEGE_ENABLED : 0;
  154. AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL);
  155. return((GetLastError() == ERROR_SUCCESS));
  156. }
  157.  
  158. int addLogMessage(char* str, int code) {
  159. errno_t err;
  160. FILE* log;
  161.  
  162. if((err = fopen_s(&log, LOGFILE, "a+")) != 0) {
  163. return -1;
  164. }
  165.  
  166. fprintf(log, "[code: %u] %s\n", code, str);
  167. fclose(log);
  168. return 0;
  169. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:20: error: stdafx.h: No such file or directory
prog.cpp:3:21: error: windows.h: No such file or directory
prog.cpp:4:22: error: tlhelp32.h: No such file or directory
prog.cpp:9: error: ‘DWORD’ does not name a type
prog.cpp:11: error: ‘BOOL’ does not name a type
prog.cpp:13: error: ISO C++ forbids declaration of ‘FARPROC’ with no type
prog.cpp:13: error: typedef ‘FARPROC’ is initialized (use __typeof__ instead)
prog.cpp:13: error: ‘WINAPI’ was not declared in this scope
prog.cpp:13: error: ‘LPMessageBox’ was not declared in this scope
prog.cpp:18: error: ‘LPMessageBox’ does not name a type
prog.cpp:25: error: too many initializers for ‘InjectData’
prog.cpp:27: error: ‘DWORD’ does not name a type
prog.cpp:35: error: variable or field ‘__declspec’ declared void
prog.cpp:35: error: ‘naked’ was not declared in this scope
prog.cpp:39: error: ‘_TCHAR’ has not been declared
prog.cpp: In function ‘int _tmain(int, int**)’:
prog.cpp:42: error: ‘HANDLE’ was not declared in this scope
prog.cpp:42: error: expected `;' before ‘hToken’
prog.cpp:43: error: expected `;' before ‘processHandel’
prog.cpp:44: error: ‘HINSTANCE’ was not declared in this scope
prog.cpp:44: error: expected `;' before ‘userHinstance’
prog.cpp:46: error: ‘DWORD’ was not declared in this scope
prog.cpp:46: error: expected `;' before ‘processID’
prog.cpp:48: error: expected `;' before ‘hCurrentProc’
prog.cpp:50: error: ‘hCurrentProc’ was not declared in this scope
prog.cpp:50: error: ‘TOKEN_QUERY’ was not declared in this scope
prog.cpp:50: error: ‘TOKEN_ADJUST_PRIVILEGES’ was not declared in this scope
prog.cpp:50: error: ‘hToken’ was not declared in this scope
prog.cpp:50: error: ‘OpenProcessToken’ was not declared in this scope
prog.cpp:51: error: ‘GetLastError’ was not declared in this scope
prog.cpp:54: error: ‘SE_DEBUG_NAME’ was not declared in this scope
prog.cpp:54: error: ‘TRUE’ was not declared in this scope
prog.cpp:54: error: ‘setPrivilege’ was not declared in this scope
prog.cpp:55: error: ‘GetLastError’ was not declared in this scope
prog.cpp:60: error: ‘processID’ was not declared in this scope
prog.cpp:61: error: ‘_T’ was not declared in this scope
prog.cpp:61: error: ‘MB_OK’ was not declared in this scope
prog.cpp:61: error: ‘MB_ICONERROR’ was not declared in this scope
prog.cpp:61: error: ‘MessageBox’ was not declared in this scope
prog.cpp:65: error: ‘processHandel’ was not declared in this scope
prog.cpp:65: error: ‘PROCESS_ALL_ACCESS’ was not declared in this scope
prog.cpp:65: error: ‘processID’ was not declared in this scope
prog.cpp:65: error: ‘OpenProcess’ was not declared in this scope
prog.cpp:67: error: ‘GetLastError’ was not declared in this scope
prog.cpp:71: error: ‘userHinstance’ was not declared in this scope
prog.cpp:71: error: ‘_T’ was not declared in this scope
prog.cpp:71: error: ‘LoadLibrary’ was not declared in this scope
prog.cpp:72: error: ‘struct InjectData’ has no member named ‘MessageB’
prog.cpp:72: error: ‘LPMessageBox’ was not declared in this scope
prog.cpp:72: error: expected `;' before ‘GetProcAddress’
prog.cpp:74: error: expected `;' before ‘ProcSize’
prog.cpp:75: error: ‘ProcSize’ was not declared in this scope
prog.cpp:75: error: ‘sprintf_s’ was not declared in this scope
prog.cpp:78: error: ‘LPVOID’ was not declared in this scope
prog.cpp:78: error: expected `;' before ‘lpProc’
prog.cpp:79: error: expected `;' before ‘lpParams’
prog.cpp:81: error: ‘lpProc’ was not declared in this scope
prog.cpp:81: error: ‘lpParams’ was not declared in this scope
prog.cpp:82: warning: deprecated conversion from string constant to ‘char*’
prog.cpp:86: error: ‘lpProc’ was not declared in this scope
prog.cpp:86: error: ‘lpParams’ was not declared in this scope
prog.cpp:89: error: expected `;' before ‘dwWritten’
prog.cpp:90: error: ‘InjectionMain’ was not declared in this scope
prog.cpp:90: error: ‘dwWritten’ was not declared in this scope
prog.cpp:90: error: ‘WriteProcessMemory’ was not declared in this scope
prog.cpp:91: error: ‘GetLastError’ was not declared in this scope
prog.cpp:95: error: ‘dwWritten’ was not declared in this scope
prog.cpp:95: error: ‘WriteProcessMemory’ was not declared in this scope
prog.cpp:96: error: ‘GetLastError’ was not declared in this scope
prog.cpp:103: error: expected `;' before ‘ThreadID’
prog.cpp:104: error: expected `;' before ‘hThread’
prog.cpp:106: error: ‘hThread’ was not declared in this scope
prog.cpp:108: error: ‘GetLastError’ was not declared in this scope
prog.cpp:111: error: ‘INFINITE’ was not declared in this scope
prog.cpp:111: error: ‘WaitForSingleObject’ was not declared in this scope
prog.cpp:114: error: ‘FreeLibrary’ was not declared in this scope
prog.cpp:116: error: ‘MEM_DECOMMIT’ was not declared in this scope
prog.cpp:116: error: ‘VirtualFreeEx’ was not declared in this scope
prog.cpp:118: error: ‘CloseHandle’ was not declared in this scope
prog.cpp:120: warning: deprecated conversion from string constant to ‘char*’
prog.cpp: At global scope:
prog.cpp:125: error: ‘DWORD’ does not name a type
prog.cpp:149: error: ‘BOOL’ does not name a type
prog.cpp: In function ‘int addLogMessage(char*, int)’:
prog.cpp:159: error: ‘errno_t’ was not declared in this scope
prog.cpp:159: error: expected `;' before ‘err’
prog.cpp:162: error: ‘err’ was not declared in this scope
prog.cpp:162: error: ‘fopen_s’ was not declared in this scope
stdout
Standard output is empty