Skip to content

Instantly share code, notes, and snippets.

@blubberdiblub
Created March 6, 2025 04:12
Show Gist options
  • Save blubberdiblub/f76fd6dd6a4390c52db01a5b8be917e7 to your computer and use it in GitHub Desktop.
Save blubberdiblub/f76fd6dd6a4390c52db01a5b8be917e7 to your computer and use it in GitHub Desktop.
Patch `libbackend.so` of Turing Complete's bleeding edge build to use `xterm` rather than `gnome-terminal`. `argparse` version
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import hashlib
import sys
from pathlib import Path
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import Literal
LIBBACKEND_FILENAME = 'libbackend.so'
OLD = b'gnome-terminal -- bash -ic "./si'
NEW = b'setsid -f xterm -e bash -c "./si'
def hexdigest(data: bytes) -> str:
return hashlib.md5(data, usedforsecurity=False).hexdigest()
def patch_backend(file: Path, action: Literal['do', 'undo', 'view']) -> None:
orig_content = file.read_bytes()
count_old = orig_content.count(OLD)
count_new = orig_content.count(NEW)
old_content = orig_content.replace(NEW, OLD)
new_content = orig_content.replace(OLD, NEW)
if action == 'view':
print(f"Found {count_old} occurrences of the original byte sequence and {count_new} of the replacement.")
print(f" md5sum with original byte sequence: {hexdigest(old_content)}")
print(f" md5sum with current file content: {hexdigest(orig_content)}")
print(f" md5sum with replacement byte sequence: {hexdigest(new_content)}")
return
if count_old + count_new == 0:
raise ValueError("Found neither original nor replacement byte sequence in file.")
if count_old + count_new > 1:
raise ValueError("Found more than one occurrence of original or replacement byte sequence in file.")
byte_offset = orig_content.find(OLD) if count_old else orig_content.find(NEW)
if byte_offset < 1024 or (byte_offset & 0xF) != 0:
raise ValueError(f"Found byte sequence at suspicious offset {byte_offset} / {byte_offset:#x}.")
match action:
case 'do':
if count_old == 0:
print("Original byte sequence does not occur in file.", file=sys.stderr)
print(f" md5sum: {hexdigest(orig_content)}", file=sys.stderr)
else:
print(f" md5sum was: {hexdigest(orig_content)}", file=sys.stderr)
file.write_bytes(new_content)
print(f" md5sum now is: {hexdigest(new_content)}", file=sys.stderr)
case 'undo':
if count_new == 0:
print("Replacement byte sequence does not occur in file.", file=sys.stderr)
print(f" md5sum: {hexdigest(orig_content)}", file=sys.stderr)
else:
print(f" md5sum was: {hexdigest(orig_content)}", file=sys.stderr)
file.write_bytes(old_content)
print(f" md5sum now is: {hexdigest(old_content)}", file=sys.stderr)
case _:
raise NotImplementedError(f"unimplemented action {action!r}")
def cli() -> None:
"""Replace debug terminal app in Turing Complete's libbackend.so."""
parser = argparse.ArgumentParser(description=cli.__doc__)
parser.add_argument('file', nargs='?', type=str, help='alternative file to patch', default=None)
parser.add_argument('--do', dest='action', action='store_const', const='do', help='replace gnome-terminal with xterm (default action)', default='do')
parser.add_argument('--undo', '-u', dest='action', action='store_const', const='undo', help='go back to using gnome-terminal')
parser.add_argument('--view', '-n', dest='action', action='store_const', const='view', help='count the occurrences and view the checksums')
args = parser.parse_args()
try:
patch_backend(
file = Path(args.file) if args.file is not None else Path(sys.argv[0]).parent / LIBBACKEND_FILENAME,
action = args.action,
)
except Exception as e:
raise SystemExit(e)
if __name__ == '__main__':
cli()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment