-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhackOnRemote.js
More file actions
85 lines (79 loc) · 2.51 KB
/
hackOnRemote.js
File metadata and controls
85 lines (79 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/** @param {NS} ns
* 0=target network
* 1=true=use ownedservers, false=use remoteservers
* 2=distance: number
*
**/
export async function main(ns) {
var hosts = [];
if (!!ns.args[1]) {
hosts = JSON.parse(ns.read('ownedServers.txt'));
}
else {
hosts = JSON.parse(ns.read('remoteServers.txt'));
}
var target = ns.args[0];
if (!await OpenPorts(ns, { name: target })) {
ns.tprint('failed to open ports on ' + target);
return;
}
for (let host of hosts) {
if (host.hackingSkillReq > ns.getHackingLevel() || host.distance > ns.args[2] || host.RAM < 4 || !(await OpenPorts(ns, host)))
continue;
/**
* check memory amount used by script
* divide memory on server by that amount, floor it, run with that many threads
* getServerMemory(host)
**/
var numThreads = Math.floor(ns.getServerMaxRam(host.name) / ns.getScriptRam('basicHack.js', 'home'));
// scp the script to the server and exec() it
if (host.name !== 'home') {
await ns.scp('basicHack.js', 'home', host.name);
ns.killall(host.name);
await ns.sleep(2000);
ns.exec('basicHack.js', host.name, numThreads, target);
}
else {
await ns.sleep(2000);
ns.exec('basicHack.js', host.name, numThreads - 15, target);
}
}
}
/** @param {NS} ns **/
var OpenPorts = async function (ns, host) {
if (host.name === 'home' || !!ns.args[1] || ns.hasRootAccess(host.name))
return true;
var openedPorts = 0;
if (ns.fileExists("BruteSSH.exe", "home")) {
ns.brutessh(host.name);
await ns.sleep(100);
openedPorts++;
}
if (ns.fileExists("FTPCrack.exe", "home")) {
ns.ftpcrack(host.name);
await ns.sleep(100);
openedPorts++;
}
if (ns.fileExists("SQLInject.exe", "home")) {
ns.sqlinject(host.name);
await ns.sleep(100);
openedPorts++;
}
if (ns.fileExists("HTTPWorm.exe", "home")) {
ns.httpworm(host.name);
await ns.sleep(100);
openedPorts++;
}
if (ns.fileExists("relaySMTP.exe", "home")) {
ns.relaysmtp(host.name);
await ns.sleep(100);
openedPorts++;
}
if (!ns.hasRootAccess(host.name) && openedPorts >= ns.getServerNumPortsRequired(host.name)) {
ns.nuke(host.name);
await ns.sleep(500);
return true;
}
ns.tprint('failed to nuke server: ' + host.name);
return false;
}