-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.cpp
More file actions
144 lines (124 loc) · 3.64 KB
/
scanner.cpp
File metadata and controls
144 lines (124 loc) · 3.64 KB
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
//
// Created by juraj on 08/04/19.
//
#include "scanner.h"
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netinet/ip.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <iostream>
#include <pcap.h>
unsigned short Scanner::csum(unsigned short *buffer,int size)
{
long sum = 0;
unsigned short oddbyte;
while (size > 1)
{
sum += *buffer++;
size -= 2;
}
if (size == 1)
{
oddbyte=0;
*((u_char*)&oddbyte)=*(u_char*)buffer;
sum += oddbyte;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return ~sum;
}
void Scanner::create_ip_hdr(int transport_layer)
{
if (transport_layer == IPPROTO_TCP)
{
iphdr->tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
iphdr->protocol = IPPROTO_TCP;
}
else if (transport_layer == IPPROTO_UDP)
{
iphdr->tot_len = sizeof(struct iphdr) + sizeof(struct udphdr);
iphdr->protocol = IPPROTO_UDP;
}
iphdr->ihl = 5;
iphdr->version = 4;
iphdr->tos = 0;
iphdr->id = htons(20290); // port of this packet -> from range 49152 – 65535:Dynamic and/or Private Ports
iphdr->frag_off = 0;
iphdr->ttl = 64; // 64 hops
iphdr->check = 0;
iphdr->saddr = inet_addr(get_local_ipaddr().c_str());
iphdr->daddr = dest_address.sin_addr.s_addr;
iphdr->check = csum((unsigned short*)buffer, iphdr->tot_len);
}
string Scanner::get_local_ipaddr()
{
int sock;
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("ERROR: socket");
exit(EXIT_FAILURE);
}
int domain_name_server_port = 53;
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("8.8.8.8"); // google public dns ip https://developers.google.com/speed/public-dns/docs/using
addr.sin_port = htons(domain_name_server_port);
if (connect(sock, (const struct sockaddr *) &addr, sizeof(addr)) != 0)
{
perror("ERROR: connect");
exit(EXIT_FAILURE);
}
struct sockaddr_in name;
socklen_t len = sizeof(name);
if (getsockname(sock, (struct sockaddr*)&name, &len) != 0)
{
perror("ERROR: getsockname");
exit(EXIT_FAILURE);
}
char buff[256];
inet_ntop(AF_INET, &name.sin_addr, buff, 256);
close(sock);
return string(buff);
}
void Scanner::find_iface(string dst_addr)
{
char err[PCAP_ERRBUF_SIZE];
pcap_if_t *all_devs;
if (pcap_findalldevs(&all_devs, err) != 0)
{
perror("pcap_findalldevs");
exit(1);
}
for (pcap_if_t *i = all_devs; i != NULL; i = i->next)
{
char buff[INET6_ADDRSTRLEN];
for (pcap_addr_t *j = i->addresses; j != NULL; j = j->next)
{
if (j->addr->sa_family == AF_INET and string(i->name) != "lo")
{
this->iface = i->name;
this->ipv4_addr = inet_ntoa(((struct sockaddr_in*)j->addr)->sin_addr);
}
if (j->addr->sa_family == AF_INET and string(i->name) == "lo")
this->lo_addr = inet_ntoa(((struct sockaddr_in*)j->addr)->sin_addr);
//else if (j->addr->sa_family == AF_INET6)
//{
// inet_ntop(AF_INET6, (void*)&(((struct sockaddr_in6*)j->addr)->sin6_addr), buff, sizeof(buff));
// cout << i->name << " " << buff << "\n";
//}
}
}
if (this->lo_addr == dst_addr)
this->iface = "any";
if (this->iface.empty())
{
fprintf(stderr, "No interface found!\n");
exit(1);
}
}