-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.c
396 lines (339 loc) · 10.2 KB
/
map.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
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
Nast - map.c
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/* pseudo BuG : If someone is arp-poisoning we must him as the owner of the ip!
------
* This function receive 1 if it must print to stdout (nast -m)
* alse 0 if is used by another funz (run in silent mode)
*
*
* Return if called from another function:
* - NULL on error
* - n=0 if localhost is the only host in network segment
* - n>0 and struct host
*/
/* Don't touch here said embyte */
#include "include/nast.h"
int arpreply (u_char *t, char *dev, u_short mode, int lg);
int send_arp(libnet_t *l, u_char *device, u_char *ip_dst, u_char *enet_src, u_long ip_src);
struct host * map_lan(char *dev, u_short mode, u_short * n);
u_int scan_ulong(char *s, u_long *u);
int line;
u_char enet_dst[6] = /* broadcast */
{
0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};
u_short k, count;
struct host * uphost;
char errbuf[256];
libnet_ptag_t ptag;
struct host * map_lan(char *dev, u_short mode, u_short * n)
{
libnet_t *l;
struct libnet_ether_addr *e;
struct in_addr addr;
char r[3];
long ip_src;
u_long u;
u_int i;
u_char ip_dst[4], orig_ip[4];
u_char netmask[4], enet_src[6], offset[4];
char *net, *mask;
u_short j[4]; /* index */
count=k=ptag=0;
line = 7;
#ifdef HAVE_LIBNCURSES
if(graph && mode)
init_scr();
#endif
/* make uphost point to at least 1 cell to avoid conflict with NULL on error */
uphost = calloc (1, sizeof (struct host));
if (demonize && mode)
{
w_error(0,"Is very useless demonize me in mapping LAN! Omit");
demonize=0;
}
if ((l = libnet_init (LIBNET_LINK, dev, errbuf))==NULL)
{
w_error(1, "libnet_init() : %s\n", errbuf);
}
if ((e = libnet_get_hwaddr(l))==NULL)
{
w_error(1, "Can't get hardware address: %s\n", errbuf);
}
memcpy (enet_src, e->ether_addr_octet, 6);
if((ip_src = libnet_get_ipaddr4(l))==-1)
{
w_error(1, "Error getting ip source\n");
}
if (pcap_lookupnet(dev, &netp, &maskp, errbuf)==-1)
{
w_error(1, "Error: %s\n", errbuf);
}
addr.s_addr = netp;
if ((net = inet_ntoa(addr))==NULL)
{
w_error(1, "Impossible get the netaddress\n");
}
/* netaddress */
i = scan_ulong(net,&u); if (!i) return NULL; ip_dst[0] = u; net += i;
if (*net != '.') return NULL; ++net;
i = scan_ulong(net,&u); if (!i) return NULL; ip_dst[1] = u; net += i;
if (*net != '.') return NULL; ++net;
i = scan_ulong(net,&u); if (!i) return NULL; ip_dst[2] = u; net += i;
if (*net != '.') return NULL; ++net;
i = scan_ulong(net,&u); if (!i) return NULL; ip_dst[3] = u; net += i;
memcpy (orig_ip, ip_dst, 4);
addr.s_addr = maskp;
if ((mask = inet_ntoa(addr))==NULL)
{
w_error(1, "Impossible get the netmask\n");
}
/* netmask */
i = scan_ulong(mask,&u); if (!i) return NULL; netmask[0] = u; mask += i;
if (*mask != '.') return NULL; ++mask;
i = scan_ulong(mask,&u); if (!i) return NULL; netmask[1] = u; mask += i;
if (*mask != '.') return NULL; ++mask;
i = scan_ulong(mask,&u); if (!i) return NULL; netmask[2] = u; mask += i;
if (*mask != '.') return NULL; ++mask;
i = scan_ulong(mask,&u); if (!i) return NULL; netmask[3] = u; mask += i;
/* computate offset from netaddress and netmask */
for (i=0; i<=3; i++) offset[i]=255-netmask[i];
/* large netmask */
if (offset[1] && offset[2] && offset[3])
{
if (mode)
{
n_print ("winfo",1,2,lg,"You are going to scan a large network (%s netmask)! Are you sure? (y/n) : ", nast_atoda(netmask));
fgets(r, 3, stdin);
if (!(r[0]=='s' || r[0]=='S' || r[0]=='y' || r[0]=='Y')) goto refuse;
printf ("\n");
}
else
n_print ("winfo",2,2,lg,"Warning, scanning a large netmask (%s), this will take a long time\n", nast_atoda(netmask));
}
/* begin to map */
if (mode)
{
n_print("princ",1,1,lg,"Mapping the Lan for %s subnet ... please wait\n\n", nast_atoda(netmask));
n_print("princ",3,1,lg,"MAC address\t\tIp address (hostname)\n");
n_print("princ",4,1,lg,"===========================================================\n");
}
/* print il localhost */
if (mode)
{
n_print ("princ",6,1,lg,"%s \t", nast_hex_ntoa (e->ether_addr_octet));
n_print ("princ",6,24,lg,"%s (%s) (*)\n",libnet_addr2name4(ip_src , 0),libnet_addr2name4(ip_src , LIBNET_RESOLVE));
}
/* open descriptor to read */
if ((descr = pcap_open_live (dev, BUFSIZ, NOT_PROMISC, 10, errbuf))==NULL)
{
w_error(1, "pcap_open_live() error : %s\n", errbuf);
}
/* put filter on arp */
if(pcap_compile(descr, &fp, "arp", 0, netp) == -1)
{
w_error(1,"Error calling pcap_compile\n\n");
}
if(pcap_setfilter(descr, &fp) == -1)
{
w_error(1, "Error calling pcap_setfilter\n\n");
}
/* begin! */
/* don't arp request subnet ip */
ip_dst[3]++;
/* 255.255.255.XXX */
if (!offset[0] && !offset[1] && !offset[2] && offset[3])
for (j[3]=0; j[3]<=offset[3]; j[3]++)
{
if (send_arp(l, dev, ip_dst, enet_src, ip_src)==-1) goto error;
arpreply(ip_dst, dev, mode, lg);
ip_dst[3]++;
}
/* 255.255.XXX.XXX */
else if (!offset[0] && !offset[1] && offset[2] && offset[3])
for (j[2]=0; j[2]<=offset[2]; j[2]++)
{
for (j[3]=0; j[3]<=offset[3]; j[3]++)
{
if (send_arp(l, dev, ip_dst, enet_src, ip_src)==-1) goto error;
arpreply(ip_dst, dev, mode, lg);
ip_dst[3]++;
}
ip_dst[2]++;
ip_dst[3]=orig_ip[3];
}
/* 255.XXX.XXX.XXX */
else if (!offset[0] && offset[1] && offset[2] && offset[3])
{
for (j[1]=0; j[1]<=offset[1]; j[1]++)
{
for (j[2]=0; j[2]<=offset[2]; j[2]++)
{
for (j[3]=0; j[3]<=offset[3]; j[3]++)
{
if (send_arp(l, dev, ip_dst, enet_src, ip_src)==-1) goto error;
arpreply(ip_dst, dev, mode, lg);
ip_dst[3]++;
}
ip_dst[2]++;
ip_dst[3]=orig_ip[3];
}
ip_dst[1]++;
ip_dst[2]=orig_ip[2];
}
}
/* XXX.XXX.XXX.XXX */
else if (offset[0] && offset[1] && offset[2] && offset[3])
{
for (j[0]=0; j[0]<=offset[1]; j[0]++)
{
for (j[1]=0; j[1]<=offset[1]; j[1]++)
{
for (j[2]=0; j[2]<=offset[2]; j[2]++)
{
for (j[3]=0; j[3]<=offset[3]; j[3]++)
{
if (send_arp(l, dev, ip_dst, enet_src, ip_src)==-1) goto error;
arpreply(ip_dst, dev, mode, lg);
ip_dst[3]++;
}
ip_dst[2]++;
ip_dst[3]=orig_ip[3];
}
ip_dst[1]++;
ip_dst[2]=orig_ip[2];
}
ip_dst[0]++;
ip_dst[1]=orig_ip[1];
}
}
/* paranoic test */
else
{
w_error(1, "Netmask error: %s is invalid\n\n", nast_atoda(netmask));
}
error:
if (mode) n_print ("winfo",2,1,lg,"\n(*) This is localhost\n\n");
refuse:
if (descr) pcap_close (descr);
if (l) libnet_destroy(l);
/* print to video (map has been called from cmd line) */
if (mode)
{
n_print("winfo",1,1,lg," \n");
n_print("winfo",1,1,lg,"Finished\n");
return NULL;
}
/* map has been called from another funz */
else
{
/* number of found hosts */
*n = k;
return (uphost);
}
}
/* stolen from arpreply by ? */
u_int scan_ulong(char *s, u_long *u)
{
u_int pos;
u_long c, result;
pos = result = 0;
while ((c = (u_long) (u_char) (s[pos] - '0')) < 10)
{
result = result * 10 + c;
++pos;
}
*u = result;
return (pos);
}
/* is it alive? */
int arpreply(u_char *t, char *dev, u_short mode,int lg)
{
struct nast_arp_hdr *arp;
struct libnet_ethernet_hdr *eptr;
u_short sd, pcount;
u_char ip[20];
struct timeval tv;
fd_set rfsd;
/* retrive socket descriptor for select() funz */
sd = pcap_fileno(descr);
/* timeout is 5 packet or timer.. */
pcount = 0;
/* try for an answer ... */
for (;;)
{
FD_ZERO (&rfsd);
FD_SET (sd ,&rfsd);
tv.tv_sec = 0;
tv.tv_usec = 20000;
if (pcount == 5) break;
if (!select(sd+1, &rfsd, NULL, NULL, &tv))
break; /* timeout */
if ((packet = (u_char *) pcap_next (descr, &hdr))==NULL)
continue;
offset=(device(dev,descr));
eptr = (struct libnet_ethernet_hdr *) (packet);
arp = (struct nast_arp_hdr *)(packet+offset);
/* It's an arp reply! */
if ((ntohs(arp->ar_op)) == 2)
{
sprintf (ip, "%d.%d.%d.%d", arp->__ar_sip[0],arp->__ar_sip[1],arp->__ar_sip[2],arp->__ar_sip[3]);
if (memcmp (t, arp->__ar_sip, sizeof(arp->__ar_sip)))
continue;
/* it's it! */
else
{
if (mode)
{
n_print("princ",line,1,lg,"%s \t%s (%s)\n", nast_hex_ntoa (eptr->ether_shost), ip, libnet_addr2name4(inet_addr(ip), LIBNET_RESOLVE));
++line;
}
else
{
/* ask for new memory */
if (k) uphost = realloc (uphost, (k+1)*sizeof(struct host));
memcpy (uphost[k].ip, arp->__ar_sip, 4);
memcpy (uphost[k].mac, eptr->ether_shost, 6);
k++;
}
}
break;
}
pcount ++;
}
return 0;
}
/* Build our arp request */
int send_arp(libnet_t *l, u_char *device, u_char *ip_dst, u_char *enet_src, u_long ip_src)
{
if ((ptag = libnet_build_arp(ARPHRD_ETHER, ETHERTYPE_IP, 6, 4, ARPOP_REQUEST, enet_src,
(u_char *)&ip_src, enet_dst, ip_dst, NULL, 0, l, ptag)) == -1)
{
w_error(1, "libnet_build_arp error : %s\n", libnet_geterror(l));
}
if (!count)
{
count ++;
if (libnet_build_ethernet(enet_dst, enet_src, ETHERTYPE_ARP, NULL, 0, l, 0)==-1)
{
w_error(1, "libnet_build_ethereal error : %s\n", libnet_geterror(l));
}
}
if (libnet_write(l)==-1)
{
w_error(1, "Error writing arp request : %s\n", libnet_geterror(l));
}
return 0;
}