https://github.com/bradsec/hexreplace
A shell script which uses Perl to replace all instances of a specific hexadecimal string within a binary file
https://github.com/bradsec/hexreplace
Last synced: 9 months ago
JSON representation
A shell script which uses Perl to replace all instances of a specific hexadecimal string within a binary file
- Host: GitHub
- URL: https://github.com/bradsec/hexreplace
- Owner: bradsec
- License: mit
- Created: 2023-07-16T02:24:06.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-07-16T02:36:21.000Z (almost 3 years ago)
- Last Synced: 2025-01-07T17:24:39.808Z (over 1 year ago)
- Language: Shell
- Size: 2.93 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# HEX String Replacement Script
This script replaces **all instances** of a specific hexadecimal string within a binary file. If more than one match of the `search_hex` is found in the input file, each match will be replaced with the `replace_hex` string. The script first checks if the `search_hex` exists in the file. If it does, it proceeds to use Perl to perform the search and replace operation on the binary file. This script can handle hex string values that span across multiple lines. The script creates a backup of the original file by appending the extension ".bak".
## Usage
*`sudo` may be required in front of command depending on permissions of the file being modified.*
```terminal
# clone repo
git clone https://github.com/bradsec/hexreplace.git
# make executable
cd hexreplace
chmod +x hexreplace.sh
```
```terminal
# run with command-line arguments
./hexreplace.sh
# Example
sudo ./hexreplace.sh "/path/thisfile" "C0 0F 00 00 01 01" "C1 FF 00 01 02 02"
```
## Parameters
- `input_file`: Path to the input file.
- `search_hex`: Hex string to search for.
- `replace_hex`: Hex string to replace with.
## Prerequisites
- This script requires Perl to be installed on the system. It is commonly installed by default on many Linux distros and most macOS versions. Perl was used over sed, grep combined with hexdump or xxd to more simply handle the string replacement. It also worked more reliabably with strings which span multiple lines.