File size: 2,332 Bytes
5463b45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""Disable the console UI in the checksum-pinned Linux 0.5.5 executable.

The official release retains ELF symbols. Replace only InGameConsole::Open
with an x86-64 return. Simulation, numeric inputs, plugin logging and the
shared engine library remain untouched. Refuse other binaries/architectures.
This task image is a restricted derivative, not the unmodified release.
"""
import hashlib
import json
from pathlib import Path
import struct
import sys

EXPECTED = "cf0df169bc7ff738381a3e54c8e5ecdcd6b623a4ac7f1260bc713d753d803677"
SYMBOL = "_ZN8OpenRCT22Ui13InGameConsole4OpenEv"


def restrict(path):
    data = bytearray(path.read_bytes())
    digest = hashlib.sha256(data).hexdigest()
    if digest != EXPECTED:
        raise ValueError("Console restriction requires the pinned official Linux executable")
    if data[:6] != b"\x7fELF\x02\x01" or struct.unpack_from("<H", data, 18)[0] != 62:
        raise ValueError("Expected little-endian ELF64 x86-64")
    offset = struct.unpack_from("<Q", data, 40)[0]
    size, count = struct.unpack_from("<HH", data, 58)
    sections = [struct.unpack_from("<IIQQQQIIQQ", data, offset + i * size) for i in range(count)]
    matches = []
    for section in sections:
        if section[1] != 2:  # SHT_SYMTAB
            continue
        strings_section = sections[section[6]]
        strings = data[strings_section[4]:strings_section[4] + strings_section[5]]
        for position in range(section[4], section[4] + section[5], section[9]):
            name, info, _, index, address, length = struct.unpack_from("<IBBHQQ", data, position)
            if strings[name:].split(b"\0")[0].decode() == SYMBOL:
                target = sections[index]
                assert info & 15 == 2 and target[2] & 4 and length == 346
                matches.append(target[4] + address - target[3])
    if len(matches) != 1:
        raise ValueError("Expected exactly one console entry point")
    position = matches[0]
    before = data[position]
    data[position] = 0xC3  # ret; no stack/register changes
    path.write_bytes(data)
    return {"original_sha256": digest, "restricted_sha256": hashlib.sha256(data).hexdigest(),
            "symbol": SYMBOL, "file_offset": position, "before": before, "after": 195}


if __name__ == "__main__":
    print(json.dumps(restrict(Path(sys.argv[1])), indent=2))