Advertisement
kolya5544

first experience

Mar 20th, 2020
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. // C_hack.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include <windows.h>
  5. #include <iostream>
  6. #include <string>
  7. #include <tlhelp32.h>
  8. #include <cstdlib>
  9. #include <inttypes.h>
  10. #include <list>
  11.  
  12. using namespace std;
  13. int64_t S64(const char* s) {
  14.     int64_t i;
  15.     char c;
  16.     int scanned = sscanf_s(s, "%" SCNd64 "%c", &i, &c);
  17.     if (scanned == 1) return i;
  18.     if (scanned > 1) {
  19.         // TBD about extra data found
  20.         return i;
  21.     }
  22.     // TBD failed to scan;  
  23.     return 0;
  24. }
  25.  
  26.  
  27.  
  28. DWORD FindProcessId(const std::wstring& processName)
  29. {
  30.     PROCESSENTRY32 processInfo;
  31.     processInfo.dwSize = sizeof(processInfo);
  32.  
  33.     HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
  34.     if (processesSnapshot == INVALID_HANDLE_VALUE)
  35.         return 0;
  36.  
  37.     Process32First(processesSnapshot, &processInfo);
  38.     if (!processName.compare(processInfo.szExeFile))
  39.     {
  40.         CloseHandle(processesSnapshot);
  41.         return processInfo.th32ProcessID;
  42.     }
  43.  
  44.     while (Process32Next(processesSnapshot, &processInfo))
  45.     {
  46.         if (!processName.compare(processInfo.szExeFile))
  47.         {
  48.             CloseHandle(processesSnapshot);
  49.             return processInfo.th32ProcessID;
  50.         }
  51.     }
  52.  
  53.     CloseHandle(processesSnapshot);
  54.     return 0;
  55. }
  56.  
  57. HANDLE pHandle;
  58. SYSTEM_INFO si;
  59. MEMORY_BASIC_INFORMATION mbi;
  60. LPVOID lpMem;
  61. DWORD ret, totalRead;
  62.  
  63. string readText(DWORD pid, uint64_t loc) {
  64.    
  65.     lpMem = (void*)loc;
  66.    
  67.  
  68.     string raw;
  69.     list<char> text;
  70.     uint64_t offset = loc;
  71.     bool flag = true;
  72.     while (flag) {
  73.         lpMem = (void*)offset;
  74.         char buf;
  75.         ReadProcessMemory(pHandle, lpMem, (LPVOID)(&buf), 1, &totalRead);
  76.         if (buf != '\0' && buf != '\r' && buf != '\n') {
  77.             raw.push_back(buf);
  78.            
  79.         }
  80.         else {  
  81.             flag = false;
  82.         }
  83.         offset++;
  84.     }
  85.     return raw;
  86. }
  87.  
  88. int main()
  89. {
  90.     std::cout << "Hello World!" <<endl;
  91.     printf("Test\n");
  92.     std::cout << "Please, enter process name to try and inject: ";
  93.     wstring pname;
  94.     std::getline(wcin, pname);
  95.     std::cout << "Please, enter offset in integer to read some text from: ";
  96.     string offset;
  97.     std::getline(cin, offset);
  98.  
  99.  
  100.     DWORD processID = FindProcessId(pname);
  101.  
  102.     pHandle = OpenProcess(PROCESS_ALL_ACCESS, 0, processID);
  103.     if (pHandle == NULL) {
  104.         exit(5);
  105.     }
  106.  
  107.    
  108.  
  109.     std::cout << readText(processID, S64(offset.c_str()));
  110.     std::getline(cin, offset);
  111. }
  112.  
  113. // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
  114. // Debug program: F5 or Debug > Start Debugging menu
  115.  
  116. // Tips for Getting Started:
  117. //   1. Use the Solution Explorer window to add/manage files
  118. //   2. Use the Team Explorer window to connect to source control
  119. //   3. Use the Output window to see build output and other messages
  120. //   4. Use the Error List window to view errors
  121. //   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
  122. //   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement