-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
383 lines (305 loc) · 9.4 KB
/
main.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include "includes.h"
////////////////////////////////////////////////////////////////////////
// DEFINICOES
const char *__progname = NULL;
////////////////////////////////////////////////////////////////////////
// FUNCOES
static void
associa_interface(struct sock_ctx *ctx)
{
int sys_sock;
struct ifreq ifr;
char buf[INET6_ADDRSTRLEN];
sys_sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sys_sock == -1)
err(1, "%s", __FUNCTION__);
memset(&ifr, 0, sizeof(struct ifreq));
strcpy(ifr.ifr_name, ctx->ifname);
if (ioctl(sys_sock, SIOCGIFINDEX, &ifr) == -1)
err(1, "%s", __FUNCTION__);
ctx->ifindex = ifr.ifr_ifindex;
if (inet_ntop(AF_INET6, &ctx->ifrom.sin6_addr,
buf, INET6_ADDRSTRLEN) == NULL)
err(1, "%s", __FUNCTION__);
debug_msg("DEBUG: [interface=%s][IP=%s]",
ctx->ifname, buf);
return;
}
void
inicializa_socket(struct sock_ctx *ctx)
{
memset(ctx, 0, sizeof(struct sock_ctx));
ctx->sd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (ctx->sd == -1)
err(1, "%s", __FUNCTION__);
}
static int
mac_str2bin(const char *mac, u_int8_t mac_bin[ETH_ALEN])
{
if (sscanf(mac, "%02x:%02x:%02x:%02x:%02x:%02x",
(u_int *) &mac_bin[0], (u_int *) &mac_bin[1],
(u_int *) &mac_bin[2], (u_int *) &mac_bin[3],
(u_int *) &mac_bin[4], (u_int *) &mac_bin[5])
== ETH_ALEN)
return(0);
return(-1);
}
static void
constroi_header(struct sock_ctx *ctx)
{
struct ether_header *eth;
struct ip6_hdr *ip6;
struct icmp6_hdr *icmp6;
/************************************
* XXX inicio da montagem da camada *
************************************/
eth = (struct ether_header *) ctx->pbuff;
/* XXX ETH_ALEN foi retirado do arquivo: net/ethernet.h */
memcpy(eth->ether_dhost, ctx->mto, ETH_ALEN);
memcpy(eth->ether_shost, ctx->mfrom, ETH_ALEN);
/* XXX IDs dos tipos de ethernet tambem podem ser achados no
* arquivo referenciado acima.
*/
eth->ether_type = htons(ETHERTYPE_IPV6);
ctx->ppos = ctx->pbuff + sizeof(struct ether_header);
/************************************
* XXX inicio da montagem da camada *
************************************/
ip6 = (struct ip6_hdr *) ctx->ppos;
/* nem vamo usa */
ip6->ip6_flow = htonl(0);
/* XXX guarda ponteiro para o tamanho do
* pacote de dados para atualizar mais tarde
*/
ctx->plen = &ip6->ip6_plen;
ip6->ip6_plen = htons(64);
/* next header -> 0x3a == ICMPv6 */
ip6->ip6_nxt = 0x3a;
/* XXX hop-limit a.k.a. ttl */
ip6->ip6_hlim = 64;
memcpy(&ip6->ip6_src, &ctx->ifrom.sin6_addr, sizeof(struct in6_addr));
memcpy(&ip6->ip6_dst, &ctx->ito.sin6_addr, sizeof(struct in6_addr));
ctx->ppos += sizeof(struct ip6_hdr);
/************************************
* XXX inicio da montagem da camada *
************************************/
icmp6 = (struct icmp6_hdr *) ctx->ppos;
if (ctx->proxy)
icmp6->icmp6_type = ICMP6_ECHO_REPLY;
else
icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
/* Sempre zero */
icmp6->icmp6_code = 0;
/* XXX ponteiro para a posicao do pacote onde fica o checksum sera
* guardada para ser usada mais tarde
*/
ctx->icmp_cksum = &icmp6->icmp6_cksum;
icmp6->icmp6_seq = htons(ctx->seq_atual++);
/* XXX numero gerado por dados, garantido de ser aleartório */
icmp6->icmp6_id = htons(42);
ctx->ppos += sizeof(struct icmp6_hdr);
/**************************************
* XXX inicio da montagem do checksum *
**************************************/
/* preenche o pacote que eh descrito pela RFC para o checksum */
memcpy(&ctx->ckf.ip6_src, &ip6->ip6_src, sizeof(struct in6_addr));
memcpy(&ctx->ckf.ip6_dst, &ip6->ip6_dst, sizeof(struct in6_addr));
ctx->ckf.nxthdr = 0x3a;
/* XXX copia header do ICMP para checksum */
memcpy(&ctx->ckf.icmp6, (ctx->ppos - sizeof(struct icmp6_hdr)),
sizeof(struct icmp6_hdr));
}
static void
constroi_header_link(struct sock_ctx *ctx)
{
/* Familia sempre AF_PACKET */
ctx->saddr.sll_family = AF_PACKET;
/* tipo de protocolo */
ctx->saddr.sll_protocol = htons(ETH_P_ALL);
/* indice da interface */
ctx->saddr.sll_ifindex = ctx->ifindex;
/* tamanho do endereco de hardware */
//ctx->saddr.sll_halen = ETH_ALEN;
/* ??? */
//ctx->saddr.sll_pkttype = PACKET_OTHERHOST;
/* endereco de hardware */
//memcpy(&ctx->saddr.sll_addr, ctx->mfrom, ETH_ALEN);
ctx->saddr_len = sizeof(struct sockaddr_ll);
}
static void
constroi_header_tcp(struct sock_ctx *ctx)
{
struct tcphdr *tcp;
tcp = (struct tcphdr *) ctx->ppos;
/* source port */
tcp->source = htons(ctx->tcp_port);
/* destination port */
tcp->dest = htons(ctx->tcp_port);
/* sequential number */
tcp->seq = htonl(ctx->seq_atual);
/* ack number */
tcp->ack_seq = htonl(ctx->seq_atual);
/* data offset */
tcp->doff = 0;
/* TCP flags, TH_FIN, TH_SYN, TH_RST, TH_PUSH, TH_ACK, TH_URG */
//tcp->th_flags = TH_SYN | TH_ACK; // BSD-Style
tcp->fin = 0;
tcp->syn = 1;
tcp->rst = 0;
tcp->psh = 0;
tcp->ack = 1;
tcp->urg = 0;
/* TCP window */
tcp->window = htons(0);
/* TCP checksum */
tcp->check = htons(0);
/* TCP urgent pointer ??? */
tcp->urg_ptr = htons(0);
ctx->ppos += sizeof(struct tcphdr);
memcpy(&ctx->ckf.tcp, (ctx->ppos - sizeof(struct tcphdr)),
sizeof(struct tcphdr));
}
void
constroi_mensagem(struct sock_ctx *ctx, u_int8_t *mensagem, u_int32_t total)
{
constroi_header(ctx);
constroi_header_tcp(ctx);
memcpy(ctx->ppos, mensagem, total);
memcpy(ctx->ckf.msg, mensagem, total);
*ctx->plen = htons(sizeof(struct icmp6_hdr) +
sizeof(struct tcphdr) + total);
ctx->ckf.pkt_len = htonl(ntohs(*ctx->plen));
ctx->ppos += total;
*ctx->icmp_cksum =
(in_cksum((u_int16_t *) &ctx->ckf, sizeof(ctx->ckf)));
}
void
manda_mensagem(struct sock_ctx *ctx)
{
constroi_header_link(ctx);
debug_msg("[PACKET_LEN=%lu]", GET_PACKET_LEN(ctx));
if (sendto(ctx->sd, ctx->pbuff, GET_PACKET_LEN(ctx), 0,
(struct sockaddr *) &ctx->saddr, ctx->saddr_len) == -1)
err(1, "%s", __FUNCTION__);
}
////////////////////////////////////////////////////////////////////////
// MAIN
static void
usage(void)
{
fprintf(stderr, "%1$s <-c> -i interface\n"
"-s XX:XX:XX:XX:XX:XX -d XX:XX:XX:XX:XX:XX\n"
"-o IP6_ADDRESS -w IP6_ADDRESS -p PORTA\n"
"========\n"
"\t-i - Interface para onde deve se enviar o pacote. (OBRIGATORIO)\n"
"\t-w - Endereco de origem. (OBRIGATORIO)\n"
"\t-o - Endereco de destino. (OBRIGATORIO)\n"
"\t-s - MAC da interface de origem. (OBRIGATORIO)\n"
"\t-d - MAC da interface de destino. (OBRIGATORIO)\n"
"\t-c - Programa ira rodar como proxy (default cliente).\n"
"\t-p - Porta do TCP (default 8050).\n",
__progname);
exit(EXIT_FAILURE);
}
int
main(int argc, char **argv)
{
struct sock_ctx ctx;
int opt, if_set, ip_set, ip2_set;
int mac_set, mac2_set;
char mensagem[512];
int len;
fd_set read;
int nfds, bread;
struct timeval timer;
struct sockaddr_in6 recfrom;
socklen_t ifrom_len;
__progname = argv[0];
memset(&ctx, 0, sizeof(ctx));
if (argc == 1)
usage();
inicializa_socket(&ctx);
if_set = ip_set = ip2_set = 0;
mac_set = mac2_set = 0;
while ((opt = getopt(argc, argv, "w:o:i:cs:d:")) != -1) {
switch (opt) {
case 'c':
ctx.proxy = 1;
break;
case 's':
if (mac_str2bin(optarg, ctx.mfrom) == -1) {
fprintf(stderr, "Endereco de mac %s invalido.\n", optarg);
exit(EXIT_FAILURE);
}
mac2_set = 1;
break;
case 'd':
if (mac_str2bin(optarg, ctx.mto) == -1) {
fprintf(stderr, "Endereco de mac %s invalido.\n", optarg);
exit(EXIT_FAILURE);
}
mac_set = 1;
break;
case 'i':
memcpy(ctx.ifname, optarg, strlen(optarg));
if_set = 1;
break;
case 'o':
ip_set = 1;
inet_pton(AF_INET6, optarg, &ctx.ito.sin6_addr);
break;
case 'w':
ip2_set = 1;
inet_pton(AF_INET6, optarg, &ctx.ifrom.sin6_addr);
break;
default:
usage();
/* NOTREACHED */
}
}
if (if_set == 0 || ip_set == 0 || ip2_set == 0
|| mac_set == 0 || mac2_set == 0)
usage();
associa_interface(&ctx);
if (ctx.proxy) {
run_proxy(&ctx);
exit(EXIT_SUCCESS);
}
len = sprintf(mensagem, "TESTE PERGUNTA?");
constroi_mensagem(&ctx, (u_int8_t *) mensagem, len + 1);
manda_mensagem(&ctx);
/* XXX espera mensagem de volta */
retry_get:
memset(&timer, 0, sizeof(struct timeval));
timer.tv_sec = TEMPO_TIMEOUT;
timer.tv_usec = 0;
FD_ZERO(&read);
FD_SET(ctx.sd, &read);
nfds = select((ctx.sd + 1), &read, NULL, NULL, &timer);
if (nfds < 0)
err(1, "%s", __FUNCTION__);
else if (nfds == 0) {
fprintf(stderr, "ERRO: nao consegui receber nada em %d segundos.\n",
TEMPO_TIMEOUT);
exit(EXIT_FAILURE);
}
ifrom_len = sizeof(struct sockaddr_in6);
memset(&recfrom, 0, sizeof(struct sockaddr_in6));
bread = recvfrom(ctx.sd, ctx.sbuff, BUFSIZ, 0,
(struct sockaddr *) &recfrom, &ifrom_len);
debug_msg("[leu=%d]", bread);
if (bread == -1)
err(1, "%s", __FUNCTION__);
else if (bread == 0) {
fprintf(stderr, "ERRO: nao consegui ler nada do socket.\n");
exit(EXIT_SUCCESS);
}
/* XXX pega posicao da mensagem */
ctx.spos = le_mensagem(&ctx, ctx.sbuff, bread);
if (ctx.spos == NULL) {
debug_msg("[DESCARTANDO PACOTE DESCONHECIDO]");
goto retry_get;
}
debug_msg("[mensagem=%s]", ctx.spos);
exit(EXIT_SUCCESS);
}