-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauto-spread-v2.js
More file actions
262 lines (214 loc) · 7.22 KB
/
auto-spread-v2.js
File metadata and controls
262 lines (214 loc) · 7.22 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**Auto-Spread v2
* Will seek out, root, copy files to, and begin scripts for all servers in the networks.
* It will keep searching for new servers until you have all port opening programs to do so.
* This is how the coordinator is ran. This is also where it gets the starting list of targets.
*
* Modified By: Zharay
* Original Spreader By: KrunoSaho
* Original URL: https://gist.github.com/KrunoSaho/f0aa418e16e828b0ebc0585d1ebcf6b5
* Mod URL: https://github.com/Zharay/BitburnerBotnet/blob/main/corpo.js
**/
/** @param {NS} ns **/
export async function main(ns) {
ns.disableLog("ALL");
// Options
const useArgsToCoord = false; // Use a traditional argument pass to the coordinator instead of a port (old method)
var initialSpread = true;
var serverHistory = [];
var unrootedServers = [];
// Loop until sent kill command
while (ns.peek(20) == "NULL PORT DATA") {
ns.print(" ");
ns.print("------------------------------------");
ns.print(" ");
// Collect all the servers recursively
let [servers, serverCons] = collect_server_names(ns);
// Figure out what we can hack
var maxPorts = 0;
if (ns.fileExists("BruteSSH.exe", "home"))
maxPorts++;
if (ns.fileExists("FTPCrack.exe", "home"))
maxPorts++;
if (ns.fileExists("relaySMTP.exe", "home"))
maxPorts++;
if (ns.fileExists("HTTPWorm.exe", "home"))
maxPorts++;
if (ns.fileExists("SQLInject.exe", "home"))
maxPorts++;
// Filter out home for everything else
servers = servers.filter(x => !x.includes("home"));
// Filter any servers found in previous runs and then add in our unrooted servers
servers = servers.filter(s => !serverHistory.includes(s));
if (initialSpread) serverHistory = servers;
else servers = servers.filter(x => !x.includes("pserv"));
servers = servers.concat(unrootedServers);
unrootedServers.length = 0;
// This only should happen is if ports we can handle < 5
if (servers.length == 0) {
ns.print(`No new servers found. Sleeping for 1 minutes... (Max Ports: ${maxPorts})`);
await ns.sleep(1000 * 60 * 1);
continue;
} else if (servers.length > 0) {
serverHistory = serverHistory.concat(servers);
}
if (ns.getHostname() == "home" && initialSpread) {
ns.print("Posting Servers to Port 8...")
await ns.tryWritePort(8 , servers.filter(x => !x.includes("pserv")).toString());
ns.print("Running coordinator...");
if (useArgsToCoord)
ns.exec("coordinator.js", "home", 1, servers.filter(x => !x.includes("pserv")).toString());
else
ns.exec("coordinator.js", "home", 1);
ns.print("Running status script...");
ns.exec("check-status.js", "home", 1);
ns.print("Waiting 10 seconds for coordinator to settle...")
await ns.sleep(10000);
}
// Open their ports + nuke them
ns.print("Attempting to root servers... ");
servers.forEach((s) => {
if (s.includes("pserv")) return;
ns.print("Server found: " + s);
if (ns.hasRootAccess(s)) {
ns.print(`[${s}] is already rooted!`);
return;
}
if (ns.getServerNumPortsRequired(s) > maxPorts) {
ns.print(`[${s}] cannot be rooted at this time. (Ports: ${maxPorts} / ${ns.getServerNumPortsRequired(s)})`);
if (!unrootedServers.includes(s)) unrootedServers.push(s);
return;
}
if (ns.fileExists("BruteSSH.exe", "home")) {
ns.print("Brute forcing SSH...");
ns.brutessh(s);
}
if (ns.fileExists("FTPCrack.exe", "home")) {
ns.print("FTP Cracking...");
ns.ftpcrack(s);
}
if (ns.fileExists("relaySMTP.exe", "home")) {
ns.print("Relaying SMTP...");
ns.relaysmtp(s);
}
if (ns.fileExists("HTTPWorm.exe", "home")) {
ns.print("Inserting HTTP Worm...");
ns.httpworm(s);
}
if (ns.fileExists("SQLInject.exe", "home")) {
ns.print("Injecting SQL...");
ns.sqlinject(s);
}
ns.print("Nuking...");
ns.nuke(s);
// Update filter list to not include this nuked server
unrootedServers = unrootedServers.filter(f => f != s);
});
ns.print(" ");
ns.print("------------------------------------");
ns.print(" ");
// We only want servers that have root access for the rest.
servers = servers.filter(x => ns.hasRootAccess(x));
for (var s of servers) {
if (!ns.hasRootAccess(s)) continue;
ns.print(`[${s}] is rooted! Setting it up...`);
// Killall scripts
ns.print(`[${s}]KillAll command sent.`);
ns.killall(s);
await ns.sleep(1000);
// Transfer files to server.
var files = ["hack-daemon.js", "easy-hack.js", "weaken.js", "grow.js", "hack.js", "shareCPU.js"];
ns.print(`[${s}] Copying files...`);
var success = await ns.scp(files, "home", s);
ns.print((success ? "Successfully transferred." : `ERROR: Transfer to [${s}] failed!`));
// Run hacker-daemon/easy-hack
if (ns.getServerMaxRam(s) < 8) {
ns.print(`[${s}] Beginning easy-hack...`);
ns.exec("easy-hack.js", s, Math.max(Math.floor(ns.getServerMaxRam(s) / ns.getScriptRam("easy-hack.js")), 1));
} else {
ns.print(`[${s}] Beginning hack-daemon...`);
ns.exec("hack-daemon.js", s, 1);
}
}
// Backdoor
/*servers.forEach((s) => {
if (ns.hasRootAccess(s)) {
// Connect to each server
let path = find_path_to_home(s, serverCons);
for (let server of path) {
ns.connect(server);
}
await ns.installBackdoor();
ns.connect("home");
}
});*/
ns.print(" ");
ns.print("------------------------------------");
ns.print(" ");
// All is done. Time to hack things.
if (initialSpread && ns.getServerMaxRam(ns.getHostname()) < 8) {
ns.print("[home] Beginning easy-hack...");
ns.exec("easy-hack.js", "home", Math.max(Math.floor(ns.getServerMaxRam(ns.getHostname()) / ns.getScriptRam("easy-hack.js")), 1));
} else if (initialSpread) {
ns.print("[home] Beginning hack-daemon...");
ns.exec("hack-daemon.js", "home", 1);
}
initialSpread = false;
if (maxPorts < 5) {
ns.print("Sleeping for 1 minutes...");
await ns.sleep(1000 * 60 * 1);
} else {
ns.print("All servers should be rooted.")
return;
}
}
}
function collect_server_names(ns) {
let fromServers = ['home'];
let checkedServers = [];
let serverConnections = new Map();
for (let i = 0; i < 10000; i++) { // 'infinite' loop
if (fromServers.length == 0) {
break;
}
let server = fromServers.pop();
checkedServers.push(server);
serverConnections.set(server, []);
for (let conServer of ns.scan(server)) {
//if (conServer == ".") { continue; }
serverConnections.get(server)
.push(conServer);
if (!checkedServers.includes(conServer)) {
fromServers.push(conServer);
}
}
}
checkedServers.shift(); // remove home
return [checkedServers, serverConnections];
}
/**
* @param {string} targetServer
* @param {Map<string, string[]>} serverCons
* **/
function find_path_to_home(targetServer, serverCons) {
let path = [];
let target = targetServer;
// check every value for targetServer, store the key
for (let i = 0; i < 100; i++) { // 'infinite' loop
if (target == 'home') {
break;
}
find_keys: {
for (let server of serverCons.keys()) {
let serversToSearch = serverCons.get(server);
if (serversToSearch.includes(target)) {
if (!path.includes(server)) {
path.unshift(server);
target = server;
}
break find_keys;
}
}
}
}
return path;
}