#!/usr/bin/env python3 """Apply security guards to the bundled sondibd binary. The repository ships the service as a release binary. The patch removes implicit admin access from fresh connections, fixes session resume ownership, skips deleted records, and filters private/deleted search results. """ from __future__ import annotations import sys from pathlib import Path TEXT_VA_BASE = 0x400000 GUARD_VA = 0x404120 ENTRY_VA = 0x40410A MATCH_VA = 0x404154 SEARCH_HELPER_VA = 0x40410C SEARCH_CALL_VA = 0x402ACA RESUME_VA = 0x403467 CONN_INIT_VA = 0x402045 UNAUTH_CAVE_VA = 0x40412D GUARD_SIZE = 0x20 OLD_GUARD = bytes.fromhex( "66 66 2e 0f 1f 84 00 00 00 00 00 " "66 66 2e 0f 1f 84 00 00 00 00 00 " "66 2e 0f 1f 84 00 00 00 00 00" ) OLD_BRANCH = bytes.fromhex("74 0e") ENTRY_JUMP = bytes.fromhex("eb 34") OLD_ENTRY = OLD_GUARD[:len(ENTRY_JUMP)] OLD_SEARCH_HELPER = OLD_GUARD[len(ENTRY_JUMP):len(ENTRY_JUMP) + 16] SEARCH_HELPER = bytes.fromhex( "66 83 be 1c 02 00 00 00 " # cmp word ptr [rsi+0x21c], 0 "75 05 " # jne ret "e9 b5 e7 ff ff " # jmp send_row "c3" # ret ) OLD_SEARCH_CALL = bytes.fromhex("e8 01 fe ff ff") FILTERED_SEARCH_CALL = bytes.fromhex("e8 3d 16 00 00") OLD_RESUME = bytes.fromhex( "74 16 48 89 e9 48 89 c5 3b 10 75 ed " "48 89 cd 48 85 c9 0f 84 19 02 00 00" ) FIXED_RESUME = bytes.fromhex( "74 0c 48 89 e9 48 89 c5 3b 10 74 0c " "eb eb e9 1e 02 00 00 90 90 90 90 90" ) OLD_CONN_INIT = bytes.fromhex("89 57 04 89 57 20") FIXED_CONN_INIT = bytes.fromhex("e8 e3 20 00 00 90") OLD_UNAUTH_CAVE = b"\x90" * 15 FIXED_UNAUTH_CAVE = bytes.fromhex( "c7 47 04 ff ff ff ff " # conn.user = -1 "c7 47 20 00 00 00 00 " # conn parser state = 0 "c3" ) def file_offset(virtual_address: int) -> int: return virtual_address - TEXT_VA_BASE def patch(path: Path) -> None: data = bytearray(path.read_bytes()) guard_offset = file_offset(GUARD_VA) match_offset = file_offset(MATCH_VA) entry_offset = file_offset(ENTRY_VA) search_helper_offset = file_offset(SEARCH_HELPER_VA) search_call_offset = file_offset(SEARCH_CALL_VA) resume_offset = file_offset(RESUME_VA) conn_init_offset = file_offset(CONN_INIT_VA) unauth_cave_offset = file_offset(UNAUTH_CAVE_VA) guard = bytes(data[guard_offset:guard_offset + GUARD_SIZE]) branch = bytes(data[match_offset:match_offset + len(OLD_BRANCH)]) entry = bytes(data[entry_offset:entry_offset + len(ENTRY_JUMP)]) search_helper = bytes(data[search_helper_offset:search_helper_offset + len(SEARCH_HELPER)]) search_call = bytes(data[search_call_offset:search_call_offset + len(OLD_SEARCH_CALL)]) resume = bytes(data[resume_offset:resume_offset + len(OLD_RESUME)]) conn_init = bytes(data[conn_init_offset:conn_init_offset + len(OLD_CONN_INIT)]) unauth_cave = bytes(data[unauth_cave_offset:unauth_cave_offset + len(OLD_UNAUTH_CAVE)]) if branch == bytes.fromhex("74 ca"): changed = False if entry == OLD_ENTRY: data[entry_offset:entry_offset + len(ENTRY_JUMP)] = ENTRY_JUMP changed = True elif entry != ENTRY_JUMP: raise SystemExit(f"unexpected sondibd entry bytes in {path}") # Upgrade the first revision: returning NULL immediately made callers # that probe an old SID take a slow error path. if data[guard_offset + 7:guard_offset + 9] == bytes.fromhex("75 39"): data[guard_offset + 7:guard_offset + 9] = bytes.fromhex("75 2d") changed = True if search_helper == OLD_SEARCH_HELPER and search_call == OLD_SEARCH_CALL: data[search_helper_offset:search_helper_offset + len(SEARCH_HELPER)] = SEARCH_HELPER data[search_call_offset:search_call_offset + len(FILTERED_SEARCH_CALL)] = FILTERED_SEARCH_CALL changed = True elif search_helper != SEARCH_HELPER or search_call != FILTERED_SEARCH_CALL: raise SystemExit(f"unexpected sondibd search-filter bytes in {path}") if resume == OLD_RESUME: data[resume_offset:resume_offset + len(FIXED_RESUME)] = FIXED_RESUME changed = True elif resume != FIXED_RESUME: raise SystemExit(f"unexpected sondibd resume bytes in {path}") if conn_init == OLD_CONN_INIT and unauth_cave == OLD_UNAUTH_CAVE: data[conn_init_offset:conn_init_offset + len(FIXED_CONN_INIT)] = FIXED_CONN_INIT data[unauth_cave_offset:unauth_cave_offset + len(FIXED_UNAUTH_CAVE)] = FIXED_UNAUTH_CAVE changed = True elif conn_init != FIXED_CONN_INIT or unauth_cave != FIXED_UNAUTH_CAVE: raise SystemExit(f"unexpected sondibd connection-init bytes in {path}") if changed: path.write_bytes(data) return if branch != OLD_BRANCH or guard != OLD_GUARD or entry != OLD_ENTRY: raise SystemExit(f"unexpected sondibd bytes in {path}") if search_helper != OLD_SEARCH_HELPER or search_call != OLD_SEARCH_CALL: raise SystemExit(f"unexpected sondibd search-filter bytes in {path}") if resume != OLD_RESUME: raise SystemExit(f"unexpected sondibd resume bytes in {path}") if conn_init != OLD_CONN_INIT or unauth_cave != OLD_UNAUTH_CAVE: raise SystemExit(f"unexpected sondibd connection-init bytes in {path}") # je 0x404120; then skip track+0x21d when it is non-zero. guard_code = bytes.fromhex( "80 b9 1d 02 00 00 00 " # cmp byte ptr [rcx+0x21d], 0 "75 2d " # jne 0x404156 (continue the search) "48 89 c8 c3" # mov rax, rcx; ret ) data[entry_offset:entry_offset + len(ENTRY_JUMP)] = ENTRY_JUMP data[guard_offset:guard_offset + GUARD_SIZE] = guard_code.ljust(GUARD_SIZE, b"\x90") data[match_offset:match_offset + len(OLD_BRANCH)] = bytes.fromhex("74 ca") data[search_helper_offset:search_helper_offset + len(SEARCH_HELPER)] = SEARCH_HELPER data[search_call_offset:search_call_offset + len(FILTERED_SEARCH_CALL)] = FILTERED_SEARCH_CALL data[resume_offset:resume_offset + len(FIXED_RESUME)] = FIXED_RESUME data[conn_init_offset:conn_init_offset + len(FIXED_CONN_INIT)] = FIXED_CONN_INIT data[unauth_cave_offset:unauth_cave_offset + len(FIXED_UNAUTH_CAVE)] = FIXED_UNAUTH_CAVE path.write_bytes(data) if __name__ == "__main__": patch(Path(sys.argv[1]) if len(sys.argv) == 2 else Path(__file__).with_name("bin") / "sondibd")