-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathdesync.c
264 lines (237 loc) · 6.34 KB
/
desync.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <stdio.h>
#include <string.h>
#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
#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
#else
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#include <params.h>
#include <packets.h>
#include <error.h>
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;
}
int setttl(int fd, int ttl, int family) {
int _ttl = ttl;
if (family == AF_INET) {
if (setsockopt(fd, IPPROTO_IP,
IP_TTL, (char *)&_ttl, sizeof(_ttl)) < 0) {
uniperror("setsockopt IP_TTL");
return -1;
}
}
else if (setsockopt(fd, IPPROTO_IPV6,
IPV6_UNICAST_HOPS, (char *)&_ttl, sizeof(_ttl)) < 0) {
uniperror("setsockopt IPV6_UNICAST_HOPS");
return -1;
}
return 0;
}
#ifndef _WIN32
int fake_attack(int sfd, char *buffer,
size_t n, int cnt, int pos, int fa)
{
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) {
uniperror("memfd_create");
return -1;
}
char *p = 0;
int status = -1;
while (status) {
if (ftruncate(ffd, pos) < 0) {
uniperror("ftruncate");
break;
}
p = mmap(0, pos, PROT_WRITE, MAP_SHARED, ffd, 0);
if (p == MAP_FAILED) {
uniperror("mmap");
p = 0;
break;
}
memcpy(p, pkt.data, psz < pos ? psz : pos);
if (setttl(sfd, params.ttl, fa) < 0) {
break;
}
if (_sendfile(sfd, ffd, 0, pos) < 0) {
uniperror("sendfile");
break;
}
struct timespec delay = {
.tv_nsec = params.sfdelay * 1000
};
nanosleep(&delay, 0);
memcpy(p, buffer, pos);
if (setttl(sfd, params.def_ttl, fa) < 0) {
break;
}
if (send(sfd, buffer + pos, n - pos, 0) < 0) {
uniperror("send");
break;
}
status = 0;
}
if (p) munmap(p, pos);
close(ffd);
return status;
}
#endif
int disorder_attack(int sfd, char *buffer,
ssize_t n, int pos, int fa)
{
int bttl = 1;
if (setttl(sfd, bttl, fa) < 0) {
return -1;
}
if (send(sfd, buffer, pos, 0) < 0) {
uniperror("send");
return -1;
}
if (setttl(sfd, params.def_ttl, fa) < 0) {
return -1;
}
if (send(sfd, buffer + pos, n - pos, 0) < 0) {
uniperror("send");
return -1;
}
return 0;
}
int oob_attack(int sfd, char *buffer,
ssize_t n, int pos, int fa)
{
struct packet pkt = oob_data;
if (send(sfd, buffer, pos, 0) < 0) {
uniperror("send");
return -1;
}
for (int i = 0; i < pkt.size; i++) {
if (send(sfd, pkt.data + i, 1, MSG_OOB) < 0) {
uniperror("send");
return -1;
}
#ifndef _WIN32
if (pkt.size == 1) {
break;
}
struct timespec delay = {
.tv_nsec = params.sfdelay * 1000
};
nanosleep(&delay, 0);
#endif
}
if (send(sfd, buffer + pos, n - pos, 0) < 0) {
uniperror("send");
return -1;
}
return 0;
}
int desync(int sfd, char *buffer, size_t bfsize,
ssize_t n, struct sockaddr *dst)
{
int pos = params.split;
char *host = 0;
int len = 0, type = 0;
int fa = get_family(dst);
if ((len = parse_tls(buffer, n, &host))) {
type = IS_HTTPS;
}
else if ((len = parse_http(buffer, n, &host, 0))) {
type = IS_HTTP;
}
if (len && host) {
LOG(LOG_S, "host: %.*s\n", len, host);
}
if (type == IS_HTTP && params.mod_http) {
LOG(LOG_S, "modify HTTP: n=%ld\n", n);
if (mod_http(buffer, n, params.mod_http)) {
LOG(LOG_E, "mod http error\n");
return -1;
}
}
else if (type == IS_HTTPS && params.tlsrec) {
int o = params.tlsrec_pos;
if (params.tlsrec_sni) {
o += (host - buffer - 5);
}
else if (o < 0) {
o += n;
}
LOG(LOG_S, "tlsrec: pos=%d, n=%ld\n", o, n);
n = part_tls(buffer, bfsize, n, o);
}
if (params.split_host) {
if (host)
pos += (host - buffer);
else
pos = 0;
}
else if (pos < 0) {
pos += n;
}
LOG(LOG_L, "split-pos=%d, n=%ld\n", pos, n);
if (params.custom_ttl) {
if (setttl(sfd, params.def_ttl, fa) < 0) {
return -1;
}
}
if (pos <= 0 || pos >= n ||
params.attack == DESYNC_NONE ||
(!type && params.de_known))
{
if (send(sfd, buffer, n, 0) < 0) {
uniperror("send");
return -1;
}
}
else switch (params.attack) {
#ifndef _WIN32
case DESYNC_FAKE:
return fake_attack(sfd, buffer, n, type, pos, fa);
#endif
case DESYNC_DISORDER:
return disorder_attack(sfd, buffer, n, pos, fa);
case DESYNC_OOB:
return oob_attack(sfd, buffer, n, pos, fa);
case DESYNC_SPLIT:
default:
if (send(sfd, buffer, pos, 0) < 0) {
uniperror("send");
return -1;
}
if (send(sfd, buffer + pos, n - pos, 0) < 0) {
uniperror("send");
return -1;
}
}
return 0;
}