#!/usr/bin/env python3 """Review stand-in for powershell.exe (based on sm-dev/fakewin). Behaviour is steered by the file $FAKE_CTL (one word): ok | stall (alive, no frames) | die (exit with an error) | failsvc (Spooler fails) | nosvc (Spooler gone) | reboot (uptime restarts). One-shot scripts are appended to $FAKE_LOG; a script containing HANG sleeps 60 s. Every text field carries HTML to test escaping.""" import math, os, random, re, sys, time src = sys.stdin.read() ctl = lambda: (open(os.environ['FAKE_CTL']).read().strip() if os.path.exists(os.environ.get('FAKE_CTL', '')) else 'ok') X = '' if '@@SNAP_BEGIN' not in src: cmd = src.strip().splitlines() open(os.environ['FAKE_LOG'], 'a').write('\n'.join(cmd[2:-2]) + '\n') if 'HANG' in src: time.sleep(60) print('[fake powershell] would run:'); print('\n'.join(cmd[-6:])); print(X + ''); sys.exit(3 if 'EXIT3' in src else 0) open(os.environ['FAKE_LOG'], 'a').write(f'COLLECTOR {time.time():.1f}\n') iv = float((re.search(r'\$iv\s*=\s*([\d.]+)', src) or [0, 2])[1]) GB = 1 << 30; n = 0; rx = tx = 5e9; cput = {4: 9e9, 620: 1e8, 1200: 5e9, 2040: 3e9} names = {4: 'System', 620: 'svchost.exe', 1200: X, 2040: 'chrome.exe'} boot = time.time() - 4 * 86400 if ctl() == 'die': sys.stderr.write('boom: cannot collect\n'); sys.exit(7) while True: c = ctl() if c == 'die': sys.stderr.write('boom: cannot collect\n'); sys.exit(7) if c == 'stall': time.sleep(1); continue if c == 'reboot': boot = time.time() - 30 t = time.time(); L = ['@@SNAP_BEGIN'] if n % 15 == 0: L += ['@@S info', 'hostname=' + X, 'os=' + X, 'kernel=10.0.1' + X, 'arch=64-bit', 'cpumodel=' + X, 'mhz=2500', 'virt=' + X, 'admin=1', '@@S users'] + [f'{p}|{X}' for p in names] L += ['@@S os', f'ts={int(t * 1000)}', f'uptime={int(t - boot)}', f'memtotal={16 * GB}', f'swaptotal={8 * GB}', f'swapfree={int(6.5 * GB)}', '@@S cpu'] for k in ['_Total', '0', '1']: L.append(f'{k}|40|28|12|1|3000') L += ['@@S sys', 'queue=0', 'ctxt=8000', 'threads=1832', '@@S mem', f'avail={6 * GB}', f'free={2 * GB}', f'standby={4 * GB}', f'syscache={GB // 2}', f'modified={GB // 8}', f'pool={GB}', '@@S net'] rx += 5e5 * iv; tx += 1e5 * iv L += [f'{X}|Up|10000|Ethernet|{int(rx)}|{int(tx)}|{int(rx / 900)}|{int(tx / 700)}|0|3|{X}', '@@S tcp', 'Established 42', 'Listen 27', '@@S disk', f'0 {X}|1000|2000|3|4|90', '@@S df', f'{X}|{X}|NTFS|{127 * GB}|{int(21.5 * GB)}', '@@S procs'] for p, nm in names.items(): cput[p] += .05 * iv * 1e7 L.append(f'{p}|4|{int(cput[p])}|8|12|{int(boot + p)}|{2 * GB}|{int(.3 * GB)}|{nm}') L += ['@@S sessions', '1'] if n % 2 == 0: L += ['@@S services', f'W3SVC|Running|Auto|0|{X}', 'BrokenSvc|Stopped|Auto|1067|Broken Demo Service', 'gupdate|Stopped|Auto||Empty exit code', 'RemoteRegistry|Stopped|Disabled|1077|Remote Registry'] if c != 'nosvc': L += ['Spooler|Stopped|Auto|1067|Print Spooler' if c == 'failsvc' else 'Spooler|Running|Auto|0|Print Spooler'] L += ['@@S thermal', f'{X}|45.5', '@@SNAP_END'] try: sys.stdout.write('\n'.join(L) + '\n'); sys.stdout.flush() except OSError: sys.exit(0) n += 1; time.sleep(iv)