byedpi/desync.c

520 lines
14 KiB
C
Raw Normal View History

2024-03-26 14:15:34 +00:00
#define _GNU_SOURCE
2023-06-12 04:00:33 +00:00
#include <stdio.h>
2023-06-12 08:01:40 +00:00
#include <string.h>
2023-06-03 19:52:10 +00:00
2024-02-18 20:20:52 +00:00
#ifndef _WIN32
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#ifdef __linux__
2024-03-13 19:18:16 +00:00
#include <sys/mman.h>
#include <sys/sendfile.h>
2024-03-26 14:15:34 +00:00
#include <fcntl.h>
#include <desync.h>
2024-03-13 19:18:16 +00:00
2024-02-18 20:20:52 +00:00
#ifdef MFD_CLOEXEC
#include <sys/syscall.h>
#define memfd_create(name, flags) syscall(__NR_memfd_create, name, flags);
#else
#define memfd_create(name, flags) fileno(tmpfile())
#endif
2024-03-13 19:18:16 +00:00
#endif
2023-06-03 19:52:10 +00:00
#else
2024-02-18 20:20:52 +00:00
#include <winsock2.h>
2024-03-06 17:37:59 +00:00
#include <windows.h>
2024-02-18 20:20:52 +00:00
#include <ws2tcpip.h>
2024-03-17 23:23:10 +00:00
#include <mswsock.h>
2023-06-03 19:52:10 +00:00
#endif
#define STR_MODE
2023-06-03 19:52:10 +00:00
#include <params.h>
#include <packets.h>
2024-02-18 20:20:52 +00:00
#include <error.h>
2023-06-03 19:52:10 +00:00
2023-07-30 11:23:11 +00:00
static inline int get_family(struct sockaddr *dst)
{
if (dst->sa_family == AF_INET6) {
struct sockaddr_in6 *d6 = (struct sockaddr_in6 *)dst;
static char *pat = "\0\0\0\0\0\0\0\0\0\0\xff\xff";
if (!memcmp(&d6->sin6_addr, pat, 12)) {
return AF_INET;
}
}
return dst->sa_family;
}
2023-07-03 17:59:39 +00:00
int setttl(int fd, int ttl, int family) {
int _ttl = ttl;
2023-07-30 11:23:11 +00:00
2023-07-03 17:59:39 +00:00
if (family == AF_INET) {
2023-07-30 11:23:11 +00:00
if (setsockopt(fd, IPPROTO_IP,
2024-02-18 20:20:52 +00:00
IP_TTL, (char *)&_ttl, sizeof(_ttl)) < 0) {
uniperror("setsockopt IP_TTL");
2023-07-03 17:59:39 +00:00
return -1;
}
}
2023-07-30 11:23:11 +00:00
else if (setsockopt(fd, IPPROTO_IPV6,
2024-02-18 20:20:52 +00:00
IPV6_UNICAST_HOPS, (char *)&_ttl, sizeof(_ttl)) < 0) {
uniperror("setsockopt IPV6_UNICAST_HOPS");
2023-07-03 17:59:39 +00:00
return -1;
}
return 0;
}
2024-03-01 15:00:25 +00:00
#ifndef _WIN32
static inline void delay(long ms)
2024-03-01 15:00:25 +00:00
{
struct timespec time = {
2024-03-11 23:53:57 +00:00
.tv_nsec = ms * 1e6
2024-03-01 15:00:25 +00:00
};
nanosleep(&time, 0);
}
#else
#define delay(ms) Sleep(ms)
2024-03-01 15:00:25 +00:00
#endif
2024-03-20 22:01:36 +00:00
#ifdef __linux__
void wait_send(int sfd)
{
for (int i = 0; params.wait_send; i++) {
struct tcpi tcpi = {};
2024-03-20 22:01:36 +00:00
socklen_t ts = sizeof(tcpi);
2024-03-20 22:01:36 +00:00
if (getsockopt(sfd, IPPROTO_TCP,
TCP_INFO, (char *)&tcpi, &ts) < 0) {
perror("getsockopt TCP_INFO");
break;
}
if (tcpi.state != 1) {
LOG(LOG_E, "state: %d\n", tcpi.state);
return;
}
2024-03-20 22:01:36 +00:00
if (ts < sizeof(tcpi)) {
LOG(LOG_E, "tcpi_notsent_bytes not provided\n");
params.wait_send = 0;
break;
}
if (tcpi.notsent_bytes == 0) {
return;
}
LOG(LOG_S, "not sent after %d ms\n", i);
delay(1);
}
delay(params.sfdelay);
}
#define wait_send_if_support(sfd) \
if (params.wait_send) wait_send(sfd)
2024-03-20 22:01:36 +00:00
#else
#define wait_send(sfd) delay(params.sfdelay)
#define wait_send_if_support(sfd) // :(
2024-03-20 22:01:36 +00:00
#endif
2024-03-13 19:18:16 +00:00
#ifdef __linux__
ssize_t send_fake(int sfd, char *buffer,
2024-03-26 14:15:34 +00:00
int cnt, long pos, struct sockaddr *dst, struct desync_params *opt)
2023-06-03 19:52:10 +00:00
{
2024-03-26 14:15:34 +00:00
int fa = get_family(dst);
2023-06-03 19:52:10 +00:00
struct packet pkt = cnt != IS_HTTP ? fake_tls : fake_http;
size_t psz = pkt.size;
int ffd = memfd_create("name", O_RDWR);
if (ffd < 0) {
2024-02-24 17:44:54 +00:00
uniperror("memfd_create");
2023-06-03 19:52:10 +00:00
return -1;
}
char *p = 0;
ssize_t len = -1;
2023-06-03 19:52:10 +00:00
while (1) {
2023-06-03 19:52:10 +00:00
if (ftruncate(ffd, pos) < 0) {
2024-02-24 17:44:54 +00:00
uniperror("ftruncate");
2023-06-03 19:52:10 +00:00
break;
}
p = mmap(0, pos, PROT_WRITE, MAP_SHARED, ffd, 0);
if (p == MAP_FAILED) {
2024-02-24 17:44:54 +00:00
uniperror("mmap");
2023-08-20 13:30:03 +00:00
p = 0;
2023-06-03 19:52:10 +00:00
break;
}
memcpy(p, pkt.data, psz < pos ? psz : pos);
2024-03-20 22:01:36 +00:00
if (setttl(sfd, opt->ttl ? opt->ttl : 8, fa) < 0) {
break;
}
2024-03-26 14:15:34 +00:00
if (opt->md5sig) {
struct tcp_md5sig md5 = {
.tcpm_keylen = 5
};
memcpy(&md5.tcpm_addr, dst, sizeof(struct sockaddr_in6));
if (setsockopt(sfd, IPPROTO_TCP,
TCP_MD5SIG, (char *)&md5, sizeof(md5)) < 0) {
perror("setsockopt TCP_MD5SIG");
break;
}
}
if (opt->ip_options && fa == AF_INET
2024-03-20 22:01:36 +00:00
&& setsockopt(sfd, IPPROTO_IP, IP_OPTIONS,
opt->ip_options, opt->ip_options_len) < 0) {
perror("setsockopt IP_OPTIONS");
2023-06-03 19:52:10 +00:00
break;
}
2024-03-26 14:15:34 +00:00
len = sendfile(sfd, ffd, 0, pos);
if (len < 0) {
uniperror("sendfile");
2023-06-03 19:52:10 +00:00
break;
}
2024-03-20 22:01:36 +00:00
wait_send(sfd);
2023-06-03 19:52:10 +00:00
memcpy(p, buffer, pos);
2023-07-03 17:59:39 +00:00
if (setttl(sfd, params.def_ttl, fa) < 0) {
2023-06-03 19:52:10 +00:00
break;
}
2024-03-26 14:15:34 +00:00
if (opt->ip_options && fa == AF_INET
2024-03-20 22:01:36 +00:00
&& setsockopt(sfd, IPPROTO_IP,
IP_OPTIONS, opt->ip_options, 0) < 0) {
perror("setsockopt IP_OPTIONS");
break;
}
2024-03-26 14:15:34 +00:00
if (opt->md5sig) {
struct tcp_md5sig md5 = {
.tcpm_keylen = 0
};
memcpy(&md5.tcpm_addr, dst, sizeof(struct sockaddr_in6));
if (setsockopt(sfd, IPPROTO_TCP,
TCP_MD5SIG, (char *)&md5, sizeof(md5)) < 0) {
perror("setsockopt TCP_MD5SIG");
break;
}
}
break;
2023-06-03 19:52:10 +00:00
}
if (p) munmap(p, pos);
close(ffd);
return len;
2023-06-03 19:52:10 +00:00
}
2024-02-18 20:20:52 +00:00
#endif
2023-06-03 19:52:10 +00:00
2024-03-17 23:23:10 +00:00
#ifdef _WIN32
ssize_t send_fake(int sfd, char *buffer,
2024-03-26 14:15:34 +00:00
int cnt, long pos, struct sockaddr *dst, struct desync_params *opt)
2024-03-17 23:23:10 +00:00
{
2024-03-26 14:15:34 +00:00
int fa = get_family(dst);
2024-03-17 23:23:10 +00:00
struct packet pkt = cnt != IS_HTTP ? fake_tls : fake_http;
size_t psz = pkt.size;
char path[MAX_PATH + 1];
int ps = GetTempPath(sizeof(path), path);
if (!ps) {
2024-03-18 01:42:40 +00:00
uniperror("GetTempPath");
return -1;
}
if (!GetTempFileName(path, "t", 0, path)) {
uniperror("GetTempFileName");
return -1;
}
LOG(LOG_L, "temp file: %s\n", path);
2024-03-17 23:23:10 +00:00
HANDLE hfile = CreateFileA(path, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
2024-03-18 01:42:40 +00:00
CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, NULL);
2024-03-17 23:23:10 +00:00
if (hfile == INVALID_HANDLE_VALUE) {
2024-03-18 01:42:40 +00:00
uniperror("CreateFileA");
2024-03-17 23:23:10 +00:00
return -1;
}
OVERLAPPED ov = {};
ssize_t len = -1;
2024-03-17 23:23:10 +00:00
while (1) {
2024-03-17 23:23:10 +00:00
ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!ov.hEvent) {
2024-03-18 01:42:40 +00:00
uniperror("CreateEvent");
break;
2024-03-17 23:23:10 +00:00
}
2024-03-18 01:42:40 +00:00
if (!WriteFile(hfile, pkt.data, psz < pos ? psz : pos, 0, 0)) {
uniperror("WriteFile");
break;
}
if (psz < pos) {
if (SetFilePointer(hfile, pos, 0, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
uniperror("SetFilePointer");
break;
}
if (!SetEndOfFile(hfile)) {
uniperror("SetFileEnd");
break;
}
}
if (SetFilePointer(hfile, 0, 0, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
uniperror("SetFilePointer");
break;
}
2024-03-20 22:01:36 +00:00
if (setttl(sfd, opt->ttl ? opt->ttl : 8, fa) < 0) {
break;
}
2024-03-26 14:15:34 +00:00
if (opt->ip_options && fa == AF_INET
2024-03-20 22:01:36 +00:00
&& setsockopt(sfd, IPPROTO_IP, IP_OPTIONS,
opt->ip_options, opt->ip_options_len) < 0) {
uniperror("setsockopt IP_OPTIONS");
2024-03-18 01:42:40 +00:00
break;
}
if (!TransmitFile(sfd, hfile, pos, pos, &ov,
NULL, TF_USE_KERNEL_APC | TF_WRITE_BEHIND)) {
if ((GetLastError() != ERROR_IO_PENDING)
&& (WSAGetLastError() != WSA_IO_PENDING)) {
uniperror("TransmitFile");
break;
}
}
wait_send(sfd);
2024-03-18 01:42:40 +00:00
if (SetFilePointer(hfile, 0, 0, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
uniperror("SetFilePointer");
break;
}
if (!WriteFile(hfile, buffer, pos, 0, 0)) {
uniperror("WriteFile");
break;
}
if (setttl(sfd, params.def_ttl, fa) < 0) {
break;
}
2024-03-26 14:15:34 +00:00
if (opt->ip_options && fa == AF_INET
2024-03-20 22:01:36 +00:00
&& setsockopt(sfd, IPPROTO_IP, IP_OPTIONS,
opt->ip_options, 0) < 0) {
uniperror("setsockopt IP_OPTIONS");
2024-03-20 22:01:36 +00:00
break;
}
len = pos;
break;
2024-03-18 01:42:40 +00:00
}
2024-03-17 23:23:10 +00:00
if (!CloseHandle(hfile)) {
2024-03-18 01:42:40 +00:00
uniperror("CloseHandle hfile");
}
if (ov.hEvent && !CloseHandle(ov.hEvent)) {
uniperror("CloseHandle hEvent");
}
return len;
2024-03-17 23:23:10 +00:00
}
#endif
ssize_t send_oob(int sfd, char *buffer,
ssize_t n, long pos)
2023-06-03 19:52:10 +00:00
{
2024-03-06 17:37:59 +00:00
ssize_t size = oob_data.size - 1;
char *data = oob_data.data + 1;
2024-03-03 14:32:56 +00:00
2024-03-06 17:37:59 +00:00
char rchar = buffer[pos];
buffer[pos] = oob_data.data[0];
ssize_t len = send(sfd, buffer, pos + 1, MSG_OOB);
buffer[pos] = rchar;
if (len < 0) {
2024-03-06 17:37:59 +00:00
uniperror("send");
return -1;
}
len--;
if (len != pos) {
return len;
}
if (size) {
wait_send(sfd);
}
for (long i = 0; i < size; i++) {
2024-03-01 15:00:25 +00:00
if (send(sfd, data + i, 1, MSG_OOB) < 0) {
uniperror("send");
if (get_e() == EAGAIN) {
return len;
}
}
if (size != 1) {
wait_send(sfd);
2024-03-01 15:00:25 +00:00
}
2024-02-29 17:07:59 +00:00
}
return len;
}
ssize_t send_disorder(int sfd,
char *buffer, long pos, int fa)
{
int bttl = 1;
2024-03-19 23:23:56 +00:00
if (setttl(sfd, bttl, fa) < 0) {
return -1;
}
ssize_t len = send(sfd, buffer, pos, 0);
if (len < 0) {
2024-02-29 17:07:59 +00:00
uniperror("send");
}
wait_send_if_support(sfd);
if (setttl(sfd, params.def_ttl, fa) < 0) {
return -1;
}
return len;
2024-02-29 17:07:59 +00:00
}
2024-03-20 22:01:36 +00:00
ssize_t desync(int sfd, char *buffer, size_t bfsize,
2024-03-19 23:23:56 +00:00
ssize_t n, ssize_t offset, struct sockaddr *dst, int dp_c)
2023-06-03 19:52:10 +00:00
{
2024-03-08 00:37:02 +00:00
struct desync_params dp = params.dp[dp_c];
2024-03-11 23:53:57 +00:00
2023-06-03 19:52:10 +00:00
char *host = 0;
int len = 0, type = 0;
2023-07-30 11:23:11 +00:00
int fa = get_family(dst);
// parse packet
2023-06-03 19:52:10 +00:00
if ((len = parse_tls(buffer, n, &host))) {
type = IS_HTTPS;
}
else if ((len = parse_http(buffer, n, &host, 0))) {
type = IS_HTTP;
}
2023-08-20 13:30:03 +00:00
if (len && host) {
2024-03-06 17:37:59 +00:00
LOG(LOG_S, "host: %.*s (%ld)\n",
len, host, host - buffer);
2023-08-20 13:30:03 +00:00
}
// modify packet
2024-03-08 00:37:02 +00:00
if (type == IS_HTTP && dp.mod_http) {
2024-02-24 17:44:54 +00:00
LOG(LOG_S, "modify HTTP: n=%ld\n", n);
2024-03-08 00:37:02 +00:00
if (mod_http(buffer, n, dp.mod_http)) {
LOG(LOG_E, "mod http error\n");
2023-06-03 19:52:10 +00:00
return -1;
}
}
2024-03-08 00:37:02 +00:00
else if (type == IS_HTTPS && dp.tlsrec_n) {
2024-03-04 12:30:23 +00:00
long lp = 0;
2024-03-08 00:37:02 +00:00
for (int i = 0; i < dp.tlsrec_n; i++) {
struct part part = dp.tlsrec[i];
long pos = part.pos + i * 5;
if (part.flag == OFFSET_SNI) {
2024-03-04 12:30:23 +00:00
pos += (host - buffer - 5);
}
else if (pos < 0) {
pos += n;
}
if (pos < lp) {
LOG(LOG_E, "tlsrec cancel: %ld < %ld\n", pos, lp);
break;
}
2024-03-04 12:30:23 +00:00
if (!part_tls(buffer + lp,
bfsize - lp, n - lp, pos - lp)) {
LOG(LOG_E, "tlsrec error: pos=%ld, n=%ld\n", pos, n);
2024-03-04 12:30:23 +00:00
break;
}
LOG(LOG_S, "tlsrec: pos=%ld, n=%ld\n", pos, n);
2024-03-04 12:30:23 +00:00
n += 5;
lp = pos + 5;
2024-02-18 14:19:11 +00:00
}
}
// set custom TTL
2023-10-16 12:44:24 +00:00
if (params.custom_ttl) {
if (setttl(sfd, params.def_ttl, fa) < 0) {
return -1;
}
}
// desync
2024-03-19 23:23:56 +00:00
long lp = offset;
2024-03-06 17:37:59 +00:00
if (!type && params.de_known) {
// cancel
2023-06-03 19:52:10 +00:00
}
2024-03-08 00:37:02 +00:00
else for (int i = 0; i < dp.parts_n; i++) {
struct part part = dp.parts[i];
// change pos
long pos = part.pos;
if (part.flag == OFFSET_SNI) {
if (type != IS_HTTPS)
2024-03-06 17:37:59 +00:00
continue;
else
pos += (host - buffer);
}
else if (part.flag == OFFSET_HOST) {
if (type != IS_HTTP)
2024-03-06 17:37:59 +00:00
continue;
else
pos += (host - buffer);
}
else if (pos < 0) {
pos += n;
}
2024-03-19 23:23:56 +00:00
// after EAGAIN
if (pos <= offset) {
continue;
}
else if (pos <= 0 || pos >= n || pos <= lp) {
LOG(LOG_E, "split cancel: pos=%ld-%ld, n=%ld\n", lp, pos, n);
break;
}
// send part
LOG(LOG_S, "split: pos=%ld-%ld, m: %s\n", lp, pos, demode_str[part.m]);
ssize_t s = 0;
switch (part.m) {
#ifdef FAKE_SUPPORT
case DESYNC_FAKE:
s = send_fake(sfd,
2024-03-26 14:15:34 +00:00
buffer + lp, type, pos - lp, dst, &dp);
break;
#endif
case DESYNC_DISORDER:
s = send_disorder(sfd,
buffer + lp, pos - lp, fa);
break;
2024-02-29 17:07:59 +00:00
case DESYNC_OOB:
s = send_oob(sfd,
buffer + lp, n - lp, pos - lp);
wait_send_if_support(sfd);
break;
case DESYNC_SPLIT:
default:
s = send(sfd, buffer + lp, pos - lp, 0);
wait_send_if_support(sfd);
}
2024-03-19 23:23:56 +00:00
if (s < 0) {
if (get_e() == EAGAIN) {
2024-03-19 23:23:56 +00:00
return lp;
}
return -1;
}
else if (s != (pos - lp)) {
LOG(LOG_E, "%ld != %ld\n", s, pos - lp);
return lp + s;
}
lp = pos;
}
// send all/rest
if (lp < n) {
2024-03-11 15:38:39 +00:00
LOG((lp ? LOG_S : LOG_L), "send: pos=%ld-%ld\n", lp, n);
if (send(sfd, buffer + lp, n - lp, 0) < 0) {
2024-03-19 23:23:56 +00:00
if (get_e() == EAGAIN) {
return lp;
}
uniperror("send");
return -1;
}
2023-06-03 19:52:10 +00:00
}
2024-03-19 23:23:56 +00:00
return n;
2023-07-07 20:07:27 +00:00
}