-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserverside_payload_generation.cna
More file actions
284 lines (256 loc) · 12.9 KB
/
serverside_payload_generation.cna
File metadata and controls
284 lines (256 loc) · 12.9 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
debug(7 | 34); # 34 is required for try/catch
include(script_resource("../cs_rest_api_lib.cna"));
popup payloads_server {
item("Stager Payload Generator", {open_serverside_stager_payload_generator_dialog();});
item("Stageless Payload Generator", {open_serverside_stageless_payload_generator_dialog();});
item("Download payload", {open_serverside_payload_download_dialog();});
}
menubar("Server-Side &Payloads", "payloads_server");
sub open_serverside_stageless_payload_generator_dialog {
local('$dialog');
sub callback {
if ($2 eq "Generate") {
if ($3["listener"] eq "") {
show_message("Please specify a listener!");
return;
}
generate_stageless_payload($3);
}
else {
exit();
}
}
@available_udrls = sorta(values(get_artifacts_list_by_type("udrls","^.*(?:\.cna|\.CNA)$")));
add(@available_udrls, "None", 0); # Add empty option
@available_sleepmasks = sorta(values(get_artifacts_list_by_type("sleepmasks","^.*(?:\.cna|\.CNA)$")));
add(@available_sleepmasks, "None", 0); # Add empty option
$dialog = dialog("Payload generator (Stageless)", %(listener => "", guardrails => "IP= User= Server= Domain=", options => '"sleep_mask_name"="default-sleepmask" "loader_name"="obfuscation-loader"', output => "Raw", udrlCNA => "None", sleepmaskCNA => "None", exit_function => "Process", system_call_method => "None", http_library => "winhttp", dns_comm_mode => "dns", x64 => "true"), &callback);
dialog_description($dialog, "This dialog exports a Cobalt Strike stageless Beacon. Several output options are available.")
drow_text($dialog, "filename", "Filename: ");
drow_listener($dialog, "listener", "Listener: ");
drow_text_big($dialog, "guardrails", "Guardrails: ");
drow_combobox($dialog, "output", "Output: ", @("C", "C#", "Java", "Perl", "Python", "Raw", "Ruby", "VBA"));
drow_combobox($dialog, "udrlCNA", "UDRL Aggressor Script: ", @available_udrls);
drow_combobox($dialog, "sleepmaskCNA", "Sleepmask Aggressor Script: ", @available_sleepmasks);
drow_text_big($dialog, "options", "Options: ");
drow_combobox($dialog, "exit_function", "Exit Function: ", @("Process", "Thread"));
drow_combobox($dialog, "system_call_method", "System Call: ", @("None", "Direct", "Indirect"));
drow_combobox($dialog, "http_library", "HTTP Library: ", @("wininet", "winhttp"));
drow_combobox($dialog, "dns_comm_mode", "DNS Comm Mode: ", @("dns", "dns-over-http"));
drow_checkbox($dialog, "x64", "x64: ", "Use x64 payload");
dbutton_action($dialog, "Generate");
dbutton_help($dialog, "https://github.com/Cobalt-Strike/sleep2rest/blob/main/examples/Readme_serverside_payload_generation.md#stageless-payload-generator");
dialog_show($dialog);
}
sub open_serverside_stager_payload_generator_dialog {
local('$dialog');
sub callback {
if ($3["listener"] eq "") {
show_message("Please specify a listener!");
exit();
}
generate_stager_payload($3);
}
$dialog = dialog("Stager Payload Generator", %(listener => "", x64 => "true"), &callback);
dialog_description($dialog, "This dialog generates a payload to stage a Cobalt Strike listener. Several output options are available.")
drow_text($dialog, "filename", "Filename: ");
drow_listener_stage($dialog, "listener", "Listener: ");
drow_combobox($dialog, "output", "Output: ", @("C", "C#", "COM Scriptlet", "Java", "Perl", "PowerShell", "PowerShell Command", "Python", "Raw", "Ruby", "VBA", "Veil"));
drow_checkbox($dialog, "x64", "x64: ", "Use x64 payload");
dbutton_action($dialog, "Generate");
dbutton_help($dialog, "https://github.com/Cobalt-Strike/sleep2rest/blob/main/examples/Readme_serverside_payload_generation.md#stager-payload-generator");
dialog_show($dialog);
}
sub open_serverside_payload_download_dialog {
local('$dialog');
sub callback {
save_local_payload_file($3["filename"]);
}
@generated_payloads = keys(get_artifacts_list_by_type("generated","^.*(?<!\.info)$"));
if (size(@generated_payloads) == 0) {
show_message("No server-side generated payloads found on the server.");
exit();
}
$dialog = dialog("Download Payload", %(filename => ""), &callback);
dialog_description($dialog, "This dialog allows you to download a server-side generated payload on the client machine.")
drow_combobox($dialog, "filename", "Filename: ", sorta(@generated_payloads));
dbutton_action($dialog, "Download");
dbutton_help($dialog, "https://github.com/Cobalt-Strike/sleep2rest/blob/main/examples/Readme_serverside_payload_generation.md#download-payload");
dialog_show($dialog);
}
sub generate_stageless_payload {
local('$params $filename $useListenerGuardRails $guardrails $guardrails_struct $options $listener $isx64 $output $udrlCNA $sleepmaskCNA $exit_function $system_call_method $http_library $dns_comm_mode $arch $body $response_post');
$params = $1;
$filename = $params['filename'];
$listener = $params['listener'];
$output = $params['output'];
$udrlCNA = iff($params['udrlCNA'] eq "None", "", $params['udrlCNA']);
$sleepmaskCNA = iff($params['sleepmaskCNA'] eq "None", "", $params['sleepmaskCNA']);
$exit_function = $params['exit_function'];
$system_call_method = $params['system_call_method'];
$http_library = $params['http_library'];
$dns_comm_mode = $params['dns_comm_mode'];
$isx64 = $params['x64'];
$arch = iff($isx64 eq "true", "x64", "x86");
$guardrails = $params['guardrails'];
$options = $params['options'];
@guardrails_split = split(" ", $guardrails);
if (size(@guardrails_split) == 0) {
$guardrails_struct = "";
}
else {
$guardrails_struct = "\"guardRails\": {";
foreach $part (@guardrails_split) {
$guardrail_item = split("=", $part);
$key = iff($guardrail_item[0] ne $null,strrep($guardrail_item[0],'"',""),$null);
$value = iff($guardrail_item[1] ne $null,strrep($guardrail_item[1],'"',""),$null);
if($key eq "IP" && ($value ne "" && $value !is $null)) {
$guardrails_struct = $guardrails_struct . '"ipAddress": "' . $value . '",';
}
else if($key eq "User" && ($value ne "" && $value !is $null)) {
$guardrails_struct = $guardrails_struct . '"userName": "' . $value . '",';
}
else if($key eq "Server" && ($value ne "" && $value !is $null)) {
$guardrails_struct = $guardrails_struct . '"serverName": "' . $value . '",';
}
else if($key eq "Domain" && ($value ne "" && $value !is $null)) {
$guardrails_struct = $guardrails_struct . '"domain": "' . $value . '",';
}
}
$guardrails_struct = $guardrails_struct . "},";
$guardrails_struct = strrep($guardrails_struct, ",}", "}");
}
$guardrails_struct = iff($guardrails_struct eq "\"guardRails\": {},", "", $guardrails_struct);
$useListenerGuardRails = iff($guardrails_struct eq "", "true", "false");
@options_split = matches($options,'((?:\"[^\"]+\"|\S+)=(?:\"[^\"]+\"|\S+))');
if (size(@options_split) == 0) {
$options_struct = "";
}
else {
$options_struct = ",\"options\": {";
foreach $part (@options_split) {
$options_item = split("=", $part);
if ($options_item[1] ne "" && $options_item[1] ne '""' && $options_item[1] !is $null) {
$options_struct = $options_struct . '"' . strrep($options_item[0],'"',"") . '": "' . strrep($options_item[1],'"',"") . '",';
}
}
$options_struct = $options_struct . "}";
$options_struct = strrep($options_struct, ",}", "}");
}
$body = '{
"listenerName": "' . $listener . '",
"useListenerGuardRails": ' . $useListenerGuardRails . ',
' . $guardrails_struct . '
"architecture": "' . $arch . '",
"output": "' . $output . '",
"exitFunction": "' . $exit_function . '",
"systemCallMethod": "' . $system_call_method . '",
"httpLibrary": "' . $http_library . '",
"dnsCommMode": "' . $dns_comm_mode . '",
"payloadFileName": "' . $filename . '",
"sleepMaskCNA": "' . $sleepmaskCNA. '",
"udrlCNA": "' . $udrlCNA. '"
' . $options_struct . '
}';
println("Generating Stageless Payload with body: " . $body);
$response_post = apiPOST("/api/v1/payloads/generate/stageless", $body);
println("POST Response: " . $response_post);
if ($response_post["status"] == 200) {
println("Payload generated successfully.");
open_save_file_dialog($filename);
#show_message("Payload '" . $filename . "' generated successfully.\nCheck the artifacts folder to view it.");
} else {
println("Payload generation failed.");
show_error("Payload generation failed.\nCheck the Aggressor Script console for details.");
}
}
sub generate_stager_payload {
local('$params $filename $listener $output $isx64 $arch $body $response_post');
$params = $1;
$filename = $params['filename'];
$listener = $params['listener'];
$output = $params['output'];
$isx64 = $params['x64'];
$arch = iff($isx64 == "true", "x64", "x86");
$body = '{
"listenerName": "' . $listener . '",
"architecture": "' . $arch . '",
"output": "' . $output . '",
"payloadFileName": "' . $filename . '"
}';
println("Generating Stager Payload with body: " . $body);
$response_post = apiPOST("/api/v1/payloads/generate/stager", $body);
println("POST Response: " . $response_post);
if ($response_post["status"] == 200) {
println("Payload generated successfully.");
open_save_file_dialog($filename);
#show_message("Payload '" . $filename . "' generated successfully.\nCheck the artifacts folder to view it.");
} else {
$error_msg = iff($response_post["content"]['message'] !is $null, $response_post["content"]['message'], "Unknown error");
$error_msg_details = iff($response_post["content"]['errors'] !is $null, $response_post["content"]['errors'], "Unknown error");
show_error("Payload generation failed: " . $error_msg . ".\nDetails: " . $error_msg_details);
}
}
sub save_local_payload_file {
global('$filename');
$filename = $1;
println("Downloading payload file: " . $filename);
prompt_file_save($filename,{
println("Payload generated successfully.");
$response_get_file = apiGET("/api/v1/payloads/" . $filename);
if ($response_get_file["status"] == 200 && $response_get_file["isFileDownload"] == 1) {
# It's a binary stream
$handle = openf(">" . $1);
writeb($handle, $response_get_file["content"]);
closef($handle);
println("File Downloaded to: " . $1);
show_message("Payload '" . $filename . "' downloaded successfully to " . $1 . ".");
} else {
println("Failed to download the file. Status: " . $response_get_file["status"]);
show_error("Failed to download the payload file.\nCheck the Aggressor Script console for details.");
}
});
}
sub open_save_file_dialog {
sub callback {
if ($2 eq "Yes") {
save_local_payload_file($3["filename"]);
}
else {
exit();
}
}
$filename = $1;
$dialog = dialog("Download Payload", %(filename => $filename), &callback);
dialog_description($dialog, "The payload has been stored server-side. Do you want to download the generated payload file?");
dbutton_action($dialog, "Yes");
dbutton_action($dialog, "No");
dbutton_help($dialog, "https://github.com/Cobalt-Strike/sleep2rest");
dialog_show($dialog);
}
####### Auxiliary Functions for Serverside Artifact Execution ########
sub get_artifacts_list_by_type {
local('$artifact_type $filter_regex $response_get_artifacts', '$artifacts_list', '@artifact_names', '$artifact', '$artifact_file_name', '$dialog');
$artifact_type = $1;
if (size(@_) == 1) {
$filter_regex = $null;
}
else if (size(@_) == 2) {
$filter_regex = $2;
}
$response_get_artifacts = apiGET("/api/v1/artifacts");
if ($response_get_artifacts["status"] == 200){
$artifacts_list = $response_get_artifacts["content"];
%artifact_names = %();
foreach $artifact ($artifacts_list) {
$artifact_file_name = split('/',$artifact["key"]);
if ($artifact_file_name[0] eq $artifact_type && ($filter_regex is $null || $artifact["key"] ismatch $filter_regex)) {
%artifact_names[$artifact_file_name[1]] = $artifact["symbolicReference"];
}
}
return %artifact_names;
} else {
show_error("Failed to retrieve artifacts list from server.\nCheck the Aggressor Script console for details.");
exit();
}
}