Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** @param {NS} ns **/
- export async function main(ns) {
- //if no arguments provided tell the user how to use script.
- if (ns.args.length === 0) {
- ns.alert("Please include one to five arguments as server names to hack. The script will propogate across all servers and grow, weaken and hack the specified targets. As you get new hacking tools, kill all scripts and rerun from home.");
- return;
- }
- ns.toast('Running worm on ' + ns.getHostname());
- //get all connectable servers and ram cost of script
- //get all servers you can connect to
- var servers = ns.scan(ns.getHostname());
- //get ram for this script
- var scriptram = ns.getScriptRam('worm.ns', 'home');
- //get ram for hack script
- var hackscriptram = ns.getScriptRam('hackservers.ns', 'home')
- //get available server ram for this server
- var avsram = ns.getServerMaxRam(ns.getHostname()) - ns.getServerUsedRam(ns.getHostname()) + scriptram;
- //calculate usethreads for hack script for this server
- var hsthreads = Math.floor(avsram / hackscriptram);
- for (const server of servers) {
- //count and use hack tools owned if you don't have root
- var hacktoolnum = 0;
- //attack server
- if (!ns.hasRootAccess(server)) {
- ns.toast('Opening ports on ' + server);
- if (ns.fileExists('BruteSSH.exe', 'home')) {
- ns.brutessh(server);
- hacktoolnum++;
- }
- if (ns.fileExists('FTPCrack.exe', 'home')) {
- ns.ftpcrack(server);
- hacktoolnum++;
- }
- if (ns.fileExists('relaySMTP.exe', 'home')) {
- ns.relaysmtp(server);
- hacktoolnum++;
- }
- if (ns.fileExists('HTTPWorm.exe', 'home')) {
- ns.httpworm(server);
- hacktoolnum++;
- }
- if (ns.fileExists('SQLInject.exe', 'home')) {
- ns.sqlinject(server);
- hacktoolnum++;
- }
- }
- //if you don't have access and used enough tools nuke target server
- if (ns.getServerNumPortsRequired(server) <= hacktoolnum && !ns.hasRootAccess(server)) {
- ns.toast("nuking " + server);
- ns.nuke(server);
- } else
- //if you still don't have access, skip
- if (!ns.hasRootAccess(server)) {
- ns.toast("unable to gain root to " + server, "error");
- continue;
- }
- //if the server has enough ram to run the worm script
- if (ns.getServerMaxRam(server) > ns.getServerUsedRam(server) + scriptram) {
- //copy WORM script to server and run
- if (!ns.fileExists('worm.ns', server)) {
- ns.print('worm.ns being copied to ' + server);
- await ns.scp('worm.ns', 'home', server);
- }
- //if you don't see either script running on target server, run worm on it.
- if (!ns.scriptRunning('worm.ns', server) && !ns.scriptRunning('hackservers.ns', server)) {
- ns.print('running worm on ' + server);
- await ns.sleep(11000);
- await ns.scp('worm.ns', 'home', server);
- if (ns.args.length === 1) {
- ns.exec('worm.ns', server, 1, ns.args[0]);
- }
- if (ns.args.length === 2) {
- ns.exec('worm.ns', server, 1, ns.args[0], ns.args[1]);
- }
- if (ns.args.length === 3) {
- ns.exec('worm.ns', server, 1, ns.args[0], ns.args[1], ns.args[2]);
- }
- if (ns.args.length === 4) {
- ns.exec('worm.ns', server, 1, ns.args[0], ns.args[1], ns.args[2], ns.args[3]);
- }
- if (ns.args.length === 5) {
- ns.exec('worm.ns', server, 1, ns.args[0], ns.args[1], ns.args[2], ns.args[3], ns.args[4]);
- }
- }
- } else {
- //if server can't run script, look at servers it can connect to, gain root, and run script there
- var moreservs = ns.scan(server);
- for (const server2 of moreservs) {
- var hacktoolnum2 = 0;
- //attack server
- if (!ns.hasRootAccess(server2)) {
- ns.toast('Opening ports on ' + server2)
- if (ns.fileExists('BruteSSH.exe', 'home')) {
- ns.brutessh(server2);
- hacktoolnum2++;
- }
- if (ns.fileExists('FTPCrack.exe', 'home')) {
- ns.ftpcrack(server2);
- hacktoolnum2++;
- }
- if (ns.fileExists('relaySMTP.exe', 'home')) {
- ns.relaysmtp(server2);
- hacktoolnum2++;
- }
- if (ns.fileExists('HTTPWorm.exe', 'home')) {
- ns.httpworm(server2);
- hacktoolnum2++;
- }
- if (ns.fileExists('SQLInject.exe', 'home')) {
- ns.sqlinject(server2);
- hacktoolnum2++;
- }
- }
- if (ns.getServerNumPortsRequired(server2) <= hacktoolnum2 && !ns.hasRootAccess(server2)) {
- ns.toast("nuking " + server2);
- ns.nuke(server2);
- } else
- //if you still don't have access, skip
- if (!ns.hasRootAccess(server2)) {
- ns.toast("unable to gain root to " + server2, "error");
- continue;
- }
- if (ns.getServerMaxRam(server2) > ns.getServerUsedRam(server2) + scriptram) {
- //copy WORM script to server and run
- if (!ns.fileExists('worm.ns', server2)) {
- ns.print('worm.ns being copied to ' + server2);
- await ns.scp('worm.ns', 'home', server2);
- }
- if (!ns.scriptRunning('worm.ns', server2) && !ns.scriptRunning('hackservers.ns', server2)) {
- ns.print('running worm on ' + server2);
- await ns.sleep(11000);
- await ns.scp('worm.ns', 'home', server2);
- if (ns.args.length === 1) {
- ns.exec('worm.ns', server2, 1, ns.args[0]);
- }
- if (ns.args.length === 2) {
- ns.exec('worm.ns', server2, 1, ns.args[0], ns.args[1]);
- }
- if (ns.args.length === 3) {
- ns.exec('worm.ns', server2, 1, ns.args[0], ns.args[1], ns.args[2]);
- }
- if (ns.args.length === 4) {
- ns.exec('worm.ns', server2, 1, ns.args[0], ns.args[1], ns.args[2], ns.args[3]);
- }
- if (ns.args.length === 5) {
- ns.exec('worm.ns', server2, 1, ns.args[0], ns.args[1], ns.args[2], ns.args[3], ns.args[4]);
- }
- }
- }
- }
- }
- }
- //if usethreads exists for this script, build args array of parameters based on this scripts args
- if (hsthreads) {
- var hsargs = [];
- for (const argument of ns.args) {
- hsargs.push(argument);
- hsargs.push(ns.getServerMinSecurityLevel(argument));
- hsargs.push(ns.getServerMaxMoney(argument));
- hsargs.push(ns.getServerRequiredHackingLevel(argument));
- }
- //copy hack script to this server and spawn script with threads and arguments as a single string
- if (ns.getHostname() != 'home') {
- await ns.scp('hackservers.ns', 'home', ns.getHostname());
- }
- ns.spawn('hackservers.ns', hsthreads, hsargs.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement