2024-09-08 17:21:26 +00:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
2024-08-07 11:25:26 +00:00
|
|
|
#include "desync.h"
|
|
|
|
|
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>
|
2024-08-19 12:35:13 +00:00
|
|
|
#include <sys/mman.h>
|
2024-02-18 20:20:52 +00:00
|
|
|
#include <arpa/inet.h>
|
2024-08-19 12:35:13 +00:00
|
|
|
#include <fcntl.h>
|
2024-08-19 12:56:18 +00:00
|
|
|
|
2024-08-19 12:35:13 +00:00
|
|
|
#ifndef __linux__
|
2024-07-25 15:27:52 +00:00
|
|
|
#include <netinet/tcp.h>
|
2024-08-09 19:49:44 +00:00
|
|
|
#else
|
2024-08-19 12:35:13 +00:00
|
|
|
#include <linux/tcp.h>
|
2024-08-09 19:49:44 +00:00
|
|
|
#include <linux/filter.h>
|
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>
|
|
|
|
#include <ws2tcpip.h>
|
2024-03-17 23:23:10 +00:00
|
|
|
#include <mswsock.h>
|
2023-06-03 19:52:10 +00:00
|
|
|
#endif
|
2024-03-21 18:44:17 +00:00
|
|
|
#define STR_MODE
|
2023-06-03 19:52:10 +00:00
|
|
|
|
2024-05-02 16:36:29 +00:00
|
|
|
#include "params.h"
|
|
|
|
#include "packets.h"
|
|
|
|
#include "error.h"
|
2023-06-03 19:52:10 +00:00
|
|
|
|
2024-11-19 01:21:02 +00:00
|
|
|
#define WAIT_LIMIT_MS 500
|
2024-11-19 18:28:23 +00:00
|
|
|
#define DEFAULT_TTL 8
|
2024-11-19 01:21:02 +00:00
|
|
|
|
2023-06-03 19:52:10 +00:00
|
|
|
|
2024-10-15 18:52:18 +00:00
|
|
|
int setttl(int fd, int ttl)
|
2023-07-30 11:23:11 +00:00
|
|
|
{
|
2024-10-15 18:52:18 +00:00
|
|
|
int ret6 = setsockopt(fd, IPPROTO_IPV6,
|
|
|
|
IPV6_UNICAST_HOPS, (char *)&ttl, sizeof(ttl));
|
|
|
|
int ret4 = setsockopt(fd, IPPROTO_IP,
|
|
|
|
IP_TTL, (char *)&ttl, sizeof(ttl));
|
2023-07-30 11:23:11 +00:00
|
|
|
|
2024-10-15 18:52:18 +00:00
|
|
|
if (ret4 && ret6) {
|
|
|
|
uniperror("setttl");
|
2023-07-03 17:59:39 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-11-19 18:28:23 +00:00
|
|
|
|
2024-08-09 19:49:44 +00:00
|
|
|
#ifdef __linux__
|
2024-11-19 18:28:23 +00:00
|
|
|
static int get_family(const struct sockaddr_in6 *dst)
|
|
|
|
{
|
|
|
|
static const char map[12] = "\0\0\0\0\0\0\0\0\0\0\xff\xff";
|
|
|
|
if (dst->sin6_family == AF_INET6
|
|
|
|
&& !memcmp(&dst->sin6_addr, map, sizeof(map))) {
|
|
|
|
return AF_INET;
|
|
|
|
}
|
|
|
|
return dst->sin6_family;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-11-09 15:07:27 +00:00
|
|
|
static int drop_sack(int fd)
|
2024-08-09 19:49:44 +00:00
|
|
|
{
|
|
|
|
struct sock_filter code[] = {
|
|
|
|
{ 0x30, 0, 0, 0x0000000c },
|
|
|
|
{ 0x74, 0, 0, 0x00000004 },
|
|
|
|
{ 0x35, 0, 3, 0x0000000b },
|
|
|
|
{ 0x30, 0, 0, 0x00000022 },
|
|
|
|
{ 0x15, 0, 1, 0x00000005 },
|
|
|
|
{ 0x6, 0, 0, 0x00000000 },
|
|
|
|
{ 0x6, 0, 0, 0x00040000 },
|
|
|
|
};
|
|
|
|
struct sock_fprog bpf = {
|
|
|
|
.len = sizeof(code)/sizeof(*code),
|
|
|
|
.filter = code
|
|
|
|
};
|
|
|
|
if (setsockopt(fd, SOL_SOCKET,
|
|
|
|
SO_ATTACH_FILTER, (char *)&bpf, sizeof(bpf)) == -1) {
|
|
|
|
uniperror("setsockopt SO_ATTACH_FILTER");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-03-05 20:22:12 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-11-19 18:28:23 +00:00
|
|
|
|
2024-11-19 01:21:02 +00:00
|
|
|
static void wait_send_if_support(int sfd)
|
2024-03-20 22:01:36 +00:00
|
|
|
{
|
2024-11-19 01:21:02 +00:00
|
|
|
int i = 0;
|
|
|
|
for (; params.wait_send && i < WAIT_LIMIT_MS; i++) {
|
|
|
|
struct tcp_info tcpi;
|
2024-03-20 22:01:36 +00:00
|
|
|
socklen_t ts = sizeof(tcpi);
|
2024-03-21 18:44:17 +00:00
|
|
|
|
2024-03-20 22:01:36 +00:00
|
|
|
if (getsockopt(sfd, IPPROTO_TCP,
|
|
|
|
TCP_INFO, (char *)&tcpi, &ts) < 0) {
|
2024-08-19 12:56:18 +00:00
|
|
|
perror("getsockopt TCP_INFO");
|
2024-03-20 22:01:36 +00:00
|
|
|
break;
|
|
|
|
}
|
2024-08-19 12:56:18 +00:00
|
|
|
if (tcpi.tcpi_state != 1) {
|
|
|
|
LOG(LOG_E, "state: %d\n", tcpi.tcpi_state);
|
2024-11-19 01:21:02 +00:00
|
|
|
break;
|
2024-03-21 18:44:17 +00:00
|
|
|
}
|
2024-11-19 01:21:02 +00:00
|
|
|
if (ts <= offsetof(struct tcp_info, tcpi_notsent_bytes)) {
|
2024-03-20 22:01:36 +00:00
|
|
|
LOG(LOG_E, "tcpi_notsent_bytes not provided\n");
|
|
|
|
params.wait_send = 0;
|
|
|
|
break;
|
|
|
|
}
|
2024-08-19 12:56:18 +00:00
|
|
|
if (tcpi.tcpi_notsent_bytes == 0) {
|
2024-11-19 01:21:02 +00:00
|
|
|
break;
|
2024-03-20 22:01:36 +00:00
|
|
|
}
|
|
|
|
delay(1);
|
|
|
|
}
|
2024-11-19 01:21:02 +00:00
|
|
|
if (i) LOG(LOG_S, "waiting for send: %d ms\n", i);
|
2024-03-20 22:01:36 +00:00
|
|
|
}
|
|
|
|
#else
|
2024-11-19 01:21:02 +00:00
|
|
|
#define wait_send_if_support(sfd)
|
2024-03-20 22:01:36 +00:00
|
|
|
#endif
|
|
|
|
|
2024-11-19 18:28:23 +00:00
|
|
|
|
2024-11-16 17:59:25 +00:00
|
|
|
static struct packet get_tcp_fake(const char *buffer, size_t n,
|
|
|
|
struct proto_info *info, const struct desync_params *opt)
|
2023-06-03 19:52:10 +00:00
|
|
|
{
|
2024-04-23 05:47:27 +00:00
|
|
|
struct packet pkt;
|
|
|
|
if (opt->fake_data.data) {
|
|
|
|
pkt = opt->fake_data;
|
|
|
|
}
|
|
|
|
else {
|
2024-11-16 17:59:25 +00:00
|
|
|
if (!info->type) {
|
|
|
|
if (is_tls_chello(buffer, n)) info->type = IS_HTTPS;
|
|
|
|
else if (is_http(buffer, n)) info->type = IS_HTTP;
|
|
|
|
}
|
|
|
|
pkt = info->type == IS_HTTP ? fake_http : fake_tls;
|
2024-04-23 05:47:27 +00:00
|
|
|
}
|
2024-08-06 16:11:01 +00:00
|
|
|
if (opt->fake_offset) {
|
|
|
|
if (pkt.size > opt->fake_offset) {
|
|
|
|
pkt.size -= opt->fake_offset;
|
|
|
|
pkt.data += opt->fake_offset;
|
|
|
|
}
|
|
|
|
else pkt.size = 0;
|
|
|
|
}
|
2024-11-16 17:59:25 +00:00
|
|
|
return pkt;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
static ssize_t send_fake(int sfd, const char *buffer,
|
|
|
|
long pos, const struct desync_params *opt, struct packet pkt)
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 addr = {};
|
|
|
|
socklen_t addr_size = sizeof(addr);
|
2024-11-19 18:28:23 +00:00
|
|
|
|
|
|
|
if (opt->md5sig || opt->ip_options) {
|
2024-11-16 17:59:25 +00:00
|
|
|
if (getpeername(sfd,
|
|
|
|
(struct sockaddr *)&addr, &addr_size) < 0) {
|
|
|
|
uniperror("getpeername");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2024-09-08 17:21:26 +00:00
|
|
|
int fds[2];
|
|
|
|
if (pipe(fds) < 0) {
|
|
|
|
uniperror("pipe");
|
2023-06-03 19:52:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
char *p = 0;
|
2024-03-21 18:44:17 +00:00
|
|
|
ssize_t len = -1;
|
2023-06-03 19:52:10 +00:00
|
|
|
|
2024-03-21 18:44:17 +00:00
|
|
|
while (1) {
|
2024-09-08 17:21:26 +00:00
|
|
|
p = mmap(0, pos, PROT_WRITE | PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
|
2023-06-03 19:52:10 +00:00
|
|
|
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;
|
|
|
|
}
|
2024-08-06 16:11:01 +00:00
|
|
|
memcpy(p, pkt.data, pkt.size < pos ? pkt.size : pos);
|
2023-06-03 19:52:10 +00:00
|
|
|
|
2024-11-19 18:28:23 +00:00
|
|
|
if (setttl(sfd, opt->ttl ? opt->ttl : DEFAULT_TTL) < 0) {
|
2024-03-20 22:01:36 +00:00
|
|
|
break;
|
|
|
|
}
|
2024-03-26 14:15:34 +00:00
|
|
|
if (opt->md5sig) {
|
|
|
|
struct tcp_md5sig md5 = {
|
|
|
|
.tcpm_keylen = 5
|
|
|
|
};
|
2024-03-28 20:28:09 +00:00
|
|
|
memcpy(&md5.tcpm_addr, &addr, addr_size);
|
2024-03-26 14:15:34 +00:00
|
|
|
|
|
|
|
if (setsockopt(sfd, IPPROTO_TCP,
|
|
|
|
TCP_MD5SIG, (char *)&md5, sizeof(md5)) < 0) {
|
2024-07-22 17:20:26 +00:00
|
|
|
uniperror("setsockopt TCP_MD5SIG");
|
2024-03-26 14:15:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-11-19 18:28:23 +00:00
|
|
|
if (opt->ip_options && get_family(&addr) == AF_INET
|
2024-03-20 22:01:36 +00:00
|
|
|
&& setsockopt(sfd, IPPROTO_IP, IP_OPTIONS,
|
|
|
|
opt->ip_options, opt->ip_options_len) < 0) {
|
2024-07-22 17:20:26 +00:00
|
|
|
uniperror("setsockopt IP_OPTIONS");
|
2024-11-19 18:28:23 +00:00
|
|
|
break;
|
2023-06-03 19:52:10 +00:00
|
|
|
}
|
2024-09-08 17:21:26 +00:00
|
|
|
struct iovec vec = { .iov_base = p, .iov_len = pos };
|
2024-08-22 14:53:17 +00:00
|
|
|
|
2024-09-08 17:21:26 +00:00
|
|
|
len = vmsplice(fds[1], &vec, 1, SPLICE_F_GIFT);
|
|
|
|
if (len < 0) {
|
|
|
|
uniperror("vmsplice");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
len = splice(fds[0], 0, sfd, 0, len, 0);
|
2024-03-21 18:44:17 +00:00
|
|
|
if (len < 0) {
|
2024-09-08 17:21:26 +00:00
|
|
|
uniperror("splice");
|
2023-06-03 19:52:10 +00:00
|
|
|
break;
|
|
|
|
}
|
2024-11-19 01:21:02 +00:00
|
|
|
wait_send_if_support(sfd);
|
2023-06-03 19:52:10 +00:00
|
|
|
memcpy(p, buffer, pos);
|
|
|
|
|
2024-10-15 18:52:18 +00:00
|
|
|
if (setttl(sfd, params.def_ttl) < 0) {
|
2023-06-03 19:52:10 +00:00
|
|
|
break;
|
|
|
|
}
|
2024-11-19 18:28:23 +00:00
|
|
|
if (opt->ip_options && get_family(&addr) == AF_INET
|
2024-03-20 22:01:36 +00:00
|
|
|
&& setsockopt(sfd, IPPROTO_IP,
|
|
|
|
IP_OPTIONS, opt->ip_options, 0) < 0) {
|
2024-07-22 17:20:26 +00:00
|
|
|
uniperror("setsockopt IP_OPTIONS");
|
2024-11-19 18:28:23 +00:00
|
|
|
break;
|
2024-03-20 22:01:36 +00:00
|
|
|
}
|
2024-03-26 14:15:34 +00:00
|
|
|
if (opt->md5sig) {
|
|
|
|
struct tcp_md5sig md5 = {
|
|
|
|
.tcpm_keylen = 0
|
|
|
|
};
|
2024-03-28 20:28:09 +00:00
|
|
|
memcpy(&md5.tcpm_addr, &addr, addr_size);
|
2024-03-26 14:15:34 +00:00
|
|
|
|
|
|
|
if (setsockopt(sfd, IPPROTO_TCP,
|
|
|
|
TCP_MD5SIG, (char *)&md5, sizeof(md5)) < 0) {
|
2024-07-22 17:20:26 +00:00
|
|
|
uniperror("setsockopt TCP_MD5SIG");
|
2024-03-26 14:15:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-03-21 18:44:17 +00:00
|
|
|
break;
|
2023-06-03 19:52:10 +00:00
|
|
|
}
|
|
|
|
if (p) munmap(p, pos);
|
2024-09-08 17:21:26 +00:00
|
|
|
close(fds[0]);
|
|
|
|
close(fds[1]);
|
2024-03-21 18:44:17 +00:00
|
|
|
return len;
|
2023-06-03 19:52:10 +00:00
|
|
|
}
|
2024-09-30 10:39:10 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2024-07-22 17:20:26 +00:00
|
|
|
OVERLAPPED ov = {};
|
|
|
|
|
2024-11-09 15:07:27 +00:00
|
|
|
static ssize_t send_fake(int sfd, const char *buffer,
|
2024-11-16 17:59:25 +00:00
|
|
|
long pos, const struct desync_params *opt, struct packet pkt)
|
2024-03-17 23:23:10 +00:00
|
|
|
{
|
2024-05-14 02:07:01 +00:00
|
|
|
char path[MAX_PATH], temp[MAX_PATH + 1];
|
2024-05-03 08:49:21 +00:00
|
|
|
int ps = GetTempPath(sizeof(temp), temp);
|
2024-03-17 23:23:10 +00:00
|
|
|
if (!ps) {
|
2024-03-18 01:42:40 +00:00
|
|
|
uniperror("GetTempPath");
|
|
|
|
return -1;
|
|
|
|
}
|
2024-05-03 08:49:21 +00:00
|
|
|
if (!GetTempFileName(temp, "t", 0, path)) {
|
2024-03-18 01:42:40 +00:00
|
|
|
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,
|
2024-05-14 02:07:01 +00:00
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, 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;
|
|
|
|
}
|
2024-03-21 18:44:17 +00:00
|
|
|
ssize_t len = -1;
|
2024-03-17 23:23:10 +00:00
|
|
|
|
2024-03-21 18:44:17 +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-08-25 08:28:24 +00:00
|
|
|
DWORD wrtcnt = 0;
|
|
|
|
if (!WriteFile(hfile, pkt.data, pkt.size < pos ? pkt.size : pos, &wrtcnt, 0)) {
|
2024-03-18 01:42:40 +00:00
|
|
|
uniperror("WriteFile");
|
|
|
|
break;
|
|
|
|
}
|
2024-08-25 08:28:24 +00:00
|
|
|
if (pkt.size < pos) {
|
2024-03-18 01:42:40 +00:00
|
|
|
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-11-19 18:28:23 +00:00
|
|
|
if (setttl(sfd, opt->ttl ? opt->ttl : DEFAULT_TTL) < 0) {
|
2024-03-20 22:01:36 +00:00
|
|
|
break;
|
|
|
|
}
|
2024-03-18 01:42:40 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2024-11-19 01:21:02 +00:00
|
|
|
//Sleep(3);
|
2024-03-18 01:42:40 +00:00
|
|
|
|
|
|
|
if (SetFilePointer(hfile, 0, 0, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
|
|
|
|
uniperror("SetFilePointer");
|
|
|
|
break;
|
|
|
|
}
|
2024-08-25 08:28:24 +00:00
|
|
|
if (!WriteFile(hfile, buffer, pos, &wrtcnt, 0)) {
|
2024-03-18 01:42:40 +00:00
|
|
|
uniperror("WriteFile");
|
|
|
|
break;
|
|
|
|
}
|
2024-10-15 18:52:18 +00:00
|
|
|
if (setttl(sfd, params.def_ttl) < 0) {
|
2024-03-18 01:42:40 +00:00
|
|
|
break;
|
|
|
|
}
|
2024-03-21 18:44:17 +00:00
|
|
|
len = pos;
|
|
|
|
break;
|
2024-03-18 01:42:40 +00:00
|
|
|
}
|
2024-05-14 02:07:01 +00:00
|
|
|
if (!CloseHandle(hfile)
|
|
|
|
|| (ov.hEvent && !CloseHandle(ov.hEvent))) {
|
|
|
|
uniperror("CloseHandle");
|
|
|
|
return -1;
|
2024-03-18 01:42:40 +00:00
|
|
|
}
|
2024-03-21 18:44:17 +00:00
|
|
|
return len;
|
2024-03-17 23:23:10 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2024-11-09 15:07:27 +00:00
|
|
|
static ssize_t send_oob(int sfd, char *buffer,
|
|
|
|
ssize_t n, long pos, const char *c)
|
2023-06-03 19:52:10 +00:00
|
|
|
{
|
2024-03-06 17:37:59 +00:00
|
|
|
char rchar = buffer[pos];
|
2024-08-16 16:08:22 +00:00
|
|
|
buffer[pos] = c[1] ? c[0] : 'a';
|
2024-03-06 17:37:59 +00:00
|
|
|
|
2024-03-21 18:44:17 +00:00
|
|
|
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;
|
|
|
|
}
|
2024-08-16 16:08:22 +00:00
|
|
|
wait_send_if_support(sfd);
|
|
|
|
|
2024-03-21 18:44:17 +00:00
|
|
|
len--;
|
|
|
|
if (len != pos) {
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
return len;
|
2024-03-04 01:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-11-09 15:07:27 +00:00
|
|
|
static ssize_t send_disorder(int sfd,
|
|
|
|
const char *buffer, long pos)
|
2024-03-04 01:49:08 +00:00
|
|
|
{
|
|
|
|
int bttl = 1;
|
2024-03-19 23:23:56 +00:00
|
|
|
|
2024-10-15 18:52:18 +00:00
|
|
|
if (setttl(sfd, bttl) < 0) {
|
2024-03-04 01:49:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2024-03-21 18:44:17 +00:00
|
|
|
ssize_t len = send(sfd, buffer, pos, 0);
|
|
|
|
if (len < 0) {
|
2024-02-29 17:07:59 +00:00
|
|
|
uniperror("send");
|
|
|
|
}
|
2024-08-16 16:08:22 +00:00
|
|
|
else wait_send_if_support(sfd);
|
2024-03-21 18:44:17 +00:00
|
|
|
|
2024-10-15 18:52:18 +00:00
|
|
|
if (setttl(sfd, params.def_ttl) < 0) {
|
2024-03-04 01:49:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2024-03-21 18:44:17 +00:00
|
|
|
return len;
|
2024-02-29 17:07:59 +00:00
|
|
|
}
|
|
|
|
|
2024-03-04 01:49:08 +00:00
|
|
|
|
2024-11-09 15:07:27 +00:00
|
|
|
static ssize_t send_late_oob(int sfd, char *buffer,
|
|
|
|
ssize_t n, long pos, const char *c)
|
2024-08-06 16:11:01 +00:00
|
|
|
{
|
|
|
|
int bttl = 1;
|
|
|
|
|
2024-10-15 18:52:18 +00:00
|
|
|
if (setttl(sfd, bttl) < 0) {
|
2024-08-06 16:11:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2024-08-16 16:08:22 +00:00
|
|
|
ssize_t len = send_oob(sfd, buffer, n, pos, c);
|
2024-08-06 16:11:01 +00:00
|
|
|
if (len < 0) {
|
|
|
|
uniperror("send");
|
|
|
|
}
|
2024-10-15 18:52:18 +00:00
|
|
|
if (setttl(sfd, params.def_ttl) < 0) {
|
2024-08-06 16:11:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-11-16 17:59:25 +00:00
|
|
|
static void init_proto_info(
|
|
|
|
const char *buffer, size_t n, struct proto_info *info)
|
|
|
|
{
|
|
|
|
if (!info->init) {
|
|
|
|
char *host = 0;
|
|
|
|
|
|
|
|
if ((info->host_len = parse_tls(buffer, n, &host))) {
|
|
|
|
info->type = IS_HTTPS;
|
|
|
|
}
|
|
|
|
else if ((info->host_len = parse_http(buffer, n, &host, 0))) {
|
|
|
|
info->type = IS_HTTP;
|
|
|
|
}
|
|
|
|
info->host_pos = host ? host - buffer : 0;
|
|
|
|
info->init = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-15 01:33:20 +00:00
|
|
|
static long gen_offset(long pos, int flag,
|
2024-11-16 17:59:25 +00:00
|
|
|
const char *buffer, size_t n, long lp, struct proto_info *info)
|
2024-10-15 01:33:20 +00:00
|
|
|
{
|
|
|
|
if (flag & (OFFSET_SNI | OFFSET_HOST)) {
|
2024-11-16 17:59:25 +00:00
|
|
|
init_proto_info(buffer, n, info);
|
|
|
|
|
|
|
|
if (!info->host_pos
|
|
|
|
|| ((flag & OFFSET_SNI) && info->type != IS_HTTPS)) {
|
2024-10-15 01:33:20 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2024-11-16 17:59:25 +00:00
|
|
|
pos += info->host_pos;
|
2024-10-15 01:33:20 +00:00
|
|
|
|
|
|
|
if (flag & OFFSET_END)
|
2024-11-16 17:59:25 +00:00
|
|
|
pos += info->host_len;
|
2024-10-15 01:33:20 +00:00
|
|
|
else if (flag & OFFSET_MID)
|
2024-11-16 17:59:25 +00:00
|
|
|
pos += (info->host_len / 2);
|
2024-10-15 01:33:20 +00:00
|
|
|
else if (flag & OFFSET_RAND)
|
2024-11-16 17:59:25 +00:00
|
|
|
pos += (rand() % info->host_len);
|
2024-10-15 01:33:20 +00:00
|
|
|
}
|
|
|
|
else if (flag & OFFSET_RAND) {
|
|
|
|
pos += lp + (rand() % (n - lp));
|
|
|
|
}
|
|
|
|
else if (flag & OFFSET_MID) {
|
|
|
|
pos += (n / 2);
|
|
|
|
}
|
|
|
|
else if (pos < 0 || (flag & OFFSET_END)) {
|
|
|
|
pos += n;
|
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-11-16 17:59:25 +00:00
|
|
|
static ssize_t tamp(char *buffer, size_t bfsize, ssize_t n,
|
|
|
|
const struct desync_params *dp, struct proto_info *info)
|
2023-06-03 19:52:10 +00:00
|
|
|
{
|
2024-11-16 17:59:25 +00:00
|
|
|
if (dp->mod_http && is_http(buffer, n)) {
|
2024-08-07 11:44:12 +00:00
|
|
|
LOG(LOG_S, "modify HTTP: n=%zd\n", n);
|
2024-11-16 17:59:25 +00:00
|
|
|
if (mod_http(buffer, n, dp->mod_http)) {
|
2024-02-24 21:40:54 +00:00
|
|
|
LOG(LOG_E, "mod http error\n");
|
2023-06-03 19:52:10 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-16 17:59:25 +00:00
|
|
|
else if (dp->tlsrec_n && is_tls_chello(buffer, n)) {
|
2024-03-04 12:30:23 +00:00
|
|
|
long lp = 0;
|
2024-10-17 05:45:50 +00:00
|
|
|
struct part part;
|
|
|
|
int i = 0, r = 0, rc = 0;
|
|
|
|
|
2024-11-16 17:59:25 +00:00
|
|
|
for (; r > 0 || i < dp->tlsrec_n; rc++, r--) {
|
2024-10-17 06:44:18 +00:00
|
|
|
if (r <= 0) {
|
2024-11-16 17:59:25 +00:00
|
|
|
part = dp->tlsrec[i];
|
2024-10-17 05:45:50 +00:00
|
|
|
r = part.r; i++;
|
|
|
|
}
|
|
|
|
long pos = rc * 5;
|
2024-10-15 17:07:55 +00:00
|
|
|
pos += gen_offset(part.pos,
|
2024-11-16 17:59:25 +00:00
|
|
|
part.flag, buffer, n - pos, lp, info);
|
|
|
|
|
2024-12-06 08:33:47 +00:00
|
|
|
if (part.pos < 0 || part.flag) {
|
2024-11-16 17:59:25 +00:00
|
|
|
pos -= 5;
|
|
|
|
}
|
|
|
|
pos += (long )part.s * (part.r - r);
|
2024-03-05 20:22:12 +00:00
|
|
|
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)) {
|
2024-08-07 11:44:12 +00:00
|
|
|
LOG(LOG_E, "tlsrec error: pos=%ld, n=%zd\n", pos, n);
|
2024-03-04 12:30:23 +00:00
|
|
|
break;
|
|
|
|
}
|
2024-08-07 11:44:12 +00:00
|
|
|
LOG(LOG_S, "tlsrec: pos=%ld, n=%zd\n", pos, n);
|
2024-03-04 12:30:23 +00:00
|
|
|
n += 5;
|
|
|
|
lp = pos + 5;
|
2024-02-18 14:19:11 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-16 17:59:25 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ssize_t desync(int sfd, char *buffer, size_t bfsize,
|
|
|
|
ssize_t n, ssize_t offset, const struct sockaddr *dst, int dp_c)
|
|
|
|
{
|
|
|
|
struct desync_params dp = params.dp[dp_c];
|
|
|
|
struct proto_info info = { 0 };
|
|
|
|
|
|
|
|
if (offset == 0 && params.debug) {
|
|
|
|
init_proto_info(buffer, n, &info);
|
|
|
|
|
|
|
|
if (info.host_pos) {
|
|
|
|
LOG(LOG_S, "host: %.*s (%d)\n",
|
|
|
|
info.host_len, buffer + info.host_pos, info.host_pos);
|
|
|
|
} else {
|
|
|
|
INIT_HEX_STR(buffer, (n > 16 ? 16 : n));
|
|
|
|
LOG(LOG_S, "bytes: %s (%zd)\n", HEX_STR, n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n = tamp(buffer, bfsize, n, &dp, &info);
|
2024-08-09 19:49:44 +00:00
|
|
|
#ifdef __linux__
|
2024-10-20 15:50:08 +00:00
|
|
|
if (!offset && dp.drop_sack && drop_sack(sfd)) {
|
2024-08-09 19:49:44 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
2024-10-20 15:50:08 +00:00
|
|
|
long lp = 0;
|
2024-10-17 05:45:50 +00:00
|
|
|
struct part part;
|
|
|
|
int i = 0, r = 0;
|
2024-03-04 01:49:08 +00:00
|
|
|
|
2024-10-17 05:45:50 +00:00
|
|
|
for (; r > 0 || i < dp.parts_n; r--) {
|
2024-10-17 06:44:18 +00:00
|
|
|
if (r <= 0) {
|
2024-10-17 05:45:50 +00:00
|
|
|
part = dp.parts[i];
|
|
|
|
r = part.r; i++;
|
|
|
|
}
|
2024-11-16 17:59:25 +00:00
|
|
|
long pos = gen_offset(part.pos, part.flag, buffer, n, lp, &info);
|
|
|
|
pos += (long )part.s * (part.r - r);
|
2024-10-17 05:45:50 +00:00
|
|
|
|
2024-10-20 15:50:08 +00:00
|
|
|
if (!(part.flag & OFFSET_START) && offset && pos <= offset) {
|
|
|
|
LOG(LOG_S, "offset: %zd, skip\n", offset);
|
2024-03-19 23:23:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
2024-10-20 15:50:08 +00:00
|
|
|
if (pos < 0 || pos > n || pos < lp) {
|
2024-08-07 11:44:12 +00:00
|
|
|
LOG(LOG_E, "split cancel: pos=%ld-%ld, n=%zd\n", lp, pos, n);
|
2024-10-17 05:45:50 +00:00
|
|
|
break;
|
2024-03-04 01:49:08 +00:00
|
|
|
}
|
2024-10-20 15:50:08 +00:00
|
|
|
|
2024-03-21 18:44:17 +00:00
|
|
|
ssize_t s = 0;
|
2024-03-05 20:22:12 +00:00
|
|
|
switch (part.m) {
|
2024-03-21 18:44:17 +00:00
|
|
|
#ifdef FAKE_SUPPORT
|
|
|
|
case DESYNC_FAKE:
|
2024-08-16 16:08:22 +00:00
|
|
|
if (pos != lp) s = send_fake(sfd,
|
2024-11-16 17:59:25 +00:00
|
|
|
buffer + lp, pos - lp, &dp, get_tcp_fake(buffer, n, &info, &dp));
|
2024-03-21 18:44:17 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case DESYNC_DISORDER:
|
|
|
|
s = send_disorder(sfd,
|
2024-10-15 18:52:18 +00:00
|
|
|
buffer + lp, pos - lp);
|
2024-03-21 18:44:17 +00:00
|
|
|
break;
|
2024-02-29 17:07:59 +00:00
|
|
|
|
2024-03-21 18:44:17 +00:00
|
|
|
case DESYNC_OOB:
|
|
|
|
s = send_oob(sfd,
|
2024-08-16 16:08:22 +00:00
|
|
|
buffer + lp, n - lp, pos - lp, dp.oob_char);
|
2024-03-21 18:44:17 +00:00
|
|
|
break;
|
|
|
|
|
2024-08-16 16:08:22 +00:00
|
|
|
case DESYNC_DISOOB:
|
2024-08-06 16:11:01 +00:00
|
|
|
s = send_late_oob(sfd,
|
2024-10-15 18:52:18 +00:00
|
|
|
buffer + lp, n - lp, pos - lp, dp.oob_char);
|
2024-08-06 16:11:01 +00:00
|
|
|
break;
|
|
|
|
|
2024-03-21 18:44:17 +00:00
|
|
|
case DESYNC_SPLIT:
|
2024-05-14 13:16:52 +00:00
|
|
|
case DESYNC_NONE:
|
2024-03-21 18:44:17 +00:00
|
|
|
s = send(sfd, buffer + lp, pos - lp, 0);
|
|
|
|
wait_send_if_support(sfd);
|
2024-05-14 13:23:17 +00:00
|
|
|
break;
|
2024-05-14 02:07:01 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return -1;
|
2024-03-04 01:49:08 +00:00
|
|
|
}
|
2024-08-07 11:44:12 +00:00
|
|
|
LOG(LOG_S, "split: pos=%ld-%ld (%zd), m: %s\n", lp, pos, s, demode_str[part.m]);
|
2024-05-14 02:07:01 +00:00
|
|
|
|
2024-03-19 23:23:56 +00:00
|
|
|
if (s < 0) {
|
2024-03-21 18:44:17 +00:00
|
|
|
if (get_e() == EAGAIN) {
|
2024-03-19 23:23:56 +00:00
|
|
|
return lp;
|
|
|
|
}
|
2024-03-04 01:49:08 +00:00
|
|
|
return -1;
|
2024-03-21 18:44:17 +00:00
|
|
|
}
|
|
|
|
else if (s != (pos - lp)) {
|
2024-08-07 11:44:12 +00:00
|
|
|
LOG(LOG_E, "%zd != %ld\n", s, pos - lp);
|
2024-03-21 18:44:17 +00:00
|
|
|
return lp + s;
|
2024-03-04 01:49:08 +00:00
|
|
|
}
|
|
|
|
lp = pos;
|
|
|
|
}
|
2024-03-21 18:44:17 +00:00
|
|
|
// send all/rest
|
2024-03-04 01:49:08 +00:00
|
|
|
if (lp < n) {
|
2024-08-07 11:44:12 +00:00
|
|
|
LOG((lp ? LOG_S : LOG_L), "send: pos=%ld-%zd\n", lp, n);
|
2024-03-04 01:49:08 +00:00
|
|
|
if (send(sfd, buffer + lp, n - lp, 0) < 0) {
|
2024-03-19 23:23:56 +00:00
|
|
|
if (get_e() == EAGAIN) {
|
|
|
|
return lp;
|
|
|
|
}
|
2024-03-04 01:49:08 +00:00
|
|
|
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
|
|
|
}
|
2024-07-29 09:26:42 +00:00
|
|
|
|
|
|
|
|
2024-08-09 19:49:44 +00:00
|
|
|
int post_desync(int sfd, int dp_c)
|
|
|
|
{
|
|
|
|
struct desync_params *dp = ¶ms.dp[dp_c];
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
if (dp->drop_sack) {
|
|
|
|
if (setsockopt(sfd, SOL_SOCKET,
|
|
|
|
SO_DETACH_FILTER, &dp_c, sizeof(dp_c)) == -1) {
|
|
|
|
uniperror("setsockopt SO_DETACH_FILTER");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-29 09:26:42 +00:00
|
|
|
ssize_t desync_udp(int sfd, char *buffer, size_t bfsize,
|
2024-11-09 15:07:27 +00:00
|
|
|
ssize_t n, const struct sockaddr *dst, int dp_c)
|
2024-07-29 09:26:42 +00:00
|
|
|
{
|
|
|
|
struct desync_params *dp = ¶ms.dp[dp_c];
|
|
|
|
|
|
|
|
if (dp->udp_fake_count != 0) {
|
|
|
|
struct packet pkt;
|
|
|
|
if (dp->fake_data.data) {
|
|
|
|
pkt = dp->fake_data;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pkt = fake_udp;
|
|
|
|
}
|
2024-08-16 16:08:22 +00:00
|
|
|
if (dp->fake_offset) {
|
|
|
|
if (pkt.size > dp->fake_offset) {
|
|
|
|
pkt.size -= dp->fake_offset;
|
|
|
|
pkt.data += dp->fake_offset;
|
|
|
|
}
|
|
|
|
else pkt.size = 0;
|
|
|
|
}
|
2024-11-19 18:28:23 +00:00
|
|
|
int bttl = dp->ttl ? dp->ttl : DEFAULT_TTL;
|
2024-10-15 18:52:18 +00:00
|
|
|
if (setttl(sfd, bttl) < 0) {
|
2024-07-29 09:26:42 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < dp->udp_fake_count; i++) {
|
|
|
|
ssize_t len = sendto(sfd, pkt.data,
|
|
|
|
pkt.size, 0, dst, sizeof(struct sockaddr_in6));
|
|
|
|
if (len < 0) {
|
|
|
|
uniperror("send");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2024-10-15 18:52:18 +00:00
|
|
|
if (setttl(sfd, params.def_ttl) < 0) {
|
2024-07-29 09:26:42 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sendto(sfd, buffer, n, 0,
|
|
|
|
dst, sizeof(struct sockaddr_in6));
|
|
|
|
}
|