41 lines
3.6 KiB
JavaScript
41 lines
3.6 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import path from 'node:path';
|
|
import { spawn, execFileSync } from 'node:child_process';
|
|
import { once } from 'node:events';
|
|
import { observeWindowsProcess, windowsProcessObservationScript } from '../src/core/desktop/process-identity.mjs';
|
|
import { projectEnvironment } from '../src/core/process-environment.mjs';
|
|
|
|
const shell = path.join(process.env.SystemRoot || 'C:\\Windows', 'System32/WindowsPowerShell/v1.0/powershell.exe');
|
|
const encoded = script => ['-NoProfile', '-NonInteractive', '-EncodedCommand', Buffer.from(script, 'utf16le').toString('base64')];
|
|
const execute = script => execFileSync(shell, encoded(script), { windowsHide: true, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], timeout: 10000, env: projectEnvironment() });
|
|
|
|
test('a process exiting between the Windows snapshot and owner query is absent only after a fresh query', { timeout: 30000 }, async t => {
|
|
const current = observeWindowsProcess(process.pid);
|
|
assert.equal(current.pid, process.pid); assert.equal(current.executable, process.execPath);
|
|
const child = spawn(process.execPath, ['-e', "process.stdin.once('data',()=>process.exit(0));setInterval(()=>{},1000)"], { windowsHide: true, stdio: ['pipe', 'ignore', 'ignore'], env: projectEnvironment() });
|
|
const ended = once(child, 'close');
|
|
t.after(async () => { if (child.exitCode === null) child.stdin.end('exit'); await ended; });
|
|
const script = windowsProcessObservationScript(child.pid).replace('$awbOwner=Invoke-CimMethod', '[Console]::Out.WriteLine("SNAPSHOT");[Console]::Out.Flush();[Console]::In.ReadLine()|Out-Null;$awbOwner=Invoke-CimMethod');
|
|
const observer = spawn(shell, encoded(script), { windowsHide: true, stdio: ['pipe', 'pipe', 'pipe'], env: projectEnvironment() });
|
|
const observed = once(observer, 'close'); let output = '', errors = '';
|
|
const snapshot = new Promise(resolve => observer.stdout.on('data', bytes => { output += bytes.toString('utf8'); if (output.includes('SNAPSHOT')) resolve(); }));
|
|
observer.stderr.on('data', bytes => { errors += bytes.toString('utf8'); });
|
|
t.after(() => { if (observer.exitCode === null) { observer.stdin.end('\n'); observer.kill(); } });
|
|
await Promise.race([snapshot, observed.then(() => { throw new Error('Observer ended before the snapshot: ' + errors); })]);
|
|
child.stdin.end('exit'); assert.equal((await ended)[0], 0);
|
|
observer.stdin.end('\n'); assert.equal((await observed)[0], 0, errors);
|
|
assert.equal(output.replace('SNAPSHOT', '').trim(), 'null');
|
|
assert.equal(observeWindowsProcess(child.pid), null);
|
|
});
|
|
|
|
test('owner observation errors remain errors while a process is present, replaced, or cannot be rechecked', { timeout: 30000 }, () => {
|
|
const script = windowsProcessObservationScript(process.pid);
|
|
assert.throws(() => execute('function Invoke-CimMethod {throw "injected owner denial"};' + script));
|
|
assert.throws(() => execute('function Invoke-CimMethod {[pscustomobject]@{ReturnValue=2;Sid=$null}};' + script));
|
|
const fakeSnapshot = '[pscustomobject]@{ProcessId=' + process.pid + ';ParentProcessId=1;CreationDate=[DateTime]::UtcNow;ExecutablePath="existing"}';
|
|
assert.throws(() => execute('$awbQueries=0;function Get-CimInstance {$script:awbQueries++;if($script:awbQueries -gt 1){throw "injected query denial"};' + fakeSnapshot + '};function Invoke-CimMethod {throw "injected owner denial"};' + script));
|
|
assert.throws(() => execute('function Get-CimInstance {' + fakeSnapshot + '};function Invoke-CimMethod {throw "injected replaced process owner"};' + script));
|
|
assert.throws(() => windowsProcessObservationScript('1;exit 0'));
|
|
});
|