Skip to content

Instantly share code, notes, and snippets.

@DrunkenAlcoholic
Created October 12, 2024 00:34
Show Gist options
  • Save DrunkenAlcoholic/4e63e0c8ac41312bd829e033e8941ce7 to your computer and use it in GitHub Desktop.
Save DrunkenAlcoholic/4e63e0c8ac41312bd829e033e8941ce7 to your computer and use it in GitHub Desktop.
Binary Write Patches
import os
type
Patch = tuple
address: int64
data: seq[byte]
let FilePath: string = "/opt/sublime_text/sublime_text"
let Patches: seq[Patch] = @[(address: 0x003F75D9, data: @[0x48, 0xC7, 0xC0, 0x00, 0x00, 0x00, 0x00, 0xC3]),
(address: 0x003E0E7A, data: @[0x90, 0x90, 0x90, 0x90, 0x90]),
(address: 0x003E0E92, data: @[0x90, 0x90, 0x90, 0x90, 0x90]),
(address: 0x003E46EA, data: @[0x90, 0x90, 0x90]),
(address: 0x003F733A, data: @[0xC3])]
proc applyPatches(filename: string, patches: seq[Patch]) =
var file: File
var backupFilename = filename & ".backup"
# Create a backup of the original file
try:
copyFile(filename, backupFilename)
except IOError as e:
echo "Failed to create backup:", e.msg
# Apply patches
try:
if not open(file, filename, fmReadWriteExisting):
raise newException(IOError, "Unable to open file: " & filename)
defer: close(file)
# Iterate over patches
for patch in patches:
setFilePos(file, patch.address)
if file.getFilePos() != patch.address:
raise newException(IOError, "Unable to set file position to " & $patch.address)
let bytesWritten = file.writeBytes(patch.data, 0, patch.data.len)
if bytesWritten != patch.data.len:
raise newException(IOError, "Failed to write all bytes at address " & $patch.address)
except IOError as e:
echo "Write Error: ", e.msg
when isMainModule:
try:
applyPatches(FilePath, Patches)
echo "Successfully wrote all the patches"
except IOError as e:
echo "Write Error: ", e.msg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment