Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <iostream>
- #include <vector>
- uintptr_t FindPatternWithMask(HANDLE processHandle, uintptr_t baseAddress, const char* pattern, const char* mask) {
- SIZE_T bytesRead;
- MEMORY_BASIC_INFORMATION memInfo;
- std::vector<char> buffer(4096);
- while (VirtualQueryEx(processHandle, (LPCVOID)baseAddress, &memInfo, sizeof(memInfo))) {
- if (memInfo.State == MEM_COMMIT && memInfo.Protect != PAGE_NOACCESS) {
- ReadProcessMemory(processHandle, memInfo.BaseAddress, buffer.data(), buffer.size(), &bytesRead);
- for (size_t i = 0; i < bytesRead; ++i) {
- bool found = true;
- for (size_t j = 0; j < strlen(mask); ++j) {
- if (mask[j] == 'x' && buffer[i + j] != pattern[j]) {
- found = false;
- break;
- }
- }
- if (found) {
- return (uintptr_t)memInfo.BaseAddress + i;
- }
- }
- }
- baseAddress = (uintptr_t)memInfo.BaseAddress + memInfo.RegionSize;
- }
- return 0;
- }
- int main() {
- // Example usage to find a pattern with a mask in an external process
- const char* processName = "TARGET_PROCESS.exe";
- const char* pattern = "\x12\x34\xAB\xCD";
- const char* mask = "xx?x";
- HWND targetWindow = FindWindowA(NULL, processName);
- if (targetWindow == NULL) {
- std::cout << "Could not find the target process window." << std::endl;
- return 1;
- }
- DWORD processId;
- GetWindowThreadProcessId(targetWindow, &processId);
- HANDLE processHandle = OpenProcess(PROCESS_VM_READ, FALSE, processId);
- if (processHandle == NULL) {
- std::cout << "Could not open the target process." << std::endl;
- return 1;
- }
- uintptr_t baseAddress = 0x0; // Start scanning from the base address (modify as needed)
- uintptr_t result = FindPatternWithMask(processHandle, baseAddress, pattern, mask);
- if (result != 0) {
- std::cout << "Pattern found at address: 0x" << std::hex << result << std::endl;
- } else {
- std::cout << "Pattern not found." << std::endl;
- }
- CloseHandle(processHandle);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement