-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathserver.c
339 lines (316 loc) · 8.6 KB
/
server.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <ev.h>
#include <libnet.h>
#include <assert.h>
#include <stdio.h>
#include <stdint.h>
#include <getopt.h>
#include "stun.h"
#include "utils.h"
#include "bithacks.h"
#include "cripple_log.h"
#define STUN_PORT 3478
static uint32_t FORGED_IP;
struct stun_address4 {
uint32_t ip;
uint16_t port;
};
struct stun_ctx {
/*
* ev_io io 放最前面确保元素 io 地址即该结构起始地址,
* read_cb() 获取 struct stun_ctx server 地址语句:
* struct stun_ctx *server = (typeof(server))w;
* 依赖于此
*/
ev_io io;
struct stun_address4 addr;
struct stun_address4 change_addr;
int fd;
uint8_t buf[STUN_MAX_MESSAGE_SIZE];
};
static void stun_msg_hdr_parse(const uint8_t *msg, ssize_t len,
struct stun_msg_hdr *hdr)
{
int arrsize;
assert(len >= 20);
hdr->type = msg[1] + (msg[0] << 8);
hdr->len = msg[3] + (msg[2] << 8);
arrsize = ARRAY_SIZE(hdr->transaction_id);
memcpy(hdr->transaction_id, msg + 4, arrsize);
}
static int get_change_request_attr(const uint8_t *msg, ssize_t len,
int *is_change_ip, int *is_change_port)
{
const uint8_t *pos;
uint32_t attr_len;
if (len < 28)
return -1;
pos = msg + 20;
while (1) {
if (pos[0] == 0x00 && pos[1] == 0x03 &&
pos[2] == 0x00 && pos[3] == 0x04) {
if (B_IS_SET(pos[7], 2))
*is_change_port = 1;
else
*is_change_port = 0;
if (B_IS_SET(pos[7], 3))
*is_change_ip = 1;
else
*is_change_ip = 0;
return 0;
}
attr_len = (pos[2] << 8) + pos[3];
if (pos + 4 + attr_len - msg >= len)
break;
pos += 4 + attr_len;
}
return -1;
}
static ssize_t attach_mapped_addr_attr(uint8_t *buf_end,
const struct sockaddr_in *addr)
{
/* attribute type: MAPPED-ADDRESS 0x0001 */
buf_end[0] = 0x0;
buf_end[1] = 0x01;
/* attribute length: 0x0008 */
buf_end[2] = 0x0;
buf_end[3] = 0x08;
/* protocol family: IPv4(0x0001) */
buf_end[4] = 0x0;
buf_end[5] = 0x01;
/* Port */
uint16_t port = ntohs(addr->sin_port);
buf_end[6] = (port >> 8) & 0xff;
buf_end[7] = port & 0xff;
/* IP */
uint32_t ip = ntohl(addr->sin_addr.s_addr);
buf_end[8] = (ip >> 24) & 0xff;
buf_end[9] = (ip >> 16) & 0xff;
buf_end[10] = (ip >> 8) & 0xff;
buf_end[11] = ip & 0xff;
return 12;
}
static ssize_t attach_source_addr_attr(uint8_t *buf_end,
const struct stun_address4 *addr)
{
/* attribute type: SOURCE-ADDRESS 0x0004 */
buf_end[0] = 0x0;
buf_end[1] = 0x04;
/* attribute length: 0x0008 */
buf_end[2] = 0x0;
buf_end[3] = 0x08;
/* protocol family: IPv4(0x0001) */
buf_end[4] = 0x0;
buf_end[5] = 0x01;
/* Port */
uint16_t port = ntohs(addr->port);
buf_end[6] = port & 0xff;
buf_end[7] = (port >> 8) & 0xff;
/* IP */
uint32_t ip = ntohl(addr->ip);
buf_end[8] = (ip >> 24) & 0xff;
buf_end[9] = (ip >> 16) & 0xff;
buf_end[10] = (ip >> 8) & 0xff;
buf_end[11] = ip & 0xff;
return 12;
}
static ssize_t attach_changed_addr_attr(uint8_t *buf_end,
uint16_t port, uint32_t ip)
{
/* attribute type: CHANGED-ADDRESS 0x0005 */
buf_end[0] = 0x0;
buf_end[1] = 0x05;
/* attribute length: 0x0008 */
buf_end[2] = 0x0;
buf_end[3] = 0x08;
/* protocol family: IPv4(0x0001) */
buf_end[4] = 0x0;
buf_end[5] = 0x01;
/* Port */
buf_end[6] = (port >> 8) & 0xff;
buf_end[7] = port & 0xff;
/* IP */
buf_end[8] = ip & 0xff;
buf_end[9] = (ip >> 8) & 0xff;
buf_end[10] = (ip >> 16) & 0xff;
buf_end[11] = (ip >> 24) & 0xff;
return 12;
}
static ssize_t set_binding_resp(uint8_t *resp, const struct stun_ctx *server,
const struct stun_msg_hdr *from_hdr,
const struct sockaddr_in *from)
{
int arrsize;
uint8_t *pos;
ssize_t ret;
resp[0] = 0x01;
resp[1] = 0x01;
arrsize = ARRAY_SIZE(from_hdr->transaction_id);
memcpy(resp + 4, from_hdr->transaction_id, arrsize);
pos = resp + 4 + arrsize;
ret = attach_mapped_addr_attr(pos, from);
pos += ret;
ret = attach_source_addr_attr(pos, &server->addr);
pos += ret;
/* test */
ret = attach_changed_addr_attr(pos, server->change_addr.port,
server->change_addr.ip);
pos += ret;
ssize_t len = pos - resp - 20;
resp[2] = (len >> 8) & 0xff;
resp[3] = len & 0xff;
return pos - resp;
}
static int set_forgedip_binding_resp(const struct stun_ctx *server,
const struct stun_msg_hdr *from_hdr,
const struct sockaddr_in *from,
int is_forge_ip, int is_forge_port)
{
uint8_t resp[2048];
char errbuf[LIBNET_ERRBUF_SIZE];
libnet_t *l = libnet_init(LIBNET_RAW4, NULL, errbuf);
int packet_size = 0;
int arrsize;
uint8_t *pos;
ssize_t ret;
struct stun_address4 forged_addr;
if (is_forge_ip)
forged_addr.ip = server->change_addr.ip;
else
forged_addr.ip = server->addr.ip;
if (is_forge_port)
forged_addr.port = server->change_addr.port;
else
forged_addr.port = server->addr.port;
packet_size += LIBNET_UDP_H;
resp[0] = 0x01;
resp[1] = 0x01;
arrsize = ARRAY_SIZE(from_hdr->transaction_id);
memcpy(resp + 4, from_hdr->transaction_id, arrsize);
pos = resp + 4 + arrsize;
ret = attach_mapped_addr_attr(pos, from);
pos += ret;
ret = attach_source_addr_attr(pos, &forged_addr);
pos += ret;
ret = attach_changed_addr_attr(pos, server->addr.port, server->addr.ip);
pos += ret;
ssize_t len = pos - resp - 20;
resp[2] = (len >> 8) & 0xff;
resp[3] = len & 0xff;
packet_size += len + 20;
ret = libnet_build_udp(forged_addr.port, htons(from->sin_port),
packet_size, 0, resp, len + 20, l, 0);
if (ret < 0) {
fprintf(stderr, "libnet_build_udp() fail: %s\n",
libnet_geterror(l));
return -1;
}
ret = libnet_build_ipv4(packet_size + LIBNET_IPV4_H, 0, 0, 0, 255,
IPPROTO_UDP, 0, forged_addr.ip,
from->sin_addr.s_addr, NULL, 0, l, 0);
if (ret < 0) {
fprintf(stderr, "libnet_build_ipv4() fail: %s\n",
libnet_geterror(l));
return -1;
}
if (libnet_write(l) < 0) {
fprintf(stderr, "libnet_write fail: %s\n", libnet_geterror(l));
return -1;
}
libnet_destroy(l);
return 0;
}
static void read_cb(EV_P_ ev_io *w, int revents)
{
struct stun_ctx *server = (typeof(server))w;
ssize_t len;
struct stun_msg_hdr msg_hdr;
struct sockaddr_in from;
int fromlen = sizeof(from);
int is_change_ip, is_change_port;
int ret;
len = recvfrom(server->fd, server->buf, STUN_MAX_MESSAGE_SIZE, 0,
(struct sockaddr *)&from, (socklen_t *)&fromlen);
if (len <= 0) {
perror("recvfrom");
return;
}
stun_msg_hdr_parse(server->buf, len, &msg_hdr);
cri_log("request from: %s, type: %d",
inet_ntoa(from.sin_addr), msg_hdr.type);
if (msg_hdr.type == BINDING_REQUEST && msg_hdr.len == 0) {
/* send Binding Resp msg */
len = set_binding_resp(server->buf, server, &msg_hdr, &from);
sendto(server->fd, server->buf, len, 0,
(const struct sockaddr *)&from, (socklen_t)fromlen);
} else if (msg_hdr.type == BINDING_REQUEST &&
get_change_request_attr(server->buf, len,
&is_change_ip, &is_change_port) == 0) {
ret = set_forgedip_binding_resp(server, &msg_hdr, &from,
is_change_ip, is_change_port);
if (ret < 0)
fprintf(stderr, "set_forgedip_binding_resp fail\n");
}
}
static void usage(char **argv)
{
fprintf(stderr, "Usage: %s [-b, --background]\n", argv[0]);
}
int main(int argc, char **argv)
{
struct ev_loop *loop = EV_DEFAULT;
struct stun_ctx stun_server;
struct stun_ctx stun_server2;
uint32_t my_ip = get_first_network_addr();
int opt, index;
static struct option long_opts[] = {
{"help", no_argument, 0, 'h'},
{"background", no_argument, 0, 'b'},
{0, 0, 0, 0}
};
FILE *logfp;
while ((opt = getopt_long(argc, argv, "hb", long_opts, &index)) != -1) {
switch (opt) {
case 'b':
logfp = fopen("cripple.log", "a");
if (logfp == NULL) {
perror("fopen");
exit(1);
}
dup2(fileno(logfp), STDERR_FILENO);
fclose(logfp);
daemon(0, 1);
break;
case 0:
case 'h':
default:
usage(argv);
exit(0);
}
}
cri_log("cripple server init\n");
stun_server.addr.ip = my_ip;
stun_server2.addr.ip = my_ip;
stun_server.addr.port = STUN_PORT;
stun_server2.addr.port = STUN_PORT + 1;
stun_server.fd = init_server_UDP_fd(STUN_PORT, stun_server.addr.ip);
stun_server2.fd = init_server_UDP_fd(STUN_PORT+1, stun_server2.addr.ip);
assert(stun_server.fd > 0 && stun_server2.fd > 0);
if ((stun_server.addr.ip & 0xff) < 0xfe)
FORGED_IP = stun_server.addr.ip + 0x1000000;
else
FORGED_IP = stun_server.addr.ip - 0x1000000;
stun_server.change_addr.ip = FORGED_IP;
stun_server.change_addr.port = stun_server2.addr.port;
stun_server2.change_addr.ip = stun_server2.addr.ip;
stun_server2.change_addr.port = stun_server2.addr.port + 1;
ev_io_init(&stun_server.io, read_cb, stun_server.fd, EV_READ);
ev_io_start(loop, &stun_server.io);
ev_io_init(&stun_server2.io, read_cb, stun_server2.fd, EV_READ);
ev_io_start(loop, &stun_server2.io);
ev_run(loop, 0);
return 0;
}