Advertisement
lanolinoil

Bitburner worm.ns

Dec 18th, 2021
13,034
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 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.");
  6.       return;
  7.    }
  8.    ns.toast('Running worm on ' + ns.getHostname());
  9.    //get all connectable servers and ram cost of script
  10.  
  11.    //get all servers you can connect to
  12.    var servers = ns.scan(ns.getHostname());
  13.    //get ram for this script
  14.    var scriptram = ns.getScriptRam('worm.ns', 'home');
  15.    //get ram for hack script
  16.    var hackscriptram = ns.getScriptRam('hackservers.ns', 'home')
  17.    //get available server ram for this server
  18.    var avsram = ns.getServerMaxRam(ns.getHostname()) - ns.getServerUsedRam(ns.getHostname()) + scriptram;
  19.    //calculate usethreads for hack script for this server
  20.    var hsthreads = Math.floor(avsram / hackscriptram);
  21.    for (const server of servers) {
  22.       //count and use hack tools owned if you don't have root
  23.       var hacktoolnum = 0;
  24.       //attack server
  25.       if (!ns.hasRootAccess(server)) {
  26.          ns.toast('Opening ports on ' + server);
  27.          if (ns.fileExists('BruteSSH.exe', 'home')) {
  28.             ns.brutessh(server);
  29.             hacktoolnum++;
  30.          }
  31.          if (ns.fileExists('FTPCrack.exe', 'home')) {
  32.             ns.ftpcrack(server);
  33.             hacktoolnum++;
  34.  
  35.          }
  36.          if (ns.fileExists('relaySMTP.exe', 'home')) {
  37.             ns.relaysmtp(server);
  38.             hacktoolnum++;
  39.  
  40.          }
  41.          if (ns.fileExists('HTTPWorm.exe', 'home')) {
  42.             ns.httpworm(server);
  43.             hacktoolnum++;
  44.  
  45.          }
  46.          if (ns.fileExists('SQLInject.exe', 'home')) {
  47.             ns.sqlinject(server);
  48.             hacktoolnum++;
  49.  
  50.          }
  51.       }
  52.       //if you don't have access and used enough tools nuke target server
  53.       if (ns.getServerNumPortsRequired(server) <= hacktoolnum && !ns.hasRootAccess(server)) {
  54.          ns.toast("nuking " + server);
  55.          ns.nuke(server);
  56.       } else
  57.          //if you still don't have access, skip
  58.          if (!ns.hasRootAccess(server)) {
  59.             ns.toast("unable to gain root to " + server, "error");
  60.             continue;
  61.          }
  62.       //if the server has enough ram to run the worm script
  63.       if (ns.getServerMaxRam(server) > ns.getServerUsedRam(server) + scriptram) {
  64.          //copy WORM script to server and run
  65.          if (!ns.fileExists('worm.ns', server)) {
  66.             ns.print('worm.ns being copied to ' + server);
  67.             await ns.scp('worm.ns', 'home', server);
  68.          }
  69.          //if you don't see either script running on target server, run worm on it.
  70.          if (!ns.scriptRunning('worm.ns', server) && !ns.scriptRunning('hackservers.ns', server)) {
  71.             ns.print('running worm on ' + server);
  72.             await ns.sleep(11000);
  73.             await ns.scp('worm.ns', 'home', server);
  74.             if (ns.args.length === 1) {
  75.                ns.exec('worm.ns', server, 1, ns.args[0]);
  76.             }
  77.             if (ns.args.length === 2) {
  78.                ns.exec('worm.ns', server, 1, ns.args[0], ns.args[1]);
  79.             }
  80.             if (ns.args.length === 3) {
  81.                ns.exec('worm.ns', server, 1, ns.args[0], ns.args[1], ns.args[2]);
  82.             }
  83.             if (ns.args.length === 4) {
  84.                ns.exec('worm.ns', server, 1, ns.args[0], ns.args[1], ns.args[2], ns.args[3]);
  85.             }
  86.             if (ns.args.length === 5) {
  87.                ns.exec('worm.ns', server, 1, ns.args[0], ns.args[1], ns.args[2], ns.args[3], ns.args[4]);
  88.             }
  89.          }
  90.       } else {
  91.          //if server can't run script, look at servers it can connect to, gain root, and run script there
  92.          var moreservs = ns.scan(server);
  93.          for (const server2 of moreservs) {
  94.             var hacktoolnum2 = 0;
  95.  
  96.             //attack server
  97.             if (!ns.hasRootAccess(server2)) {
  98.                ns.toast('Opening ports on ' + server2)
  99.                if (ns.fileExists('BruteSSH.exe', 'home')) {
  100.                   ns.brutessh(server2);
  101.                   hacktoolnum2++;
  102.                }
  103.                if (ns.fileExists('FTPCrack.exe', 'home')) {
  104.                   ns.ftpcrack(server2);
  105.                   hacktoolnum2++;
  106.  
  107.                }
  108.                if (ns.fileExists('relaySMTP.exe', 'home')) {
  109.                   ns.relaysmtp(server2);
  110.                   hacktoolnum2++;
  111.  
  112.                }
  113.                if (ns.fileExists('HTTPWorm.exe', 'home')) {
  114.                   ns.httpworm(server2);
  115.                   hacktoolnum2++;
  116.  
  117.                }
  118.                if (ns.fileExists('SQLInject.exe', 'home')) {
  119.                   ns.sqlinject(server2);
  120.                   hacktoolnum2++;
  121.  
  122.                }
  123.             }
  124.             if (ns.getServerNumPortsRequired(server2) <= hacktoolnum2 && !ns.hasRootAccess(server2)) {
  125.                ns.toast("nuking " + server2);
  126.                ns.nuke(server2);
  127.             } else
  128.                //if you still don't have access, skip
  129.                if (!ns.hasRootAccess(server2)) {
  130.                   ns.toast("unable to gain root to " + server2, "error");
  131.                   continue;
  132.                }
  133.             if (ns.getServerMaxRam(server2) > ns.getServerUsedRam(server2) + scriptram) {
  134.                //copy WORM script to server and run
  135.                if (!ns.fileExists('worm.ns', server2)) {
  136.                   ns.print('worm.ns being copied to ' + server2);
  137.                   await ns.scp('worm.ns', 'home', server2);
  138.                }
  139.                if (!ns.scriptRunning('worm.ns', server2) && !ns.scriptRunning('hackservers.ns', server2)) {
  140.                   ns.print('running worm on ' + server2);
  141.                   await ns.sleep(11000);
  142.                   await ns.scp('worm.ns', 'home', server2);
  143.                   if (ns.args.length === 1) {
  144.                      ns.exec('worm.ns', server2, 1, ns.args[0]);
  145.                   }
  146.                   if (ns.args.length === 2) {
  147.                      ns.exec('worm.ns', server2, 1, ns.args[0], ns.args[1]);
  148.                   }
  149.                   if (ns.args.length === 3) {
  150.                      ns.exec('worm.ns', server2, 1, ns.args[0], ns.args[1], ns.args[2]);
  151.                   }
  152.                   if (ns.args.length === 4) {
  153.                      ns.exec('worm.ns', server2, 1, ns.args[0], ns.args[1], ns.args[2], ns.args[3]);
  154.                   }
  155.                   if (ns.args.length === 5) {
  156.                      ns.exec('worm.ns', server2, 1, ns.args[0], ns.args[1], ns.args[2], ns.args[3], ns.args[4]);
  157.                   }
  158.                }
  159.  
  160.             }
  161.          }
  162.       }
  163.    }
  164.    //if usethreads exists for this script, build args array of parameters based on this scripts args
  165.    if (hsthreads) {
  166.       var hsargs = [];
  167.       for (const argument of ns.args) {
  168.          hsargs.push(argument);
  169.          hsargs.push(ns.getServerMinSecurityLevel(argument));
  170.          hsargs.push(ns.getServerMaxMoney(argument));
  171.          hsargs.push(ns.getServerRequiredHackingLevel(argument));
  172.       }
  173.       //copy hack script to this server and spawn script with threads and arguments as a single string
  174.       if (ns.getHostname() != 'home') {
  175.          await ns.scp('hackservers.ns', 'home', ns.getHostname());
  176.       }
  177.       ns.spawn('hackservers.ns', hsthreads, hsargs.toString());
  178.    }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement