Advertisement
R3v3rs3r

Pattern scan and replace [C++]

Jul 24th, 2023
589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ (WinAPI) 2.77 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. // Function to read the content of the file into a vector
  7. std::vector<uint8_t> readBinaryFile(const std::string& filename) {
  8.     std::ifstream file(filename, std::ios::binary | std::ios::ate);
  9.     if (!file.is_open()) {
  10.         throw std::runtime_error("Failed to open file");
  11.     }
  12.  
  13.     std::streamsize fileSize = file.tellg();
  14.     file.seekg(0, std::ios::beg);
  15.  
  16.     std::vector<uint8_t> buffer(fileSize);
  17.     if (!file.read(reinterpret_cast<char*>(buffer.data()), fileSize)) {
  18.         throw std::runtime_error("Failed to read file");
  19.     }
  20.  
  21.     return buffer;
  22. }
  23.  
  24. // Function to modify the bytes in the vector using a mask
  25. void modifyBytes(std::vector<uint8_t>& data, const std::vector<uint8_t>& pattern, const std::vector<uint8_t>& mask, const std::vector<uint8_t>& replacement) {
  26.     if (pattern.size() != mask.size() || pattern.size() != replacement.size()) {
  27.         throw std::runtime_error("Pattern, mask, and replacement must have the same size");
  28.     }
  29.  
  30.     auto it = std::search(data.begin(), data.end(), pattern.begin(), pattern.end());
  31.  
  32.     while (it != data.end()) {
  33.         auto startPos = std::distance(data.begin(), it);
  34.  
  35.         // Check if the pattern matches using the mask
  36.         bool patternMatches = true;
  37.         for (size_t i = 0; i < pattern.size(); ++i) {
  38.             if ((data[startPos + i] & mask[i]) != pattern[i]) {
  39.                 patternMatches = false;
  40.                 break;
  41.             }
  42.         }
  43.  
  44.         // If the pattern matches, replace the bytes
  45.         if (patternMatches) {
  46.             std::copy(replacement.begin(), replacement.end(), data.begin() + startPos);
  47.         }
  48.  
  49.         it = std::search(it + 1, data.end(), pattern.begin(), pattern.end());
  50.     }
  51. }
  52.  
  53. int main() {
  54.     try {
  55.         std::string filename = "example.exe";
  56.         std::vector<uint8_t> fileData = readBinaryFile(filename);
  57.  
  58.         // Define the pattern, mask, and replacement
  59.         std::vector<uint8_t> pattern = {0x12, 0x34, 0x56};
  60.         std::vector<uint8_t> mask = {0xFF, 0xFF, 0xFF}; // Use 0xFF for exact match, 0x00 to ignore the byte
  61.         std::vector<uint8_t> replacement = {0xAB, 0xCD, 0xEF};
  62.  
  63.         modifyBytes(fileData, pattern, mask, replacement);
  64.  
  65.         // Write the modified data back to the file (optional)
  66.         std::ofstream outputFile(filename, std::ios::binary);
  67.         if (!outputFile.write(reinterpret_cast<const char*>(fileData.data()), fileData.size())) {
  68.             throw std::runtime_error("Failed to write to file");
  69.         }
  70.  
  71.         std::cout << "Modification successful.\n";
  72.     }
  73.     catch (const std::exception& ex) {
  74.         std::cerr << "Error: " << ex.what() << std::endl;
  75.         return 1;
  76.     }
  77.  
  78.     return 0;
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement