youtubeUnblock/args.c

568 lines
14 KiB
C
Raw Normal View History

2024-08-10 18:44:04 +00:00
#include "config.h"
#include <stdbool.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include "types.h"
#include "args.h"
2024-08-10 18:44:04 +00:00
static char custom_fake_buf[MAX_FAKE_SIZE];
2024-08-10 18:44:04 +00:00
struct config_t config = {
.threads = THREADS_NUM,
.queue_start_num = DEFAULT_QUEUE_NUM,
.mark = DEFAULT_RAWSOCKET_MARK,
.use_ipv6 = 1,
.verbose = VERBOSE_DEBUG,
.use_gso = true,
.default_config = default_section_config,
.custom_configs_len = 0
2024-08-10 18:44:04 +00:00
};
#define OPT_SNI_DOMAINS 1
2024-09-03 18:22:17 +00:00
#define OPT_EXCLUDE_DOMAINS 25
2024-08-10 18:44:04 +00:00
#define OPT_FAKE_SNI 2
2024-08-10 23:10:18 +00:00
#define OPT_FAKING_TTL 3
#define OPT_FAKING_STRATEGY 10
#define OPT_FAKE_SNI_SEQ_LEN 11
#define OPT_FAKE_SNI_TYPE 27
#define OPT_FAKE_CUSTOM_PAYLOAD 28
#define OPT_START_SECTION 29
#define OPT_END_SECTION 30
2024-08-10 18:44:04 +00:00
#define OPT_FRAG 4
2024-08-10 23:10:18 +00:00
#define OPT_FRAG_SNI_REVERSE 12
#define OPT_FRAG_SNI_FAKED 13
2024-08-17 09:51:53 +00:00
#define OPT_FRAG_MIDDLE_SNI 18
#define OPT_FRAG_SNI_POS 19
2024-08-11 21:45:30 +00:00
#define OPT_FK_WINSIZE 14
#define OPT_TRACE 15
#define OPT_QUIC_DROP 16
#define OPT_SNI_DETECTION 17
#define OPT_NO_IPV6 20
#define OPT_FAKE_SEQ_OFFSET 21
#define OPT_PACKET_MARK 22
2024-08-29 11:45:27 +00:00
#define OPT_SYNFAKE 23
2024-08-29 12:49:01 +00:00
#define OPT_SYNFAKE_LEN 24
2024-08-10 18:44:04 +00:00
#define OPT_SEG2DELAY 5
#define OPT_THREADS 6
#define OPT_SILENT 7
#define OPT_NO_GSO 8
#define OPT_QUEUE_NUM 9
#define OPT_MAX OPT_END_SECTION
2024-08-10 23:10:18 +00:00
2024-08-10 18:44:04 +00:00
static struct option long_opt[] = {
2024-08-10 23:10:18 +00:00
{"help", 0, 0, 'h'},
{"version", 0, 0, 'v'},
{"sni-domains", 1, 0, OPT_SNI_DOMAINS},
2024-09-03 18:22:17 +00:00
{"exclude-domains", 1, 0, OPT_EXCLUDE_DOMAINS},
2024-08-10 23:10:18 +00:00
{"fake-sni", 1, 0, OPT_FAKE_SNI},
2024-08-29 11:45:27 +00:00
{"synfake", 1, 0, OPT_SYNFAKE},
2024-08-29 12:49:01 +00:00
{"synfake-len", 1, 0, OPT_SYNFAKE_LEN},
2024-08-10 23:10:18 +00:00
{"fake-sni-seq-len", 1, 0, OPT_FAKE_SNI_SEQ_LEN},
{"fake-sni-type", 1, 0, OPT_FAKE_SNI_TYPE},
{"fake-custom-payload", 1, 0, OPT_FAKE_CUSTOM_PAYLOAD},
2024-08-10 23:10:18 +00:00
{"faking-strategy", 1, 0, OPT_FAKING_STRATEGY},
{"fake-seq-offset", 1, 0, OPT_FAKE_SEQ_OFFSET},
2024-08-10 23:10:18 +00:00
{"faking-ttl", 1, 0, OPT_FAKING_TTL},
{"frag", 1, 0, OPT_FRAG},
{"frag-sni-reverse", 1, 0, OPT_FRAG_SNI_REVERSE},
{"frag-sni-faked", 1, 0, OPT_FRAG_SNI_FAKED},
2024-08-17 09:51:53 +00:00
{"frag-middle-sni", 1, 0, OPT_FRAG_MIDDLE_SNI},
{"frag-sni-pos", 1, 0, OPT_FRAG_SNI_POS},
2024-08-11 21:45:30 +00:00
{"fk-winsize", 1, 0, OPT_FK_WINSIZE},
{"quic-drop", 0, 0, OPT_QUIC_DROP},
{"sni-detection", 1, 0, OPT_SNI_DETECTION},
2024-08-10 23:10:18 +00:00
{"seg2delay", 1, 0, OPT_SEG2DELAY},
{"threads", 1, 0, OPT_THREADS},
{"silent", 0, 0, OPT_SILENT},
{"trace", 0, 0, OPT_TRACE},
2024-08-10 23:10:18 +00:00
{"no-gso", 0, 0, OPT_NO_GSO},
{"no-ipv6", 0, 0, OPT_NO_IPV6},
2024-08-10 23:10:18 +00:00
{"queue-num", 1, 0, OPT_QUEUE_NUM},
{"packet-mark", 1, 0, OPT_PACKET_MARK},
{"fbegin", 0, 0, OPT_START_SECTION},
{"fend", 0, 0, OPT_END_SECTION},
2024-08-10 18:44:04 +00:00
{0,0,0,0}
};
static long parse_numeric_option(const char* value) {
errno = 0;
if (*value == '\0') {
errno = EINVAL;
return 0;
}
char* end;
long result = strtol(value, &end, 10);
if (*end != '\0') {
errno = EINVAL;
return 0;
}
return result;
}
void print_version() {
2024-10-28 22:09:23 +00:00
printf("youtubeUnblock"
#if defined(PKG_VERSION)
" " PKG_VERSION
#endif
"\n"
);
printf("Bypasses deep packet inspection systems that rely on SNI\n");
2024-08-11 18:43:32 +00:00
printf("\n");
2024-08-10 18:44:04 +00:00
}
void print_usage(const char *argv0) {
print_version();
printf("Usage: %s [ OPTIONS ] \n", argv0);
printf("Options:\n");
printf("\t--queue-num=<number of netfilter queue>\n");
printf("\t--sni-domains=<comma separated domain list>|all\n");
2024-09-03 18:22:17 +00:00
printf("\t--exclude-domains=<comma separated domain list>\n");
2024-08-10 23:10:18 +00:00
printf("\t--fake-sni={1|0}\n");
printf("\t--fake-sni-seq-len=<length>\n");
printf("\t--fake-sni-type={default|random|custom}\n");
printf("\t--fake-custom-payload=<hex payload>\n");
printf("\t--fake-seq-offset=<offset>\n");
2024-08-10 23:10:18 +00:00
printf("\t--faking-ttl=<ttl>\n");
2024-09-13 16:04:48 +00:00
printf("\t--faking-strategy={randseq|ttl|tcp_check|pastseq|md5sum}\n");
2024-08-29 11:45:27 +00:00
printf("\t--synfake={1|0}\n");
2024-08-29 12:49:01 +00:00
printf("\t--synfake-len=<len>\n");
2024-08-10 18:44:04 +00:00
printf("\t--frag={tcp,ip,none}\n");
2024-08-10 23:10:18 +00:00
printf("\t--frag-sni-reverse={0|1}\n");
printf("\t--frag-sni-faked={0|1}\n");
2024-08-17 09:51:53 +00:00
printf("\t--frag-middle-sni={0|1}\n");
printf("\t--frag-sni-pos=<pos>\n");
2024-08-11 21:45:30 +00:00
printf("\t--fk-winsize=<winsize>\n");
printf("\t--quic-drop\n");
printf("\t--sni-detection={parse|brute}\n");
2024-08-10 18:44:04 +00:00
printf("\t--seg2delay=<delay>\n");
printf("\t--threads=<threads number>\n");
printf("\t--packet-mark=<mark>\n");
2024-08-10 18:44:04 +00:00
printf("\t--silent\n");
printf("\t--trace\n");
2024-08-10 18:44:04 +00:00
printf("\t--no-gso\n");
printf("\t--no-ipv6\n");
printf("\t--fbegin\n");
printf("\t--fend\n");
2024-08-10 18:44:04 +00:00
printf("\n");
}
int parse_args(int argc, char *argv[]) {
int opt;
int optIdx = 0;
2024-08-10 18:44:04 +00:00
long num;
struct section_config_t *sect_config = &config.default_config;
#define SECT_ITER_DEFAULT 1
#define SECT_ITER_INSIDE 2
#define SECT_ITER_OUTSIDE 3
int section_iter = SECT_ITER_DEFAULT;
2024-08-10 18:44:04 +00:00
while ((opt = getopt_long(argc, argv, "hv", long_opt, &optIdx)) != -1) {
switch (opt) {
/* config_t scoped configs */
2024-08-12 12:22:04 +00:00
case 'h':
print_usage(argv[0]);
goto stop_exec;
case 'v':
print_version();
goto stop_exec;
case OPT_TRACE:
if (section_iter != SECT_ITER_DEFAULT)
goto invalid_opt;
config.verbose = 2;
break;
2024-08-12 12:22:04 +00:00
case OPT_SILENT:
if (section_iter != SECT_ITER_DEFAULT)
goto invalid_opt;
2024-08-12 12:22:04 +00:00
config.verbose = 0;
break;
case OPT_NO_GSO:
if (section_iter != SECT_ITER_DEFAULT)
goto invalid_opt;
2024-08-12 12:22:04 +00:00
config.use_gso = 0;
break;
case OPT_NO_IPV6:
if (section_iter != SECT_ITER_DEFAULT)
goto invalid_opt;
config.use_ipv6 = 0;
break;
case OPT_THREADS:
if (section_iter != SECT_ITER_DEFAULT)
goto invalid_opt;
num = parse_numeric_option(optarg);
if (errno != 0 || num < 0 || num > MAX_THREADS) {
goto invalid_opt;
}
config.threads = num;
break;
case OPT_QUEUE_NUM:
if (section_iter != SECT_ITER_DEFAULT)
goto invalid_opt;
num = parse_numeric_option(optarg);
if (errno != 0 || num < 0) {
goto invalid_opt;
2024-08-12 12:22:04 +00:00
}
config.queue_start_num = num;
break;
case OPT_PACKET_MARK:
if (section_iter != SECT_ITER_DEFAULT)
goto invalid_opt;
num = parse_numeric_option(optarg);
if (errno != 0 || num < 0) {
goto invalid_opt;
}
config.mark = num;
2024-09-03 18:22:17 +00:00
break;
case OPT_START_SECTION:
if (section_iter != SECT_ITER_DEFAULT && section_iter != SECT_ITER_OUTSIDE)
goto invalid_opt;
sect_config = &config.custom_configs[config.custom_configs_len++];
2024-10-13 21:11:58 +00:00
*sect_config = (struct section_config_t)default_section_config;
section_iter = SECT_ITER_INSIDE;
break;
case OPT_END_SECTION:
if (section_iter != SECT_ITER_INSIDE)
goto invalid_opt;
section_iter = SECT_ITER_OUTSIDE;
sect_config = &config.default_config;
break;
/* section_config_t scoped configs */
case OPT_SNI_DOMAINS:
if (!strcmp(optarg, "all")) {
sect_config->all_domains = 1;
}
sect_config->domains_str = optarg;
sect_config->domains_strlen = strlen(sect_config->domains_str);
break;
case OPT_EXCLUDE_DOMAINS:
sect_config->exclude_domains_str = optarg;
sect_config->exclude_domains_strlen = strlen(sect_config->exclude_domains_str);
2024-08-12 12:22:04 +00:00
break;
case OPT_FRAG:
if (strcmp(optarg, "tcp") == 0) {
sect_config->fragmentation_strategy = FRAG_STRAT_TCP;
2024-08-12 12:22:04 +00:00
} else if (strcmp(optarg, "ip") == 0) {
sect_config->fragmentation_strategy = FRAG_STRAT_IP;
2024-08-12 12:22:04 +00:00
} else if (strcmp(optarg, "none") == 0) {
sect_config->fragmentation_strategy = FRAG_STRAT_NONE;
2024-08-12 12:22:04 +00:00
} else {
goto invalid_opt;
}
break;
case OPT_FRAG_SNI_FAKED:
if (strcmp(optarg, "1") == 0) {
sect_config->frag_sni_faked = 1;
2024-08-12 12:22:04 +00:00
} else if (strcmp(optarg, "0") == 0) {
sect_config->frag_sni_faked = 0;
2024-08-12 12:22:04 +00:00
} else {
goto invalid_opt;
}
break;
case OPT_FRAG_SNI_REVERSE:
if (strcmp(optarg, "1") == 0) {
sect_config->frag_sni_reverse = 1;
2024-08-12 12:22:04 +00:00
} else if (strcmp(optarg, "0") == 0) {
sect_config->frag_sni_reverse = 0;
2024-08-12 12:22:04 +00:00
} else {
goto invalid_opt;
}
2024-08-17 09:51:53 +00:00
break;
case OPT_FRAG_MIDDLE_SNI:
if (strcmp(optarg, "1") == 0) {
sect_config->frag_middle_sni = 1;
2024-08-17 09:51:53 +00:00
} else if (strcmp(optarg, "0") == 0) {
sect_config->frag_middle_sni = 0;
2024-08-17 09:51:53 +00:00
} else {
goto invalid_opt;
}
break;
case OPT_FRAG_SNI_POS:
num = parse_numeric_option(optarg);
if (errno != 0 || num < 0) {
goto invalid_opt;
}
sect_config->frag_sni_pos = num;
2024-08-12 12:22:04 +00:00
break;
case OPT_FAKING_STRATEGY:
if (strcmp(optarg, "randseq") == 0) {
sect_config->faking_strategy = FAKE_STRAT_RAND_SEQ;
2024-08-12 12:22:04 +00:00
} else if (strcmp(optarg, "ttl") == 0) {
sect_config->faking_strategy = FAKE_STRAT_TTL;
} else if (strcmp(optarg, "tcp_check") == 0) {
sect_config->faking_strategy = FAKE_STRAT_TCP_CHECK;
} else if (strcmp(optarg, "pastseq") == 0) {
sect_config->faking_strategy = FAKE_STRAT_PAST_SEQ;
2024-09-13 16:04:48 +00:00
} else if (strcmp(optarg, "md5sum") == 0) {
sect_config->faking_strategy = FAKE_STRAT_TCP_MD5SUM;
2024-08-12 12:22:04 +00:00
} else {
goto invalid_opt;
}
break;
case OPT_FAKING_TTL:
num = parse_numeric_option(optarg);
if (errno != 0 || num < 0 || num > 255) {
goto invalid_opt;
}
sect_config->faking_ttl = num;
2024-08-12 12:22:04 +00:00
break;
case OPT_FAKE_SEQ_OFFSET:
num = parse_numeric_option(optarg);
if (errno != 0) {
goto invalid_opt;
}
2024-08-12 12:22:04 +00:00
sect_config->fakeseq_offset = num;
break;
2024-08-12 12:22:04 +00:00
case OPT_FAKE_SNI:
if (strcmp(optarg, "1") == 0) {
sect_config->fake_sni = 1;
2024-08-12 12:22:04 +00:00
} else if (strcmp(optarg, "0") == 0) {
sect_config->fake_sni = 0;
2024-08-12 12:22:04 +00:00
} else {
goto invalid_opt;
}
break;
case OPT_FAKE_SNI_SEQ_LEN:
num = parse_numeric_option(optarg);
if (errno != 0 || num < 0 || num > 255) {
goto invalid_opt;
}
sect_config->fake_sni_seq_len = num;
break;
case OPT_FAKE_SNI_TYPE:
if (strcmp(optarg, "default") == 0) {
sect_config->fake_sni_type = FAKE_PAYLOAD_DEFAULT;
} else if (strcmp(optarg, "random") == 0) {
sect_config->fake_sni_type = FAKE_PAYLOAD_RANDOM;
} else if (strcmp(optarg, "custom") == 0) {
sect_config->fake_sni_type = FAKE_PAYLOAD_CUSTOM;
} else {
goto invalid_opt;
}
break;
case OPT_FAKE_CUSTOM_PAYLOAD: {
uint8_t *const custom_buf = (uint8_t *)custom_fake_buf;
const char *custom_hex_fake = optarg;
size_t custom_hlen = strlen(custom_hex_fake);
if ((custom_hlen & 1) == 1) {
printf("Custom fake hex should be divisible by two\n");
goto invalid_opt;
}
size_t custom_len = custom_hlen >> 1;
if (custom_len > MAX_FAKE_SIZE) {
printf("Custom fake is too large\n");
goto invalid_opt;
}
for (int i = 0; i < custom_len; i++) {
sscanf(custom_hex_fake + (i << 1), "%2hhx", custom_buf + i);
}
sect_config->fake_custom_pkt_sz = custom_len;
sect_config->fake_custom_pkt = (char *)custom_buf;
2024-09-28 20:06:45 +00:00
}
break;
2024-08-12 12:22:04 +00:00
case OPT_FK_WINSIZE:
num = parse_numeric_option(optarg);
if (errno != 0 || num < 0) {
goto invalid_opt;
}
sect_config->fk_winsize = num;
2024-08-29 11:45:27 +00:00
break;
2024-08-12 12:22:04 +00:00
case OPT_SEG2DELAY:
num = parse_numeric_option(optarg);
if (errno != 0 || num < 0) {
goto invalid_opt;
}
sect_config->seg2_delay = num;
2024-08-12 12:22:04 +00:00
break;
case OPT_QUIC_DROP:
sect_config->quic_drop = 1;
2024-08-12 12:22:04 +00:00
break;
case OPT_SNI_DETECTION:
if (strcmp(optarg, "parse") == 0) {
sect_config->sni_detection = SNI_DETECTION_PARSE;
} else if (strcmp(optarg, "brute") == 0) {
sect_config->sni_detection = SNI_DETECTION_BRUTE;
} else {
goto invalid_opt;
}
break;
case OPT_SYNFAKE:
if (strcmp(optarg, "1") == 0) {
sect_config->synfake = 1;
} else if (strcmp(optarg, "0") == 0) {
sect_config->synfake = 0;
} else {
goto invalid_opt;
}
break;
case OPT_SYNFAKE_LEN:
num = parse_numeric_option(optarg);
if (errno != 0 || num < 0) {
goto invalid_opt;
}
sect_config->synfake_len = num;
break;
2024-08-12 12:22:04 +00:00
default:
goto error;
2024-08-10 18:44:04 +00:00
}
2024-08-10 18:44:04 +00:00
}
errno = 0;
return 0;
2024-08-12 12:22:04 +00:00
stop_exec:
2024-08-10 18:44:04 +00:00
errno = 0;
return 1;
2024-08-12 12:22:04 +00:00
invalid_opt:
2024-08-12 01:52:03 +00:00
printf("Invalid option %s\n", long_opt[optIdx].name);
2024-08-12 12:22:04 +00:00
error:
2024-08-10 18:44:04 +00:00
print_usage(argv[0]);
errno = EINVAL;
return -1;
}
2024-08-11 18:43:32 +00:00
void print_welcome() {
if (config.use_gso) {
printf("GSO is enabled\n");
2024-08-11 18:43:32 +00:00
}
if (config.use_ipv6) {
printf("IPv6 is enabled\n");
2024-08-11 18:43:32 +00:00
} else {
printf("IPv6 is disabled\n");
2024-08-11 18:43:32 +00:00
}
printf("Detected %d config sections\n", config.custom_configs_len + 1);
2024-10-28 21:12:27 +00:00
printf("The sections will be processed in ordred they goes in this output\n");
ITER_CONFIG_SECTIONS(section) {
int section_number = CONFIG_SECTION_NUMBER(section);
printf("Section #%d\n", section_number);
switch (section->fragmentation_strategy) {
case FRAG_STRAT_TCP:
printf("Using TCP segmentation\n");
break;
case FRAG_STRAT_IP:
printf("Using IP fragmentation\n");
break;
default:
printf("SNI fragmentation is disabled\n");
break;
}
2024-08-11 18:43:32 +00:00
if (section->seg2_delay) {
printf("Some outgoing googlevideo request segments will be delayed for %d ms as of seg2_delay define\n", section->seg2_delay);
}
2024-08-11 18:43:32 +00:00
if (section->fake_sni) {
printf("Fake SNI will be sent before each target client hello\n");
} else {
printf("Fake SNI is disabled\n");
}
2024-08-11 18:43:32 +00:00
if (section->frag_sni_reverse) {
printf("Fragmentation Client Hello will be reversed\n");
}
2024-08-11 18:43:32 +00:00
if (section->frag_sni_faked) {
printf("Fooling packets will be sent near the original Client Hello\n");
}
2024-08-11 21:45:30 +00:00
if (section->fake_sni_seq_len > 1) {
printf("Faking sequence of length %d will be built as fake sni\n", section->fake_sni_seq_len);
}
2024-08-29 11:45:27 +00:00
switch (section->faking_strategy) {
case FAKE_STRAT_TTL:
printf("TTL faking strategy will be used with TTL %d\n", section->faking_ttl);
break;
case FAKE_STRAT_RAND_SEQ:
printf("Random seq faking strategy will be used\n");
printf("Fake seq offset set to %u\n", section->fakeseq_offset);
break;
case FAKE_STRAT_TCP_CHECK:
printf("TCP checksum faking strategy will be used\n");
break;
case FAKE_STRAT_PAST_SEQ:
printf("Past seq faking strategy will be used\n");
break;
case FAKE_STRAT_TCP_MD5SUM:
printf("md5sum faking strategy will be used\n");
break;
}
2024-08-11 18:43:32 +00:00
if (section->fk_winsize) {
printf("Response TCP window will be set to %d with the appropriate scale\n", section->fk_winsize);
}
2024-08-11 18:43:32 +00:00
if (section->synfake) {
printf("Fake SYN payload will be sent with each TCP request SYN packet\n");
}
if (section->quic_drop) {
printf("All QUIC packets will be dropped\n");
}
if (section->sni_detection == SNI_DETECTION_BRUTE) {
printf("Server Name Extension will be parsed in the bruteforce mode\n");
}
if (section->all_domains) {
printf("All Client Hello will be targeted by youtubeUnblock!\n");
} else {
printf("Target sni domains: %s\n", section->domains_str);
}
2024-08-11 18:43:32 +00:00
}
}