Advertisement
MaksNew

Untitled

Oct 23rd, 2022
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. extern "C" __declspec(dllimport) void __stdcall ReplaceString(
  8. DWORD pid,
  9. const char* srcString,
  10. const char* resString);
  11.  
  12. typedef void __stdcall TReplaceString(DWORD, const char*, const char*);
  13.  
  14. void ReplaceStringDynamic(DWORD, const char*, const char*);
  15. void InjectLibrary(DWORD);
  16.  
  17. int main()
  18. {
  19. DWORD pid = GetCurrentProcessId();
  20. bool isExit = false;
  21.  
  22. const char* src_str = "Hello, world";
  23. const char* res_str = "dlrow ,olleH";
  24.  
  25. while (!isExit)
  26. {
  27. cout << "Available actions:" << endl
  28. << "\t0 - Static import and call" << endl
  29. << "\t1 - Dynamic import and call" << endl
  30. << "\t2 - Dll injection" << endl
  31. << "Enter action: ";
  32.  
  33. char action;
  34.  
  35. cin >> action;
  36.  
  37. switch (action)
  38. {
  39. case '0':
  40. cout << endl << "String before change: " << src_str << endl;
  41. ReplaceString(pid, src_str, res_str);
  42. cout << "String after changes: " << src_str << endl << endl;
  43. break;
  44. case '1':
  45. cout << endl << "String before change: " << src_str << endl;
  46. ReplaceStringDynamic(pid, src_str, res_str);
  47. cout << "String after changes: " << src_str << endl << endl;
  48. break;
  49. case '2':
  50. cout << endl << "String before change: " << src_str << endl;
  51. InjectLibrary(pid);
  52. cout << "String after changes: " << src_str << endl << endl;
  53. break;
  54. default:
  55. isExit = true;
  56. break;
  57. }
  58. }
  59.  
  60. system("pause");
  61. return 0;
  62. }
  63.  
  64. void ReplaceStringDynamic(DWORD pid, const char* src_str, const char* res_str)
  65. {
  66. HMODULE hDll = LoadLibrary("DllStringReplacement.dll");
  67.  
  68. if (hDll != NULL)
  69. {
  70. TReplaceString* lpReplaceString = (TReplaceString*)GetProcAddress(hDll, "_ReplaceString@12");
  71.  
  72. if (lpReplaceString != NULL)
  73. {
  74. lpReplaceString(pid, src_str, res_str);
  75. }
  76.  
  77. FreeLibrary(hDll);
  78. }
  79. }
  80.  
  81. void InjectLibrary(DWORD procID)
  82. {
  83. HANDLE hProc = OpenProcess(PROCESS_VM_READ | PROCESS_VM_OPERATION | PROCESS_VM_WRITE |
  84. PROCESS_CREATE_THREAD | PROCESS_CREATE_PROCESS,
  85. FALSE, procID);
  86.  
  87. if (hProc)
  88. {
  89. LPVOID baseAddress = VirtualAllocEx(hProc, NULL, strlen("InjectableDll.dll") + 1,
  90. MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
  91.  
  92. if (baseAddress)
  93. {
  94. WriteProcessMemory(hProc, baseAddress, "InjectableDll.dll",
  95. strlen("InjectableDll.dll") + 1, NULL);
  96.  
  97. DWORD threadId;
  98.  
  99. HANDLE hThread = CreateRemoteThread(hProc, NULL, NULL,
  100. (LPTHREAD_START_ROUTINE)LoadLibraryA, (LPVOID)baseAddress, NULL, &threadId);
  101.  
  102. if (hThread == NULL)
  103. cout << "Error" << endl;
  104. else
  105. WaitForSingleObject(hThread, INFINITE);
  106. }
  107.  
  108. CloseHandle(hProc);
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement