-
Notifications
You must be signed in to change notification settings - Fork 1
/
operationshandler.h
312 lines (292 loc) · 10.4 KB
/
operationshandler.h
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#ifndef OPERATIONSHANDLER_H
#define OPERATIONSHANDLER_H
#include <QObject>
#include "stdlib.h"
#include "IpAddress.h"
#include "PcapLiveDeviceList.h"
#include "PlatformSpecificUtils.h"
#include "Packet.h"
#include "TcpLayer.h"
#include "UdpLayer.h"
#include <arpa/inet.h>
#include <QDebug>
#include <QDateTime>
class OperationsHandler : public QObject
{
Q_OBJECT
struct PacketStats
{
/// Packet Count
long ethPacketCount;
long ipv4PacketCount;
long ipv6PacketCount;
long tcpPacketCount;
long udpPacketCount;
long dnsPacketCount;
long httpPacketCount;
long sslPacketCount;
long sipPacketCount ;
int voipPacketCount;
long rtpPacketCount;
long ftpPacketCount;
long dhcpPacketCount;
long icmpPacketCount;
long pppoePacketCount;
long arpPacketCount;
long httpsPacketCount;
long totalPackets ;
long lastSIPPacketArrivedAtPosition;
/// LastRecieved
QString ethLastPacketRecievedTime;
QString ipv4LastPacketRecievedTime;
QString ipv6LastPacketRecievedTime;
QString tcpLastPacketRecievedTime;
QString udpLastPacketRecievedTime;
QString dnsLastPacketRecievedTime;
QString httpLastPacketRecievedTime;
QString sslLastPacketRecievedTime;
QString sipLastPacketRecievedTime ;
QString voipLastPacketRecievedTime;
QString rtpLastPacketRecievedTime;
QString ftpLastPacketRecievedTime;
QString dhcpLastPacketRecievedTime;
QString icmpLastPacketRecievedTime;
QString pppoeLastPacketRecievedTime;
QString arpLastPacketRecievedTime;
QString httpsLastPacketRecievedTime;
public:
/**
* Clear all stats
*/
void clear()
{
voipPacketCount =0 ;
rtpPacketCount = 0;
ftpPacketCount = 0;
dhcpPacketCount = 0;
icmpPacketCount = 0;
pppoePacketCount = 0;
arpPacketCount = 0;
httpsPacketCount = 0;
sipPacketCount = 0;
ethPacketCount = 0; //
ipv4PacketCount = 0; //
ipv6PacketCount = 0; //
tcpPacketCount = 0; //
udpPacketCount = 0;
dnsPacketCount = 0;
httpPacketCount = 0;
sslPacketCount = 0;
totalPackets=0;
lastSIPPacketArrivedAtPosition=0;
}
/**
* C'tor
*/
PacketStats() { clear(); }
pcpp::tcphdr* extractTcpHeaderFromPacket(pcpp::Packet& packet)
{
pcpp::Layer* layer ;
if (packet.isPacketOfType(pcpp::TCP))
{
layer = packet.getLayerOfType<pcpp::TcpLayer>();
return ((pcpp::TcpLayer*)layer)->getTcpHeader();
}
else
{
qDebug()<<"The packet does not have a TCP Layer"<<endl ;
return NULL ;
}
}
pcpp::udphdr* extractUdpHeaderFromPacket(pcpp::Packet& packet)
{
pcpp::Layer* layer ;
if (packet.isPacketOfType(pcpp::UDP))
{
layer = packet.getLayerOfType<pcpp::UdpLayer>();
return ((pcpp::UdpLayer*)layer)->getUdpHeader();
}
else
{
qDebug()<<"The packet does not have a UDP Layer"<<endl ;
return NULL ;
}
}
/**
* Collect stats from a packet
*/
void consumePacket(pcpp::Packet& packet)
{
pcpp::tcphdr * tcpHeader= NULL ;
pcpp::udphdr * udpHeader = NULL ;
bool isHttp =false ;
QString currentTime = QDateTime::currentDateTime().toString();
if (packet.isPacketOfType(pcpp::Ethernet))
{
ethPacketCount++;
ethLastPacketRecievedTime= currentTime;
}
if (packet.isPacketOfType(pcpp::IPv4))
{
ipv4PacketCount++;
ipv4LastPacketRecievedTime= currentTime;
}
if (packet.isPacketOfType(pcpp::IPv6))
{
ipv6PacketCount++;
ipv6LastPacketRecievedTime= currentTime;
}
if (packet.isPacketOfType(pcpp::TCP))
{
tcpPacketCount++;
tcpLastPacketRecievedTime= currentTime;
tcpHeader = extractTcpHeaderFromPacket(packet);
}
if (packet.isPacketOfType(pcpp::UDP))
{
udpPacketCount++;
udpLastPacketRecievedTime= currentTime;
udpHeader = extractUdpHeaderFromPacket(packet);
}
if (packet.isPacketOfType(pcpp::DNS))
{
dnsPacketCount++;
ethLastPacketRecievedTime= currentTime;
}
if (packet.isPacketOfType(pcpp::HTTP))
{
isHttp = true ;
httpPacketCount++;
httpLastPacketRecievedTime= currentTime;
}
if (packet.isPacketOfType(pcpp::SSL))
{
sslPacketCount++;
sslLastPacketRecievedTime= currentTime;
if(isHttp) // Both HTTP and SSL -> HTTPS
{
httpsPacketCount++;
}
}
if (packet.isPacketOfType(pcpp::SIP))
{
sipPacketCount++;
sipLastPacketRecievedTime= currentTime;
lastSIPPacketArrivedAtPosition = totalPackets;
}
if (packet.isPacketOfType(pcpp::DHCP))
{
dhcpPacketCount++;
dhcpLastPacketRecievedTime= currentTime;
}
if (packet.isPacketOfType(pcpp::ICMP))
{
icmpPacketCount++;
icmpLastPacketRecievedTime= currentTime;
}
if (packet.isPacketOfType(pcpp::PPPoE))
{
pppoePacketCount++;
pppoeLastPacketRecievedTime= currentTime;
}
if (packet.isPacketOfType(pcpp::ARP))
{
arpPacketCount++;
arpLastPacketRecievedTime= currentTime;
}
if(tcpHeader)
{
if(tcpHeader->portDst == 5060 || tcpHeader->portDst == 5061 ||
tcpHeader->portSrc == 5060 || tcpHeader->portSrc == 5061)
{
voipLastPacketRecievedTime= currentTime;
voipPacketCount++;
}
if(tcpHeader->portDst == 20 || tcpHeader->portDst == 21 ||
tcpHeader->portSrc == 20 || tcpHeader->portSrc == 21)
{
ftpLastPacketRecievedTime= currentTime;
ftpPacketCount++;
}
}
else if(udpHeader)
{
if((udpHeader->portDst>1024 && udpHeader->portSrc<65535) &&
(lastSIPPacketArrivedAtPosition!=0 && totalPackets-lastSIPPacketArrivedAtPosition<10))
{ // If is a UDP packet and SIP packet was arrived recently
rtpLastPacketRecievedTime= currentTime;
rtpPacketCount++;
}
}
totalPackets++;
}
/**
* Print stats to console
*/
void printToConsole()
{
printf("Ethernet packet count: %d\n", ethPacketCount);
printf("IPv4 packet count: %d\n", ipv4PacketCount);
printf("IPv6 packet count: %d\n", ipv6PacketCount);
printf("TCP packet count: %d\n", tcpPacketCount);
printf("UDP packet count: %d\n", udpPacketCount);
printf("DNS packet count: %d\n", dnsPacketCount);
printf("HTTP packet count: %d\n", httpPacketCount);
printf("SSL packet count: %d\n", sslPacketCount);
}
};
static void onPacketArrives(pcpp::RawPacket* packet, pcpp::PcapLiveDevice* dev, void* cookie)
{
// extract the stats object form the cookie
PacketStats* stats = (PacketStats*)cookie;
// parsed the raw packet
pcpp::Packet parsedPacket(packet);
// collect stats from packet
stats->consumePacket(parsedPacket);
}
PacketStats * packetAnayzer ;
pcpp::PcapLiveDevice* dev;
public:
Q_INVOKABLE long getEthPacketCount();
Q_INVOKABLE long getIpv4PacketCount();
Q_INVOKABLE long getIpv6PacketCount();
Q_INVOKABLE long getTcpPacketCount();
Q_INVOKABLE long getUdpPacketCount();
Q_INVOKABLE long getDnsPacketCount();
Q_INVOKABLE long getHttpPacketCount();
Q_INVOKABLE long getSslPacketCount();
Q_INVOKABLE long getSipPacketCount();
Q_INVOKABLE long getVoipPacketCount();
Q_INVOKABLE long getRtpPacketCount();
Q_INVOKABLE long getFtpPacketCount();
Q_INVOKABLE long getDhcpPacketCount();
Q_INVOKABLE long getIcmpPacketCount();
Q_INVOKABLE long getPppoePacketCount();
Q_INVOKABLE long getArpPacketCount();
Q_INVOKABLE long getHttpsPacketCount();
Q_INVOKABLE long getTotalPackets();
Q_INVOKABLE QString getEthLastPacketRecievedTime();
Q_INVOKABLE QString getIpv4LastPacketRecievedTime();
Q_INVOKABLE QString getIpv6LastPacketRecievedTime();
Q_INVOKABLE QString getTcpLastPacketRecievedTime();
Q_INVOKABLE QString getUdpLastPacketRecievedTime();
Q_INVOKABLE QString getDnsLastPacketRecievedTime();
Q_INVOKABLE QString getHttpLastPacketRecievedTime();
Q_INVOKABLE QString getSslLastPacketRecievedTime();
Q_INVOKABLE QString getSipLastPacketRecievedTime();
Q_INVOKABLE QString getVoipLastPacketRecievedTime();
Q_INVOKABLE QString getRtpLastPacketRecievedTime();
Q_INVOKABLE QString getFtpLastPacketRecievedTime();
Q_INVOKABLE QString getDhcpLastPacketRecievedTime();
Q_INVOKABLE QString getIcmpLastPacketRecievedTime();
Q_INVOKABLE QString getPppoeLastPacketRecievedTime();
Q_INVOKABLE QString getArpLastPacketRecievedTime();
Q_INVOKABLE QString getHttpsLastPacketRecievedTime();
Q_INVOKABLE void startLiveAnalysis();
Q_INVOKABLE void pauseLiveAnalysis();
Q_INVOKABLE void stopLiveAnalysis();
explicit OperationsHandler(QObject *parent = nullptr);
signals:
public slots:
};
#endif // OPERATIONSHANDLER_H