-
Notifications
You must be signed in to change notification settings - Fork 4
/
scan.c
253 lines (211 loc) · 6.63 KB
/
scan.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
#include <stdio.h>
#include <stdlib.h>
#include <libnet.h>
#include <stdint.h>
#include <pthread.h>
#include <pcap.h>
#include <stdbool.h>
#include "args.h"
#include "cap.h"
#include "printer.h"
#define RESET "\033[0m"
#define BOLD "\033[1m"
static const char* VERSION = "0.11";
void printHelp(char *argv[]) {
printf("Usage: %s --dev DEVICE [--help] [--version]\n\
Options: \n\
--dev Set network interface\n\
--help Print this help and exit\n\
--version Print snetscan version and exit\n",
argv[0]);
}
void printVersion() {
printf("snetscan v%s\n",VERSION);
}
void printInterfaces() {
char errbuf[PCAP_ERRBUF_SIZE];
pcap_if_t *interfaces;
pcap_if_t *temp;
if(pcap_findalldevs(&interfaces, errbuf) == -1) {
printf("%s\n", errbuf);
return;
}
printf("Available devices are: \n");
for(temp=interfaces; temp != NULL; temp=temp->next) {
if(temp->addresses != NULL) {
printf(" * " BOLD "%s" RESET "\n",temp->name);
}
}
pcap_freealldevs(interfaces);
}
bool validForScan(pcap_if_t *iface) {
if(!(iface->flags & PCAP_IF_LOOPBACK) &&
#ifndef PCAP_IF_CONNECTION_STATUS
iface->flags & PCAP_IF_UP) {
#else
(iface->flags & PCAP_IF_CONNECTION_STATUS) == PCAP_IF_CONNECTION_STATUS_CONNECTED) {
#endif
if(iface->addresses != NULL) {
pcap_addr_t* list = iface->addresses;
for(; list->next != NULL; list = list->next) {
struct sockaddr* saddr = list->addr;
if(saddr->sa_family == AF_INET) {
//printf("Found candidate (%s) with address %s\n", iface->name, inet_ntoa(((struct sockaddr_in*)list->addr)->sin_addr));
return true;
}
}
}
}
return false;
}
/*
* Search for a network device that can
* be scanned. If more than one device is found,
* the function returns NULL, because in that case
* it is not clear what device should be used.
* If only one is found, that device is returned.
*/
char* getDefaultDevice() {
char* devname = NULL;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_if_t *interfaces;
pcap_if_t *temp;
if(pcap_findalldevs(&interfaces, errbuf) == -1) {
printf("%s\n", errbuf);
return NULL;
}
bool found = false;
for(temp=interfaces; temp != NULL; temp=temp->next) {
if(validForScan(temp)) {
if(found) {
return NULL;
}
else {
found = true;
devname = temp->name;
}
}
}
return devname;
}
int main(int argc, char* argv[]) {
parseArgs(argc, argv);
if(showHelp()) {
printHelp(argv);
return EXIT_SUCCESS;
}
if(showVersion()) {
printVersion();
return EXIT_SUCCESS;
}
/* pcap */
bpf_u_int32 mask;
bpf_u_int32 net;
char pcap_errbuf[PCAP_ERRBUF_SIZE];
/* libnet */
libnet_t *l;
libnet_ptag_t arp_tag = 0;
int bytes_written;
char errbuf[LIBNET_ERRBUF_SIZE];
u_int8_t mac_broadcast_addr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
u_int8_t mac_zero_addr[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
/* This host */
const char* devname;
u_int32_t src_ip_addr;
struct libnet_ether_addr *src_mac_addr;
/* Capture thread */
sem_t thread_sem;
pthread_t cap_thread;
struct cap_struct caps;
struct host_list *list;
/* User did not specify any device */
if((devname = getDevice()) == NULL) {
/* Try to find the interface in use */
devname = getDefaultDevice();
if(devname == NULL) {
printf("ERROR: DEVICE option was not specified and no device could be selected automatically\n");
printInterfaces();
printHelp(argv);
return EXIT_SUCCESS;
}
}
if ((l = libnet_init(LIBNET_LINK, devname, errbuf)) == NULL) {
fprintf(stderr, "libnet_init: %s\n", errbuf);
return EXIT_FAILURE;
}
printf("Using interface: '%s'\n",devname);
if(sem_init(&thread_sem, 0 , 0) == -1) {
perror("client");
return EXIT_FAILURE;
}
caps.sem = &thread_sem;
caps.ok = malloc(sizeof(bool));
caps.dev = devname;
if(pthread_create(&cap_thread, NULL, &cap, &caps) == -1) {
perror("pthread_create");
return EXIT_FAILURE;
}
if(sem_wait(&thread_sem) == -1) {
perror("client");
return EXIT_FAILURE;
}
if(!*caps.ok) {
return EXIT_FAILURE;
}
if (pcap_lookupnet(devname, &net, &mask, pcap_errbuf) == -1) {
fprintf(stderr, "%s\n", errbuf);
return EXIT_FAILURE;
}
if ((src_ip_addr = libnet_get_ipaddr4(l)) == (u_int32_t)-1) {
fprintf(stderr, "%s\n", libnet_geterror(l));
libnet_destroy(l);
return EXIT_FAILURE;
}
if ((src_mac_addr = libnet_get_hwaddr(l)) == NULL) {
fprintf(stderr, "%s\n", libnet_geterror(l));
libnet_destroy(l);
return EXIT_FAILURE;
}
mask = htonl(mask);
uint32_t network_address = htonl(src_ip_addr) & mask;
uint32_t broadcast_address = htonl(src_ip_addr) | ~mask;
printf("Scanning from %d.%d.%d.%d to %d.%d.%d.%d\n", ((network_address + 1) & 0xFF000000) >> 24,
((network_address + 1) & 0x00FF0000) >> 16,
((network_address + 1) & 0x0000FF00) >> 8,
(network_address + 1) & 0x000000FF,
((broadcast_address - 1) & 0xFF000000) >> 24,
((broadcast_address - 1) & 0x00FF0000) >> 16,
((broadcast_address - 1) & 0x0000FF00) >> 8,
(broadcast_address - 1) & 0x000000FF);
for (uint32_t ip = network_address + 1; ip < broadcast_address; ip++) {
uint32_t target_ip_addr = ntohl(ip);
if ((arp_tag = libnet_build_arp (ARPHRD_ETHER, ETHERTYPE_IP, 6, 4, ARPOP_REQUEST, src_mac_addr->ether_addr_octet, (u_int8_t*)(&src_ip_addr), mac_zero_addr, (u_int8_t*)(&target_ip_addr), NULL, 0, l, arp_tag)) == -1) {
fprintf(stderr, "%s\n", libnet_geterror(l));
libnet_destroy(l);
return EXIT_FAILURE;
}
if(ip == network_address + 1) {
/* Just build at first iteration(reused in the others iterations) */
if (libnet_autobuild_ethernet (mac_broadcast_addr, ETHERTYPE_ARP, l) == -1 ) {
fprintf(stderr, "%s\n", libnet_geterror(l));
libnet_destroy(l);
return EXIT_FAILURE;
}
}
bytes_written = libnet_write(l);
if (bytes_written == -1) {
fprintf(stderr, "%s\n", libnet_geterror(l));
}
}
libnet_destroy(l);
printf("Waiting for requests...\n");
sleep(1);
/* End cap thread */
pcap_breakloop(caps.ctx);
if(pthread_join(cap_thread, NULL) == -1) {
perror("pthread_join");
return EXIT_FAILURE;
}
free(caps.ok);
return print_hosts(caps.list->next, src_ip_addr);
}