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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
#include <chrono>
#include <EthLayer.h>
#include <exception>
#include <IPLayer.h>
#include <IPv4Layer.h>
#include <IPv6Layer.h>
#include <random>
#include <SystemUtils.h>
#include <TcpLayer.h>
#include <tuple>
#include <UdpLayer.h>
#include "pcapplusplus.h"
PcapPlusPlus::PcapPlusPlus(std::string fileName) : rawPackets(), parsedPackets()
{
reader = pcpp::IFileReaderDevice::getReader(fileName);
if (!reader) {
std::string err("PCAP Reader failed for: " + fileName);
throw std::runtime_error(err);
}
if (!reader->open()) {
std::string err("PCAP Open failed for: " + fileName);
throw std::runtime_error(err);
}
}
PcapPlusPlus::~PcapPlusPlus()
{
if (reader) {
reader->close();
delete reader;
reader = nullptr;
}
}
bool PcapPlusPlus::openPcap()
{
if (!reader)
return false;
if (reader->isOpened())
throw std::runtime_error("PCAP File already open. Close it first.");
return reader->open();
}
void PcapPlusPlus::closePcap()
{
if (reader)
reader->close();
}
bool PcapPlusPlus::setFilter(QString filter)
{
if (reader)
return reader->setFilter(filter.toStdString());
return false;
}
bool PcapPlusPlus::processPacket(pcpp::Packet & packet)
{
pcpp::RawPacket rawPacket;
if (!reader)
return false;
if (reader->getNextPacket(rawPacket)) {
if (rawPackets.size() == 0) {
const auto & pktTs = rawPacket.getPacketTimeStamp();
firstPacketTs = pktTs.tv_sec;
firstPacketTs += pktTs.tv_nsec / 10e9f;
}
rawPackets.emplace_back(std::move(rawPacket));
pcpp::Packet parsedPacket(&rawPackets.back(), false);
packet = parsedPacket;
parsedPackets.emplace_back(std::move(parsedPacket));
return true;
}
return false;
}
pcpp::RawPacket& PcapPlusPlus::getRawPacket(size_t index)
{
return rawPackets.at(index);
}
pcpp::Packet& PcapPlusPlus::getParsedPacket(size_t index)
{
return parsedPackets.at(index);
}
pcpp::LinkLayerType PcapPlusPlus::getLinkLayer()
{
const auto& pcap_file_reader = dynamic_cast<pcpp::PcapFileReaderDevice*>(reader);
if (pcap_file_reader)
return pcap_file_reader->getLinkLayerType();
else
return pcpp::LinkLayerType::LINKTYPE_NULL;
}
std::vector<pcpp::RawPacket>::iterator PcapPlusPlus::rawPacketsBegin()
{
return rawPackets.begin();
}
std::vector<pcpp::RawPacket>::iterator PcapPlusPlus::rawPacketsEnd()
{
return rawPackets.end();
}
std::vector<pcpp::Packet>::iterator PcapPlusPlus::parsedPacketsBegin()
{
return parsedPackets.begin();
}
std::vector<pcpp::Packet>::iterator PcapPlusPlus::parsedPacketsEnd()
{
return parsedPackets.end();
}
bool PcapPlusPlus::randomizeEth(size_t index, bool isSourceEth)
{
std::default_random_engine generator(std::chrono::system_clock::now().time_since_epoch().count());
auto retval = false;
auto parsedPacket = pcpp::Packet(&getRawPacket(index));
auto * ethLayer = parsedPacket.getLayerOfType<pcpp::EthLayer>();
if (ethLayer) {
std::uniform_int_distribution<unsigned long long int> ethDistribution(0, 0xFFFFFFFFFFFFFFFF);
unsigned long long int newEthAddr = ethDistribution(generator);
if (isSourceEth)
std::memcpy(ethLayer->getEthHeader()->srcMac, reinterpret_cast<uint8_t *>(&newEthAddr), 6);
else
std::memcpy(ethLayer->getEthHeader()->dstMac, reinterpret_cast<uint8_t *>(&newEthAddr), 6);
retval = true;
}
parsedPackets[index] = parsedPacket;
return retval;
}
bool PcapPlusPlus::randomizeIp(size_t index, bool isSourceIp)
{
std::default_random_engine generator(std::chrono::system_clock::now().time_since_epoch().count());
auto retval = false;
auto parsedPacket = pcpp::Packet(&getRawPacket(index));
auto * ip4Layer = parsedPacket.getLayerOfType<pcpp::IPv4Layer>();
auto * ip6Layer = parsedPacket.getLayerOfType<pcpp::IPv6Layer>();
if (ip4Layer) {
std::uniform_int_distribution<unsigned int> ip4Distribution(0, 0xFFFFFFFF);
if (isSourceIp)
ip4Layer->getIPv4Header()->ipSrc = pcpp::IPv4Address(ip4Distribution(generator)).toInt();
else
ip4Layer->getIPv4Header()->ipDst = pcpp::IPv4Address(ip4Distribution(generator)).toInt();
retval = true;
}
if (ip6Layer) {
std::uniform_int_distribution<unsigned long long int> ip6Distribution(0, 0xFFFFFFFFFFFFFFFF);
unsigned long long int newIp6Addr[2] { ip6Distribution(generator), ip6Distribution(generator) };
if (isSourceIp)
std::memcpy(ip6Layer->getIPv6Header()->ipSrc, reinterpret_cast<uint8_t *>(newIp6Addr), 16);
else
std::memcpy(ip6Layer->getIPv6Header()->ipDst, reinterpret_cast<uint8_t *>(newIp6Addr), 16);
retval = true;
}
parsedPackets[index] = parsedPacket;
return retval;
}
bool PcapPlusPlus::randomizePort(size_t index, bool isSourcePort)
{
std::default_random_engine generator(std::chrono::system_clock::now().time_since_epoch().count());
auto retval = false;
auto parsedPacket = pcpp::Packet(&getRawPacket(index));
auto * udpLayer = parsedPacket.getLayerOfType<pcpp::UdpLayer>();
auto * tcpLayer = parsedPacket.getLayerOfType<pcpp::TcpLayer>();
std::uniform_int_distribution<unsigned short> portDistribution(0, 0xFFFF);
if (udpLayer) {
if (isSourcePort)
udpLayer->getUdpHeader()->portSrc = portDistribution(generator);
else
udpLayer->getUdpHeader()->portDst = portDistribution(generator);
retval = true;
}
if (tcpLayer) {
if (isSourcePort)
tcpLayer->getTcpHeader()->portSrc = portDistribution(generator);
else
tcpLayer->getTcpHeader()->portDst = portDistribution(generator);
retval = true;
}
parsedPackets[index] = parsedPacket;
return retval;
}
bool PcapPlusPlus::setEth(size_t index, const std::string & eth, bool isSourceEth)
{
auto retval = false;
auto parsedPacket = pcpp::Packet(&getRawPacket(index));
auto * ethLayer = parsedPacket.getLayerOfType<pcpp::EthLayer>();
if (ethLayer) {
pcpp::MacAddress mac(eth);
if (isSourceEth)
std::memcpy(ethLayer->getEthHeader()->srcMac, mac.getRawData(), 6);
else
std::memcpy(ethLayer->getEthHeader()->dstMac, mac.getRawData(), 6);
retval = true;
}
parsedPackets[index] = parsedPacket;
return retval;
}
bool PcapPlusPlus::setIp(size_t index, const std::string & ip, bool isSourceIp)
{
auto retval = false;
auto parsedPacket = pcpp::Packet(&getRawPacket(index));
auto * ip4Layer = parsedPacket.getLayerOfType<pcpp::IPv4Layer>();
auto * ip6Layer = parsedPacket.getLayerOfType<pcpp::IPv6Layer>();
if (ip4Layer) {
pcpp::IPv4Address addr(ip);
if (isSourceIp)
ip4Layer->getIPv4Header()->ipSrc = addr.toInt();
else
ip4Layer->getIPv4Header()->ipDst = addr.toInt();
retval = true;
}
if (ip6Layer) {
pcpp::IPv6Address addr(ip);
if (isSourceIp)
std::memcpy(ip6Layer->getIPv6Header()->ipSrc, addr.toBytes(), 16);
else
std::memcpy(ip6Layer->getIPv6Header()->ipDst, addr.toBytes(), 16);
retval = true;
}
parsedPackets[index] = parsedPacket;
return retval;
}
bool PcapPlusPlus::setPort(size_t index, const std::string & port, bool isSourcePort)
{
auto retval = false;
auto parsedPacket = pcpp::Packet(&getRawPacket(index));
auto * udpLayer = parsedPacket.getLayerOfType<pcpp::UdpLayer>();
auto * tcpLayer = parsedPacket.getLayerOfType<pcpp::TcpLayer>();
const unsigned short portNum = pcpp::hostToNet16(std::stoi(port));
if (udpLayer) {
if (isSourcePort)
udpLayer->getUdpHeader()->portSrc = portNum;
else
udpLayer->getUdpHeader()->portDst = portNum;
retval = true;
}
if (tcpLayer) {
if (isSourcePort)
tcpLayer->getTcpHeader()->portSrc = portNum;
else
tcpLayer->getTcpHeader()->portDst = portNum;
retval = true;
}
parsedPackets[index] = parsedPacket;
return retval;
}
void PcapPlusPlus::fixupHeaders(size_t index)
{
auto parsedPacket = pcpp::Packet(&getRawPacket(index));
auto * ip4Layer = parsedPacket.getLayerOfType<pcpp::IPv4Layer>();
auto * ip6Layer = parsedPacket.getLayerOfType<pcpp::IPv6Layer>();
auto * udpLayer = parsedPacket.getLayerOfType<pcpp::UdpLayer>();
auto * tcpLayer = parsedPacket.getLayerOfType<pcpp::TcpLayer>();
if (ip4Layer)
ip4Layer->computeCalculateFields();
if (ip6Layer)
ip6Layer->computeCalculateFields();
if (udpLayer)
udpLayer->computeCalculateFields();
if (tcpLayer)
tcpLayer->computeCalculateFields();
}
bool PcapPlusPlus::getPcapStatistics(pcpp::IFileDevice::PcapStats & stats)
{
if (reader) {
reader->getStatistics(stats);
return true;
}
return false;
}
const pcpp::Layer *PcapPlusPlus::getFirstLayer(const pcpp::Packet & packet)
{
const auto *curLayer = packet.getFirstLayer();
if (curLayer == nullptr)
{
throw std::runtime_error("No Ethernet Layer found.");
return nullptr;
}
return curLayer;
}
QString PcapPlusPlus::getProtocolTypeAsString(pcpp::ProtocolType protocolType)
{
switch (protocolType)
{
case pcpp::Ethernet:
return "Ethernet";
case pcpp::ARP:
return "ARP";
case pcpp::IPv4:
return "IPv4";
case pcpp::IPv6:
return "IPv6";
case pcpp::TCP:
return "TCP";
case pcpp::UDP:
return "UDP";
case pcpp::HTTPRequest:
case pcpp::HTTPResponse:
return "HTTP";
case pcpp::UnknownProtocol:
return "Unknown Protocol";
default:
return "Unknown";
}
}
std::tuple<QString, QString> PcapPlusPlus::getEthTuple(const pcpp::Packet & packet)
{
const auto *ethernetLayer = packet.getLayerOfType<pcpp::EthLayer>();
if (ethernetLayer == NULL)
return std::make_tuple("", "");
return std::make_tuple(QString::fromStdString(ethernetLayer->getSourceMac().toString()), QString::fromStdString(ethernetLayer->getDestMac().toString()));
}
std::tuple<QString, QString> PcapPlusPlus::getIpTuple(const pcpp::Packet & packet)
{
const auto *ipLayer = packet.getLayerOfType<pcpp::IPLayer>();
if (ipLayer == NULL)
return std::make_tuple("", "");
return std::make_tuple(QString::fromStdString(ipLayer->getSrcIPAddress().toString()), QString::fromStdString(ipLayer->getDstIPAddress().toString()));
}
std::tuple<uint16_t, uint16_t> PcapPlusPlus::getLayer4Tuple(const pcpp::Packet & packet)
{
const auto *udpLayer = packet.getLayerOfType<pcpp::UdpLayer>();
if (udpLayer != NULL)
return std::make_tuple(udpLayer->getSrcPort(), udpLayer->getDstPort());
const auto *tcpLayer = packet.getLayerOfType<pcpp::TcpLayer>();
if (tcpLayer != NULL)
return std::make_tuple(tcpLayer->getSrcPort(), tcpLayer->getDstPort());
return std::make_tuple(0, 0);
}
|