Advertisement
lanolinoil

Bitburner worm.ns

Dec 19th, 2021
10,872
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** @param {NS} ns **/
  2. export async function main(ns) {
  3.    //if no arguments provided tell the user how to use script.
  4.    if (ns.args.length === 0) {
  5.       ns.alert("Please include one or more 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.");
  6.       return;
  7.    }
  8.    var ogArgs = ns.args;
  9.    ns.toast('Running worm on ' + ns.getHostname());
  10.    var hostservers = ns.scan(ns.getHostname());                                                             //get all servers you can connect to
  11.    var scriptram = ns.getScriptRam('worm.ns', 'home');                                                      //get ram for this script
  12.    var hackscriptram = ns.getScriptRam('hackservers.ns', 'home');                                           //get ram for hack script
  13.    var avsram = ns.getServerMaxRam(ns.getHostname()) - ns.getServerUsedRam(ns.getHostname()) + scriptram;   //get available server ram for this server
  14.    var hsthreads = Math.floor(avsram / hackscriptram);                                                      //calculate usethreads for hack script for this server
  15.  
  16.  
  17.    await attackAll(hostservers,ns.getHostname());
  18.  
  19.    if (hsthreads) {                                                                                          //if usethreads exists for this script, build args array of parameters based on this scripts args
  20.       var hsargs = [];
  21.       for (const argument of ns.args) {
  22.          hsargs.push(argument);
  23.          hsargs.push(ns.getServerMinSecurityLevel(argument));
  24.          hsargs.push(ns.getServerMaxMoney(argument));
  25.          hsargs.push(ns.getServerRequiredHackingLevel(argument));
  26.       }
  27.       if (ns.getHostname() != 'home') {                                                                       //copy hack script to this server and spawn script with threads and arguments as a single string
  28.          await ns.scp('hackservers.ns', 'home', ns.getHostname());
  29.       }
  30.       ns.spawn('hackservers.ns', hsthreads, hsargs.toString());
  31.    }
  32.  
  33.  
  34.    async function attack(server) {
  35.       var hacktoolnum = 0;                                                                                   //count and use hack tools owned if you don't have root
  36.       if (!ns.hasRootAccess(server)) {
  37.          ns.toast('Opening ports on ' + server);
  38.          if (ns.fileExists('BruteSSH.exe', 'home')) {
  39.             ns.brutessh(server);
  40.             hacktoolnum++;
  41.          }
  42.          if (ns.fileExists('FTPCrack.exe', 'home')) {
  43.             ns.ftpcrack(server);
  44.             hacktoolnum++;
  45.  
  46.          }
  47.          if (ns.fileExists('relaySMTP.exe', 'home')) {
  48.             ns.relaysmtp(server);
  49.             hacktoolnum++;
  50.  
  51.          }
  52.          if (ns.fileExists('HTTPWorm.exe', 'home')) {
  53.             ns.httpworm(server);
  54.             hacktoolnum++;
  55.  
  56.          }
  57.          if (ns.fileExists('SQLInject.exe', 'home')) {
  58.             ns.sqlinject(server);
  59.             hacktoolnum++;
  60.  
  61.          }
  62.       }
  63.       if (ns.getServerNumPortsRequired(server) <= hacktoolnum && !ns.hasRootAccess(server)) {
  64.          ns.toast("nuking " + server);
  65.          ns.nuke(server);
  66.       }
  67.       if (!ns.hasRootAccess(server)) {
  68.          ns.toast("unable to gain root to " + server, "error");
  69.       }
  70.    }
  71.  
  72.    async function worm(server) {
  73.       //copy WORM script to server and run
  74.       if (!ns.fileExists('worm.ns', server)) {
  75.          ns.print('worm.ns being copied to ' + server);
  76.          await ns.scp('worm.ns', 'home', server);
  77.       }
  78.       //if you don't see either script running on target server, run worm on it.
  79.       if (!ns.scriptRunning('worm.ns', server) && !ns.scriptRunning('hackservers.ns', server)) {
  80.          ns.print('running worm on ' + server);
  81.          await ns.sleep(11000);
  82.          await ns.scp('worm.ns', 'home', server);
  83.          ns.exec('worm.ns', server, 1, ...ogArgs);
  84.       }
  85.    }
  86.  
  87.    async function attackAll(servers,host) {
  88.       for (const server of servers) {
  89.          await attack(server);
  90.          if (ns.getServerMaxRam(server) >= ns.getServerUsedRam(server) + scriptram) {                           //if the server has enough ram to run the worm script
  91.             await worm(server);
  92.          } else {                                                                                               //if server can't run script, look at servers it can connect to, gain root, and run script there
  93.             var moreservs = ns.scan(server);
  94.             moreservs.splice(moreservs.indexOf(host),1);
  95.             await attackAll(moreservs,server);
  96.  
  97.          }
  98.       }
  99.    }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement