Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<Windows.h>
- #include<TlHelp32.h>
- #pragma comment(lib, "kernel32.lib")
- #include<string>
- #include<vector>
- class MemoryReader {
- private:
- std::string sProcessName;
- std::vector<MODULEENTRY32> MemoryMap;
- HANDLE hProcess;
- DWORD dwProcessID;
- bool DebugOn() {
- TOKEN_PRIVILEGES tp;
- LUID luid;
- memset(&tp, 0, sizeof(TOKEN_PRIVILEGES));
- memset(&luid, 0, sizeof(LUID));
- if (!LookupPrivilegeValueA(NULL, SE_DEBUG_NAME, &luid)) {
- return false;
- }
- tp.PrivilegeCount = 1;
- tp.Privileges[0].Luid = luid;
- tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
- HANDLE hToken;
- if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken)) {
- return false;
- }
- if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL)) {
- return false;
- }
- CloseHandle(hToken);
- return true;
- }
- bool CreateMemoryMap(DWORD dwPID) {
- HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID);
- if (hSnapshot == INVALID_HANDLE_VALUE) {
- return false;
- }
- MODULEENTRY32 me;
- memset(&me, 0, sizeof(MODULEENTRY32));
- me.dwSize = sizeof(MODULEENTRY32);
- if (Module32First(hSnapshot, &me) == FALSE) {
- CloseHandle(hSnapshot);
- return false;
- }
- MemoryMap.clear();
- do {
- MemoryMap.push_back(me);
- } while (Module32Next(hSnapshot, &me));
- CloseHandle(hSnapshot);
- return true;
- }
- bool SearchProcess() {
- HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
- if (hSnapshot == INVALID_HANDLE_VALUE) {
- return false;
- }
- PROCESSENTRY32 pe;
- pe.dwSize = sizeof(PROCESSENTRY32);
- if (!Process32First(hSnapshot, &pe)) {
- CloseHandle(hSnapshot);
- return false;
- }
- do {
- if (!_strnicmp(pe.szExeFile, sProcessName.c_str(), sProcessName.length())) {
- dwProcessID = pe.th32ProcessID;
- return CreateMemoryMap(pe.th32ProcessID);
- }
- } while (Process32Next(hSnapshot, &pe));
- CloseHandle(hSnapshot);
- return false;
- }
- ULONGLONG FindModuleBase(std::string sModuleName, DWORD dwOffset) {
- for (SIZE_T i = 0; i < MemoryMap.size(); i++) {
- if (!_strnicmp(MemoryMap[i].szModule, sModuleName.c_str(), sModuleName.length())) {
- if (dwOffset < MemoryMap[i].modBaseSize) {
- return (ULONGLONG)MemoryMap[i].modBaseAddr;
- }
- }
- }
- return 0;
- }
- public:
- MemoryReader(const char *cProcessName) {
- sProcessName = cProcessName;
- hProcess = NULL;
- dwProcessID = 0;
- }
- ~MemoryReader() {
- dwProcessID = 0;
- if (hProcess) {
- CloseHandle(hProcess);
- hProcess = NULL;
- }
- }
- bool Attach() {
- if (!DebugOn()) {
- return false;
- }
- if (!SearchProcess()) {
- return false;
- }
- hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, dwProcessID);
- if (!hProcess) {
- return false;
- }
- return true;
- }
- bool Read(const char* cModuleName, DWORD dwOffset, BYTE* bResult, ULONGLONG tLength) {
- if (!hProcess) {
- return false;
- }
- std::string sModuleName = cModuleName;
- ULONGLONG ulBase = FindModuleBase(sModuleName, dwOffset);
- if (!ulBase) {
- return false;
- }
- SIZE_T tRet;
- if (!ReadProcessMemory(hProcess, (void*)(ulBase + dwOffset), bResult, tLength, &tRet)) {
- return false;
- }
- return true;
- }
- bool Read(ULONGLONG ulAddress, BYTE* bResult, ULONGLONG tLength) {
- if (!hProcess) {
- return false;
- }
- SIZE_T tRet;
- if (!ReadProcessMemory(hProcess, (void*)(ulAddress), bResult, tLength, &tRet)) {
- return false;
- }
- return true;
- }
- };
- int main() {
- MemoryReader mr("MapleStory2.exe");
- if (mr.Attach()) {
- puts("Attach");
- ULONGLONG Pointer;
- if (mr.Read("libglesv2.dll", 0x4524E8, (BYTE*)& Pointer, sizeof(ULONGLONG))) {
- puts("Read-1");
- printf("%016llX\n", Pointer);
- if (mr.Read(Pointer, (BYTE*)& Pointer, sizeof(ULONGLONG))) {
- puts("Read-2");
- printf("%016llX\n", Pointer);
- BYTE Memory[8192];
- if (mr.Read(Pointer, Memory, sizeof(Memory))) {
- puts("Read-3");
- std::string Path;
- std::vector<std::string> CommandLine;
- Path = (char *)Memory;
- if (Path.length()) {
- SIZE_T tpos = Path.length() + 1;
- for (int i = 0; i < 4; i++) {
- CommandLine.push_back((char*)& Memory[tpos]);
- tpos += CommandLine[i].length() + 1;
- }
- }
- if (CommandLine.size() == 4) {
- printf("Path = %s\n", Path.c_str());
- for (SIZE_T i = 0; i < CommandLine.size(); i++) {
- printf("CommandLine[%lld] = %s\n", i, CommandLine[i].c_str());
- }
- std::string Run;
- Run = "\"" + Path + "\"";
- for (SIZE_T i = 0; i < CommandLine.size(); i++) {
- Run += " " + CommandLine[i];
- }
- printf("Run = %s\n", Run.c_str());
- }
- }
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement