Advertisement
Riremito

MapleStory2 Get Session

May 29th, 2019
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.77 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<Windows.h>
  3. #include<TlHelp32.h>
  4. #pragma comment(lib, "kernel32.lib")
  5. #include<string>
  6. #include<vector>
  7.  
  8. class MemoryReader {
  9. private:
  10.     std::string sProcessName;
  11.     std::vector<MODULEENTRY32> MemoryMap;
  12.     HANDLE hProcess;
  13.     DWORD dwProcessID;
  14.  
  15.     bool DebugOn() {
  16.         TOKEN_PRIVILEGES tp;
  17.         LUID luid;
  18.  
  19.         memset(&tp, 0, sizeof(TOKEN_PRIVILEGES));
  20.         memset(&luid, 0, sizeof(LUID));
  21.  
  22.         if (!LookupPrivilegeValueA(NULL, SE_DEBUG_NAME, &luid)) {
  23.             return false;
  24.         }
  25.  
  26.         tp.PrivilegeCount = 1;
  27.         tp.Privileges[0].Luid = luid;
  28.         tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  29.  
  30.         HANDLE hToken;
  31.  
  32.         if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken)) {
  33.             return false;
  34.         }
  35.  
  36.         if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL)) {
  37.             return false;
  38.         }
  39.  
  40.         CloseHandle(hToken);
  41.         return true;
  42.     }
  43.  
  44.     bool CreateMemoryMap(DWORD dwPID) {
  45.         HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID);
  46.  
  47.         if (hSnapshot == INVALID_HANDLE_VALUE) {
  48.             return false;
  49.         }
  50.  
  51.         MODULEENTRY32 me;
  52.         memset(&me, 0, sizeof(MODULEENTRY32));
  53.         me.dwSize = sizeof(MODULEENTRY32);
  54.  
  55.         if (Module32First(hSnapshot, &me) == FALSE) {
  56.             CloseHandle(hSnapshot);
  57.             return false;
  58.         }
  59.  
  60.         MemoryMap.clear();
  61.  
  62.         do {
  63.             MemoryMap.push_back(me);
  64.         } while (Module32Next(hSnapshot, &me));
  65.  
  66.         CloseHandle(hSnapshot);
  67.         return true;
  68.     }
  69.  
  70.     bool SearchProcess() {
  71.         HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
  72.  
  73.         if (hSnapshot == INVALID_HANDLE_VALUE) {
  74.             return false;
  75.         }
  76.  
  77.         PROCESSENTRY32 pe;
  78.         pe.dwSize = sizeof(PROCESSENTRY32);
  79.  
  80.         if (!Process32First(hSnapshot, &pe)) {
  81.             CloseHandle(hSnapshot);
  82.             return false;
  83.         }
  84.  
  85.         do {
  86.             if (!_strnicmp(pe.szExeFile, sProcessName.c_str(), sProcessName.length())) {
  87.                 dwProcessID = pe.th32ProcessID;
  88.                 return CreateMemoryMap(pe.th32ProcessID);
  89.             }
  90.         } while (Process32Next(hSnapshot, &pe));
  91.  
  92.         CloseHandle(hSnapshot);
  93.         return false;
  94.     }
  95.  
  96.     ULONGLONG FindModuleBase(std::string sModuleName, DWORD dwOffset) {
  97.         for (SIZE_T i = 0; i < MemoryMap.size(); i++) {
  98.             if (!_strnicmp(MemoryMap[i].szModule, sModuleName.c_str(), sModuleName.length())) {
  99.                 if (dwOffset < MemoryMap[i].modBaseSize) {
  100.                     return (ULONGLONG)MemoryMap[i].modBaseAddr;
  101.                 }
  102.             }
  103.         }
  104.         return 0;
  105.     }
  106.  
  107. public:
  108.     MemoryReader(const char *cProcessName) {
  109.         sProcessName = cProcessName;
  110.         hProcess = NULL;
  111.         dwProcessID = 0;
  112.     }
  113.  
  114.     ~MemoryReader() {
  115.         dwProcessID = 0;
  116.         if (hProcess) {
  117.             CloseHandle(hProcess);
  118.             hProcess = NULL;
  119.         }
  120.     }
  121.  
  122.     bool Attach() {
  123.         if (!DebugOn()) {
  124.             return false;
  125.         }
  126.  
  127.         if (!SearchProcess()) {
  128.             return false;
  129.         }
  130.  
  131.         hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, dwProcessID);
  132.  
  133.         if (!hProcess) {
  134.             return false;
  135.         }
  136.  
  137.         return true;
  138.     }
  139.  
  140.     bool Read(const char* cModuleName, DWORD dwOffset, BYTE* bResult, ULONGLONG tLength) {
  141.         if (!hProcess) {
  142.             return false;
  143.         }
  144.  
  145.  
  146.         std::string sModuleName = cModuleName;
  147.  
  148.         ULONGLONG ulBase = FindModuleBase(sModuleName, dwOffset);
  149.  
  150.         if (!ulBase) {
  151.             return false;
  152.         }
  153.  
  154.         SIZE_T tRet;
  155.  
  156.         if (!ReadProcessMemory(hProcess, (void*)(ulBase + dwOffset), bResult, tLength, &tRet)) {
  157.             return false;
  158.         }
  159.  
  160.         return true;
  161.     }
  162.  
  163.     bool Read(ULONGLONG ulAddress, BYTE* bResult, ULONGLONG tLength) {
  164.         if (!hProcess) {
  165.             return false;
  166.         }
  167.  
  168.         SIZE_T tRet;
  169.  
  170.         if (!ReadProcessMemory(hProcess, (void*)(ulAddress), bResult, tLength, &tRet)) {
  171.             return false;
  172.         }
  173.  
  174.         return true;
  175.     }
  176. };
  177.  
  178. int main() {
  179.  
  180.     MemoryReader mr("MapleStory2.exe");
  181.  
  182.     if (mr.Attach()) {
  183.         puts("Attach");
  184.         ULONGLONG Pointer;
  185.         if (mr.Read("libglesv2.dll", 0x4524E8, (BYTE*)& Pointer, sizeof(ULONGLONG))) {
  186.             puts("Read-1");
  187.             printf("%016llX\n", Pointer);
  188.  
  189.             if (mr.Read(Pointer, (BYTE*)& Pointer, sizeof(ULONGLONG))) {
  190.                 puts("Read-2");
  191.                 printf("%016llX\n", Pointer);
  192.  
  193.                 BYTE Memory[8192];
  194.                 if (mr.Read(Pointer, Memory, sizeof(Memory))) {
  195.                     puts("Read-3");
  196.                     std::string Path;
  197.                     std::vector<std::string> CommandLine;
  198.  
  199.                     Path = (char *)Memory;
  200.                     if (Path.length()) {
  201.                         SIZE_T tpos = Path.length() + 1;
  202.  
  203.                         for (int i = 0; i < 4; i++) {
  204.                             CommandLine.push_back((char*)& Memory[tpos]);
  205.                             tpos += CommandLine[i].length() + 1;
  206.                         }
  207.                     }
  208.  
  209.                     if (CommandLine.size() == 4) {
  210.                         printf("Path = %s\n", Path.c_str());
  211.                         for (SIZE_T i = 0; i < CommandLine.size(); i++) {
  212.                             printf("CommandLine[%lld] = %s\n", i, CommandLine[i].c_str());
  213.                         }
  214.                         std::string Run;
  215.  
  216.                         Run = "\"" + Path + "\"";
  217.  
  218.                         for (SIZE_T i = 0; i < CommandLine.size(); i++) {
  219.                             Run += " " + CommandLine[i];
  220.                         }
  221.  
  222.                         printf("Run = %s\n", Run.c_str());
  223.                     }
  224.                 }
  225.             }
  226.         }
  227.     }
  228.     return 0;
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement