2022-03-06 19:13:00 -05:00
|
|
|
import {ArgError, ArgParser} from './scripts/lib/arg-parser';
|
2022-02-24 16:58:39 -05:00
|
|
|
|
|
|
|
export async function main(ns) {
|
2022-03-06 19:13:00 -05:00
|
|
|
// Setup
|
2022-02-24 16:58:39 -05:00
|
|
|
ns.disableLog('ALL');
|
2022-03-09 14:06:14 -05:00
|
|
|
const argParser = new ArgParser('network-graph.js', 'Scan the network for devices and display as an ASCII tree:\n home\n ├─ n00dles (ROOTED)\n | └─ max-hardware (80|1)\n | └─ neo-net (50|1)\n ├─ foodnstuff (ROOTED)\n └─ sigma-cosmetics (ROOTED)', null, [
|
|
|
|
{name: 'target', desc: 'Point to start scan from, defaults to current machine', optional: true, default: ns.getHostname(), type: 'string'},
|
2022-03-06 19:13:00 -05:00
|
|
|
{name: 'depth', desc: 'Depth to scan to, defaults to 3', flags: ['-d', '--depth'], default: Infinity, type: 'num'},
|
2022-03-09 14:06:14 -05:00
|
|
|
{name: 'filter', desc: 'Display devices matching name', flags: ['-f', '--filter'], type: 'string'},
|
|
|
|
{name: 'regex', desc: 'Display devices matching pattern', flags: ['-r', '--regex'], type: 'string'},
|
2022-03-06 19:13:00 -05:00
|
|
|
{name: 'verbose', desc: 'Displays the required hack level & ports needed to root: (level|port)', flags: ['-v', '--verbose'], type: 'bool'},
|
|
|
|
]);
|
|
|
|
let args;
|
|
|
|
try {
|
|
|
|
args = argParser.parse(ns.args);
|
|
|
|
} catch(err) {
|
|
|
|
if(err instanceof ArgError) return ns.tprint(argParser.help(err.message));
|
|
|
|
throw err;
|
|
|
|
}
|
2022-02-24 16:58:39 -05:00
|
|
|
|
2022-03-09 14:06:14 -05:00
|
|
|
/**
|
|
|
|
* Prune tree down to devices that match name or pattern.
|
|
|
|
* @param tree {object} - Tree to search
|
|
|
|
* @param find {string} - Device name or pattern to search for
|
|
|
|
* @param regex {boolean} - True to use regex, false for raw check
|
|
|
|
* @returns {object} - Pruned tree
|
|
|
|
*/
|
|
|
|
function filter(tree, find, regex = false) {
|
|
|
|
const found = new Set();
|
|
|
|
function buildWhitelist(tree, find, path = []) {
|
|
|
|
const keys = Object.keys(tree);
|
|
|
|
if(!keys.length) return;
|
|
|
|
Object.keys(tree).forEach(n => {
|
|
|
|
const matches = regex ? new RegExp(find).test(n) : n == find;
|
|
|
|
if(n == 'n00dles') console.log(n, find, matches);
|
|
|
|
if(matches) {
|
|
|
|
found.add(n);
|
|
|
|
path.forEach(p => found.add(p));
|
|
|
|
}
|
|
|
|
buildWhitelist(tree[n], find, [...path, n]);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
function prune(tree, whitelist) {
|
|
|
|
Object.keys(tree).forEach(n => {
|
|
|
|
if(Object.keys(tree[n]).length) prune(tree[n], whitelist);
|
|
|
|
if(!whitelist.includes(n)) delete tree[n];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
buildWhitelist(tree, find);
|
|
|
|
prune(tree, Array.from(found));
|
|
|
|
}
|
|
|
|
|
2022-02-24 16:58:39 -05:00
|
|
|
/**
|
|
|
|
* Recursively search network & build a tree
|
|
|
|
* @param host {string} - Point to scan from
|
|
|
|
* @param depth {number} - Current scanning depth
|
|
|
|
* @param blacklist {String[]} - Devices already discovered
|
|
|
|
* @returns Dicionary of discovered devices
|
|
|
|
*/
|
2022-03-06 19:13:00 -05:00
|
|
|
function scan(host, depth = 1, blacklist = [host]) {
|
2022-03-09 14:06:14 -05:00
|
|
|
if(depth > args['depth']) return {};
|
2022-02-24 16:58:39 -05:00
|
|
|
const localTargets = ns.scan(host).filter(target => !blacklist.includes(target));
|
2022-03-09 14:06:14 -05:00
|
|
|
blacklist = [...blacklist, ...localTargets];
|
2022-02-24 16:58:39 -05:00
|
|
|
return localTargets.reduce((acc, target) => {
|
|
|
|
const info = ns.getServer(target);
|
2022-03-06 19:13:00 -05:00
|
|
|
const verb = args['verbose'] ? ` (${info.hasAdminRights ? 'ROOTED' : `${info.requiredHackingSkill}|${info.numOpenPortsRequired}`})` : '';
|
|
|
|
const name = `${target}${verb}`;
|
|
|
|
acc[name] = scan(target, depth + 1, blacklist);
|
2022-02-24 16:58:39 -05:00
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterate tree & print to screen
|
2022-03-06 19:13:00 -05:00
|
|
|
* @param tree {object} - Tree to parse
|
|
|
|
* @param spacer {string} - Spacer text for tree formatting
|
2022-02-24 16:58:39 -05:00
|
|
|
*/
|
2022-03-06 19:13:00 -05:00
|
|
|
function render(tree, spacer = ' ') {
|
2022-02-24 16:58:39 -05:00
|
|
|
Object.keys(tree).forEach((key, i, arr) => {
|
|
|
|
const last = i == arr.length - 1;
|
|
|
|
const branch = last ? '└─ ' : '├─ ';
|
|
|
|
ns.tprint(`${spacer}${branch}${key}`);
|
2022-02-26 09:39:04 -05:00
|
|
|
render(tree[key], spacer + (last ? ' ' : '| '));
|
2022-02-24 16:58:39 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-06 19:13:00 -05:00
|
|
|
// Run
|
2022-03-09 14:06:14 -05:00
|
|
|
ns.tprint(args['target']);
|
|
|
|
const found = scan(args['target']);
|
|
|
|
if(args['regex']) filter(found, args['regex'], true);
|
|
|
|
else if(args['filter']) filter(found, args['filter']);
|
2022-03-06 19:13:00 -05:00
|
|
|
render(found);
|
|
|
|
ns.tprint('');
|
2022-02-24 16:58:39 -05:00
|
|
|
}
|
2022-02-26 09:39:04 -05:00
|
|
|
|
|
|
|
export function autocomplete(data) {
|
|
|
|
return [...data.servers];
|
2022-03-09 14:06:14 -05:00
|
|
|
}
|