-
Notifications
You must be signed in to change notification settings - Fork 7
/
Exec.cs
421 lines (354 loc) · 16.7 KB
/
Exec.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
413
414
415
416
417
418
419
420
421
using System;
using System.Linq;
using System.Net;
using System.IO;
using System.IO.Pipes;
using System.Net.Sockets;
using System.Threading;
using System.Runtime.InteropServices;
using System.Text;
// using DInvoke.DynamicInvoke;
// Shamelessly ported from do_exec.c in the original Windows netcat
namespace Sharpcat
{
public class Exec
{
[StructLayout(LayoutKind.Sequential)]
public struct STARTUPINFO
{
public int cb;
public IntPtr lpReserved;
public IntPtr lpDesktop;
public IntPtr lpTitle;
public int dwX;
public int dwY;
public int dwXSize;
public int dwYSize;
public int dwXCountChars;
public int dwYCountChars;
public int dwFillAttribute;
public int dwFlags;
public short wShowWindow;
public short cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}
[StructLayout(LayoutKind.Sequential)]
public struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public int dwProcessId;
public int dwThreadId;
}
[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
public int nLength;
public IntPtr lpSecurityDescriptor;
public bool bInheritHandle;
}
[StructLayout(LayoutKind.Sequential)]
public struct SESSION_DATA
{
public IntPtr ReadPipeHandle; // Handle to shell stdout pipe
public IntPtr WritePipeHandle; // Handle to shell stdin pipe
public IntPtr ProcessHandle; // Handle to shell process
public Socket ClientSocket;
public IntPtr StdinPipe;
public IntPtr StdoutPipe;
public IntPtr StdErrPipe;
}
public const int BUFFER_SIZE = 200;
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern bool CreateProcessW(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes,
IntPtr lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,
[In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool CreatePipe(ref IntPtr hReadPipe, ref IntPtr hWritePipe, IntPtr lpPipeAttributes, int nSize);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool CloseHandle(IntPtr handle);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool DuplicateHandle(IntPtr hProcessSource, IntPtr hSource, IntPtr hProcessDest, ref IntPtr hDest, int DesiredAccess, bool inherit, int options);
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetCurrentProcess();
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool PeekNamedPipe(IntPtr namedPipe, byte[] buffer, int buffersize, out int bytesread, out int bytesavailable, IntPtr bytesleft);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool DisconnectNamedPipe(IntPtr namedPipe);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool ReadFile(IntPtr filehandle, byte[] buffer, int readnumber, IntPtr bytesread, IntPtr overlapbytes);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool WriteFile(IntPtr filehandle, byte[] buffer, uint writenumber, out int byteswritten, IntPtr overlapbytes);
[DllImport("kernel32.dll", SetLastError = true)]
static extern int WaitForMultipleObjects(int nCount, IntPtr[] lpHandles, bool waitall, int waitms);
// static IntPtr StartShell(String commandline, SESSION_DATA pSession, IntPtr ShellStdinPipeHandle, IntPtr ShellStdoutPipeHandle)
static IntPtr StartShell(String commandline, SESSION_DATA pSession)
{
IntPtr processHandle = IntPtr.Zero;
IntPtr currentProcess;
STARTUPINFO si = new STARTUPINFO();
PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
// Code
// Console.WriteLine("Starting shell");
si.cb = Marshal.SizeOf(si);
si.lpReserved = IntPtr.Zero;
si.lpTitle = IntPtr.Zero;
si.lpDesktop = IntPtr.Zero;
si.dwX = 0;
si.dwXSize = 0;
si.dwY = 0;
si.dwYSize = 0;
si.dwFlags = 0x101; // STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW
si.wShowWindow = 0; // W_HIDE;
si.hStdInput = pSession.StdinPipe;
si.hStdOutput = pSession.StdoutPipe;
currentProcess = GetCurrentProcess();
// Console.WriteLine("Got current process handle {0}", currentProcess);
if (!DuplicateHandle(currentProcess, pSession.StdoutPipe, currentProcess, ref si.hStdError, 0, true, 0x2))
{
// Console.WriteLine("error duplicating handle");
return IntPtr.Zero;
} // DUPLICATE_SAME_ACCESS
// Console.WriteLine("Duplicated ShellStdoutPipeHandle handle {0}", si.hStdError);
if (!(CreateProcessW(null, commandline, IntPtr.Zero, IntPtr.Zero, true, 0, IntPtr.Zero, null, ref si, out pi)))
{
// bailout
// Console.WriteLine("Error creating process, error code: {0}, bailing out", Marshal.GetLastWin32Error());
return IntPtr.Zero;
}
// Console.WriteLine("Process created with PID: {0}.", pi.dwProcessId);
processHandle = pi.hProcess;
CloseHandle(pi.hThread);
return processHandle;
}
static bool CreateSession(String commandline, ref SESSION_DATA pSession)
{
SECURITY_ATTRIBUTES SecurityAttributes = new SECURITY_ATTRIBUTES();
IntPtr ShellStdinPipe = IntPtr.Zero;
IntPtr ShellStdoutPipe = IntPtr.Zero;
// Code
// Console.WriteLine("Creating Session");
SecurityAttributes.nLength = (int)System.Runtime.InteropServices.Marshal.SizeOf(SecurityAttributes);
SecurityAttributes.lpSecurityDescriptor = IntPtr.Zero;
SecurityAttributes.bInheritHandle = true;
IntPtr lpattr = Marshal.AllocHGlobal(Marshal.SizeOf(SecurityAttributes));
Marshal.StructureToPtr(SecurityAttributes, lpattr, true);
if (!CreatePipe(ref ShellStdinPipe, ref pSession.WritePipeHandle, lpattr, 0))
{
//Console.WriteLine("WritePipe creation error");
CloseHandle(ShellStdoutPipe);
CloseHandle(pSession.ReadPipeHandle);
return false;
}
pSession.StdinPipe = ShellStdinPipe;
// Console.WriteLine("write pipe created, starting shell {0}", commandline);
// Console.WriteLine("Creating read pipe");
if (!CreatePipe(ref pSession.ReadPipeHandle, ref ShellStdoutPipe, lpattr, 0))
{
// Console.WriteLine("ReadPipe creation error");
return false;
}
pSession.StdoutPipe = ShellStdoutPipe;
// Console.WriteLine("read pipe created, continuing wth write pipe");
//pSession.ProcessHandle = StartShell(commandline, pSession, ShellStdinPipe, ShellStdoutPipe);
pSession.ProcessHandle = StartShell(commandline, pSession);
CloseHandle(ShellStdoutPipe);
CloseHandle(ShellStdinPipe);
if (pSession.ProcessHandle == null)
{
Console.WriteLine("Error starting process");
CloseHandle(pSession.ReadPipeHandle);
CloseHandle(pSession.WritePipeHandle);
return false;
}
return true;
}
static void CleanUp( ref SESSION_DATA pSession)
{
//Console.WriteLine("Cleaning up");
if(pSession.StdinPipe != IntPtr.Zero) {
//Console.WriteLine("Closing StdinPipe");
DisconnectNamedPipe(pSession.StdinPipe);
CloseHandle(pSession.StdinPipe);
}
if (pSession.StdoutPipe != IntPtr.Zero) {
//Console.WriteLine("Closing StdoutPipe");
DisconnectNamedPipe(pSession.StdoutPipe);
CloseHandle(pSession.StdoutPipe);
}
if (pSession.ReadPipeHandle != IntPtr.Zero) {
//Console.WriteLine("Closing ReadPipeHandle");
DisconnectNamedPipe(pSession.ReadPipeHandle);
CloseHandle(pSession.ReadPipeHandle);
}
if (pSession.WritePipeHandle != IntPtr.Zero)
{
//Console.WriteLine("Closing WritePipeHandle");
DisconnectNamedPipe(pSession.WritePipeHandle);
CloseHandle(pSession.WritePipeHandle);
}
if (pSession.ProcessHandle != IntPtr.Zero)
{
//Console.WriteLine("Stopping process");
CloseHandle(pSession.ProcessHandle);
}
//Console.WriteLine("Cleaned up");
}
static void SessionReadShellThread(ref SESSION_DATA session )
{
var buffer = new byte[BUFFER_SIZE];
int bufsize;
int bytesread = 0, bytesavailable = 0;
var buffer2 = new byte[BUFFER_SIZE * 2 + 30];
int bytestowrite;
SocketError senderror;
// Code
bytesavailable = 0;
bufsize = BUFFER_SIZE;
while (PeekNamedPipe(session.ReadPipeHandle, buffer, bufsize, out bytesread, out bytesavailable, IntPtr.Zero))
{
int buffercount;
char prevchar = '\0';
if( bytesread <= 0 )
{
System.Threading.Thread.Sleep(50);
}
else
{
// Console.WriteLine("Processing buffer");
// readFile(session.ReadPipeHandle, buffer, bytesread, out realbytesread, IntPtr.Zero);
ReadFile(session.ReadPipeHandle, buffer, bytesread, IntPtr.Zero, IntPtr.Zero);
//bytestowrite = realbytesread;
//if (realbytesread > 0)
bytestowrite = bytesread;
if (bytesread > 0)
{
// Console.WriteLine("read buffer bytes {0}", bytesread);
// Console.Write(Encoding.Default.GetString(buffer, 0, bytesread));
bytestowrite = 0;
for (buffercount = 0; buffercount < bytesread; buffercount++)
{
//Console.Write("[{0:X}]", Convert.ToUInt32(buffer[buffercount]));
if (buffer[buffercount] == (byte)'\n' && prevchar != (byte)'\r')
{
buffer2[bytestowrite++] = (byte)'\r';
}
buffer2[bytestowrite++] = buffer[buffercount];
prevchar = (char)buffer[buffercount];
}
}
// send data
// Console.WriteLine("Received data from shell [{0}]", buffer2);
// Console.WriteLine("Send data over socket");
session.ClientSocket.Send(buffer2, 0, bytestowrite, 0, out senderror);
if (senderror != SocketError.Success)
{
//Console.WriteLine("Socket error {0}", senderror);
CleanUp(ref session);
return;
}
}
}
}
static void SessionWriteShellThread(ref SESSION_DATA session)
{
var buffer = new byte[BUFFER_SIZE+1];
int bufcnt = 0;
// int bufsize = BUFFER_SIZE;
int byteswritten = 0;
var recvbuf = new byte[1];
SocketError recverror;
// Code
while(session.ClientSocket.Connected)
{
session.ClientSocket.Receive(recvbuf, 0, 1, 0, out recverror);
if( recverror == SocketError.Success)
{
buffer[bufcnt++] = recvbuf[0];
if( recvbuf[0] == (byte)'\r' )
{
buffer[bufcnt++] = (byte)'\n';
}
if( bufcnt >= BUFFER_SIZE || recvbuf[0] == (byte)'\n' || recvbuf[0] == (byte)'\r')
{
// Console.WriteLine("Sending command [{0}] to server", Encoding.Default.GetString(buffer, 0, (int)bufcnt));
// Check for exit command
String command = Encoding.Default.GetString(buffer, 0, (int)bufcnt-1);
//Console.WriteLine("Checking command [{0}]", command);
if(String.Equals(command, "exit", StringComparison.OrdinalIgnoreCase))
{
//Console.WriteLine("Exit");
break;
}
WriteFile(session.WritePipeHandle, buffer, (uint)bufcnt, out byteswritten, IntPtr.Zero);
// Console.WriteLine("{0} bytes sent", byteswritten);
if( bufcnt > byteswritten)
{
Array.Copy(buffer, byteswritten, buffer, 0, bufcnt - byteswritten);
bufcnt -= byteswritten;
}
else
{
bufcnt = 0;
}
}
}
}
//Console.WriteLine("Receive socket disconnected");
CleanUp(ref session);
return;
}
//public bool DoExec(String commandline, Socket client)
public static bool DoExec(String commandline, ref Socket client)
{
//
SESSION_DATA session = new SESSION_DATA();
var HandleArray = new IntPtr[3];
//Console.WriteLine("DoExec with command {0}", commandline);
// Code
if(!CreateSession(commandline, ref session))
{
//Console.WriteLine("Cannot create session, bailing out");
return false;
}
//Console.WriteLine("Created session");
session.ClientSocket = client;
//Console.WriteLine("Starting reading pipe thread");
Thread readerThread = new Thread(() => SessionReadShellThread(ref session));
readerThread.Start();
//SessionReadShellThread(ref session);
//Console.WriteLine("thread started");
//Console.WriteLine("Starting writing pipe thread");
Thread writerThread = new Thread(() => SessionWriteShellThread(ref session));
writerThread.Start();
//SessionReadShellThread(ref session);
//Console.WriteLine("thread started");
//System.Threading.Thread.Sleep(50000);
//Console.WriteLine("Idling");
HandleArray[0] = session.ReadPipeHandle;
HandleArray[1] = session.WritePipeHandle;
HandleArray[2] = session.ProcessHandle;
int i = WaitForMultipleObjects(3, HandleArray, false, -1);
//Console.WriteLine("My snowflake is triggered!");
switch(i)
{
case 0:
//Console.WriteLine("ReadShellHandle");
break;
case 1:
//Console.WriteLine("WriteShellHandle");
break;
case 2:
//Console.WriteLine("ProcessHandle");
break;
default:
//Console.WriteLine("Something else broke me");
break;
}
return true;
}
}
}