222 lines
11 KiB
JavaScript
222 lines
11 KiB
JavaScript
import { createHash } from 'node:crypto';
|
|
import { readFile } from 'node:fs/promises';
|
|
import { isIPv4 } from 'node:net';
|
|
import { pathToFileURL } from 'node:url';
|
|
|
|
const routes = Object.freeze(['/system/resource', '/ip/traffic-flow', '/ip/traffic-flow/target']);
|
|
const policyRoutes = Object.freeze(['/system/resource', '/ip/settings', '/ip/firewall/connection/tracking',
|
|
'/ip/firewall/filter', '/ip/firewall/raw', '/ip/firewall/mangle', '/ip/firewall/nat', '/routing/rule', '/ip/vrf']);
|
|
const counterFields = ['ifindex', 'rx-packet', 'tx-packet', 'fp-rx-byte', 'fp-tx-byte', 'fp-rx-packet', 'fp-tx-packet'];
|
|
const nativeId = value => typeof value === 'string' && /^\*[a-f0-9]{1,16}$/i.test(value);
|
|
const ports = Object.freeze({ '194.226.121.150': 19081, '104.165.244.7': 19082 });
|
|
const attestation = 'verified-known-host-161.104.46.222-owned-forward';
|
|
const reviewedTransportHash = '67d86dbae2710a25145df8ab9469029182f21a31972c49f063cdb6bc3daa8ea6';
|
|
const sha = bytes => createHash('sha256').update(bytes).digest('hex');
|
|
const deny = () => { throw new Error('NetFlow discovery refused'); };
|
|
|
|
export async function loadPinnedNetFlowTransport(policy = false) {
|
|
const allowedPaths = policy ? policyRoutes : routes;
|
|
return loadTransport(allowedPaths, allowedPaths.length * 2, policy ? 131072 : 65536);
|
|
}
|
|
|
|
async function loadTransport(allowedPaths, requests, responseLimit, totalLimit = responseLimit * requests) {
|
|
const bytes = await readFile(new URL('./chr-recursive-observation.mjs', import.meta.url));
|
|
if (sha(bytes) !== reviewedTransportHash) deny();
|
|
const reviewed = bytes.toString('utf8');
|
|
const source = reviewed.slice(0, reviewed.indexOf('async function privateStdin()'))
|
|
.replace(/const ROOT = .*?;/, 'const ROOT = null;')
|
|
.replace(/export const PATHS = Object.freeze\(\[[\s\S]*?\]\);/, `export const PATHS = Object.freeze(${JSON.stringify(allowedPaths)});`)
|
|
.replace('response: 1048576, bytes: 8388608, requests: 256',
|
|
`response: ${responseLimit}, bytes: ${totalLimit}, requests: ${requests}`);
|
|
const transport = await import('data:text/javascript;base64,' + Buffer.from(source).toString('base64'));
|
|
if (JSON.stringify(transport.PATHS) !== JSON.stringify(allowedPaths)) deny();
|
|
for (const [host, port] of Object.entries(ports)) {
|
|
if (endpoint(transport, host) !== `http://127.0.0.1:${port}`) deny();
|
|
}
|
|
return transport;
|
|
}
|
|
|
|
export async function discoverPinnedInterfaceCounterShape(read) {
|
|
const devices = [];
|
|
for (const host of Object.keys(ports)) {
|
|
const inventory = await read(host, '/interface');
|
|
if (!Array.isArray(inventory) || inventory.length > 256 ||
|
|
inventory.some(row => !nativeId(row?.['.id'])) ||
|
|
new Set(inventory.map(row => row['.id'])).size !== inventory.length) deny();
|
|
const preferred = type => inventory.find(row => row.type === type &&
|
|
observedFlag(row.running) === true && observedFlag(row.disabled) === false);
|
|
const chosen = preferred('wg') ?? preferred('ether');
|
|
if (!chosen) {
|
|
devices.push({ status: 'no-eligible-interface' });
|
|
continue;
|
|
}
|
|
const exact = single(await read(host, '/interface/' + encodeURIComponent(chosen['.id'])));
|
|
if (exact['.id'] !== chosen['.id'] || exact.type !== chosen.type ||
|
|
observedFlag(exact.running) !== true || observedFlag(exact.disabled) !== false) deny();
|
|
const fields = Object.fromEntries(counterFields.map(key => {
|
|
const value = exact[key];
|
|
return [key, { present: Object.hasOwn(exact, key),
|
|
type: value === null ? 'null' : Array.isArray(value) ? 'array' : typeof value }];
|
|
}));
|
|
devices.push({ status: 'shape-observed', fields });
|
|
}
|
|
return { devices, ingressVerified: false, capturePerformed: false, mutationsPerformed: false };
|
|
}
|
|
|
|
function endpoint(transport, host) {
|
|
return transport.configuration(['--source', host, '--transport', 'ssh-loopback', '--output',
|
|
'tmp/netflow-endpoint-discovery', '--owned-tunnel-attestation', attestation]).endpoint;
|
|
}
|
|
|
|
function bool(value) {
|
|
if (value === true || value === 'true') return true;
|
|
if (value === false || value === 'false') return false;
|
|
return deny();
|
|
}
|
|
|
|
function single(rows) {
|
|
if (rows.length !== 1) deny();
|
|
return rows[0];
|
|
}
|
|
|
|
function observedFlag(value) {
|
|
if ([true, 'true', 'yes'].includes(value)) return true;
|
|
if ([false, 'false', 'no'].includes(value)) return false;
|
|
return null;
|
|
}
|
|
|
|
export async function discoverPinnedNetFlowPolicy(read) {
|
|
const exporters = [];
|
|
for (const host of Object.keys(ports)) {
|
|
const resource = single(await read(host, policyRoutes[0]));
|
|
if (typeof resource.version !== 'string' || !/^\d+\.\d+(?:\.\d+)?(?: \([a-z-]+\))?$/.test(resource.version)) deny();
|
|
const ip = single(await read(host, policyRoutes[1]));
|
|
const tracking = single(await read(host, policyRoutes[2]));
|
|
const firewall = {};
|
|
const vetoes = [];
|
|
const ipForward = observedFlag(ip['ip-forward']);
|
|
const allowFastPath = observedFlag(ip['allow-fast-path']);
|
|
const conntrack = tracking.enabled === 'auto' ? 'auto' : observedFlag(tracking.enabled);
|
|
if (ipForward !== true) vetoes.push('ip-forward-not-confirmed');
|
|
if (allowFastPath !== false) vetoes.push('fast-path-not-confirmed-disabled');
|
|
if (conntrack !== true) vetoes.push('conntrack-not-explicitly-enabled');
|
|
for (const path of policyRoutes.slice(3, 7)) {
|
|
const rows = await read(host, path);
|
|
const enabled = rows.filter(row => observedFlag(row.disabled) !== true);
|
|
firewall[path.split('/').at(-1)] = {
|
|
total: rows.length,
|
|
notExplicitlyDisabled: enabled.length,
|
|
fastTrack: enabled.filter(row => row.action === 'fasttrack-connection').length,
|
|
};
|
|
}
|
|
const routingRules = await read(host, policyRoutes[7]);
|
|
const activeRoutingRules = routingRules.filter(row => observedFlag(row.disabled) !== true).length;
|
|
if (activeRoutingRules) vetoes.push('routing-policy-present');
|
|
const vrfs = await read(host, policyRoutes[8]);
|
|
const mainVrfOnly = vrfs.length === 1 && vrfs[0].name === 'main' && vrfs[0].interfaces === 'all' &&
|
|
observedFlag(vrfs[0].disabled) !== true;
|
|
if (!mainVrfOnly) vetoes.push('main-vrf-only-not-confirmed');
|
|
exporters.push({ host, version: resource.version, ipForward, allowFastPath, conntrack, firewall,
|
|
activeRoutingRules, vrfCount: vrfs.length, mainVrfOnly, vetoes });
|
|
}
|
|
return {
|
|
status: exporters.some(item => item.vetoes.length) ? 'pre-topology-veto' : 'no-global-veto-observed',
|
|
exporters, routeSelectionVerified: false, tuplePolicyVerified: false,
|
|
ingressVerified: false, capturePerformed: false, mutationsPerformed: false,
|
|
};
|
|
}
|
|
|
|
function target(row) {
|
|
if (bool(row.disabled) || row.version !== 'ipfix') return null;
|
|
const destination = row['dst-address'];
|
|
const portText = String(row.port);
|
|
if (!isIPv4(destination) || !/^[1-9][0-9]{0,4}$/.test(portText) || Number(portText) > 65535) deny();
|
|
const source = row['src-address'];
|
|
if (source !== undefined && source !== '' && !isIPv4(source)) deny();
|
|
return { destination, port: Number(portText), configuredSource: source && source !== '0.0.0.0' ? source : null };
|
|
}
|
|
|
|
export async function discoverPinnedNetFlowEndpoints(read) {
|
|
const exporters = [];
|
|
for (const host of Object.keys(ports)) {
|
|
const resource = single(await read(host, routes[0]));
|
|
if (typeof resource.version !== 'string' || !/^\d+\.\d+(?:\.\d+)?(?: \([a-z-]+\))?$/.test(resource.version)) deny();
|
|
const enabled = bool(single(await read(host, routes[1])).enabled);
|
|
const rows = await read(host, routes[2]);
|
|
if (rows.length > 64) deny();
|
|
const configuredTargets = rows.map(target).filter(Boolean);
|
|
exporters.push({ host, version: resource.version, enabled, configuredTargets });
|
|
}
|
|
const [first, second] = exporters;
|
|
const sameEndpoint = (a, b) => a.destination === b.destination && a.port === b.port;
|
|
const common = first.enabled && second.enabled
|
|
? first.configuredTargets.filter(a => second.configuredTargets.some(b => sameEndpoint(a, b)))
|
|
: [];
|
|
const unique = common.length === 1 && second.configuredTargets.filter(b => sameEndpoint(common[0], b)).length === 1;
|
|
return {
|
|
status: common.length === 0 ? 'no-common-active-ipfix-target' : unique ? 'configured-endpoint' : 'ambiguous-endpoint',
|
|
configuredEndpoint: unique ? { destination: common[0].destination, port: common[0].port } : null,
|
|
exporters,
|
|
ingressVerified: false,
|
|
capturePerformed: false,
|
|
mutationsPerformed: false,
|
|
};
|
|
}
|
|
|
|
async function main() {
|
|
const mode = process.argv.slice(2);
|
|
if (mode.length !== 1) deny();
|
|
const policy = mode[0] === '--check-policy' || mode[0] === '--read-pinned-policy';
|
|
const counters = mode[0] === '--check-counters' || mode[0] === '--read-pinned-counters';
|
|
const transport = counters ? await loadTransport(['/interface'], 4, 131072) : await loadPinnedNetFlowTransport(policy);
|
|
if (mode[0] === '--check-only' || mode[0] === '--check-policy') {
|
|
console.log(JSON.stringify({ transportSha256: reviewedTransportHash, loopbackPorts: ports, readPaths: transport.PATHS, networkUsed: false }));
|
|
return;
|
|
}
|
|
if (mode[0] === '--check-counters') {
|
|
const detail = await loadTransport(['/interface/*A'], 4, 16384);
|
|
if (detail.PATHS[0] !== '/interface/*A') deny();
|
|
console.log(JSON.stringify({ transportSha256: reviewedTransportHash, loopbackPorts: ports,
|
|
readPaths: ['/interface', '/interface/<observed-native-id>'], networkUsed: false }));
|
|
return;
|
|
}
|
|
if (!['--read-pinned', '--read-pinned-policy', '--read-pinned-counters'].includes(mode[0]) || process.stdin.isTTY) deny();
|
|
const chunks = [];
|
|
let length = 0;
|
|
for await (const chunk of process.stdin) {
|
|
length += chunk.length;
|
|
if (length > 4096) deny();
|
|
chunks.push(chunk);
|
|
}
|
|
const input = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(Buffer.concat(chunks)));
|
|
if (!input || input.attestation !== attestation ||
|
|
Object.keys(input).length !== 2 || !input.passwords || Object.keys(input.passwords).length !== 2) deny();
|
|
const passwords = Object.keys(ports).map(host => input.passwords[host]);
|
|
if (passwords.some(password => typeof password !== 'string' || !/^[^\u0000-\u001f\u007f]{1,512}$/u.test(password))) deny();
|
|
const authorizations = passwords.map(password => 'Basic ' + Buffer.from('admin:' + password).toString('base64'));
|
|
const secrets = [...passwords, ...authorizations, ...authorizations.map(value => value.slice(6))].map(value => Buffer.from(value));
|
|
const budget = { requests: 0, bytes: 0, rows: 0, deadline: Date.now() + 30000 };
|
|
const discovery = counters ? discoverPinnedInterfaceCounterShape : policy ? discoverPinnedNetFlowPolicy : discoverPinnedNetFlowEndpoints;
|
|
const report = await discovery(async (host, route) => {
|
|
const authorization = authorizations[Object.keys(ports).indexOf(host)];
|
|
let selected = transport;
|
|
if (counters && route !== '/interface') {
|
|
if (!route.startsWith('/interface/') || !nativeId(route.slice('/interface/'.length))) deny();
|
|
selected = await loadTransport([route], 4, 16384, 524288);
|
|
}
|
|
const response = await selected.readResponse(endpoint(selected, host), route, authorization, budget);
|
|
if (secrets.some(secret => response.body.includes(secret))) deny();
|
|
return response.rows;
|
|
});
|
|
const output = JSON.stringify(report);
|
|
if (secrets.some(secret => Buffer.from(output).includes(secret))) deny();
|
|
console.log(output);
|
|
}
|
|
|
|
if (process.argv[1] && pathToFileURL(process.argv[1]).href === import.meta.url) {
|
|
main().catch(() => {
|
|
console.error('NetFlow discovery refused');
|
|
process.exitCode = 1;
|
|
});
|
|
}
|