Files
mikrotik-olsr/tools/patch_update_host.py
T
feedc0de 9217046b33
Check source / check (push) Successful in 12s
Build RouterOS package release / release (push) Successful in 5m14s
Publish and promote RouterOS project site / site (push) Successful in 8s
Add self-hosted RouterOS package feed and pinned updater redirect
2026-11-22 11:50:00 +01:00

59 lines
2.3 KiB
Python

#!/usr/bin/env python3
"""Redirect the verified 7.25.1 ARM/ARM64 RouterOS updater host.
The two references are ELF32 absolute literal words. The second string has
four trailing NUL bytes, enough for mikrotik.brunner.ninja without moving a
section. The first reference is redirected to that second string.
"""
import argparse
import hashlib
from pathlib import Path
import struct
LAYOUTS = {
"6fa7c71d0ff6014d3d65f3e696893bd87a4b3635b9a06d51de8619524aee9512":
(0x37ce9, 0xfed4, 0x4200c, 0x359c4),
"237ccca17a91d06a52b91d24b4cda60b2a5aca83a47fa1c38719b8b2bba47bea":
(0x384bf, 0x118a8, 0x4286c, 0x35b1c),
}
OLD = b"upgrade.mikrotik.com\0"
SLOT_SIZE = 24
def patch(data: bytes, host: str) -> bytes:
if host != "mikrotik.brunner.ninja":
raise ValueError("Only the verified project hostname is supported")
layout = LAYOUTS.get(hashlib.sha256(data).hexdigest())
if layout is None:
raise ValueError("Unsupported sys2 build; updater host was not changed")
first, first_ref, second, second_ref = layout
if (data[first:first + len(OLD)] != OLD or data[second:second + SLOT_SIZE] !=
OLD + b"\0" * (SLOT_SIZE - len(OLD))):
raise ValueError("Unexpected updater string layout")
if (data[first_ref:first_ref + 4] != struct.pack("<I", first + 0x10000) or
data[second_ref:second_ref + 4] != struct.pack("<I", second + 0x10000)):
raise ValueError("Unexpected updater reference layout")
result = bytearray(data)
result[first:first + len(OLD)] = b"\0" * len(OLD)
result[first_ref:first_ref + 4] = struct.pack("<I", second + 0x10000)
replacement = host.encode("ascii") + b"\0"
result[second:second + SLOT_SIZE] = replacement.ljust(SLOT_SIZE, b"\0")
if OLD in result or result.count(replacement) != 1:
raise ValueError("Updater host replacement did not verify")
return bytes(result)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("source", type=Path)
parser.add_argument("output", type=Path)
parser.add_argument("--host", default="mikrotik.brunner.ninja")
args = parser.parse_args()
args.output.write_bytes(patch(args.source.read_bytes(), args.host))
print(f"Patched updater host in {args.output}")
if __name__ == "__main__":
main()