140 lines
6.3 KiB
Python
140 lines
6.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Actual transaction C + exact retirement function, mocked Win32 only.
|
|
|
|
No process attachment, executable patching, credentials or client. --baseline-ref
|
|
replays the old retirement body against the same diagnostic expectation, showing
|
|
missing evidence rather than claiming a change to transaction refusal behavior.
|
|
"""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
WINDOWS = r'''
|
|
#pragma once
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
typedef uint32_t DWORD; typedef int32_t LONG; typedef size_t SIZE_T;
|
|
typedef int BOOL; typedef void *HANDLE; typedef void *HMODULE;
|
|
typedef int SRWLOCK;
|
|
#define SRWLOCK_INIT 0
|
|
#define FALSE 0
|
|
#define MEM_COMMIT 0x1000
|
|
#define PAGE_NOACCESS 1
|
|
#define PAGE_GUARD 0x100
|
|
#define PAGE_EXECUTE_READ 0x20
|
|
#define PAGE_EXECUTE_READWRITE 0x40
|
|
#define THREAD_SUSPEND_RESUME 2
|
|
#define THREAD_GET_CONTEXT 8
|
|
#define THREAD_QUERY_INFORMATION 0x40
|
|
#define CONTEXT_CONTROL 1
|
|
#define STILL_ACTIVE 259
|
|
#define ERROR_NO_MORE_FILES 18
|
|
#define INVALID_HANDLE_VALUE ((HANDLE)(intptr_t)-1)
|
|
#define INFINITE 0xffffffffu
|
|
#define MemoryBarrier() __sync_synchronize()
|
|
typedef struct { DWORD dwPageSize; } SYSTEM_INFO;
|
|
typedef struct { DWORD State, Protect; } MEMORY_BASIC_INFORMATION;
|
|
typedef struct { DWORD ContextFlags; uintptr_t Rip; } CONTEXT;
|
|
static inline LONG InterlockedExchange(volatile LONG *p, LONG v) { return __atomic_exchange_n(p,v,__ATOMIC_SEQ_CST); }
|
|
void AcquireSRWLockExclusive(SRWLOCK*); void ReleaseSRWLockExclusive(SRWLOCK*);
|
|
void GetSystemInfo(SYSTEM_INFO*); SIZE_T VirtualQuery(const void*,MEMORY_BASIC_INFORMATION*,SIZE_T);
|
|
DWORD GetLastError(void); DWORD GetCurrentProcessId(void); DWORD GetCurrentThreadId(void);
|
|
HANDLE GetCurrentProcess(void); HANDLE OpenThread(DWORD,BOOL,DWORD); BOOL ReadProcessMemory(HANDLE,const void*,void*,SIZE_T,SIZE_T*);
|
|
DWORD SuspendThread(HANDLE); DWORD ResumeThread(HANDLE); BOOL GetThreadContext(HANDLE,CONTEXT*);
|
|
BOOL GetExitCodeThread(HANDLE,DWORD*); BOOL CloseHandle(HANDLE);
|
|
BOOL VirtualProtect(void*,SIZE_T,DWORD,DWORD*); BOOL FlushInstructionCache(HANDLE,const void*,SIZE_T);
|
|
BOOL TerminateProcess(HANDLE,unsigned); void Sleep(DWORD);
|
|
'''
|
|
TOOLHELP = r'''
|
|
#pragma once
|
|
#include <windows.h>
|
|
#define TH32CS_SNAPTHREAD 4
|
|
typedef struct { DWORD dwSize,th32OwnerProcessID,th32ThreadID; } THREADENTRY32;
|
|
HANDLE CreateToolhelp32Snapshot(DWORD,DWORD);
|
|
BOOL Thread32First(HANDLE,THREADENTRY32*); BOOL Thread32Next(HANDLE,THREADENTRY32*);
|
|
'''
|
|
|
|
|
|
def function(text, signature):
|
|
start = text.index(signature)
|
|
brace = text.index('{', start)
|
|
depth = 1
|
|
end = brace + 1
|
|
# These specific retained functions contain no brace-bearing literals.
|
|
while depth:
|
|
depth += (text[end] == '{') - (text[end] == '}')
|
|
end += 1
|
|
return text[start:end]
|
|
|
|
|
|
def structure(text, name):
|
|
start = text.index('struct ' + name + ' {')
|
|
return text[start:text.index('};', start) + 2]
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--runtime-root', type=Path, default=ROOT)
|
|
parser.add_argument('--baseline-ref')
|
|
parser.add_argument('--output', type=Path)
|
|
args = parser.parse_args()
|
|
root = args.runtime_root.resolve()
|
|
nohelper = root / 'native/nohelper'
|
|
source = (nohelper / 'direct_two_site_install.c').read_text()
|
|
if args.baseline_ref:
|
|
source = subprocess.check_output(['git', '-C', str(root), 'show',
|
|
args.baseline_ref + ':native/nohelper/direct_two_site_install.c'], text=True)
|
|
header = (nohelper / 'direct_two_site_install.h').read_text()
|
|
# Expected API is the six numeric fields specified by Q92, also usable to
|
|
# demonstrate that the pre-change function leaves that evidence untouched.
|
|
diagnostic = ('struct GwDirectRetireDiagnostic { uint32_t transaction_result, '
|
|
'reason, index, winerror, state, protect; };')
|
|
if 'struct GwDirectRetireDiagnostic {' in header:
|
|
diagnostic = structure(header, 'GwDirectRetireDiagnostic')
|
|
unit = '\n'.join([structure((nohelper / 'direct_near_relays.h').read_text(), 'GwDirectRelays'),
|
|
structure(header, 'GwDirectInstallation'), diagnostic,
|
|
'struct GwDirectRetireDiagnostic gw_direct_retire_diagnostic;',
|
|
'static volatile LONG login_retired;',
|
|
'int gw_direct_login_site_retired(void) { return login_retired == 1; }',
|
|
'uint32_t gw_direct_login_published_generation(void) { return 9; }',
|
|
'static SIZE_T last_exact_read_got;', 'static DWORD last_exact_read_error;',
|
|
function(source, 'static int exact_read('),
|
|
function(source, 'static void reverse_plan('),
|
|
function(source, 'int gw_direct_retire_login_site_after_capture(')])
|
|
cc = shutil.which('cc')
|
|
if not cc:
|
|
raise RuntimeError('Host C compiler required')
|
|
with tempfile.TemporaryDirectory(prefix='transaction92-') as tmp:
|
|
work = Path(tmp)
|
|
(work / 'windows.h').write_text(WINDOWS)
|
|
(work / 'tlhelp32.h').write_text(TOOLHELP)
|
|
(work / 'retirement.inc').write_text(unit)
|
|
executable = work / 'transaction-test'
|
|
command = [cc, '-std=c11', '-O1', '-Wall', '-Wextra', '-Werror', '-D_WIN64',
|
|
'-DGW_OWNED_SIGNIN_ONLY=0', '-I' + str(work), '-I' + str(nohelper),
|
|
str(ROOT / 'tests/direct_two_site_transaction_host_test.c'),
|
|
str(nohelper / 'direct_two_site_transaction.c'), '-o', str(executable)]
|
|
subprocess.run(command, check=True, capture_output=True, text=True)
|
|
result = subprocess.run([str(executable)], capture_output=True, text=True)
|
|
receipt = dict(runtime_root=str(root), baseline_ref=args.baseline_ref,
|
|
transaction_sha256=hashlib.sha256((nohelper / 'direct_two_site_transaction.c').read_bytes()).hexdigest(),
|
|
retirement_source_sha256=hashlib.sha256(source.encode()).hexdigest(),
|
|
extracted_unit_sha256=hashlib.sha256(unit.encode()).hexdigest(),
|
|
returncode=result.returncode, stdout=result.stdout, stderr=result.stderr)
|
|
if args.output:
|
|
args.output.parent.mkdir(parents=True, exist_ok=True)
|
|
args.output.write_text(json.dumps(receipt, indent=2) + '\n')
|
|
print(result.stdout, end='')
|
|
if result.returncode:
|
|
print(result.stderr, end='')
|
|
raise SystemExit(1)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|