Skip to content

Instantly share code, notes, and snippets.

@DrunkenAlcoholic
Last active December 19, 2024 14:48
Show Gist options
  • Save DrunkenAlcoholic/1f2abb927572d86d444645279b973b12 to your computer and use it in GitHub Desktop.
Save DrunkenAlcoholic/1f2abb927572d86d444645279b973b12 to your computer and use it in GitHub Desktop.
Bash Wildcard S&R patcher
#!/bin/bash
#############################
# Colors
#############################
red=$'\e[1;31m'
grnn=$'\e[1;32m'
yel=$'\e[1;33m'
blu=$'\e[1;34m'
cyn=$'\e[1;36m'
end=$'\e[0m'
#############################
# File Exist?
#############################
if [[ $# -lt 1 ]]; then
echo "${red}Usage: $0 <binary_file>${end}"
exit 1
fi
sFile="$1"
if [[ ! -f "$sFile" ]]; then
echo "${red}File not found: $sFile${end}"
exit 1
fi
#############################
# Patterns and Patches Arrays
#############################
declare -a patterns=(
"\x55\x41\x57\x41\x56\x41\x55\x41\x54\x53\x48\x81\xEC....\x4C\x89\x8C\x24....\x4C\x89\xC3\x48\x89\x8C\x24"
"\xE8....\x48\x8B\xB3....\x48\x8D\x3D....\xBA....\xE8....\x48\x89\xDF"
"\xE8....\x48\x89\xDF\xE8....\xBF....\xE8....\x49\x89\xC7"
"\xFF\x8B\x77\x20"
"\x41\x57\x41\x56\x41\x54\x53\x48\x81\xEC....\x48\x89\xFB\x48\x8D\x3D"
)
declare -a patches=(
"\x48\xC7\xC0\x00\x00\x00\x00\xC3"
"\x90\x90\x90\x90\x90"
"\x90\x90\x90\x90\x90"
"\xFF\xC3"
"\xC3"
)
# Empty array to hold the found Offsets
declare -a offsets=()
#############################
# Search for Patterns
#############################
for i in "${!patterns[@]}"; do
offset=$(LC_ALL=C grep -obUaP "${patterns[i]}" "$sFile" | cut -f1 -d ':')
if [[ -z "$offset" ]]; then
offsets+=("N/A")
else
hex_offset=$(printf "%x" "$offset")
offsets+=("$hex_offset")
fi
done
#############################
# Display Results
#############################
echo "${blu}----------------------------${end}"
echo "${cyn} Offsets & Patches ${end}"
echo "${blu}----------------------------${end}"
for i in "${!offsets[@]}"; do
if [[ "${offsets[i]}" == "N/A" ]]; then
printf "${red}Pattern %d not found${end}\n" "$i"
else
printf "${yel}Offset:${end} ${grnn}0x%s${end} ${cyn}Patch:${end} ${grnn}%s${end}\n" \
"${offsets[i]}" "${patches[i]}"
fi
done
echo "${blu}----------------------------${end}"
#############################
# Write Patches to File
#############################
read -p "Do you want to write patches? (y/n): " -n 1 -r
echo # Leave a line
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "${cyn}Checking permissions...${end}"
if [[ "$EUID" -ne 0 ]]; then
echo "${red}Please run as root to write patches.${end}"
exit 1
fi
#############################
# Backup File
#############################
backupFile="${sFile}.backup"
cp "$sFile" "$backupFile"
if [[ $? -ne 0 ]]; then
echo "${red}Failed to create backup file: $backupFile${end}"
exit 1
fi
echo "${grnn}Backup created: $backupFile${end}"
echo "${grnn}Patching file: $sFile${end}"
for i in "${!offsets[@]}"; do
if [[ "${offsets[i]}" != "N/A" ]]; then
patch_offset=$((16#${offsets[i]}))
printf "%s" "${patches[i]}" | dd of="$sFile" bs=1 seek="$patch_offset" conv=notrunc status=none
echo "${grnn}Patched offset 0x${offsets[i]} with ${patches[i]}${end}"
else
echo "${red}Skipping pattern $i: Not found${end}"
fi
done
echo "${grnn}Patching complete.${end}"
else
echo "${yel}Patching aborted.${end}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment