2023-06-12 07:00:33 +03:00
|
|
|
#include <stdio.h>
|
2023-06-12 11:01:40 +03:00
|
|
|
#include <string.h>
|
2023-06-03 22:52:10 +03:00
|
|
|
|
2024-02-18 23:20:52 +03: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>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
#include <sys/sendfile.h>
|
|
|
|
#define _sendfile(outfd, infd, start, len) sendfile(outfd, infd, start, len)
|
|
|
|
#else
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#define _sendfile(outfd, infd, start, len) sendfile(infd, outfd, start, len, 0, 0)
|
|
|
|
#endif
|
2023-06-03 22:52:10 +03:00
|
|
|
|
2024-02-18 23:20:52 +03: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
|
2023-06-03 22:52:10 +03:00
|
|
|
#else
|
2024-02-18 23:20:52 +03:00
|
|
|
#include <winsock2.h>
|
2024-03-06 20:37:59 +03:00
|
|
|
#include <windows.h>
|
2024-02-18 23:20:52 +03:00
|
|
|
#include <ws2tcpip.h>
|
2023-06-03 22:52:10 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <params.h>
|
|
|
|
#include <packets.h>
|
2024-02-18 23:20:52 +03:00
|
|
|
#include <error.h>
|
2023-06-03 22:52:10 +03:00
|
|
|
|
|
|
|
|
2023-07-30 14:23:11 +03: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 20:59:39 +03:00
|
|
|
int setttl(int fd, int ttl, int family) {
|
|
|
|
int _ttl = ttl;
|
2023-07-30 14:23:11 +03:00
|
|
|
|
2023-07-03 20:59:39 +03:00
|
|
|
if (family == AF_INET) {
|
2023-07-30 14:23:11 +03:00
|
|
|
if (setsockopt(fd, IPPROTO_IP,
|
2024-02-18 23:20:52 +03:00
|
|
|
IP_TTL, (char *)&_ttl, sizeof(_ttl)) < 0) {
|
|
|
|
uniperror("setsockopt IP_TTL");
|
2023-07-03 20:59:39 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2023-07-30 14:23:11 +03:00
|
|
|
else if (setsockopt(fd, IPPROTO_IPV6,
|
2024-02-18 23:20:52 +03:00
|
|
|
IPV6_UNICAST_HOPS, (char *)&_ttl, sizeof(_ttl)) < 0) {
|
|
|
|
uniperror("setsockopt IPV6_UNICAST_HOPS");
|
2023-07-03 20:59:39 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-03-01 18:00:25 +03:00
|
|
|
#ifndef _WIN32
|
2024-03-05 23:22:12 +03:00
|
|
|
static inline void delay(long ms)
|
2024-03-01 18:00:25 +03:00
|
|
|
{
|
|
|
|
struct timespec time = {
|
2024-03-05 23:22:12 +03:00
|
|
|
.tv_nsec = ms * 1000000
|
2024-03-01 18:00:25 +03:00
|
|
|
};
|
|
|
|
nanosleep(&time, 0);
|
|
|
|
}
|
|
|
|
#else
|
2024-03-05 23:22:12 +03:00
|
|
|
#define delay(ms) Sleep(ms)
|
2024-03-01 18:00:25 +03:00
|
|
|
#endif
|
|
|
|
|
2024-02-18 23:20:52 +03:00
|
|
|
#ifndef _WIN32
|
2024-03-04 04:49:08 +03:00
|
|
|
int send_fake(int sfd, char *buffer,
|
2024-03-08 03:37:02 +03:00
|
|
|
int cnt, long pos, int fa, int ttl)
|
2023-06-03 22:52:10 +03: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 20:44:54 +03:00
|
|
|
uniperror("memfd_create");
|
2023-06-03 22:52:10 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
char *p = 0;
|
|
|
|
int status = -1;
|
|
|
|
|
|
|
|
while (status) {
|
|
|
|
if (ftruncate(ffd, pos) < 0) {
|
2024-02-24 20:44:54 +03:00
|
|
|
uniperror("ftruncate");
|
2023-06-03 22:52:10 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
p = mmap(0, pos, PROT_WRITE, MAP_SHARED, ffd, 0);
|
|
|
|
if (p == MAP_FAILED) {
|
2024-02-24 20:44:54 +03:00
|
|
|
uniperror("mmap");
|
2023-08-20 16:30:03 +03:00
|
|
|
p = 0;
|
2023-06-03 22:52:10 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
memcpy(p, pkt.data, psz < pos ? psz : pos);
|
|
|
|
|
2024-03-08 03:37:02 +03:00
|
|
|
if (setttl(sfd, ttl, fa) < 0) {
|
2023-06-03 22:52:10 +03:00
|
|
|
break;
|
|
|
|
}
|
2023-07-03 20:59:39 +03:00
|
|
|
if (_sendfile(sfd, ffd, 0, pos) < 0) {
|
2024-02-25 00:40:54 +03:00
|
|
|
uniperror("sendfile");
|
2023-06-03 22:52:10 +03:00
|
|
|
break;
|
|
|
|
}
|
2024-03-04 04:49:08 +03:00
|
|
|
delay(params.sfdelay);
|
2023-06-03 22:52:10 +03:00
|
|
|
memcpy(p, buffer, pos);
|
|
|
|
|
2023-07-03 20:59:39 +03:00
|
|
|
if (setttl(sfd, params.def_ttl, fa) < 0) {
|
2023-06-03 22:52:10 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
status = 0;
|
|
|
|
}
|
|
|
|
if (p) munmap(p, pos);
|
|
|
|
close(ffd);
|
|
|
|
return status;
|
|
|
|
}
|
2024-02-18 23:20:52 +03:00
|
|
|
#endif
|
2023-06-03 22:52:10 +03:00
|
|
|
|
2024-03-04 04:49:08 +03:00
|
|
|
int send_oob(int sfd, char *buffer,
|
|
|
|
ssize_t n, long pos)
|
2023-06-03 22:52:10 +03:00
|
|
|
{
|
2024-03-06 20:37:59 +03:00
|
|
|
ssize_t size = oob_data.size - 1;
|
|
|
|
char *data = oob_data.data + 1;
|
2024-03-03 17:32:56 +03:00
|
|
|
|
2024-03-06 20:37:59 +03:00
|
|
|
char rchar = buffer[pos];
|
|
|
|
buffer[pos] = oob_data.data[0];
|
|
|
|
|
|
|
|
if (send(sfd, buffer, pos + 1, MSG_OOB) < 0) {
|
|
|
|
uniperror("send");
|
2024-03-01 18:00:25 +03:00
|
|
|
buffer[pos] = rchar;
|
2024-03-06 20:37:59 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
buffer[pos] = rchar;
|
|
|
|
if (size) {
|
|
|
|
delay(params.sfdelay);
|
2024-03-01 18:00:25 +03:00
|
|
|
}
|
2024-03-04 04:49:08 +03:00
|
|
|
for (long i = 0; i < size; i++) {
|
2024-03-01 18:00:25 +03:00
|
|
|
if (send(sfd, data + i, 1, MSG_OOB) < 0) {
|
|
|
|
uniperror("send");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (size != 1) {
|
|
|
|
delay(params.sfdelay);
|
2024-02-29 20:07:59 +03:00
|
|
|
}
|
|
|
|
}
|
2024-03-04 04:49:08 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int send_disorder(int sfd,
|
|
|
|
char *buffer, long pos, int fa)
|
|
|
|
{
|
|
|
|
int bttl = 1;
|
|
|
|
if (setttl(sfd, bttl, fa) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (send(sfd, buffer, pos, 0) < 0) {
|
2024-02-29 20:07:59 +03:00
|
|
|
uniperror("send");
|
|
|
|
return -1;
|
|
|
|
}
|
2024-03-04 04:49:08 +03:00
|
|
|
if (setttl(sfd, params.def_ttl, fa) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2024-02-29 20:07:59 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-03-04 04:49:08 +03:00
|
|
|
|
2024-02-18 17:19:11 +03:00
|
|
|
int desync(int sfd, char *buffer, size_t bfsize,
|
2024-03-08 03:37:02 +03:00
|
|
|
ssize_t n, struct sockaddr *dst, int dp_c)
|
2023-06-03 22:52:10 +03:00
|
|
|
{
|
2024-03-08 03:37:02 +03:00
|
|
|
struct desync_params dp = params.dp[dp_c];
|
|
|
|
|
2023-06-03 22:52:10 +03:00
|
|
|
char *host = 0;
|
|
|
|
int len = 0, type = 0;
|
2023-07-30 14:23:11 +03:00
|
|
|
int fa = get_family(dst);
|
2023-07-03 20:59:39 +03:00
|
|
|
|
2023-06-03 22:52:10 +03: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 16:30:03 +03:00
|
|
|
if (len && host) {
|
2024-03-06 20:37:59 +03:00
|
|
|
LOG(LOG_S, "host: %.*s (%ld)\n",
|
|
|
|
len, host, host - buffer);
|
2023-08-20 16:30:03 +03:00
|
|
|
}
|
2024-02-18 17:19:11 +03:00
|
|
|
|
2024-03-08 03:37:02 +03:00
|
|
|
if (type == IS_HTTP && dp.mod_http) {
|
2024-02-24 20:44:54 +03:00
|
|
|
LOG(LOG_S, "modify HTTP: n=%ld\n", n);
|
2024-03-08 03:37:02 +03:00
|
|
|
if (mod_http(buffer, n, dp.mod_http)) {
|
2024-02-25 00:40:54 +03:00
|
|
|
LOG(LOG_E, "mod http error\n");
|
2023-06-03 22:52:10 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2024-03-08 03:37:02 +03:00
|
|
|
else if (type == IS_HTTPS && dp.tlsrec_n) {
|
2024-03-04 15:30:23 +03:00
|
|
|
long lp = 0;
|
2024-03-08 03:37:02 +03:00
|
|
|
for (int i = 0; i < dp.tlsrec_n; i++) {
|
|
|
|
struct part part = dp.tlsrec[i];
|
2024-03-05 23:22:12 +03:00
|
|
|
|
|
|
|
long pos = part.pos + i * 5;
|
|
|
|
if (part.flag == OFFSET_SNI) {
|
2024-03-04 15:30:23 +03:00
|
|
|
pos += (host - buffer - 5);
|
|
|
|
}
|
|
|
|
else if (pos < 0) {
|
|
|
|
pos += n;
|
|
|
|
}
|
2024-03-05 23:22:12 +03:00
|
|
|
if (pos < lp) {
|
|
|
|
LOG(LOG_E, "tlsrec cancel: %ld < %ld\n", pos, lp);
|
|
|
|
break;
|
|
|
|
}
|
2024-03-04 15:30:23 +03:00
|
|
|
if (!part_tls(buffer + lp,
|
|
|
|
bfsize - lp, n - lp, pos - lp)) {
|
2024-03-05 23:22:12 +03:00
|
|
|
LOG(LOG_E, "tlsrec error: pos=%ld, n=%ld\n", pos, n);
|
2024-03-04 15:30:23 +03:00
|
|
|
break;
|
|
|
|
}
|
2024-03-05 23:22:12 +03:00
|
|
|
LOG(LOG_S, "tlsrec: pos=%ld, n=%ld\n", pos, n);
|
2024-03-04 15:30:23 +03:00
|
|
|
n += 5;
|
|
|
|
lp = pos + 5;
|
2024-02-18 17:19:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-16 15:44:24 +03:00
|
|
|
if (params.custom_ttl) {
|
|
|
|
if (setttl(sfd, params.def_ttl, fa) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2024-03-04 04:49:08 +03:00
|
|
|
long lp = 0;
|
|
|
|
|
2024-03-06 20:37:59 +03:00
|
|
|
if (!type && params.de_known) {
|
2023-06-03 22:52:10 +03:00
|
|
|
}
|
2024-03-08 03:37:02 +03:00
|
|
|
else for (int i = 0; i < dp.parts_n; i++) {
|
|
|
|
struct part part = dp.parts[i];
|
2024-03-05 23:22:12 +03:00
|
|
|
|
|
|
|
long pos = part.pos;
|
|
|
|
if (part.flag == OFFSET_SNI) {
|
|
|
|
if (type != IS_HTTPS)
|
2024-03-06 20:37:59 +03:00
|
|
|
continue;
|
2024-03-05 23:22:12 +03:00
|
|
|
else
|
|
|
|
pos += (host - buffer);
|
|
|
|
}
|
|
|
|
else if (part.flag == OFFSET_HOST) {
|
|
|
|
if (type != IS_HTTP)
|
2024-03-06 20:37:59 +03:00
|
|
|
continue;
|
2024-03-05 23:22:12 +03:00
|
|
|
else
|
2024-03-04 04:49:08 +03:00
|
|
|
pos += (host - buffer);
|
|
|
|
}
|
|
|
|
else if (pos < 0) {
|
|
|
|
pos += n;
|
|
|
|
}
|
|
|
|
if (pos <= 0 || pos >= n || pos <= lp) {
|
2024-03-05 23:22:12 +03:00
|
|
|
LOG(LOG_E, "split cancel: pos=%ld-%ld, n=%ld\n", lp, pos, n);
|
2024-03-04 04:49:08 +03:00
|
|
|
break;
|
|
|
|
}
|
2024-03-05 23:22:12 +03:00
|
|
|
LOG(LOG_S, "split: pos=%ld-%ld, m=%d\n", lp, pos, part.m);
|
2024-03-04 04:49:08 +03:00
|
|
|
|
|
|
|
int s = 0;
|
2024-03-05 23:22:12 +03:00
|
|
|
switch (part.m) {
|
2024-02-18 23:20:52 +03:00
|
|
|
#ifndef _WIN32
|
2023-06-03 22:52:10 +03:00
|
|
|
case DESYNC_FAKE:
|
2024-03-04 04:49:08 +03:00
|
|
|
s = send_fake(sfd,
|
2024-03-08 03:37:02 +03:00
|
|
|
buffer + lp, type, pos - lp, fa, dp.ttl);
|
2024-03-04 04:49:08 +03:00
|
|
|
break;
|
2024-02-18 23:20:52 +03:00
|
|
|
#endif
|
2023-06-03 22:52:10 +03:00
|
|
|
case DESYNC_DISORDER:
|
2024-03-04 04:49:08 +03:00
|
|
|
s = send_disorder(sfd,
|
|
|
|
buffer + lp, pos - lp, fa);
|
|
|
|
break;
|
2023-06-03 22:52:10 +03:00
|
|
|
|
2024-02-29 20:07:59 +03:00
|
|
|
case DESYNC_OOB:
|
2024-03-04 04:49:08 +03:00
|
|
|
s = send_oob(sfd,
|
|
|
|
buffer + lp, n - lp, pos - lp);
|
|
|
|
break;
|
2024-02-29 20:07:59 +03:00
|
|
|
|
2023-06-03 22:52:10 +03:00
|
|
|
case DESYNC_SPLIT:
|
|
|
|
default:
|
2024-03-04 04:49:08 +03:00
|
|
|
if (send(sfd, buffer + lp, pos - lp, 0) < 0) {
|
2024-02-18 23:20:52 +03:00
|
|
|
uniperror("send");
|
2023-06-03 22:52:10 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2024-03-04 04:49:08 +03:00
|
|
|
}
|
|
|
|
if (s) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
lp = pos;
|
|
|
|
}
|
|
|
|
if (lp < n) {
|
|
|
|
LOG(LOG_S, "send: pos=%ld-%ld\n", lp, n);
|
|
|
|
if (send(sfd, buffer + lp, n - lp, 0) < 0) {
|
|
|
|
uniperror("send");
|
|
|
|
return -1;
|
|
|
|
}
|
2023-06-03 22:52:10 +03:00
|
|
|
}
|
|
|
|
return 0;
|
2023-07-07 23:07:27 +03:00
|
|
|
}
|