-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_test_icmp.c
171 lines (147 loc) · 5.47 KB
/
send_test_icmp.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
/*-
* IP-over-ICMP tunnel interface.
*
* Matteo `datafl4sh` Cicuttin (C) 2021.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Author nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <errno.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/in.h>
#include <unistd.h>
#define PKT_BUFLEN 128
#include "if_icmptun.h"
/*
* Checksum routine for Internet Protocol family headers (C Version).
*
* Refer to "Computing the Internet Checksum" by R. Braden, D. Borman and
* C. Partridge, Computer Communication Review, Vol. 19, No. 2, April 1989,
* pp. 86-101, for additional details on computing this checksum.
*/
int /* return checksum in low-order 16 bits */
in_cksum_priv(void *parg, int nbytes)
{
u_short *ptr = parg;
register long sum; /* assumes long == 32 bits */
u_short oddbyte;
register u_short answer; /* assumes u_short == 16 bits */
/*
* Our algorithm is simple, using a 32-bit accumulator (sum),
* we add sequential 16-bit words to it, and at the end, fold back
* all the carry bits from the top 16 bits into the lower 16 bits.
*/
sum = 0;
while (nbytes > 1) {
sum += *ptr++;
nbytes -= 2;
}
/* mop up an odd byte, if necessary */
if (nbytes == 1) {
oddbyte = 0; /* make sure top half is zero */
*((u_char *) &oddbyte) = *(u_char *)ptr; /* one byte only */
sum += oddbyte;
}
/*
* Add back carry outs from top 16 bits to low 16 bits.
*/
sum = (sum >> 16) + (sum & 0xffff); /* add high-16 to low-16 */
sum += (sum >> 16); /* add carry */
answer = ~sum; /* ones-complement, then truncate to 16 bits */
return(answer);
}
#define TSTPKT_LEN 46
const u_char *tstpkt =
"\x45\x00\x00\x2e\x2b\x6d\x40\x00\x40\x01\x96\x69\xac\x10\x10\x41" \
"\xac\x10\x10\x97\x08\x00\xbb\x36\xca\xfe\x42\x43\x42\xca\xde\xad" \
"\x9b\x96\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19";
#define TSTPKT2_LEN 60
const u_char *tstpkt2 =
"\x45\x10\x00\x3c\x5d\x2a\x40\x00\x40\x06\x64\x89\xac\x10\x10\x41" \
"\xac\x10\x10\x97\xa7\x70\x00\x17\xf0\x03\x7d\xba\x00\x00\x00\x00" \
"\xa0\x02\xfa\xf0\x79\x27\x00\x00\x02\x04\x05\xb4\x04\x02\x08\x0a"
"\x50\x70\x7a\xae\x00\x00\x00\x00\x01\x03\x03\x0a";
#define TSTPKT3_LEN 84
const u_char *tstpkt3 =
"\x45\x00\x00\x54\x91\x13\x40\x00\x40\x01\x30\x9d\xac\x10\x10\x41" \
"\xac\x10\x10\x97\x08\x00\x54\x68\x68\xe9\x00\x01\x3d\xc3\x3f\x60" \
"\x00\x00\x00\x00\xf8\xb6\x06\x00\x00\x00\x00\x00\x10\x11\x12\x13" \
"\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23" \
"\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33" \
"\x34\x35\x36\x37";
int main(int argc, const char *argv[])
{
if (argc != 3)
{
fprintf(stderr, "%s [dst ip] [tunkey]\n", argv[0]);
return 1;
}
struct sockaddr_in dst_addr;
bzero(&dst_addr, sizeof(struct sockaddr_in));
inet_pton(AF_INET, argv[1], &dst_addr.sin_addr);
dst_addr.sin_family = AF_INET;
int tunkey = atoi(argv[2]);
int s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (s < 0)
{
perror("socket");
return 1;
}
u_char pktbuf[PKT_BUFLEN];
for (u_char i = 0; i < PKT_BUFLEN; i++)
pktbuf[i] = 0;
struct icmptun *icmptunh = (struct icmptun *) pktbuf;
icmptunh->ic_type = 8;
icmptunh->ic_code = 0;
icmptunh->ic_cksum = 0;
icmptunh->ic_ident = htons(tunkey);
icmptunh->ic_seq = htons(0x4243);
icmptunh->tun_ver = ICMPTUN_VERSION;
icmptunh->tun_flags = 0xca;
icmptunh->tun_proto = htons(4);
size_t datalen = TSTPKT3_LEN;
u_char *data = pktbuf + sizeof(struct icmptun);
memcpy(data, tstpkt3, datalen);
icmptunh->tun_cksum = in_cksum_priv(data, datalen);
size_t sz = sizeof(struct icmptun) + datalen;
icmptunh->ic_cksum = in_cksum_priv(pktbuf, sz);
int err;
err = sendto(s, pktbuf, sz, 0, (struct sockaddr *)&dst_addr,
sizeof(struct sockaddr) );
if (err < 0)
{
perror("sendto");
return 1;
}
close(s);
return 0;
}