Guest User

worm.js

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