Last active
July 29, 2024 08:19
-
-
Save R0rt1z2/a79a2272594e6286750a21f8e45fa918 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Original sequences to search for. | |
ORIGINAL_SEQUENCES=('0a2450292801080a087d0a1b28010035' '48010035822240b980420491e10313aa') | |
# Patch sequences to replace the original sequences with. | |
PATCH_SEQUENCES=('0a2450292801080a087d0a1b1f2003d5' '1f2003d5822240b980420491e10313aa') | |
function log() { | |
local colors=( "\e[34m" "\033[1;33m" "\033[1;31m" "\033[1;32m" ) | |
local levels=( "[INFO]" "[WARNING]" "[ERROR]" "[SUCCESS]" ) | |
echo -e "${colors[$1]}${levels[$1]}: $2\033[0m" | |
[[ $1 -eq 2 ]] && exit 1 # Bail out if we fail. | |
} | |
function run() { | |
local output | |
output=$(eval "$@" 2>&1) | |
if [[ $? -ne 0 ]]; then | |
log 2 "Failed to run '$@'!" | |
fi | |
} | |
function dump_image_info() { | |
local image="$1" | |
log 0 "Image name: $1" | |
log 0 "Image size: $(du -h "$image" | cut -f1)" | |
log 0 "Image type: $(file "$1" | cut -d ':' -f 2- | sed 's/^ *//')" | |
log 0 "Image hash: $(sha256sum "$1" | cut -d ' ' -f 1)" | |
log 0 "Image version: $(strings "$1" | grep -m 1 'Linux version' | grep -oP 'Linux version \K[^\s]+' | head -1)" | |
} | |
function patch_kernel() { | |
output_kernel="$1" | |
local patched=false | |
for i in "${!ORIGINAL_SEQUENCES[@]}"; do | |
if [[ $(xxd -p -c 1000 "$output_kernel" | grep -c "${ORIGINAL_SEQUENCES[$i]}") -gt 0 ]]; then | |
log 1 "Using patch sequence ${PATCH_SEQUENCES[$i]} to patch the kernel..." | |
xxd -p -c 1000 "$output_kernel" | sed "s/${ORIGINAL_SEQUENCES[$i]}/${PATCH_SEQUENCES[$i]}/g" | xxd -r -p > "$output_kernel".tmp | |
run "mv $output_kernel.tmp $output_kernel" | |
if [[ $(xxd -p -c 1000 "$output_kernel" | grep -c "${PATCH_SEQUENCES[$i]}") -gt 0 ]]; then | |
log 3 "Successfully patched the kernel!" | |
patched=true | |
fi | |
break | |
fi | |
done | |
if [[ $patched == false ]]; then | |
log 2 "Failed to patch the kernel!" | |
fi | |
} | |
function main() { | |
local original_kernel="$1" | |
local output_kernel="$2" | |
local compressed=false | |
if [[ $(head -c 2 "$original_kernel") == $'\x1f\x8b' ]]; then | |
compressed=true | |
log 1 "The original kernel is compressed. Uncompressing..." | |
run "gzip -d -c $original_kernel > $output_kernel" | |
else | |
log 1 "The original kernel is not compressed. Copying..." | |
run "cp $original_kernel $output_kernel" | |
fi | |
dump_image_info "$output_kernel" | |
log 0 "Patching the kernel..." | |
patch_kernel "$output_kernel" | |
if [[ $compressed == true ]]; then | |
log 1 "Compressing the patched kernel..." | |
run "gzip -f -9 -n $output_kernel" | |
output_kernel="$output_kernel.gz" | |
fi | |
log 3 "Done! The patched kernel is located at '$output_kernel'." | |
} | |
if [[ ! $(which gzip) ]]; then | |
log 2 "gzip is not installed. Please install it and try again." | |
fi | |
if [[ $# -lt 2 ]]; then | |
log 2 "Usage: $0 <path to original kernel> <path to output kernel>" | |
fi | |
if [[ $2 == *.* ]]; then | |
log 2 "The output kernel '$2' should not have an extension!" | |
fi | |
if [[ ! -f "$1" ]]; then | |
log 2 "The original kernel '$1' does not exist!" | |
fi | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment