Advertisement
R3v3rs3r

Pattern scan with mask [C++]

Jul 24th, 2023
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ (WinAPI) 1.83 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4.  
  5. // Function to perform a pattern scan with a mask
  6. std::vector<long> patternScanWithMask(const char* filename, const char* pattern, const char* mask) {
  7.     std::vector<long> occurrences;
  8.     std::ifstream file(filename, std::ios::binary);
  9.  
  10.     if (!file.is_open()) {
  11.         std::cout << "Error opening file: " << filename << std::endl;
  12.         return occurrences;
  13.     }
  14.  
  15.     file.seekg(0, std::ios::end);
  16.     std::streampos fileSize = file.tellg();
  17.     file.seekg(0, std::ios::beg);
  18.  
  19.     // Read the entire file into a buffer
  20.     std::vector<char> buffer(fileSize);
  21.     file.read(buffer.data(), fileSize);
  22.  
  23.     file.close();
  24.  
  25.     int patternLength = strlen(pattern);
  26.     int maskLength = strlen(mask);
  27.     int bufferLength = fileSize;
  28.  
  29.     for (int i = 0; i <= bufferLength - patternLength; ++i) {
  30.         bool found = true;
  31.         for (int j = 0; j < patternLength; ++j) {
  32.             if (mask[j] == 'x' && buffer[i + j] != pattern[j]) {
  33.                 found = false;
  34.                 break;
  35.             }
  36.         }
  37.  
  38.         if (found) {
  39.             occurrences.push_back(i);
  40.         }
  41.     }
  42.  
  43.     return occurrences;
  44. }
  45.  
  46. int main() {
  47.     const char* filename = "example.exe";
  48.     const char* pattern = "\x48\x8B\x05\x00\x00\x00\x00"; // Example pattern to search for
  49.     const char* mask = "xxx????"; // Mask: 'x' means compare, '?' means ignore
  50.  
  51.     std::vector<long> occurrences = patternScanWithMask(filename, pattern, mask);
  52.  
  53.     if (occurrences.empty()) {
  54.         std::cout << "Pattern not found in the file." << std::endl;
  55.     } else {
  56.         std::cout << "Pattern found at offsets: ";
  57.         for (const auto& offset : occurrences) {
  58.             std::cout << offset << " ";
  59.         }
  60.         std::cout << std::endl;
  61.     }
  62.  
  63.     return 0;
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement