-
Notifications
You must be signed in to change notification settings - Fork 0
/
pds-scanner.cpp
182 lines (145 loc) · 5.84 KB
/
pds-scanner.cpp
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
//
// pds-scanner.cpp
// pds-project
//
// Created by Jordán Jarolím on 13.04.17.
// Copyright © 2017 FIT VUTBR. All rights reserved.
//
#include "pds-scanner.hpp"
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <netdb.h>
#include <netinet/if_ether.h>
#include "xml/rapidxml.hpp"
#include "xml/rapidxml_print.hpp"
#include <fstream>
#include <sstream>
#include "packet.hpp"
#include "types.h"
using namespace std;
using namespace rapidxml;
string PdsScanner::filename;
string PdsScanner::interface;
/**
* Constructor
*/
PdsScanner::PdsScanner(string interface, string filename){
this->interface = interface;
this->filename = filename;
}
/**
* pcal_loop handler
*/
void callback(unsigned char *user, const struct pcap_pkthdr *pkthdr, const unsigned char *packet)
{
((Packet *)user)->saveHost(pkthdr, packet);
}
/**
* Get info about network to scan + my info
*/
void PdsScanner::resolveIPv4Network(){
pcap_if_t *all;
pcap_addr *oneIp;
struct ifreq s;
int fd;
char error[PCAP_ERRBUF_SIZE] = "\0";
/* Get IP assigned to interface + its mask - http://stackoverflow.com/questions/9443288/get-ip-adress-of-interface-in-linux-using-pcap */
if (pcap_findalldevs(&all, error) != 0) {
cerr << "Error: " << error << "\n";
exit(EXIT_FAILURE);
}
for (pcap_if_t *d = all; d!=NULL; d = d->next) {
if (!strcmp(d->name, interface.c_str())) {
for (oneIp = d->addresses; oneIp!=NULL; oneIp = oneIp->next) {
if (oneIp->addr->sa_family == AF_INET){
memcpy(&(addresses.myIPv4), oneIp->addr, sizeof(struct sockaddr));
memcpy(&(addresses.length), oneIp->netmask, sizeof(struct sockaddr));
}
}
}
}
pcap_freealldevs(all);
/* Get net IP - logical and between my address and mask */
(addresses.netIP).sin_addr.s_addr = ((sockaddr_in *)(&(addresses.myIPv4)))->sin_addr.s_addr & ((sockaddr_in *)(&(addresses.length)))->sin_addr.s_addr;
/* Get bcast address - http://stackoverflow.com/questions/777617/calculate-broadcast-address-from-ip-and-subnet-mask */
addresses.bcast.sin_addr.s_addr = ((sockaddr_in *)(&(addresses.myIPv4)))->sin_addr.s_addr | (~ ((sockaddr_in *)(&(addresses.length)))->sin_addr.s_addr);
/* Get mac address - http://stackoverflow.com/questions/1779715/how-to-get-mac-address-of-your-machine-using-a-c-program */
fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
strcpy(s.ifr_name, interface.c_str());
if (0 == ioctl(fd, SIOCGIFHWADDR, &s)) {
int i;
for (i = 0; i < 6; ++i)
addresses.myMac.push_back(s.ifr_addr.sa_data[i]);
}
/* Save string representation of addresses */
addresses.myIPv4Str = inet_ntoa(((sockaddr_in *)&(addresses.myIPv4))->sin_addr);
addresses.netIPStr = inet_ntoa(addresses.netIP.sin_addr);
addresses.bcastStr = inet_ntoa(addresses.bcast.sin_addr);
/* convert byte representation of mac to string */
for (int i = 0; i < 6; i++) {
sprintf(addresses.myMacStr + strlen(addresses.myMacStr), "%02x%s", addresses.myMac[i], (i % 2 == 1 && i < 5) ? ":" : "");
}
/* Print it */
cout << "myIP: "<< inet_ntoa(((sockaddr_in *)&(addresses.myIPv4))->sin_addr);
cout << " netIP: "<< inet_ntoa(addresses.netIP.sin_addr);
cout << " netMask: "<<inet_ntoa(((sockaddr_in *)&(addresses.length))->sin_addr);
cout << " broadcast: "<<inet_ntoa(addresses.bcast.sin_addr);
cout << " myMac: ";
for (int i = 0; i < addresses.myMac.size(); i++)
printf(":%02x", (unsigned char) addresses.myMac[i]);
cout<<"\n";
}
void PdsScanner::getHosts(){
/* Prepare xml file */
ManipulateXml::prepareXml(filename);
/* Prepare pcaps for child */
char error_buffer[PCAP_ERRBUF_SIZE] = "\0";
pcap_t *pcapChild = pcap_open_live(this->interface.c_str(), sizeof(struct ether_header) + sizeof(struct ether_arp), true, 0, error_buffer);
/* Prepare pcaps for parent */
struct bpf_program filter;
pcap_t *pcapParent = pcap_open_live(this->interface.c_str(), sizeof(struct ether_header) + sizeof(struct ether_arp), true, 1000, error_buffer);
/* Create filter */
if (pcap_compile(pcapParent, &filter, "arp", 1, (bpf_u_int32)((sockaddr_in *)&(addresses.length))->sin_addr.s_addr) < 0)
exit(EXIT_FAILURE);
/* Load filter */
if (pcap_setfilter(pcapParent, &filter) < 0)
exit(EXIT_FAILURE);
/* One process to send ARP, another to recieve them */
pid_t pid = fork();
if (pid < 0)
exit(EXIT_FAILURE);
else
cout << "fork OK\n";
Packet *packet = new Packet;
if (pid == 0) {
unsigned char byteBcastMac[6];
unsigned char byteMyMac[6];
sockaddr_in target = addresses.netIP;
while (target.sin_addr.s_addr < addresses.bcast.sin_addr.s_addr - ntohl((uint32_t)1)){
target.sin_addr.s_addr = target.sin_addr.s_addr + ntohl((uint32_t)1);
if (target.sin_addr.s_addr != ((sockaddr_in *)&(addresses.myIPv4))->sin_addr.s_addr){
packet->simplifyMac(BCASTMAC, byteBcastMac);
packet->simplifyMac(addresses.myMacStr, byteMyMac);
packet->sendPacket(pcapChild, byteBcastMac, byteMyMac, byteBcastMac, byteMyMac, inet_ntoa(target.sin_addr), addresses.myIPv4Str, ARPOP_REQUEST);
}
}
pcap_close(pcapChild);
} else {
/* http://www.tcpdump.org/pcap.html */
pcap_loop(pcapParent, -1, callback, reinterpret_cast<u_char*>(packet));
pcap_close(pcapParent);
}
delete packet;
}
/**
* Scan ipv4 network, ipv6 has not been implemented yet
*/
int PdsScanner::scanIPv4(){
this->resolveIPv4Network();
this->getHosts();
return 0;
}