forked from Dr-Noob/snetscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.c
192 lines (157 loc) · 5.08 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
#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.1";
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);
}
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) {
printf("WARNING: DEVICE option is mandatory\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);
}