Merge pull request #1 from Waujito/zabbius-kern_mod

Zabbius kern mod
This commit is contained in:
Sergey Zabodalov 2024-08-07 20:31:58 +03:00 committed by GitHub
commit 0a66da7014
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 137 additions and 93 deletions

View File

@ -1,3 +1,17 @@
struct config_t {
unsigned int queue_start_num;
int rawsocket;
int threads;
int use_gso;
int fragmentation_strategy;
unsigned char fake_sni_ttl;
int fake_sni_strategy;
int verbose;
unsigned int seg2_delay;
};
extern struct config_t config;
#define MAX_THREADS 16 #define MAX_THREADS 16
#ifndef THREADS_NUM #ifndef THREADS_NUM

View File

@ -190,9 +190,8 @@ int ip4_frag(const __u8 *pkt, __u32 buflen, __u32 payload_offset,
f2_hdr->tot_len = htons(f2_dlen); f2_hdr->tot_len = htons(f2_dlen);
#if defined(DEBUG) if (config.verbose)
printf("Packet split in portion %u %u\n", f1_plen, f2_plen); printf("Packet split in portion %u %u\n", f1_plen, f2_plen);
#endif
ip4_set_checksum(f1_hdr); ip4_set_checksum(f1_hdr);
ip4_set_checksum(f2_hdr); ip4_set_checksum(f2_hdr);
@ -226,7 +225,11 @@ int tcp4_frag(const __u8 *pkt, __u32 buflen, __u32 payload_offset,
} }
if (!(ntohs(hdr->frag_off) & IP_DF)) { if (
ntohs(hdr->frag_off) & IP_MF ||
ntohs(hdr->frag_off) & IP_OFFMASK) {
printf("tcp4_frag: frag value: %d\n",
ntohs(hdr->frag_off));
lgerror("tcp4_frag: ip fragmentation is set", -EINVAL); lgerror("tcp4_frag: ip fragmentation is set", -EINVAL);
return -EINVAL; return -EINVAL;
} }
@ -268,9 +271,8 @@ int tcp4_frag(const __u8 *pkt, __u32 buflen, __u32 payload_offset,
s2_tcph->seq = htonl(ntohl(s2_tcph->seq) + payload_offset); s2_tcph->seq = htonl(ntohl(s2_tcph->seq) + payload_offset);
#if defined(DEBUG) if (config.verbose)
printf("Packet split in portion %u %u\n", s1_plen, s2_plen); printf("Packet split in portion %u %u\n", s1_plen, s2_plen);
#endif
tcp4_set_checksum(s1_tcph, s1_hdr); tcp4_set_checksum(s1_tcph, s1_hdr);
tcp4_set_checksum(s2_tcph, s2_hdr); tcp4_set_checksum(s2_tcph, s2_hdr);

View File

@ -29,23 +29,14 @@
#include "config.h" #include "config.h"
#include "mangle.h" #include "mangle.h"
static struct { pthread_mutex_t rawsocket_lock;
uint32_t queue_start_num;
int rawsocket; struct config_t config = {
pthread_mutex_t rawsocket_lock;
int threads;
bool use_gso;
int fragmentation_strategy;
uint8_t fake_sni_ttl;
int fake_sni_strategy;
bool verbose;
uint32_t seg2_delay;
} config = {
.rawsocket = -2, .rawsocket = -2,
.threads = THREADS_NUM, .threads = THREADS_NUM,
.fragmentation_strategy = FRAGMENTATION_STRATEGY, .fragmentation_strategy = FRAGMENTATION_STRATEGY,
.fake_sni_strategy = FAKE_SNI_STRATEGY, .fake_sni_strategy = FAKE_SNI_STRATEGY,
.fake_sni_ttl = FAKE_SNI_TTL, .fake_sni_ttl = FAKE_SNI_TTL,
#ifdef SEG2_DELAY #ifdef SEG2_DELAY
.seg2_delay = SEG2_DELAY, .seg2_delay = SEG2_DELAY,
@ -68,114 +59,148 @@ static struct {
const char* get_value(const char *option, const char *prefix) const char* get_value(const char *option, const char *prefix)
{ {
errno = 0;
size_t prefix_len = strlen(prefix); size_t prefix_len = strlen(prefix);
size_t option_len = strlen(option); size_t option_len = strlen(option);
if (option_len < prefix_len || memcmp(prefix, option, prefix_len)) if (option_len <= prefix_len || strncmp(prefix, option, prefix_len)) {
return 0; return NULL;
}
return option + prefix_len; return option + prefix_len;
} }
bool parse_bool_option(const char *value) { int parse_bool_option(const char *value) {
errno = 0; errno = 0;
if (strcmp(value, "1") == 0) { if (strcmp(value, "1") == 0) {
return true; return 1;
} }
else if (strcmp(value, "0") == 0) { else if (strcmp(value, "0") == 0) {
return false; return 0;
} }
errno = EINVAL;
return false; errno = EINVAL;
return -1;
} }
uint32_t parse_uint_option(const char* value) { long parse_numeric_option(const char* value) {
errno = 0;
char* end; char* end;
uint32_t result = strtoul(value, &end, 10); long result = strtol(value, &end, 10);
if (*end != '\0') { if (*end != '\0') {
errno = EINVAL; errno = EINVAL;
return -1; return 0;
} }
return result; return result;
} }
bool parse_option(const char* option) { int parse_option(const char* option) {
const char* value; const char* value;
int ret;
if ((value = get_value(option, "--gso=")) != 0) { if (!strcmp(option, "--no-gso")) {
config.use_gso = parse_bool_option(value); config.use_gso = 0;
return errno == 0; goto out;
} }
if ((value = get_value(option, "--verbose=")) != 0) { if (!strcmp(option, "--silent")) {
config.verbose = parse_bool_option(value); config.verbose = 0;
return errno == 0; goto out;
} }
if ((value = get_value(option, "--frag=")) != 0) { if ((value = get_value(option, "--frag=")) != 0) {
if (!value) {
goto err;
}
if (strcmp(value, "tcp") == 0) { if (strcmp(value, "tcp") == 0) {
config.fragmentation_strategy = FRAG_STRAT_TCP; config.fragmentation_strategy = FRAG_STRAT_TCP;
} } else if (strcmp(value, "ip") == 0) {
else if (strcmp(value, "ip") == 0) {
config.fragmentation_strategy = FRAG_STRAT_IP; config.fragmentation_strategy = FRAG_STRAT_IP;
} } else if (strcmp(value, "none") == 0) {
else if (strcmp(value, "none") == 0) {
config.fragmentation_strategy = FRAG_STRAT_NONE; config.fragmentation_strategy = FRAG_STRAT_NONE;
} else {
goto err;
} }
else {
return false; goto out;
}
return true;
} }
if ((value = get_value(option, "--fake-sni=")) != 0) { if ((value = get_value(option, "--fake-sni=")) != 0) {
if (strcmp(value, "ack") == 0) { if (strcmp(value, "ack") == 0) {
config.fake_sni_strategy = FKSN_STRAT_ACK_SEQ; config.fake_sni_strategy = FKSN_STRAT_ACK_SEQ;
} } else if (strcmp(value, "ttl") == 0) {
else if (strcmp(value, "ttl") == 0) {
config.fake_sni_strategy = FKSN_STRAT_TTL; config.fake_sni_strategy = FKSN_STRAT_TTL;
} }
else if (strcmp(value, "none") == 0) { else if (strcmp(value, "none") == 0) {
config.fake_sni_strategy = FKSN_STRAT_NONE; config.fake_sni_strategy = FKSN_STRAT_NONE;
} else {
goto err;
} }
else {
return false; goto out;
}
return true;
} }
if ((value = get_value(option, "--seg2delay=")) != 0) { if ((value = get_value(option, "--seg2delay=")) != 0) {
config.seg2_delay = parse_uint_option(value); long num = parse_numeric_option(value);
return errno == 0; if (errno != 0 ||
num < 0)
goto err;
config.seg2_delay = num;
goto out;
} }
if ((value = get_value(option, "--threads=")) != 0) { if ((value = get_value(option, "--threads=")) != 0) {
config.threads = parse_uint_option(value); long num = parse_numeric_option(value);
return errno == 0 && config.threads <= MAX_THREADS; if (errno != 0 ||
num < 0 ||
num > MAX_THREADS) {
errno = EINVAL;
goto err;
}
config.threads = num;
goto out;
} }
if ((value = get_value(option, "--fake-sni-ttl=")) != 0) { if ((value = get_value(option, "--fake-sni-ttl=")) != 0) {
config.fake_sni_ttl = parse_uint_option(value); long num = parse_numeric_option(value);
return errno == 0; if (errno != 0 ||
num < 0 ||
num > 255) {
goto err;
}
config.fake_sni_ttl = num;
goto out;
} }
return false; err:
errno = EINVAL;
return -1;
out:
errno = 0;
return 0;
} }
static int parse_args(int argc, const char *argv[]) { static int parse_args(int argc, const char *argv[]) {
int err; int err;
char *end; char *end;
if (argc != 2) { if (argc < 2) {
errno = EINVAL; errno = EINVAL;
goto errormsg_help; goto errormsg_help;
} }
config.queue_start_num = parse_uint_option(argv[1]); config.queue_start_num = parse_numeric_option(argv[1]);
if (errno != 0) goto errormsg_help; if (errno != 0) goto errormsg_help;
for (int i = 2; i < argc; i++) { for (int i = 2; i < argc; i++) {
if (!parse_option(argv[i])) { if (parse_option(argv[i])) {
printf("Invalid option %s\n", argv[i]); printf("Invalid option %s\n", argv[i]);
goto errormsg_help; goto errormsg_help;
} }
@ -187,13 +212,13 @@ errormsg_help:
err = errno; err = errno;
printf("Usage: %s <queue_num> [OPTIONS]\n", argv[0]); printf("Usage: %s <queue_num> [OPTIONS]\n", argv[0]);
printf("Options:\n"); printf("Options:\n");
printf("\t--gso={0,1}\n");
printf("\t--fake-sni={ack,ttl,none}\n"); printf("\t--fake-sni={ack,ttl,none}\n");
printf("\t--fake-sni-ttl=<ttl>\n"); printf("\t--fake-sni-ttl=<ttl>\n");
printf("\t--frag={tcp,ip,none}\n"); printf("\t--frag={tcp,ip,none}\n");
printf("\t--seg2delay=<delay>\n"); printf("\t--seg2delay=<delay>\n");
printf("\t--threads=<threads number>\n"); printf("\t--threads=<threads number>\n");
printf("\t--verbose={0,1}\n"); printf("\t--silent\n");
printf("\t--no-gso\n");
errno = err; errno = err;
if (errno == 0) errno = EINVAL; if (errno == 0) errno = EINVAL;
@ -254,7 +279,7 @@ static int open_raw_socket(void) {
return -1; return -1;
} }
int mst = pthread_mutex_init(&config.rawsocket_lock, NULL); int mst = pthread_mutex_init(&rawsocket_lock, NULL);
if (mst) { if (mst) {
fprintf(stderr, "Mutex err: %d\n", mst); fprintf(stderr, "Mutex err: %d\n", mst);
close(config.rawsocket); close(config.rawsocket);
@ -276,11 +301,11 @@ static int close_raw_socket(void) {
if (close(config.rawsocket)) { if (close(config.rawsocket)) {
perror("Unable to close raw socket"); perror("Unable to close raw socket");
pthread_mutex_destroy(&config.rawsocket_lock); pthread_mutex_destroy(&rawsocket_lock);
return -1; return -1;
} }
pthread_mutex_destroy(&config.rawsocket_lock); pthread_mutex_destroy(&rawsocket_lock);
config.rawsocket = -2; config.rawsocket = -2;
return 0; return 0;
@ -314,13 +339,11 @@ static int send_raw_socket(const uint8_t *pkt, uint32_t pktlen) {
errno = -ret; errno = -ret;
return ret; return ret;
} }
break; break;
default: default:
errno = EINVAL; errno = EINVAL;
printf("send_raw_socket: Packet is too big but fragmentation is disabled! " printf("send_raw_socket: Packet is too big but fragmentation is disabled!\n");
"Pass -DRAWSOCK_TCP_FSTRAT or -DRAWSOCK_IP_FSTRAT as CFLAGS "
"To enable it only for raw socket\n");
return -EINVAL; return -EINVAL;
} }
@ -359,13 +382,13 @@ static int send_raw_socket(const uint8_t *pkt, uint32_t pktlen) {
} }
}; };
pthread_mutex_lock(&config.rawsocket_lock); pthread_mutex_lock(&rawsocket_lock);
int sent = sendto(config.rawsocket, int sent = sendto(config.rawsocket,
pkt, pktlen, 0, pkt, pktlen, 0,
(struct sockaddr *)&daddr, sizeof(daddr)); (struct sockaddr *)&daddr, sizeof(daddr));
pthread_mutex_unlock(&config.rawsocket_lock); pthread_mutex_unlock(&rawsocket_lock);
/* The function will return -errno on error as well as errno value set itself */ /* The function will return -errno on error as well as errno value set itself */
if (sent < 0) sent = -errno; if (sent < 0) sent = -errno;
@ -479,9 +502,6 @@ static int process_packet(const struct packet_data packet, struct queue_data qda
fprintf(stderr, "WARNING! Google video packet is too big and may cause issues!\n"); fprintf(stderr, "WARNING! Google video packet is too big and may cause issues!\n");
} }
uint8_t fake_sni[MNL_SOCKET_BUFFER_SIZE];
uint32_t fsn_len = MNL_SOCKET_BUFFER_SIZE;
uint8_t frag1[MNL_SOCKET_BUFFER_SIZE]; uint8_t frag1[MNL_SOCKET_BUFFER_SIZE];
uint8_t frag2[MNL_SOCKET_BUFFER_SIZE]; uint8_t frag2[MNL_SOCKET_BUFFER_SIZE];
uint32_t f1len = MNL_SOCKET_BUFFER_SIZE; uint32_t f1len = MNL_SOCKET_BUFFER_SIZE;
@ -494,6 +514,9 @@ static int process_packet(const struct packet_data packet, struct queue_data qda
(struct tcphdr *)tcph, (struct iphdr *)iph); (struct tcphdr *)tcph, (struct iphdr *)iph);
if (config.fake_sni_strategy != FKSN_STRAT_NONE) { if (config.fake_sni_strategy != FKSN_STRAT_NONE) {
uint8_t fake_sni[MNL_SOCKET_BUFFER_SIZE];
uint32_t fsn_len = MNL_SOCKET_BUFFER_SIZE;
ret = gen_fake_sni(iph, tcph, fake_sni, &fsn_len); ret = gen_fake_sni(iph, tcph, fake_sni, &fsn_len);
if (ret < 0) { if (ret < 0) {
errno = -ret; errno = -ret;
@ -522,8 +545,9 @@ static int process_packet(const struct packet_data packet, struct queue_data qda
errno = -ret; errno = -ret;
perror("tcp4_frag"); perror("tcp4_frag");
goto send_verd; goto fallback;
} }
break; break;
case FRAG_STRAT_IP: case FRAG_STRAT_IP:
ipd_offset = ((char *)data - (char *)tcph) + vrd.sni_offset; ipd_offset = ((char *)data - (char *)tcph) + vrd.sni_offset;
@ -535,8 +559,9 @@ static int process_packet(const struct packet_data packet, struct queue_data qda
errno = -ret; errno = -ret;
perror("ip4_frag"); perror("ip4_frag");
goto send_verd; goto fallback;
} }
break; break;
default: default:
ret = send_raw_socket(raw_payload, raw_payload_len); ret = send_raw_socket(raw_payload, raw_payload_len);
@ -544,7 +569,7 @@ static int process_packet(const struct packet_data packet, struct queue_data qda
errno = -ret; errno = -ret;
perror("raw pack send"); perror("raw pack send");
} }
goto send_verd; goto fallback;
} }
ret = send_raw_socket(frag2, f2len); ret = send_raw_socket(frag2, f2len);
@ -552,10 +577,10 @@ static int process_packet(const struct packet_data packet, struct queue_data qda
errno = -ret; errno = -ret;
perror("raw frags send: frag2"); perror("raw frags send: frag2");
goto send_verd; goto fallback;
} }
if (config.seg2_delay) { if (config.seg2_delay) {
struct dps_t *dpdt = malloc(sizeof(struct dps_t)); struct dps_t *dpdt = malloc(sizeof(struct dps_t));
dpdt->pkt = malloc(f1len); dpdt->pkt = malloc(f1len);
memcpy(dpdt->pkt, frag1, f1len); memcpy(dpdt->pkt, frag1, f1len);
@ -564,14 +589,14 @@ static int process_packet(const struct packet_data packet, struct queue_data qda
pthread_t thr; pthread_t thr;
pthread_create(&thr, NULL, delay_packet_send, dpdt); pthread_create(&thr, NULL, delay_packet_send, dpdt);
pthread_detach(thr); pthread_detach(thr);
} } else {
else {
ret = send_raw_socket(frag1, f1len); ret = send_raw_socket(frag1, f1len);
if (ret < 0) { if (ret < 0) {
errno = -ret; errno = -ret;
perror("raw frags send: frag1"); perror("raw frags send: frag1");
goto send_verd; goto fallback;
} }
} }
} }
@ -672,9 +697,9 @@ int init_queue(int queue_num) {
nlh = nfq_nlmsg_put(buf, NFQNL_MSG_CONFIG, queue_num); nlh = nfq_nlmsg_put(buf, NFQNL_MSG_CONFIG, queue_num);
nfq_nlmsg_cfg_put_params(nlh, NFQNL_COPY_PACKET, 0xffff); nfq_nlmsg_cfg_put_params(nlh, NFQNL_COPY_PACKET, 0xffff);
if (config.use_gso) { if (config.use_gso) {
mnl_attr_put_u32(nlh, NFQA_CFG_FLAGS, htonl(NFQA_CFG_F_GSO)); mnl_attr_put_u32(nlh, NFQA_CFG_FLAGS, htonl(NFQA_CFG_F_GSO));
mnl_attr_put_u32(nlh, NFQA_CFG_MASK, htonl(NFQA_CFG_F_GSO)); mnl_attr_put_u32(nlh, NFQA_CFG_MASK, htonl(NFQA_CFG_F_GSO));
} }
if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
@ -728,6 +753,7 @@ struct queue_conf {
struct queue_res { struct queue_res {
int status; int status;
}; };
static struct queue_res defqres = {0};
static struct queue_res threads_reses[MAX_THREADS]; static struct queue_res threads_reses[MAX_THREADS];
@ -760,8 +786,9 @@ int main(int argc, const char *argv[]) {
break; break;
} }
if (config.seg2_delay) if (config.seg2_delay) {
printf("Some outgoing googlevideo request segments will be delayed for %d ms as of seg2_delay define\n", config.seg2_delay); printf("Some outgoing googlevideo request segments will be delayed for %d ms as of seg2_delay define\n", config.seg2_delay);
}
switch (config.fake_sni_strategy) { switch (config.fake_sni_strategy) {
case FKSN_STRAT_TTL: case FKSN_STRAT_TTL:
@ -775,15 +802,16 @@ int main(int argc, const char *argv[]) {
break; break;
} }
if (config.use_gso) if (config.use_gso) {
printf("GSO is enabled\n"); printf("GSO is enabled\n");
}
if (open_raw_socket() < 0) { if (open_raw_socket() < 0) {
perror("Unable to open raw socket"); perror("Unable to open raw socket");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
struct queue_res *qres; struct queue_res *qres = &defqres;
if (config.threads == 1) { if (config.threads == 1) {
struct queue_conf tconf = { struct queue_conf tconf = {