1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
// DeleteMe.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include <iostream> #include <sstream> #include <map> #include <string> #include <string_view> #include <unordered_map> #include <vector> #include <Windows.h> #include <TlHelp32.h> #include <cstddef> #include <bitset> #include <iomanip> #include <psapi.h> #include <stdio.h> #include <tchar.h> #pragma comment(lib, "advapi32.lib") int decimalValue = 0; int main() { std::string input; while (true) { std::cout << "Enter a decimal string: "; std::cin >> input; decimalValue = std::stoi(input); } } // Run program: Ctrl + F5 or Debug > Start Without Debugging menu // Debug program: F5 or Debug > Start Debugging menu // Tips for Getting Started: // 1. Use the Solution Explorer window to add/manage files // 2. Use the Team Explorer window to connect to source control // 3. Use the Output window to see build output and other messages // 4. Use the Error List window to view errors // 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 // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 |
// Memory.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include <iostream> #include <sstream> #include <map> #include <string> #include <string_view> #include <unordered_map> #include <vector> #include <Windows.h> #include <TlHelp32.h> #include <cstddef> #include <bitset> #include <iomanip> #include <psapi.h> #include <stdio.h> #include <tchar.h> #include <thread> #include <chrono> #pragma comment(lib, "advapi32.lib") //const wchar_t* ProcName = L"notepad.exe"; const wchar_t* ProcName = L"deleteme.exe"; DWORD processId = 0; // GetProcId(ProcName); HANDLE hProcess = 0; // OpenProcess(PROCESS_ALL_ACCESS, NULL, processId); class MemoryModule { public: std::string ModuleName; DWORD Address; MemoryModule(DWORD Addr, std::string val) : ModuleName(val), Address(Addr) { //std::cout << "MemoryEntry Addr (" << Addr << ") = " << "0x" << std::hex << val << std::endl; } }; class MemoryEntry { public: int value = 0; //Used for Single byte checks //byte ByteArray[4]; VOID * Address = NULL; VOID * Bytevalue = NULL; //Used for Arrays of values MemoryEntry(VOID * Addr, int val) : value(val), Address(Addr) { //std::cout << "MemoryEntry Addr (" << Addr << ") = " << "0x" << std::hex << val << std::endl; } MemoryEntry(VOID* Addr, byte val[], int length){ Address = Addr; value = length; for (int i = 0; i < length; i++) { } Bytevalue = malloc(length); memcpy(Bytevalue, val, length); } /* * ~MemoryEntry() { if (Bytevalue) { free(&Bytevalue); } } */ }; //std::unordered_map<std::string, std::vector<MemoryEntry>> GMemoryMap; //std::unordered_map<MemoryModule, std::vector<MemoryEntry>> GMemoryMap; std::unordered_map<VOID *, std::vector<MemoryEntry>> GMemoryMap; DWORD GetProcId(const wchar_t* procName); uintptr_t GetModuleBaseAddress(DWORD procID, const wchar_t* modName); uintptr_t FindDMAAddy(HANDLE hProc, uintptr_t ptr, std::vector<unsigned int> offsets); BOOL SetPrivilege(HANDLE hToken, // access token handle LPCTSTR lpszPrivilege, // name of privilege to enable/disable BOOL bEnablePrivilege // to enable or disable privilege ) { TOKEN_PRIVILEGES tp; LUID luid; if (!LookupPrivilegeValue( NULL, // lookup privilege on local system lpszPrivilege, // privilege to lookup &luid)) // receives LUID of privilege { printf("LookupPrivilegeValue error: %u\n", GetLastError()); return FALSE; } tp.PrivilegeCount = 1; tp.Privileges[0].Luid = luid; if (bEnablePrivilege) tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; else tp.Privileges[0].Attributes = 0; // Enable the privilege or disable all privileges. if (!AdjustTokenPrivileges( hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL)) { printf("AdjustTokenPrivileges error: %u\n", GetLastError()); return FALSE; } if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) { printf("The token does not have the specified privilege. \n"); return FALSE; } return TRUE; } std::vector<std::pair<HMODULE, DWORD>> GetModules(HANDLE hProcess) { std::vector<std::pair<HMODULE, DWORD>> modules; if (hProcess != NULL) { HMODULE hMods[1024]; DWORD cbNeeded = 0; if (EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded)) { for (int i = 0; i < (cbNeeded / sizeof(HMODULE)); i++) { MODULEINFO mi; GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)); modules.push_back(std::make_pair(hMods[i], mi.SizeOfImage)); } } else { std::cerr << "EnumProcessModules failed. Error code: " << GetLastError() << std::endl; } } return modules; } long TotalSizeOfMemory = 0; long TotalADDRFound = 0; int ReadAllProcModules(DWORD processId, HANDLE hProcess, byte BytePattern[], int Bytelength) { if (hProcess == NULL) { std::cerr << "Failed to open process. Error code: " << GetLastError() << std::endl; return 1; } // Get all modules in the target process std::vector<std::pair<HMODULE, DWORD>> modules = GetModules(hProcess); // Loop through each module and read memory for (const auto& module : modules) { // Allocate buffer to store module's memory std::vector<BYTE> buffer(module.second); // Read memory from the module's base address HANDLE ModuleAddress = module.first; if (ReadProcessMemory(hProcess, module.first, buffer.data(), buffer.size(), nullptr)) { TotalSizeOfMemory += buffer.size(); // Process the read data here (e.g., print or manipulate) // For demonstration, let's print the first few bytes of each module std::cout << "Module Base Address: " << module.first << " Module Size: " << buffer.size() << std::endl; /* std::cout << "First 16 bytes of module:" << std::endl; for (size_t i = 0; i < std::min<size_t>(16, buffer.size()); i++) { std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(buffer[i]) << " "; } */ for (size_t i = 0; i < buffer.size(); i++) { if (Bytelength == 1) //This will not work with a pointer { if (BytePattern[0] == buffer[i]) { //MemoryModule MemoryRegionKey = MemoryModule(DWORD(module.first), "Module"); VOID* test = module.first; VOID* test2 = module.first + 1; VOID* test3 = ((char*)module.first) + i; MemoryEntry MemoryEntryToAdd = MemoryEntry(((char*)module.first) + i, BytePattern[0]); GMemoryMap[module.first].push_back(MemoryEntryToAdd); TotalADDRFound += 1; } } else { int lenth = sizeof(BytePattern); bool match = true; for (size_t ii = 0; ii < Bytelength; ii++) { if (BytePattern[ii] != buffer[i + ii]) { match = false; break; } } if (match) { MemoryEntry MemoryEntryToAdd = MemoryEntry(((char*)module.first) + i, BytePattern, Bytelength); GMemoryMap[module.first].push_back(MemoryEntryToAdd); TotalADDRFound += 1; } } } } else { std::cerr << "Failed to read memory from module." << std::endl; } //std::cout << "Total Memorysize: " << std::dec << (TotalSizeOfMemory / 1024 / 1024) << "(MBs)" << std::endl; } std::cout << "Total TotalADDRFound: " << std::dec << TotalADDRFound << std::endl; } void ReadSpecificAddress(DWORD processId, HANDLE hProcess) { //Getmodulebaseaddress uintptr_t moduleBase = GetModuleBaseAddress(processId, ProcName); //Resolve base address of the pointer chain uintptr_t dynamicPtrBaseAddr = moduleBase + 0x10f4f4; std::cout << "DynamicPtrBaseAddr = " << "0x" << std::hex << dynamicPtrBaseAddr << std::endl; //Resolve our ammo pointer chain std::vector<unsigned int> ammoOffsets = { 0x374, 0x14, 0x0 }; uintptr_t ammoAddr = FindDMAAddy(hProcess, dynamicPtrBaseAddr, ammoOffsets); std::cout << "ammoAddr = " << "0x" << std::hex << ammoAddr << std::endl; //Read Ammo value int ammoValue = 0; ReadProcessMemory(hProcess, (BYTE*)ammoAddr, &ammoValue, sizeof(ammoValue), nullptr); //std::cout << "Current ammo = " << std::dec << ammoValue << std::endl; //Write to it int newAmmo = 1338; WriteProcessMemory(hProcess, (BYTE*)ammoAddr, &newAmmo, sizeof(newAmmo), nullptr); //Read out again ReadProcessMemory(hProcess, (BYTE*)ammoAddr, &ammoValue, sizeof(ammoValue), nullptr); //std::cout << "New ammo = " << std::dec << ammoValue << std::endl; } void CheckChanged(DWORD processId, HANDLE hProcess, std::unordered_map<VOID*, std::vector<MemoryEntry>>& LMemoryMap, bool RemoveIfChangedInsteadOfKeep) { char Buffer[1] = { 0 }; for (auto& pair : LMemoryMap) { VOID* key = pair.first; std::vector<MemoryEntry>& Tempobj = pair.second; for (int i = pair.second.size() - 1; i >= 0; i--) { MemoryEntry Obj = pair.second.at(i); VOID* ObjAddr = Obj.Address; if (ReadProcessMemory(hProcess, ObjAddr, &Buffer, 1, nullptr)) { char a = Buffer[0]; char b = Obj.value; if (!RemoveIfChangedInsteadOfKeep) { if (a != b) { //std::cout << "ModuleAddress: 0x" << std::hex << key << " Address: " << ObjAddr << " - Value: 0x" << std::hex << Obj.value << " Compared to: 0x" << (int)(Buffer[0]) << "\n"; if (TotalADDRFound % 10000 == 0) { std::cout << "Change Detected ModuleAddress: 0x" << std::hex << key << " Address: " << ObjAddr << " - BeforeValue: 0x" << std::hex << Obj.value << " NowValue: 0x" << std::hex << (int)(Buffer[0]) << "\n"; } TotalADDRFound -= 1; Obj.value = Buffer[0]; } else { Tempobj.erase(Tempobj.begin() + i); //Remove if same //std::cout << "Keeping ModuleAddress: 0x" << std::hex << key << " Address: " << ObjAddr << " - BeforeValue: 0x" << std::hex << Obj.value << " NowValue: 0x" << std::hex << (int)(Buffer[0]) << " Compared to: 0x" << int(Pattern[0]) << "\n"; } } else { if (a == b) { //std::cout << "ModuleAddress: 0x" << std::hex << key << " Address: " << ObjAddr << " - Value: 0x" << std::hex << Obj.value << " Compared to: 0x" << (int)(Buffer[0]) << "\n"; } else { if (TotalADDRFound % 10000 == 0) { std::cout << "Change Detected ModuleAddress: 0x" << std::hex << key << " Address: " << ObjAddr << " - BeforeValue: 0x" << std::hex << Obj.value << " NowValue: 0x" << std::hex << (int)(Buffer[0]) << "\n"; } TotalADDRFound -= 1; Tempobj.erase(Tempobj.begin() + i); //Remove if same } } } else { std::cout << "Unable to read from: " << Obj.Address << "\n"; } } } } void CheckAllRegisters(DWORD processId, HANDLE hProcess, std::unordered_map<VOID*, std::vector<MemoryEntry>>& LMemoryMap, byte Pattern[]) { char Buffer[1] = { 0 }; for (auto& pair : LMemoryMap) { VOID* key = pair.first; std::vector<MemoryEntry>& Tempobj = pair.second; for (int i = pair.second.size() - 1; i >= 0; i--) { MemoryEntry Obj = pair.second.at(i); VOID* ObjAddr = Obj.Address; if (ReadProcessMemory(hProcess, ObjAddr, &Buffer, 1, nullptr)) { char a = Buffer[0]; char b = Pattern[0]; if (a != b) { //std::cout << "ModuleAddress: 0x" << std::hex << key << " Address: " << ObjAddr << " - Value: 0x" << std::hex << Obj.value << " Compared to: 0x" << (int)(Buffer[0]) << "\n"; Tempobj.erase(Tempobj.begin() + i); if (TotalADDRFound % 10000 == 0) { std::cout << "Removing ModuleAddress: 0x" << std::hex << key << " Address: " << ObjAddr << " - BeforeValue: 0x" << std::hex << Obj.value << " NowValue: 0x" << std::hex << (int)(Buffer[0]) << " Compared to: 0x" << int(Pattern[0]) << "\n"; std::cout << "LMemoryMap - Remaining: " << std::dec << (Tempobj.size() - i) << "/" << Tempobj.size() << " Address left to check:" << TotalADDRFound << std::endl; } TotalADDRFound -= 1; } else { //std::cout << "Keeping ModuleAddress: 0x" << std::hex << key << " Address: " << ObjAddr << " - BeforeValue: 0x" << std::hex << Obj.value << " NowValue: 0x" << std::hex << (int)(Buffer[0]) << " Compared to: 0x" << int(Pattern[0]) << "\n"; } } else { std::cout << "Unable to read from: " << Obj.Address << "\n"; } } } } void DumpMemory() { std::cout << "\r\nElements in GMemoryMap:\n"; long ItemCount = 0; long PageCount = 0; for (const auto& pair : GMemoryMap) { PageCount++; //MemoryModule key = pair.first; VOID* key = pair.first; std::vector<MemoryEntry> obj = pair.second; /* for (const auto& obj : GMemoryMap["key1"]) { std::cout << obj.value << "\n"; } */ for (const auto& myobj : obj) { //MemoryEntry Obj = pair.second.at(i); VOID* ObjAddr = myobj.Address; if (myobj.Bytevalue == NULL) { char buffer[1] = { 0 }; if (!ReadProcessMemory(hProcess, ObjAddr, &buffer, 1, nullptr)) { //Unable to get mem } std::cout << "ModuleAddress: 0x" << std::hex << key << " Address: " << myobj.Address << " - InCache:" << std::dec << myobj.value << " InMem: " << int(buffer[0]) << "\n"; } else { unsigned char * buffer = (unsigned char *)(malloc(myobj.value)); if (!ReadProcessMemory(hProcess, ObjAddr, buffer, myobj.value, nullptr)) { //Unable to get mem } std::cout << "ModuleAddress: 0x" << std::hex << key << " Address: " << myobj.Address << " - ByteLen:" << std::dec << myobj.value << "\n"; unsigned char * bytePtr = static_cast<unsigned char*>(myobj.Bytevalue); std::cout << "InCache: "; for (size_t i = 0; i < std::min<size_t>(16, myobj.value); i++) { std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(bytePtr[i]) << " "; } std::cout << std::endl; std::cout << "InMem : "; for (size_t i = 0; i < std::min<size_t>(16, myobj.value); i++) { std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(buffer[i]) << " "; } std::cout << std::endl; if (!WriteProcessMemory(hProcess, myobj.Bytevalue, (void *)buffer, 1, nullptr)) { std::cerr << "Failed to write memory at address: " << myobj.Bytevalue << std::endl; } else { std::cout << "Successfully updated " << myobj.value << " bytes from address " << ObjAddr << " to " << myobj.Bytevalue << std::endl; } free(buffer); } ItemCount++; } } //std::vector<MemoryEntry> Memory = GMemoryMap[1]; std::cout << std::endl << "LMemoryMap - PageCount:" << std::dec << PageCount << " ItemCount" << ItemCount << std::endl; } DWORD GetProcId(const wchar_t* procName) { DWORD procId = 0; HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hSnap != INVALID_HANDLE_VALUE) { PROCESSENTRY32 procEntry; procEntry.dwSize = sizeof(procEntry); if (Process32First(hSnap, &procEntry)) { do { if (!_wcsicmp(procEntry.szExeFile, procName)) { procId = procEntry.th32ProcessID; break; } } while (Process32Next(hSnap, &procEntry)); } } CloseHandle(hSnap); return procId; } uintptr_t GetModuleBaseAddress(DWORD procID, const wchar_t* modName) { uintptr_t modBaseAddr = 0; HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, procID); if (hSnap != INVALID_HANDLE_VALUE) { MODULEENTRY32 modEntry; modEntry.dwSize = sizeof(modEntry); if (Module32First(hSnap, &modEntry)) { do { if (!_wcsicmp(modEntry.szModule, modName)) { modBaseAddr = (uintptr_t)modEntry.modBaseAddr; break; } } while (Module32Next(hSnap, &modEntry)); } } CloseHandle(hSnap); return modBaseAddr; } uintptr_t FindDMAAddy(HANDLE hProc, uintptr_t ptr, std::vector<unsigned int> offsets) { uintptr_t addr = ptr; for (unsigned int i = 0; i < offsets.size(); ++i) { ReadProcessMemory(hProc, (BYTE*)addr, &addr, sizeof(addr), 0); addr += offsets[i]; } return addr; } int PrintModules(DWORD processID, HANDLE hProcess) { HMODULE hMods[1024]; DWORD cbNeeded; unsigned int i; // Print the process identifier. printf("\nProcess ID: %u\n", processID); // Get a list of all the modules in this process. if (EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded)) { for (i = 0; i < (cbNeeded / sizeof(HMODULE)); i++) { TCHAR szModName[MAX_PATH]; // Get the full path to the module's file. if (GetModuleFileNameEx(hProcess, hMods[i], szModName, sizeof(szModName) / sizeof(TCHAR))) { // Print the module name and handle value. _tprintf(TEXT("\t%s (0x%08X)\n"), szModName, hMods[i]); } } } return 0; } void ResumeProcess(DWORD processId) { HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); if (hSnapshot != INVALID_HANDLE_VALUE) { THREADENTRY32 te32; te32.dwSize = sizeof(THREADENTRY32); if (Thread32First(hSnapshot, &te32)) { do { if (te32.th32OwnerProcessID == processId) { HANDLE hThread = OpenThread(THREAD_SUSPEND_RESUME, FALSE, te32.th32ThreadID); if (hThread != NULL) { ResumeThread(hThread); CloseHandle(hThread); } } } while (Thread32Next(hSnapshot, &te32)); } CloseHandle(hSnapshot); } } void SuspendProcess(DWORD processId) { HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); if (hSnapshot != INVALID_HANDLE_VALUE) { THREADENTRY32 te32; te32.dwSize = sizeof(THREADENTRY32); if (Thread32First(hSnapshot, &te32)) { do { if (te32.th32OwnerProcessID == processId) { HANDLE hThread = OpenThread(THREAD_SUSPEND_RESUME, FALSE, te32.th32ThreadID); if (hThread != NULL) { SuspendThread(hThread); CloseHandle(hThread); } } } while (Thread32Next(hSnapshot, &te32)); } CloseHandle(hSnapshot); } } /* std::ostream& operator<<(std::ostream& os, int b) { return os << std::bitset<8>(int(b)); } */ void DisplayHelp() { std::cout << R"( Available commands: - ProcessName: Set the process image name for memory scanning. - scanstring: Scan for a specified string in memory. - scan8: Scan for a specified int8 value in memory. - same: Check for unchanged memory regions since last scan. - changed: Check for changed memory regions since last scan. - watch: Start monitoring memory for changes. - dump: Print information about loaded modules and dump memory. - recentlyremoved: Display recently removed memory addresses. - clear: Clear the memory list. - suspend: Suspend the target process. - resume: Resume the target process. - writeunistring: Overwrite a Unicode string in memory. - writestring: Overwrite an ASCII string in memory. - wint: Write an integer value to memory. - scanandoverwrite: Scan for a pattern and overwrite it in memory. - help: Display available commands. - version: Display version information. - exit/quit: Exit the program. )" << std::endl; } void DisplayVersion() { std::cout << R"(1.0)" << std::endl; } bool stopFlag = false; void threadFunction() { while (true) { if (stopFlag) { break; } byte Buffer[1] = { 0 }; for (auto& pair : GMemoryMap) { if (stopFlag) { break; } VOID* key = pair.first; std::vector<MemoryEntry>& Tempobj = pair.second; for (int i = pair.second.size() - 1; i >= 0; i--) { if (stopFlag) { break; } std::this_thread::sleep_for(std::chrono::milliseconds(50)); MemoryEntry & Obj = pair.second.at(i); VOID* ObjAddr = Obj.Address; if (ReadProcessMemory(hProcess, ObjAddr, &Buffer, 1, nullptr)) { char a = Buffer[0]; char b = Obj.value; if (a != b) { //std::cout << "ModuleAddress: 0x" << std::hex << key << " Address: " << ObjAddr << " - Value: 0x" << std::hex << Obj.value << " Compared to: 0x" << (int)(Buffer[0]) << "\n"; //Tempobj.erase(Tempobj.begin() + i); std::cout << "Watch found a value change @ ModuleAddress: 0x" << std::hex << key << " Address: " << ObjAddr << " - BeforeValue: 0x" << std::hex << Obj.value << " NowValue: 0x" << std::hex << (int)(Buffer[0]) << "\n"; Obj.value = Buffer[0]; //TotalADDRFound -= 1; } else { //std::cout << "Keeping ModuleAddress: 0x" << std::hex << key << " Address: " << ObjAddr << " - BeforeValue: 0x" << std::hex << Obj.value << " NowValue: 0x" << std::hex << (int)(Buffer[0]) << " Compared to: 0x" << int(Pattern[0]) << "\n"; } } else { std::cout << "Unable to read from: " << Obj.Address << "\n"; } } } if (stopFlag) { break; } } } std::thread t; void menu(const std::string& userInput) { /* std::vector<std::pair<std::intptr_t, std::vector<byte[]>>> MyArrayListOfAddresses; std::map<std::intptr_t, std::vector<byte[]>> MyDictionaryOfAddresses; std::map < std::intptr_t, std::pair < std::vector < byte[] >, std::vector<byte[] >> > MyArrayListOfAddressesRecentlyRemoved; */ std::intptr_t MyProcessId = 0; // Initialize with an appropriate value std::string ValueToSearchFor; std::string IntValue; int Scans = 1; if (userInput == "ProcessName") { std::string ProcessName; std::cout << "Process Image Name (ex. notepad.exe): "; std::getline(std::cin, ProcessName); //processId = GetProcId((const * wchar_t)ProcessName); hProcess = OpenProcess(PROCESS_ALL_ACCESS, NULL, processId); //MyArrayListOfAddresses.clear(); } else if (userInput == "scanstring") { std::cout << "string: "; std::getline(std::cin >> std::ws, ValueToSearchFor); //cin.getline(input, sizeof(input)); //std::cin >> ValueToSearchFor; try { std::vector<char> bytes(ValueToSearchFor.begin(), ValueToSearchFor.end()); bytes.push_back('\0'); char* c = &bytes[0]; int size = bytes.size(); ReadAllProcModules(processId, hProcess, (byte*)c, bytes.size() - 1); } catch (const std::invalid_argument& ex) { std::cerr << "Invalid input: " << ex.what() << std::endl; } catch (const std::out_of_range& ex) { std::cerr << "Out of range error: " << ex.what() << std::endl; } } else if (userInput == "scan8") { std::string param; std::cout << "int8: "; std::cin >> param; if (GMemoryMap.size() == 0) { try { int decimalValue = std::stoi(param); ReadAllProcModules(processId, hProcess, new byte[]{ (unsigned char)decimalValue }, 1); } catch (const std::invalid_argument& ex) { std::cerr << "Invalid input: " << ex.what() << std::endl; } catch (const std::out_of_range& ex) { std::cerr << "Out of range error: " << ex.what() << std::endl; } } else { try { int decimalValue = std::stoi(param); CheckAllRegisters(processId, hProcess, GMemoryMap, new byte[]{ (unsigned char)decimalValue }); } catch (const std::invalid_argument& ex) { std::cerr << "Invalid input: " << ex.what() << std::endl; } catch (const std::out_of_range& ex) { std::cerr << "Out of range error: " << ex.what() << std::endl; } } } else if (userInput == "same") { CheckChanged(processId, hProcess, GMemoryMap, true); } else if (userInput == "changed") { CheckChanged(processId, hProcess, GMemoryMap, false); } else if (userInput == "watch") { stopFlag = false; t = std::thread(threadFunction); } else if (userInput == "stopwatch") { stopFlag = true; } else if (userInput == "dump") { PrintModules(processId, hProcess); DumpMemory(); } else if (userInput == "recentlyremoved") { /* for (const auto& entry : MyArrayListOfAddressesRecentlyRemoved) { //std::cout << std::hex << entry.first << std::endl; std::cout << "BEFORE: "; for (const auto& byteValue : entry.second.first) { //std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byteValue) << " "; } std::cout << std::endl; std::cout << "AFTER: "; for (const auto& byteValue : entry.second.second) { //std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byteValue) << " "; } std::cout << std::endl << std::endl; } */ } else if (userInput == "clear") { std::cout << "Memory list cleared" << std::endl; GMemoryMap.clear(); } else if (userInput == "suspend") { SuspendProcess(processId); } else if (userInput == "resume") { ResumeProcess(processId); } else if (userInput == "writeunistring") { // DotNetMemoryScan find_aob; std::string param; std::cout << "Overwrite unistring: "; //std::getline(std::cin, param); //int decimalValue = std::stoi(param); //std::cin >> param; /* for (const auto& MyAddress : MyArrayListOfAddresses) { if (MyAddress.first > 0) { //std::cout << "[scan_all 1] result 0x" << std::hex << std::setw(16) << std::setfill('0') << MyAddress.first << std::endl; // std::cout << "Bytes written: " << find_aob.write_mem(MyProcess, MyAddress.first, ValueToWrite) << std::endl; } } */ } else if (userInput == "writestring") { // DotNetMemoryScan find_aob; std::string ValueToWrite; std::cout << "Overwrite Asciistring: "; std::getline(std::cin >> std::ws, ValueToSearchFor); try { int Lengthofbuffer = ValueToSearchFor.length(); void * buffer = malloc(ValueToSearchFor.length()); for (const auto& pair : GMemoryMap) { //MemoryModule key = pair.first; VOID* key = pair.first; std::vector<MemoryEntry> obj = pair.second; for (const auto& myobj : obj) { VOID* ObjAddr = myobj.Address; if (!ReadProcessMemory(hProcess, ObjAddr, buffer, Lengthofbuffer, nullptr)) { } SIZE_T bytesWritten = 0; if (!WriteProcessMemory(hProcess, ObjAddr, ValueToWrite.c_str(), ValueToWrite.size(), &bytesWritten)) { std::cerr << "Failed to write memory at address: " << ObjAddr << std::endl; } else { std::cout << "Successfully wrote " << bytesWritten << " bytes to address: " << ObjAddr << std::endl; } std::cout << "Written ModuleAddress: 0x" << std::hex << key << " Address: " << myobj.Address << " - InCache:" << std::dec << myobj.value << " beforeInMem: " << (char *)buffer << " nowInMem: " << ValueToSearchFor << "\n"; for (size_t i = 0; i < Lengthofbuffer; ++i) { //std::cout << static_cast<int>(buffer[i]) << " "; } } } free(buffer); } catch (const std::invalid_argument& ex) { std::cerr << "Invalid input: " << ex.what() << std::endl; } catch (const std::out_of_range& ex) { std::cerr << "Out of range error: " << ex.what() << std::endl; } catch (const std::exception& ex) { std::cerr << "Error: " << ex.what() << std::endl; } } else if (userInput == "wint") { std::string param; std::cout << "value to write: "; std::cin >> param; //std::getline(std::cin, param); int decimalValue = std::stoi(param); char buffer[1] = { 0 }; char bufferToWrite[1] = { decimalValue }; for (const auto& pair : GMemoryMap) { //MemoryModule key = pair.first; VOID* key = pair.first; std::vector<MemoryEntry> obj = pair.second; for (const auto& myobj : obj) { VOID* ObjAddr = myobj.Address; if (!ReadProcessMemory(hProcess, ObjAddr, &buffer, 1, nullptr)) { } if (!WriteProcessMemory(hProcess, ObjAddr, &bufferToWrite, 1, nullptr)) { //Unable to get mem } std::cout << "Written ModuleAddress: 0x" << std::hex << key << " Address: " << myobj.Address << " - InCache:" << std::dec << myobj.value << " beforeInMem: " << int(buffer[0]) << " nowInMem: " << decimalValue << "\n"; } } } else if (userInput == "scanandoverwrite") { std::string OverwriteString; std::cout << "Overwrite string: "; std::getline(std::cin, OverwriteString); // OverwriteAllBytePattens(MyProcess.ProcessName, ConvertStringToUniByteString(OverwriteString), ConvertStringToUniByteString(ValueToSearchFor)); } else if (userInput == "help") { DisplayHelp(); } else if (userInput == "version") { DisplayVersion(); } else if (userInput == "exit" || userInput == "quit") { exit(0); } else { std::cout << "Unrecognized command. Type 'help' for available commands." << std::endl; } } int main() { std::cout << "Starting!\nEnter 'help' for commands\r\n"; HANDLE currentToken; OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, ¤tToken); if (!SetPrivilege(currentToken, SE_DEBUG_NAME, TRUE)) { std::cout << "Unable to adjust privileges" << std::endl; } //HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId); processId = GetProcId(ProcName); hProcess = OpenProcess(PROCESS_ALL_ACCESS, NULL, processId); std::string input; while (true) { std::cout << ">"; std::cin >> input; menu(input); } /* std::cout << "Press Any Key to continue:\n"; getchar(); */ // Close the process handle CloseHandle(hProcess); return 0; } |