File size: 20,620 Bytes
543c8e8 | 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | #!/usr/bin/env python3
"""Reproduce static converter evidence and bounded Mono oracle probes (E02)."""
import argparse
import hashlib
import json
from pathlib import Path
import platform
import re
import shutil
import struct
import subprocess
from inspect_mb3dat import inspect
ROOT = Path(__file__).resolve().parents[1]
OUT = ROOT / 'assets/analysis/converter'
BINARIES = {
'2015-10-03': '66f1e3d9d53acfbe12a2be0e2a2bde64509d61d451ba7773bda68cff9d54abe5',
'2016-03-10-c': '0789c98b4150f281f92181a3368c86f5a81e033b7ecf58b8d320c5365546b5a6',
}
def sha(data):
return hashlib.sha256(data).hexdigest()
def binary(build):
p = ROOT / 'assets/references/extracted' / build / 'tools/Mb3 file formats.exe'
if sha(p.read_bytes()) != BINARIES[build]:
raise ValueError(f'wrong converter identity: {p}')
return p
def pe_info(data):
pe = struct.unpack_from('<I', data, 60)[0]
machine, sections = struct.unpack_from('<HH', data, pe + 4)
opt = pe + 24
opt_size = struct.unpack_from('<H', data, pe + 20)[0]
clr = struct.unpack_from('<I', data, opt + 96 + 14 * 8)[0]
section_table = []
for i in range(sections):
vsize, va, size, offset = struct.unpack_from('<4I', data, opt + opt_size + 40 * i + 8)
section_table.append((va, max(vsize, size), offset))
def file_offset(rva):
for va, size, offset in section_table:
if va <= rva < va + size:
return offset + rva - va
raise ValueError(f'unmapped RVA {rva:#x}')
def cstring(offset):
return data[offset:data.index(b'\0', offset)].decode('ascii')
imports = []
imp_rva = struct.unpack_from('<I', data, opt + 96 + 8)[0]
if imp_rva:
cursor = file_offset(imp_rva)
while any(data[cursor:cursor + 20]):
lookup, _, _, dll, thunk = struct.unpack_from('<5I', data, cursor)
entries = []
slot = file_offset(lookup or thunk)
while True:
entry = struct.unpack_from('<I', data, slot)[0]
if not entry:
break
entries.append({'ordinal': entry & 0xffff} if entry & 0x80000000 else cstring(file_offset(entry) + 2))
slot += 4
imports.append({'dll': cstring(file_offset(dll)), 'symbols': entries})
cursor += 20
for i in range(sections):
vsize, va, size, offset = struct.unpack_from('<4I', data, opt + opt_size + 40 * i + 8)
if va <= clr < va + max(vsize, size):
c = offset + clr - va
return {'machine': hex(machine), 'clr_rva': hex(clr), 'clr_offset': hex(c),
'corflags': hex(struct.unpack_from('<I', data, c + 16)[0]),
'entrypoint_token': hex(struct.unpack_from('<I', data, c + 20)[0]), 'pe_imports': imports}
raise ValueError('CLR header missing')
def methods(il):
result = []
for match in re.finditer(r'(?ms)^\s*\.method .*?// end of method ([^\n]+)', il):
part = match.group(0)
rva = re.search(r'Method begins at RVA (0x[0-9a-f]+)', part)
size = re.search(r'Code size: (\d+)', part)
result.append({'name': match.group(1).strip(), 'rva': rva.group(1) if rva else None,
'il_bytes': int(size.group(1)) if size else None,
'il_line': il[:match.start()].count('\n') + 1})
return result
def decompile():
version = subprocess.check_output(['ilspycmd', '--version'], text=True).strip()
manifest = {'tool': version, 'mono': subprocess.check_output(['mono', '--version'], text=True), 'binaries': {}}
for build in BINARIES:
p = binary(build)
dest = OUT / build
dest.mkdir(parents=True, exist_ok=True)
for name, options in [('decompiled.cs', []), ('assembly.il', ['-il'])]:
with (dest / name).open('wb') as stream:
subprocess.run(['ilspycmd', '--disable-updatecheck', *options, str(p)], stdout=stream, check=True, timeout=120)
for table in ['assembly', 'assemblyref', 'manifest', 'implmap', 'method']:
with (dest / (table + '.txt')).open('wb') as stream:
subprocess.run(['monodis', '--' + table, str(p)], stdout=stream, check=True, timeout=20)
il = (dest / 'assembly.il').read_text()
info = {'path': str(p.relative_to(ROOT)), 'sha256': sha(p.read_bytes()),
'size': p.stat().st_size, **pe_info(p.read_bytes()), 'methods': methods(il),
'outputs': {p.name: sha(p.read_bytes()) for p in sorted(dest.iterdir()) if p.is_file()}}
manifest['binaries'][build] = info
(OUT / 'static.json').write_text(json.dumps(manifest, indent=2) + '\n')
print('decompiled and indexed both distinct converter binaries')
def i32(n):
return struct.pack('<i', n)
def vecs(values):
return b''.join(struct.pack('<fff', *v) for v in values)
TRIPLE = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
PLANE = 'plane(1 2 3, 4 5 6, 7 8 9)'
ANCHORS = [(u, v, u + v) for v in range(3) for u in range(3)]
def vectors_text(values):
return ', '.join(' '.join(map(str, v)) for v in values)
def dat(version, brush_records=b'', brush_count=0, patch_records=b'', patch_count=0, computed=False, tables=b'\0\0', widths=0):
head = b'Mb3World' + i32(version) + bytes([int(computed) | widths])
if version == 201501280:
return head + i32(brush_count) + i32(patch_count) + brush_records + patch_records
return head + tables + i32(brush_count) + brush_records + i32(patch_count) + patch_records
def cases(version):
result = []
def add(name, src, content, dst='mb3txt', code=0, **extra):
result.append(dict(name=name, src=src, data=content.encode('ascii') if isinstance(content, str) else content,
dst=dst, expected_exit=code, **extra))
for computed in [False, True]:
add('empty-' + str(computed).lower(), 'mb3txt', f'world(computedFeatures {str(computed).lower()})', 'mb3dat')
text = 'world(computedFeatures false brush(' + PLANE + '))'
add('plane-to-dat', 'mb3txt', text, 'mb3dat')
add('plane-txt', 'mb3txt', text)
for fmt in ['hl1map', 'hl2vmf', 'q3map']:
add('plane-to-' + fmt, 'mb3txt', text, fmt)
add('obj-needs-computed', 'mb3txt', text, 'wfobj', 2)
poly = [(0, 0, 0), (1, 0, 0), (0, 1, 0)]
computed_plane = 'world(computedFeatures true brush(plane(1 2 3, 4 5 6, 7 8 9 polygon[' + vectors_text(poly) + '])))'
add('computed-plane', 'mb3txt', computed_plane, 'mb3dat')
add('computed-plane-obj', 'mb3txt', computed_plane, 'wfobj')
for computed in [False, True]:
patch = 'patch9(3 3 2 2 anchors[' + vectors_text(ANCHORS) + ']'
if computed:
patch += ' points[' + vectors_text(ANCHORS) + '] normals[' + vectors_text([(0, 1, 0)] * 9) + ']'
patch += ')'
txt = f'world(computedFeatures {str(computed).lower()} {patch})'
add('patch-' + str(computed).lower(), 'mb3txt', txt, 'mb3dat')
if computed:
add('patch-obj', 'mb3txt', txt, 'wfobj')
else:
add('patch-q3', 'mb3txt', txt, 'q3map')
add('patch-hl1-dropped', 'mb3txt', txt, 'hl1map')
base = dat(version)
add('dat-empty', 'mb3dat', base, 'mb3dat')
add('dat-trailing', 'mb3dat', base + b'EXTRA', 'mb3dat')
add('wrong-version', 'mb3dat', base[:8] + i32(0) + base[12:], code=2)
other = 201512200 if version == 201501280 else 201501280
add('cross-version', 'mb3dat', dat(other), code=2)
# A partial final scalar exercises the unchecked reused read buffer without an EOF string loop.
single = dat(version, b'\0\1' + vecs(TRIPLE), 1)
add('dat-plane', 'mb3dat', single, 'mb3dat')
add('truncated-last-float', 'mb3dat', single[:-8] + single[-4:], 'mb3dat')
for n in [255, 256]:
width = 0 if n == 255 else 1
body = bytes([width]) + struct.pack('<B' if width == 0 else '<H', n) + vecs(TRIPLE) * n
add('planes-' + str(n), 'mb3dat', dat(version, body, 1), 'mb3dat')
# Legacy selector=1 is signed; count 32768 immediately fails before reading plane records.
if version == 201501280:
add('signed-16-count', 'mb3dat', dat(version, b'\1\0\x80', 1), code=2)
else:
add('reserved-width-3', 'mb3dat', dat(version, tables=i32(0) + i32(0), widths=6 | 24), 'mb3dat')
add('reserved-world-bits', 'mb3dat', base[:12] + b'\xe0' + base[13:], 'mb3dat')
props = 'world(computedFeatures false stringProperties ["unused", "rare", "common"] uvProperties ["uv"] brush('
props += 'plane(1 2 3, 4 5 6, 7 8 9 s 1 "r" s 2 "c" uv 0 (1 2 3 4 5, 6 7 8 9 10)) '
props += 'plane(1 2 3, 4 5 6, 7 8 9 s 2 "")))'
add('properties-to-dat', 'mb3txt', props, 'mb3dat')
add('properties-to-txt', 'mb3txt', props)
for n in [255, 256]:
names = ','.join('"p' + str(i) + '"' for i in range(n))
slots = ' '.join('s ' + str(i) + ' "v"' for i in range(n))
txt = f'world(computedFeatures false stringProperties[{names}] brush(plane(1 2 3,4 5 6,7 8 9 {slots})))'
add('properties-' + str(n), 'mb3txt', txt, 'mb3dat')
for name, tail in [('duplicate-slot', 's 0 "a" s 0 "b"'), ('bad-slot', 's 1 "a"')]:
add(name, 'mb3txt', 'world(computedFeatures false stringProperties["p"] brush(plane(1 2 3,4 5 6,7 8 9 ' + tail + ')))', code=2)
add('duplicate-name', 'mb3txt', 'world(computedFeatures false stringProperties["p","p"])', code=2)
unicode_txt = 'world(computedFeatures false stringProperties["é"] brush(plane(1 2 3,4 5 6,7 8 9 s 0 "é")))'.encode('utf-8')
add('utf8-bom-ascii-loss', 'mb3txt', b'\xef\xbb\xbf' + unicode_txt, 'mb3dat')
add('utf8-no-bom-mojibake', 'mb3txt', unicode_txt)
# Duplicate binary slots are accepted with last value winning, unlike TXT.
body = b'\0\1\0first\0\0last\0\xff' + vecs(TRIPLE)
add('binary-duplicate-slot', 'mb3dat', dat(version, body, 1, tables=b'\1p\0\0'))
# Reused scalar buffer turns missing patch count into the previous float's bytes (zero).
body = b'\0\1' + vecs([(1, 2, 3), (4, 5, 6), (7, 8, 0)])
add('truncated-patch-count', 'mb3dat', dat(version, body, 1)[:-4], 'mb3dat')
for fmt, content in [
('hl1map', '{"classname" "func_detail" { (1 2 3) (4 5 6) (7 8 9) texture [1 0 0 0] [0 1 0 0] 0 1 1 }}'),
('hl2vmf', 'entity{"classname" "func_detail" solid{side{"plane" "(1 2 3) (4 5 6) (7 8 9)" "material" "secret"}}}'),
('q3map', '{"classname" "func_detail" {(1 2 3) (4 5 6) (7 8 9) texture 0 0 0 1 1 0 0 0}}')]:
add(fmt + '-import', fmt, content, 'mb3dat')
for fmt in ['hl1map', 'hl2vmf', 'q3map']:
add(fmt + '-roundtrip', fmt, b'', 'mb3dat', from_case='plane-to-' + fmt)
add('patch-dat-roundtrip', 'mb3dat', b'', 'mb3dat', from_case='patch-true')
add('patch-q3-roundtrip', 'q3map', b'', 'mb3dat', from_case='patch-q3')
add('inplace-dat', 'mb3dat', base, 'mb3dat', inplace=True)
add('overwrite-dat', 'mb3txt', text, 'mb3dat', preexisting=True)
add('computed-missing-polygon', 'mb3txt', text.replace('false', 'true'), code=2)
add('false-ignores-polygon', 'mb3txt', computed_plane.replace('true', 'false'), 'mb3dat')
body = b'\x04\x02' + vecs(TRIPLE) + struct.pack('<H', 256) + vecs([(0, 0, 0)] * 256)
body += vecs(TRIPLE) + struct.pack('<H', 1) + vecs([(1, 1, 1)])
add('contour-width-shared', 'mb3dat', dat(version, body, 1, computed=True), 'mb3dat')
for n, selector in [(256, 1), (65536, 2)]:
# Zero anchors isolate the dimension width without allocating huge geometry.
body = bytes([selector << 2]) + b'\0\0' + struct.pack('<HH' if selector == 1 else '<ii', n, 2)
add('patch-quads-' + str(n), 'mb3dat', dat(version, patch_records=body, patch_count=1), 'mb3dat')
if version == 201512200:
add('properties-dat-roundtrip', 'mb3dat', b'', 'mb3dat', from_case='properties-to-dat')
add('properties-dat-to-txt', 'mb3dat', b'', from_case='properties-to-dat')
add('utf8-txt-repeat', 'mb3txt', b'', from_case='utf8-no-bom-mojibake')
add('bad-mode', 'MB3TXT', text, code=2)
add('bad-syntax', 'mb3txt', 'world(', code=2)
add('usage', 'mb3txt', text, code=1, arguments=[])
add('missing-input', 'mb3txt', text, code=2, missing=True)
return result
def probes():
mono = shutil.which('mono')
if not mono:
raise ValueError('mono is required; enter nix develop')
report = {'environment': {'system': platform.system(), 'release': platform.release(), 'machine': platform.machine(),
'mono': subprocess.check_output([mono, '--version'], text=True),
'cwd': '.work/converter-oracle/<build>/<case>', 'timeout_seconds': 10}, 'builds': {}}
for build in BINARIES:
version = 201501280 if build.startswith('2015') else 201512200
source = binary(build)
records = []
for case in cases(version):
work = ROOT / '.work/converter-oracle' / build / case['name']
dest = OUT / 'probes' / build / case['name']
work.mkdir(parents=True, exist_ok=True)
dest.mkdir(parents=True, exist_ok=True)
shutil.copyfile(source, work / 'converter.exe')
inp, output = 'input.' + case['src'].lower(), 'output.' + case['dst']
payload = case['data']
if 'from_case' in case:
previous = OUT / 'probes' / build / case['from_case']
payload = next(previous.glob('output.*')).read_bytes()
(work / inp).write_bytes(payload)
if case.get('missing'):
(work / inp).unlink()
(work / 'conversion.log').write_bytes(b'STALE LOG\n')
if case.get('inplace'):
output = inp
elif (work / output).exists():
(work / output).unlink()
if case.get('preexisting'):
(work / output).write_bytes(b'PREEXISTING OUTPUT')
args = case.get('arguments', [case['src'], inp, case['dst'], output, 'conversion.log'])
proc = subprocess.run([mono, 'converter.exe', *args], cwd=work, capture_output=True, timeout=10)
record = {k: v for k, v in case.items() if k != 'data'}
record.update({'input_sha256': sha(payload), 'argv': ['mono', 'converter.exe', *args], 'binary_sha256': sha(source.read_bytes()),
'exit': proc.returncode, 'files': {}})
# Fixture payload retained even in missing-file experiments.
(dest / inp).write_bytes(payload)
for name, data in [('stdout.txt', proc.stdout), ('stderr.txt', proc.stderr)]:
(dest / name).write_bytes(data)
for name in [output, 'conversion.log']:
if (work / name).exists():
shutil.copyfile(work / name, dest / name)
elif (dest / name).exists():
(dest / name).unlink()
if case.get('inplace'):
(dest / 'before.mb3dat').write_bytes(payload)
if (work / output).exists() and case['dst'] == 'mb3dat':
try:
record['inspection'] = inspect((work / output).read_bytes())
except ValueError as exc:
record['inspection_error'] = str(exc)
for p in sorted(dest.iterdir()):
record['files'][p.name] = {'sha256': sha(p.read_bytes()), 'bytes': p.stat().st_size}
if proc.returncode != case['expected_exit']:
raise ValueError(f"{build}/{case['name']}: exit {proc.returncode}, expected {case['expected_exit']}; {proc.stderr!r}")
records.append(record)
report['builds'][build] = records
(OUT / 'probes.json').write_text(json.dumps(report, indent=2, allow_nan=False) + '\n')
print('captured', sum(map(len, report['builds'].values())), 'oracle probes')
def check():
manifest = json.loads((OUT / 'static.json').read_text())
for build, info in manifest['binaries'].items():
binary(build)
assert pe_info(binary(build).read_bytes())['pe_imports'] == [{'dll': 'mscoree.dll', 'symbols': ['_CorExeMain']}]
for name, digest in info['outputs'].items():
assert sha((OUT / build / name).read_bytes()) == digest, (build, name)
report = json.loads((OUT / 'probes.json').read_text())
for build, records in report['builds'].items():
expected_cases = cases(201501280 if build.startswith('2015') else 201512200)
assert [r['name'] for r in records] == [c['name'] for c in expected_cases]
for case, record in zip(expected_cases, records):
payload = case['data']
if 'from_case' in case:
previous = OUT / 'probes' / build / case['from_case']
payload = next(previous.glob('output.*')).read_bytes()
assert record['input_sha256'] == sha(payload), record['name']
assert record['binary_sha256'] == BINARIES[build]
assert record['exit'] == record['expected_exit'], record['name']
if 'inspection' in record:
output_name = record['argv'][5]
payload_file = OUT / 'probes' / build / record['name'] / output_name
assert inspect(payload_file.read_bytes()) == record['inspection'], record['name']
for name, info in record['files'].items():
p = OUT / 'probes' / build / record['name'] / name
assert sha(p.read_bytes()) == info['sha256'] and p.stat().st_size == info['bytes'], p
by = {r['name']: r for r in records}
version = 201501280 if build.startswith('2015') else 201512200
dest = OUT / 'probes' / build
assert (dest / 'empty-false/output.mb3dat').read_bytes() == dat(version)
assert (dest / 'plane-to-dat/output.mb3dat').read_bytes() == dat(version, b'\0\1' + vecs(TRIPLE), 1)
assert (dest / 'dat-plane/output.mb3dat').read_bytes() == (dest / 'plane-to-dat/output.mb3dat').read_bytes()
for name in ['hl1map-roundtrip', 'hl2vmf-roundtrip', 'q3map-roundtrip', 'overwrite-dat']:
assert by[name]['inspection'] == by['plane-to-dat']['inspection']
assert by['patch-dat-roundtrip']['inspection'] == by['patch-true']['inspection']
assert by['patch-q3-roundtrip']['inspection']['patches'][0]['quads_u'] == 4
for record in records:
log = (OUT / 'probes' / build / record['name'] / 'conversion.log').read_bytes()
if record['exit'] == 0 or record['name'] in ['usage', 'bad-mode']:
assert log == b'STALE LOG\n'
else:
assert log.startswith(b'An exception of type ')
assert by['dat-trailing']['inspection']['trailing_bytes'] == 0
assert by['dat-plane']['inspection']['brushes'][0]['planes'][0]['u'] == [4, 5, 6]
for n in [255, 256]:
assert len(by['planes-' + str(n)]['inspection']['brushes'][0]['planes']) == n
if build.startswith('2016'):
d = by['properties-to-dat']['inspection']
assert d['string_properties'] == ['common', 'rare']
assert d['brushes'][0]['planes'][0]['uv']['0'] == [1, 2, 3, 6, 7, 8, 4, 5, 9, 10]
for n in [255, 256]:
assert by['properties-' + str(n)]['inspection']['string_properties'] == ['p' + str(i) for i in range(n)]
assert by['utf8-bom-ascii-loss']['inspection']['string_properties'] == ['?']
assert by['reserved-width-3']['inspection']['flags'] == 0
assert by['truncated-patch-count']['exit'] == 0
assert by['properties-dat-roundtrip']['inspection'] == by['properties-to-dat']['inspection']
dest = OUT / 'probes' / build
assert (dest / 'properties-to-txt/output.mb3txt').read_bytes() == (dest / 'properties-dat-to-txt/output.mb3txt').read_bytes()
assert (dest / 'utf8-txt-repeat/output.mb3txt').read_bytes() != (dest / 'utf8-no-bom-mojibake/output.mb3txt').read_bytes()
print('converter evidence identities, captured exits, and semantic assertions passed')
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument('action', choices=['decompile', 'probes', 'check'])
args = p.parse_args()
try:
{'decompile': decompile, 'probes': probes, 'check': check}[args.action]()
except (ValueError, OSError, subprocess.SubprocessError, AssertionError) as exc:
p.exit(1, f'{exc}\n')
if __name__ == '__main__':
main()
|