-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoattack.js
More file actions
171 lines (146 loc) · 5.16 KB
/
autoattack.js
File metadata and controls
171 lines (146 loc) · 5.16 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
import {fetchServers} from 'serverdata.js'
import {runTerminalCommand} from 'terminalcmd.js'
import {autoConnect} from 'autoconnect.js'
export function getAttackTargetCount(ns) {
const serverResult = fetchServers(ns, "--attack", false);
return serverResult.length;
}
export function isServerNuked(ns, hostNameTarget) {
return ns.hasRootAccess(hostNameTarget) ? true : false;
}
export function isServerBackdoored(ns, targetHostname) {
return ns.getServer(targetHostname).backdoorInstalled ? true : false;
}
export function isServerNukePossible(ns, hostNameTarget, portsOpened) {
let serverRoot = ns.hasRootAccess(hostNameTarget);
let serverNumPortsRequired = ns.getServerNumPortsRequired(hostNameTarget);
return !serverRoot && portsOpened >= serverNumPortsRequired;
}
export function isServerBackdoorPossible(ns, targetHostname) {
let hasRoot = ns.hasRootAccess(targetHostname);
let isBackdoor = isServerBackdoored(ns, targetHostname);
let playerHackLvl = ns.getHackingLevel();
let serverRequiredHack = ns.getServerRequiredHackingLevel(targetHostname);
return (hasRoot && !isBackdoor && playerHackLvl >= serverRequiredHack) ? true : false;
}
export function openPortsTarget(ns, hostNameTarget, verbose) {
const portsRequired = ns.getServerNumPortsRequired(hostNameTarget);
let portsOpened = 0;
for(let exploitIdx = 0; exploitIdx < 5; exploitIdx++) {
let isPortOpened = false;
switch(exploitIdx) {
case 0: {
if(!ns.fileExists("SQLInject.exe")) {
continue;
}
isPortOpened = ns.sqlinject(hostNameTarget) ? true : false;
}
case 1:{
if(!ns.fileExists("HTTPWorm.exe")) {
continue;
}
isPortOpened = ns.httpworm(hostNameTarget) ? true : false;
}
case 2:{
if(!ns.fileExists("BruteSSH.exe")) {
continue;
}
isPortOpened = ns.brutessh(hostNameTarget) ? true : false;
}
case 3:{
if(!ns.fileExists("FTPCrack.exe")) {
continue;
}
isPortOpened = ns.ftpcrack(hostNameTarget) ? true : false;
}
case 4:{
if(!ns.fileExists("relaySMTP.exe")){
continue;
}
isPortOpened = ns.relaysmtp(hostNameTarget) ? true : false;
}
default: {
break; // out of range, break.
}
}
if(isPortOpened) {
portsOpened ++;
}
if(portsOpened >= portsRequired) {
break; // no need to open any more ports
}
}
if(verbose) {
ns.tprintf("%s -> openPortsTarget(hostNameTarget=%s, verbose=%s) = %i/%i port(s) opened.",
ns.getScriptName(), hostNameTarget, verbose, portsOpened, portsRequired
);
}
return portsOpened;
}
/**
1. auto-connects the path
2. runs backdoor on target server.
*/
export async function backdoorTarget(ns, targetHostname, skipCheck, verbose) {
await autoConnect(ns, targetHostname, verbose);
if(skipCheck || isServerBackdoorPossible(ns, targetHostname)) {
// attempt backdoor
await runTerminalCommand("backdoor");
if(verbose) {
ns.tprintf("%s -> backdoorTarget(targetHostname=%s, skipCheck=%s, verbose=%s) -> installing backdoor, please wait...",
ns.getScriptName(), targetHostname, skipCheck, verbose
);
}
while( !isServerBackdoored(ns, targetHostname) ) {
await ns.sleep(500);
}
if(verbose) {
let backdoorSuccess = isServerBackdoored(ns, targetHostname) ? true : false;
ns.tprintf("%s -> backdoorTarget(targetHostname=%s, skipCheck=%s, verbose=%s) -> backdoor install %s",
ns.getScriptName(), targetHostname, skipCheck, verbose, backdoorSuccess ? "successully." : "FAILED!"
);
}
} else {
// backdoor not possible or necessary.
if(verbose) {
ns.tprintf("%s -> backdoorTarget(targetHostname=%s, skipCheck=%s, verbose=%s) -> not possible / necessary.",
ns.getScriptName(), targetHostname, skipCheck, verbose
);
}
}
}
export async function attackTarget(ns, hostNameTarget, verbose) {
let portsOpened = 0;
if( !isServerNuked(ns, hostNameTarget)) {
portsOpened = openPortsTarget(ns, hostNameTarget, verbose);
}
if( isServerNukePossible(ns, hostNameTarget, portsOpened) ) {
let nukeSuccess = ns.nuke(hostNameTarget);
ns.tprintf("%s -> attackTarget(hostNameTarget=%s, verbose=%s) -> nuke %s",
ns.getScriptName(), hostNameTarget, verbose, nukeSuccess ? "successful." : "FAILED!"
);
}
if( isServerBackdoorPossible(ns, hostNameTarget) ) {
await backdoorTarget(ns, hostNameTarget, true, verbose);
}
}
export async function autoAttack(ns, verbose) {
const serverResult = fetchServers(ns, "--attack", verbose);
const myScriptName = ns.getScriptName();
if(verbose) {
ns.tprintf("%s -> found %i attack target(s)...", myScriptName, serverResult.length);
}
for(let i = 0; i < serverResult.length; i++) {
const targetHostName = serverResult[i];
if(verbose) {
ns.tprintf("%s -> autoAttack(verbose=%s) -> attacking target %i/%i",
myScriptName, verbose, i+1, serverResult.length);
}
await attackTarget(ns, targetHostName, verbose);
}
}
/** @param {NS} ns */
export async function main(ns) {
const verbose = true;
await autoAttack(ns, verbose);
}