bitburner/scripts/rootkit.js

106 lines
3.5 KiB
JavaScript
Raw Normal View History

2022-03-17 12:19:54 -04:00
import {ArgError, ArgParser} from '/scripts/lib/arg-parser';
import {copyWithDependencies, progressBar, slowPrint} from '/scripts/lib/utils';
/**
* BitBurner autocomplete
* @param data {server: string[], txts: string[], scripts: string[], flags: string[]} - Contextual information
* @returns {string[]} - Pool of autocomplete options
*/
export function autocomplete(data) {
return [...data.servers, ...data.scripts];
}
2022-03-09 14:06:14 -05:00
/**
* Pwn a target server with availible tools. Additionally can copy & execute a script after pwning.
* @param ns {NS} - Bitburner API
*/
export async function main(ns) {
// Setup
ns.disableLog('ALL');
2022-03-09 15:12:37 -05:00
const argParser = new ArgParser('rootkit.js', 'Automatically gain root access to a device. A file can also be uploaded & executed.', null, [
{name: 'device', desc: 'Device to root, defaults to current machine', optional: true, default: ns.getHostname(), type: 'string'},
2022-03-09 14:06:14 -05:00
{name: 'script', desc: 'Script to copy & execute', optional: true, type: 'string'},
2022-03-17 12:19:54 -04:00
{name: 'args', desc: 'Arguments for script. Forward the current target with: {{TARGET}}', optional: true, extras: true, type: 'string'},
2022-03-09 14:06:14 -05:00
{name: 'cpu', desc: 'Number of CPU threads to use with script', flags: ['-c', '--cpu'], type: 'num'},
2022-03-17 12:19:54 -04:00
{name: 'silent', desc: 'Surpress program output', flags: ['-s', '--silent'], type: 'bool'}
], true);
2022-03-17 12:19:54 -04:00
2022-03-09 14:06:14 -05:00
try {
2022-03-17 12:19:54 -04:00
const args = argParser.parse(ns.args);
2022-03-09 15:12:37 -05:00
if(args['script'] && !args['cpu']) args['cpu'] = ~~(ns.getServerMaxRam(args['device']) / ns.getScriptRam(args['script'], 'home')) || 1;
2022-03-09 14:06:14 -05:00
2022-03-17 12:19:54 -04:00
// Banner
if(!args['silent']) {
ns.tprint('===================================================');
ns.tprint(`Rooting: ${args['device']}`);
ns.tprint('===================================================');
2022-03-09 14:06:14 -05:00
}
2022-03-17 12:19:54 -04:00
let spacer = false;
const sleep = 750;
2022-03-09 14:06:14 -05:00
try {
2022-03-17 12:19:54 -04:00
// Run exploits
ns.brutessh(args['device']);
if(!args['silent']) {
await slowPrint(ns, `Attacking over SSH (${args['device']}:22)...`, 0.5, 1.5);
await ns.sleep(sleep);
spacer = true;
}
ns.ftpcrack(args['device']);
if(!args['silent']) {
await slowPrint(ns, `Attacking over FTP (${args['device']}:24)...`, 0.5, 1.5);
await ns.sleep(sleep);
}
ns.relaysmtp(args['device']);
if(!args['silent']) {
await slowPrint(ns, `Attacking over SMTP (${args['device']}:25)...`, 0.5, 1.5);
await ns.sleep(sleep);
}
} catch {
} finally {
try {
// Attempt root
if(spacer) ns.tprint('');
ns.nuke(args['device'])
if(!args['silent']) {
ns.tprint(`Root: Success!`);
ns.tprint('');
}
} catch {
if(!args['silent']) {
ns.tprint(`Root: Failed`);
ns.tprint('');
}
ns.exit();
}
2022-03-09 14:06:14 -05:00
}
2022-03-17 12:19:54 -04:00
if(args['script']) {
// Detect script dependencies & copy everything to target
const files = await copyWithDependencies(ns, args['script'], args['device']);
if(!args['silent']) {
await ns.sleep(500);
ns.tprint('Copying files:');
for(let file of files) await progressBar(ns, file);
}
2022-03-09 14:06:14 -05:00
2022-03-17 12:19:54 -04:00
// Run script
if(!args['silent']) {
ns.tprint('');
ns.tprint(`Executing with ${args['cpu']} thread${args['cpu'] > 1 ? 's' : ''}...`);
await ns.sleep(500);
}
ns.scriptKill(args['script'], args['device']);
const pid = ns.exec(args['script'], args['device'], args['cpu'], ...args['args']
.map(a => a == '{{TARGET}}' ? args['device'] : a));
if(!args['silent']) {
ns.tprint(!!pid ? 'Done!' : 'Failed to start');
ns.tprint('');
}
}
} catch(err) {
if(err instanceof ArgError) return ns.tprint(argParser.help(err.message));
throw err;
}
2022-03-09 14:06:14 -05:00
}