-
Notifications
You must be signed in to change notification settings - Fork 29
/
autoconf.cpp
572 lines (527 loc) · 21.5 KB
/
autoconf.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
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#include "dpitunnel-cli.h"
#include "autoconf.h"
#include "dns.h"
#include "desync.h"
#include "socket.h"
#include "ssl.h"
#include "utils.h"
#include <algorithm>
#include <atomic>
#include <arpa/inet.h>
#include <cerrno>
#include <chrono>
#include <cstring>
#include <iostream>
#include <vector>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <openssl/x509v3.h>
#include <sys/socket.h>
#include <thread>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <unistd.h>
extern struct Profile_s Profile;
extern struct Settings_perst_s Settings_perst;
bool verify_cert_common_name(X509 *server_cert, std::string host) {
const auto subject_name = X509_get_subject_name(server_cert);
if (subject_name != nullptr) {
char name[254];
auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName,
name, sizeof(name));
if (name_len != -1)
return check_host_name(name, static_cast<size_t>(name_len), host);
}
return false;
}
bool verify_cert_subject_alt_name(X509 *server_cert, std::string host) {
auto ret = false;
auto alt_names = static_cast<const struct stack_st_GENERAL_NAME *>(
X509_get_ext_d2i(server_cert, NID_subject_alt_name, NULL, NULL));
if (alt_names) {
auto dns_matched = false;
auto count = sk_GENERAL_NAME_num(alt_names);
for (decltype(count) i = 0; i < count && !dns_matched; i++) {
auto val = sk_GENERAL_NAME_value(alt_names, i);
if (val->type == GEN_DNS) {
auto name = (const char *) ASN1_STRING_get0_data(val->d.ia5);
auto name_len = (size_t) ASN1_STRING_length(val->d.ia5);
dns_matched = check_host_name(name, name_len, host);
}
}
ret = dns_matched;
}
GENERAL_NAMES_free((STACK_OF(GENERAL_NAME) *) alt_names);
return ret;
}
bool verify_cert(X509 *server_cert, std::string host) {
return verify_cert_subject_alt_name(server_cert, host) ||
verify_cert_common_name(server_cert, host);
}
int check_https_response(int socket, std::string host, std::string ip, int port, int local_port,
const std::string &sniffed_packet, SSL_CTX *ctx, X509_STORE *store) {
BIO *rbio = BIO_new(BIO_s_mem());
BIO *wbio = BIO_new(BIO_s_mem());
SSL *ssl = SSL_new(ctx);
SSL_set_connect_state(ssl);
SSL_set_bio(ssl, rbio, wbio);
SSL_set_tlsext_host_name(ssl, host.c_str());
SSL_set_verify(ssl, SSL_VERIFY_NONE, NULL);
int res = 0;
unsigned int last_char;
size_t offset = 0;
bool is_first_time = true; // apply desync attack only on ClientHello
bool is_failure = false;
std::string buffer(Profile.buffer_size, ' ');
auto start = std::chrono::high_resolution_clock::now();
while (SSL_do_handshake(ssl) == -1) {
res = BIO_read(wbio, &buffer[0], buffer.size());
if (res > 0) {
if (is_first_time) {
// Split packet at the middle of SNI or at user specified position
unsigned int sni_start, sni_len;
unsigned int split_pos;
// If it's https connection
if (Profile.split_at_sni) {
get_tls_sni(buffer, res, sni_start, sni_len);
if (sni_start + sni_len > res || sni_start == 0 || sni_len == 0)
split_pos = Profile.split_position;
else
split_pos = sni_start + sni_len / 2;
} else
split_pos = std::min((int) Profile.split_position, res);
if (do_desync_attack(socket, ip, port, local_port,
true, sniffed_packet,
buffer, res, split_pos) == -1) {
SSL_free(ssl);
close(socket);
return -1;
}
// Send packet to synchronize SEQ/ACK
std::string data_empty(res, '\x00');
if (Profile.desync_first_attack == DESYNC_FIRST_NONE) {
if (send_string(socket, data_empty, res) == -1) {
SSL_free(ssl);
close(socket);
return -1;
}
} else {
if (send_string(socket, data_empty, split_pos) == -1 ||
send_string(socket, data_empty, res - split_pos) == -1) {
SSL_free(ssl);
close(socket);
return -1;
}
}
is_first_time = false;
} else {
if (send_string(socket, buffer, res) == -1) {
SSL_free(ssl);
close(socket);
return -1;
}
}
} else {
if (recv_string(socket, buffer, last_char) == -1) {
SSL_free(ssl);
close(socket);
return -1;
}
offset = 0;
while (last_char - offset != 0) {
res = BIO_write(rbio, &buffer[0] + offset, last_char);
if (res <= 0) {
std::cerr << "BIO write failure" << std::endl;
SSL_free(ssl);
close(socket);
return -1;
}
offset += res;
}
}
// Check timeout
auto stop = std::chrono::high_resolution_clock::now();
if (std::chrono::duration_cast<std::chrono::seconds>(stop - start).count() >
Settings_perst.test_ssl_handshake_timeout) {
std::cout << "SSL handshake timeout" << std::endl;
SSL_free(ssl);
close(socket);
return -1;
}
}
// Verify certificate
if (SSL_get_verify_result(ssl) != X509_V_OK) {
std::cout << "Failed to verify server certificate" << std::endl;
SSL_free(ssl);
close(socket);
return -1;
}
auto server_cert = SSL_get_peer_certificate(ssl);
if (server_cert == NULL) {
std::cout << "Failed to verify server certificate" << std::endl;
SSL_free(ssl);
close(socket);
return -1;
}
if (!verify_cert(server_cert, host)) {
X509_free(server_cert);
std::cout << "Failed to verify server certificate" << std::endl;
SSL_free(ssl);
close(socket);
return -1;
}
X509_free(server_cert);
SSL_free(ssl);
close(socket);
return 0;
}
int check_http_response(int socket, std::string host, std::string ip, int port, int local_port,
const std::string &sniffed_packet, unsigned int connect_time) {
unsigned int last_char;
std::string buffer(Profile.buffer_size, ' ');
// Receive with timeout
struct timeval timeout_recv;
timeout_recv.tv_sec = 5;
timeout_recv.tv_usec = 0;
std::string request;
request += "GET / HTTP/1.1\r\nHost: ";
request += host;
request += "\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0\r\n"
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*" "/" "*;q=0.8\r\n"
"Accept-Encoding: gzip, deflate\r\n\r\n";
unsigned int split_pos = std::min(Profile.split_position, (unsigned int) request.size());
if (do_desync_attack(socket, ip, port, local_port,
true, sniffed_packet, request, request.size(), split_pos) == -1) {
close(socket);
return -1;
}
unsigned int receive_time;
if (recv_string(socket, buffer, last_char, &timeout_recv, &receive_time) == -1) {
close(socket);
return -1;
}
close(socket);
if (last_char == 0)
return -1;
// Count factors indicating that packet was send by DPI
unsigned short factors = 0;
// Check time
if (receive_time < connect_time * 2 / 3)
factors++;
// Check status code
size_t status_start_position = buffer.find(' ');
if (status_start_position == std::string::npos || status_start_position == buffer.size() - 1) {
std::cout << "Failed to parse server response" << std::endl;
return -1;
}
size_t status_end_position = buffer.find(' ', status_start_position + 1);
if (status_end_position == std::string::npos) {
std::cout << "Failed to parse server response" << std::endl;
return -1;
}
std::string code = buffer.substr(status_start_position + 1,
status_end_position - status_start_position - 1);
if (code == "301" || code == "302" || code == "303" || code == "307" || code == "308")
factors++;
// Check location
size_t location_start_position = buffer.find("Location: ");
if (location_start_position != std::string::npos ||
location_start_position == buffer.size() - 1) {
size_t location_end_position = buffer.find("\r\n", location_start_position + 1);
if (location_end_position != std::string::npos) {
std::string redirect_url = buffer.substr(location_start_position + 10,
location_end_position -
location_start_position - 10);
if (redirect_url.rfind("http://", 0) == 0)
redirect_url.erase(0, 7);
size_t slash_position = redirect_url.find('/');
redirect_url.erase(slash_position);
if (redirect_url.rfind(host, 0) != 0)
factors++;
}
}
if (factors >= 2)
return -1;
else
return 0;
}
int test_desync_attack(std::string host, std::string ip, int port, bool is_https, SSL_CTX *ctx,
X509_STORE *store) {
// Connect to server to check is it blocked by ip and get SYN, ACK packet need for desync attacks
int socket;
std::atomic<bool> flag(true);
std::atomic<int> local_port(-1);
std::atomic<int> status;
std::thread sniff_thread;
std::promise<void> sniff_thread_ready = std::promise<void>();
std::string sniffed_packet;
sniff_thread = std::thread(sniff_handshake_packet, &sniffed_packet,
ip, port, &local_port, &flag, &status, &sniff_thread_ready);
// Wait for sniff thread to init
sniff_thread_ready.get_future().wait();
auto start = std::chrono::high_resolution_clock::now();
if (init_remote_server_socket(socket, ip, port) == -1) {
std::cout << "Resource blocked by IP. I can't help. Use VPN or proxy :((" << std::endl;
// Stop sniff thread
flag.store(false);
if (sniff_thread.joinable()) sniff_thread.join();
close(socket);
return -1;
}
auto stop = std::chrono::high_resolution_clock::now();
unsigned int connect_time = std::chrono::duration_cast<std::chrono::milliseconds>(
stop - start).count();
// Disable TCP Nagle's algorithm
int yes = 1;
if (setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, (char *) &yes, sizeof(yes)) < 0) {
std::cerr << "Can't disable TCP Nagle's algorithm with setsockopt(). Errno: "
<< std::strerror(errno) << std::endl;
// Stop sniff thread
flag.store(false);
if (sniff_thread.joinable()) sniff_thread.join();
close(socket);
return -1;
}
// Get local port to choose proper SYN, ACK packet
struct sockaddr_in local_addr;
socklen_t len = sizeof(local_addr);
if (getsockname(socket, (struct sockaddr *) &local_addr, &len) == -1) {
std::cerr << "Failed to get local port. Errno: " << std::strerror(errno) << std::endl;
// Stop sniff thread
flag.store(false);
if (sniff_thread.joinable()) sniff_thread.join();
close(socket);
return -1;
}
local_port.store(ntohs(local_addr.sin_port));
// Get received ACK packet
if (sniff_thread.joinable()) sniff_thread.join();
if (status.load() == -1) {
std::cerr << "Failed to capture handshake packet" << std::endl;
close(socket);
return -1;
}
return is_https ?
check_https_response(socket, host, ip, port, local_port, sniffed_packet, ctx, store) :
check_http_response(socket, host, ip, port, local_port, sniffed_packet, connect_time);
}
void
show_configured_options(std::string host, std::string ip, int port, bool is_https, SSL_CTX *ctx,
X509_STORE *store) {
// Find minimum working ttl for fake packets
if (Profile.fake_packets_ttl) {
std::cout << "Calculating minimum working ttl..." << std::endl;
short result = -1;
int fake_packets_ttl = Profile.fake_packets_ttl;
Profile.fake_packets_ttl = 1;
while (Profile.fake_packets_ttl <= fake_packets_ttl && result == -1) {
result = test_desync_attack(host, ip, port, is_https, ctx, store);
// Test attack 3 times to ensure it work all times
if (result != -1)
for (short i = 1; i <= 3; i++)
result = std::min(result,
(short) test_desync_attack(host, ip, port, is_https, ctx,
store));
Profile.fake_packets_ttl++;
}
Profile.fake_packets_ttl--;
std::cout << std::endl;
}
std::cout << "Configuration successful! Apply these options when run program:" << std::endl;
if (Profile.builtin_dns) {
std::cout << "-builtin-dns ";
std::cout << "-builtin-dns-ip " << Profile.builtin_dns_ip << ' ';
std::cout << "-builtin-dns-port " << Profile.builtin_dns_port << ' ';
}
std::cout << "-doh ";
std::cout << "-doh-server " << Profile.doh_server << ' ';
if (Profile.split_at_sni)
std::cout << "-split-at-sni ";
if (Profile.window_size != 0)
std::cout << "-wsize " << Profile.window_size << ' ';
if (Profile.window_scale_factor != -1)
std::cout << "-wsfactor " << Profile.window_scale_factor << ' ';
if (Profile.fake_packets_ttl)
std::cout << "-ttl " << Profile.fake_packets_ttl << ' ';
if (Profile.wrong_seq)
std::cout << "-wrong-seq ";
if (is_https)
std::cout << "-ca-bundle-path \"" << Settings_perst.ca_bundle_path << "\" ";
if (Profile.desync_zero_attack != DESYNC_ZERO_NONE ||
Profile.desync_first_attack != DESYNC_FIRST_NONE)
std::cout << "-desync-attacks ";
if (Profile.desync_zero_attack != DESYNC_ZERO_NONE) {
std::cout << ZERO_ATTACKS_NAMES.at(Profile.desync_zero_attack);
if (Profile.desync_first_attack != DESYNC_FIRST_NONE)
std::cout << ",";
}
if (Profile.desync_first_attack != DESYNC_FIRST_NONE)
std::cout << FIRST_ATTACKS_NAMES.at(Profile.desync_first_attack);
std::cout << std::endl;
}
int
test_desync_attack_wrapper(std::string host, std::string ip, int port, bool is_https, SSL_CTX *ctx,
X509_STORE *store) {
if (test_desync_attack(host, ip, port, is_https, ctx, store) == -1)
std::cout << "\tFail" << std::endl << std::endl;
else {
// Check does attack work all times
short res = 0;
for (short i = 1; i <= 3; i++)
res = std::min(res, (short) test_desync_attack(host, ip, port, is_https, ctx, store));
if (res == -1)
std::cout << "\tFail. Attack don't work all times" << std::endl << std::endl;
else {
std::cout << "\tSuccess" << std::endl << std::endl;
show_configured_options(host, ip, port, is_https, ctx, store);
if (is_https)
SSL_CTX_free(ctx);
return 0;
}
}
return -1;
}
void set_profile(const std::string &doh_server, bool builtin_dns, Desync_zero_attacks zero_attack,
Desync_first_attacks first_attack,
const std::string &fake_type, short ttl, bool win_size_scale) {
Profile_s default_profile;
default_profile.doh = true;
default_profile.doh_server = doh_server;
if (builtin_dns)
default_profile.builtin_dns = true;
default_profile.desync_zero_attack = zero_attack;
default_profile.desync_first_attack = first_attack;
if (fake_type == "ttl")
default_profile.fake_packets_ttl = ttl;
else if (fake_type == "wrong-seq")
default_profile.wrong_seq = true;
if (win_size_scale) {
default_profile.window_size = 1;
default_profile.window_scale_factor = 6;
}
Profile = default_profile;
}
int run_autoconf() {
bool is_https;
int port;
std::string host;
std::string tmp;
std::cout << "Site domain you want to unblock " << std::endl
<< "(http://example.com or https://example.com or example.com. Can contain port): ";
std::getline(std::cin, host);
std::cout << "DoH server (press enter to use default " << Profile.doh_server << "): ";
std::getline(std::cin, tmp);
if (!tmp.empty())
Profile.doh_server = tmp;
if (host.rfind("http://", 0) == 0) {
is_https = false;
port = 80;
host.erase(0, 7);
} else if (host.rfind("https://", 0) == 0) {
is_https = true;
port = 443;
host.erase(0, 8);
} else {
is_https = true;
port = 443;
}
// Extract port
size_t port_start_position = host.find(':');
if (port_start_position != std::string::npos) {
port = std::stoi(host.substr(port_start_position + 1, host.size() - port_start_position));
host.erase(port_start_position, host.size() - port_start_position + 1);
}
// Load CA store to validate SSL certificates and connect to DoH server
X509_STORE *store;
SSL_CTX *ctx;
std::cout << "CA bundle path (press enter to use default location "
<< Settings_perst.ca_bundle_path << "): ";
std::getline(std::cin, tmp);
if (!tmp.empty())
Settings_perst.ca_bundle_path = tmp;
if (load_ca_bundle() == -1)
return -1;
if (is_https) {
// Init openssl
SSL_library_init();
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
ERR_load_BIO_strings();
ERR_load_crypto_strings();
store = gen_x509_store();
if (store == NULL) {
std::cout << "Failed to parse CA Bundle" << std::endl;
return -1;
}
ctx = SSL_CTX_new(SSLv23_method());
if (!ctx) {
std::cout << "Failed to init SSL context" << std::endl;
return -1;
}
SSL_CTX_set_cert_store(ctx, store);
}
// Resolve over DoH
std::cout << "Resolving host over DoH server " << Profile.doh_server << std::endl;
Profile.doh = true;
std::string ip;
bool builtin_dns = false;
if (resolve_host(host, ip) == -1) {
// Try with builtin DNS
std::cout << "DNS server (press enter to use default " << Profile.builtin_dns_ip
<< ". Can contain port): ";
std::getline(std::cin, tmp);
Profile.builtin_dns = builtin_dns = true;
if (!tmp.empty()) {
// Check if port exists
size_t port_start_position = tmp.find(':');
if (port_start_position != std::string::npos) {
Profile.builtin_dns_ip = tmp.substr(0, port_start_position);
Profile.builtin_dns_port = std::stoi(
tmp.substr(port_start_position + 1, tmp.size() - port_start_position));
} else Profile.builtin_dns_ip = tmp;
}
if (resolve_host(host, ip) == -1) {
std::cout << "Failed to resolve host " << host << std::endl;
if (is_https)
SSL_CTX_free(ctx);
return -1;
}
}
std::cout << host << " IP is " << ip << std::endl << std::endl;
std::cout << "\tCalculating network distance to server..." << std::endl;
short fakes_ttl = count_hops(ip, port);
if (fakes_ttl == -1) {
std::cout << "\tFail" << std::endl;
if (is_https)
SSL_CTX_free(ctx);
return -1;
}
std::cout << "\tHops to site: " << fakes_ttl << std::endl << std::endl;
fakes_ttl--;
// Iterate through all combinations
const std::vector<Desync_zero_attacks> zero_attacks = {DESYNC_ZERO_NONE, DESYNC_ZERO_FAKE,
DESYNC_ZERO_RST, DESYNC_ZERO_RSTACK};
const std::vector<Desync_first_attacks> first_attacks = {DESYNC_FIRST_DISORDER_FAKE,
DESYNC_FIRST_SPLIT_FAKE};
const std::vector<std::string> fake_types = {"ttl", "wrong-seq"};
const std::vector<bool> win_size_scales = {false, true};
unsigned int comb_all =
zero_attacks.size() * first_attacks.size() * fake_types.size() * win_size_scales.size();
unsigned int comb_curr = 1;
for (const Desync_zero_attacks &zero_attack: zero_attacks)
for (const Desync_first_attacks &first_attack: first_attacks)
for (const std::string &fake_type: fake_types)
for (const bool win_size_scale: win_size_scales) {
std::cout << "\tTrying " << comb_curr << '/' << comb_all << "..." << std::endl;
set_profile(Profile.doh_server, builtin_dns, zero_attack, first_attack, fake_type,
fakes_ttl, win_size_scale);
if (test_desync_attack_wrapper(host, ip, port, is_https, ctx, store) ==
0)
return 0;
comb_curr++;
}
std::cout << "Failed to find any working attack!" << std::endl;
if (is_https)
SSL_CTX_free(ctx);
return 0;
}