-
Notifications
You must be signed in to change notification settings - Fork 0
/
worm.js
191 lines (170 loc) · 4.77 KB
/
worm.js
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
/** @param {NS} ns */
export async function main(ns) {
let initialHosts = ns.scan(); // Start scanning from home to get all connected hosts initially
//if(ns.args.length == 0){
//await refresh_servers(ns, initialHosts, true)
while (true) {
await refresh_servers(ns, initialHosts)
initialHosts = ns.scan(); // Start scanning from home to get all connected hosts initially
for (let host of initialHosts) {
//Inject surrounding hosts
if (!host.includes('gloat-') || !host.includes('home')) {
await inject(ns, host)
await attack_host(ns, host);
}
}
}
}
async function refresh_servers(ns, initialHosts) {
for (let host of initialHosts)
if (host != 'home') {
let has_root = await ns.hasRootAccess(host)
if (!has_root) await break_in(ns, host) //Try breaking in
await inject(ns, host)
}
}
async function attack_host(ns, hostname, prev_hostname = 'home') {
if(hostname != 'home') await do_attack(ns, hostname)
let hosts = ns.scan(hostname)
for (let host of hosts) {
if (!prev_hostname.includes(host)) await attack_host(ns, host, hostname)
}
}
async function do_attack(ns, hostname) {
await break_in(ns, hostname)
await do_the_dirty(ns, hostname)
}
async function do_the_dirty(ns, hostname) {
try {
await weaken_wrap(ns, hostname)
await hack_or_grow(ns, hostname)
} catch {
ns.print('couldnt do the thing')
}
}
async function inject(ns, hostname, redeploy = false) {
ns.print('Injecting & Executing...' + hostname)
let script_name = await ns.getScriptName()
let script_running = await ns.isRunning(script_name, hostname)
if (!script_running) {
await ns.scp(script_name, hostname);
//if(redeploy) await ns.scriptKill(script_name, hostname)
await execute(ns, hostname, script_name)
}
}
async function execute(ns, hostname, script_name) {
let max_ram = await ns.getServerMaxRam(hostname)
let used_ram = await ns.getServerUsedRam(hostname);
let free_ram = max_ram - used_ram;
let script_cost = await ns.getScriptRam(script_name)
let threads = Math.floor(free_ram / script_cost)
if (threads > 0)
await ns.exec(script_name, hostname, threads, hostname)
}
async function break_in(ns, hostname) {
await brutessh_wrap(ns, hostname)
await ftpcrack_wrap(ns, hostname)
await relaysmtp_wrap(ns, hostname)
// await sqlinject_wrap(ns, hostname)
// await httpworm_wrap(ns, hostname)
await nuke_wrap(ns, hostname)
}
async function nuke_wrap(ns, hostname) {
try {
await ns.nuke(hostname)
} catch {
ns.print('Nuke did not work')
}
}
async function brutessh_wrap(ns, hostname) {
try {
await ns.brutessh(hostname)
} catch {
ns.print('brutessh did not work')
}
}
async function ftpcrack_wrap(ns, hostname) {
try {
await ns.ftpcrack(hostname)
} catch {
ns.print('ftpcrack did not work')
}
}
async function relaysmtp_wrap(ns, hostname) {
try {
await ns.relaysmtp(hostname)
} catch {
ns.print('relaysmtp did not work')
}
}
async function sqlinject_wrap(ns, hostname) {
try {
await ns.sqlinject(hostname)
} catch {
ns.print('sqlinject did not work')
}
}
async function httpworm_wrap(ns, hostname) {
try {
await ns.httpworm(hostname)
} catch {
ns.print('httpworm did not work')
}
}
async function weaken_wrap(ns, hostname) {
try {
let i = 0;
while (await worth_weaken(ns, hostname)) {
await ns.weaken(hostname)
//if(i >= 500) break
i++
}
} catch {
ns.print('Weaken no work: ' + hostname)
}
}
async function worth_weaken(ns, hostname) {
let min_secur = await ns.getServerMinSecurityLevel(hostname)
let curr_secur = await ns.getServerSecurityLevel(hostname)
let sec = 60
let mili = 1000 * sec
return (curr_secur > min_secur && await worth_hax(ns, hostname))
}
async function hack_or_grow(ns, hostname) {
await grow_host(ns, hostname)
await hax(ns, hostname)
}
async function grow_host(ns, hostname) {
try {
let i = 0;
while (await worth_grow(ns, hostname)) {
await ns.grow(hostname)
//Break out after 500 iterations, just incase there's something
//if(i >= 500) break
i++
}
} catch {
ns.print('Grow no work: ' + hostname)
}
}
async function worth_grow(ns, hostname) {
return (!await worth_hax(ns, hostname) && ns.getServerGrowth(hostname) > 1)
}
async function hax(ns, hostname) {
try {
let i = 0
while(await worth_hax(ns, hostname)) {
await ns.hack(hostname);
//if(i >= 500) break
i++
}
} catch {
ns.print('Hack no work: ' + hostname);
}
}
async function worth_hax(ns, hostname) {
let max_money = await ns.getServerMaxMoney(hostname)
let avail_money = await ns.getServerMoneyAvailable(hostname)
//let player_money = await ns.getServerMoneyAvailable('home')
return avail_money/max_money > 0.4
}