Drovosek01 / ReplaceHexPatcher

Multitool for Windows for native find + replace bytes in hex format and made other system modifications from parsed template
6 stars 0 forks source link

Language: Русский | English

What kind of repository is this

The code in this repository is the result of an attempt to find a Windows native way to find and replace bytes.

Native means it does not use third-party programs (only the tools included with the system, in this case Windows 10).

On UNIX systems, the search and replacement of bytes in hex format can be carried out using the utilities perl and sed (and probably some other tools) that are preinstalled in most GNU Linux distributions and in macOS too.

3 "programming tools" are preinstalled in Windows - CMD, Visual Basic Script, Powershell. CMD is too limited in capabilities. In Visual Basic Script, I have not found a way to write effective code to find and replace a byte pattern in a file of any size. But Powershell is, very roughly speaking, a C# code execution environment, and with C# you can do a lot of things, and therefore using Powershell code it is quite possible to search and replace bytes in hex format.

Alternatives

I have not found any other ready-to-use Powershell or Visual Basic Script scripts to find byte replacements. In this case, the alternative option is not a native method.:

Functions

Main:

Together with the wrappers:

For more information, see documentation

Usage examples

The main patcher script

.\ReplaceHexBytesAll.ps1 -filePath "<path to file>" -patterns "<hex search pattern>/<hex replacement pattern>",

Here is an example:

  1. Start Powershell
  2. Use cd <path> to go to the folder with the file ReplaceHexBytesAll.ps1
  3. In the Powershell window, run:
    .\ReplaceHexBytesAll.ps1 -filePath "D:\TEMP\file.exe" -patterns "48 83EC2 8BA2F 000000 488A/202 0EB1 1111 11111 111111","C42518488D4D68\90909011111175","45A8488D55A8|75EB88909090","\xAA\x7F\xBB\x08\xE3\x4D|\xBB\x90\xB1\xE8\x99\x4D" -makeBackup

Wrapper script with all the data inside

The wrappers folder contains the data inside folder and the Start.cmd file in it Fill in all the data inside the Start.cmd file and you can double-click it. Inside the file there is a memo of what needs to be done/filled in inside the file, also it is written in more detail in documentation

Wrapper script with data processing from template.txt

The wrappers folder contains the data inside folder and the files Start.cmd, Parser.ps1, template.txt

Necessary:

  1. Fill in the form template.txt depending on what you need to do
  2. If all 3 files are in 1 folder, just run Start.cmd
  3. If all files are located separately, in Start.cmd fill in the paths to them or URL links to download them and run with a double click
  4. Either run Parser.ps1 directly through Powershell and pass it the path or a link to the template as an argument: \.Parser.ps1 -templatePath"D:\path к\template.txt " you can also use the second argument to pass the path to the patch script -patcherPath "C:\path to\ReplaceHexBytesAll.ps1" and it will take precedence over those specified in the template

Where to start

  1. Start by manually performing the actions.
    • This tool automates what is usually done manually - searching and replacing bytes in the hex editor, changing the hosts file, adding or removing rules in the firewall, etc. If you can't do it manually, then not using automated tools is probably a bad idea.
  2. Read the documentation
  3. Practice using only the main script ReplaceHexBytesAll.ps1 on some binary file
  4. Decide what you need to do/automate - just byte replacement or something else
  5. Correct/rewrite template for your tasks and test the execution of your template

Documentation

In a separate file

ToDo

System requirements

All the code was written and tested on Windows 10 x64 22H2.

I have not checked the compatibility of the code and the Powershell functions used with previous versions. You will probably need Powershell 5.1, which comes bundled with Windows 10, to perform them.

If you are running on Windows 7, 8, 8.1, then you will probably need to install Microsoft.NET Framework 4.8 and Powershell 5.1 to make the code from this repository work for you.