-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
285 lines (269 loc) · 9.53 KB
/
mainwindow.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
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QIntValidator>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->editTTL->setValidator(new QIntValidator(0, 255, this));
ui->comboFlag->addItem("1");
ui->comboFlag->addItem("0");
foreach(QNetworkInterface netInterface, QNetworkInterface::allInterfaces())
{
if (!(netInterface.flags() & QNetworkInterface::IsLoopBack))
{
ui->editMac->setText(netInterface.hardwareAddress());
break;
}
}
foreach (const QHostAddress &address, QNetworkInterface::allAddresses())
{
if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress(QHostAddress::LocalHost))
{
ui->editIp->setText(address.toString());
break;
}
}
foreach(QNetworkInterface inter, QNetworkInterface::allInterfaces()){
QString msg = inter.humanReadableName();
QList<QNetworkAddressEntry> address= inter.addressEntries();
foreach (const QNetworkAddressEntry &add, address){
if (add.ip().protocol() == QAbstractSocket::IPv4Protocol){
msg.append(" - "+add.ip().toString());
ui->comboBoxInterface->addItem(msg);
}
}
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_buttonOutput_clicked()
{
output = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::homePath());
}
void MainWindow::on_buttonCapture_clicked()
{
ui->textBrowser->clear();
std::string interfaceIPAddr = ui->comboBoxInterface->currentText().split("- ")[1].toStdString();
// find the interface by IP address
dev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(interfaceIPAddr.c_str());
if (dev == NULL)
{
QMessageBox msgBox;
msgBox.setWindowTitle("Error");
msgBox.setText("Cannot find interface");
msgBox.exec();
return;
}
if(!dev->open())
{
QMessageBox msgBox;
msgBox.setWindowTitle("Error");
msgBox.setText("Cannot open device, please run with elevated privileges");
msgBox.exec();
return;
}
else
{
QMessageBox msgBox;
msgBox.setWindowTitle("Processing");
msgBox.setText("Please Wait");
msgBox.show();
dev->startCapture(onPacketArrives, &stats);
PCAP_SLEEP(ui->lineEdit->text().toInt());
msgBox.hide();
dev->stopCapture();
ui->textBrowser->clear();
ui->textBrowser->append(stats.returnCount());
}
}
void MainWindow::onPacketArrives(pcpp::RawPacket* packet, pcpp::PcapLiveDevice* dev, void* cookie)
{
PacketStats* stats = (PacketStats*)cookie;
pcpp::Packet parsedPacket(packet);
stats->consumePacket(parsedPacket);
}
void MainWindow::on_buttonEdit_clicked()
{
pcpp::PcapFileWriterDevice writer(output.toLocal8Bit().data(), pcpp::LINKTYPE_ETHERNET);
if(!writer.open()){
QMessageBox msgBox;
msgBox.setWindowTitle("Error");
msgBox.setText("Could not open file to write!");
msgBox.exec();
return;
}
//validaçao ttl
QIntValidator v(0, 255, this);
int pos = 0;
QString str = ui->editTTL->text();
if(v.validate(str, pos) == QValidator::Invalid){
QMessageBox msgBox;
msgBox.setWindowTitle("Error");
msgBox.setText("Enter a integer between 0 and 1");
msgBox.exec();
return;
}
//validaçao ip
QHostAddress addr;
if (!addr.setAddress(ui->editIp->text())) {
QMessageBox msgBox;
msgBox.setWindowTitle("Error");
msgBox.setText("Invalid ip");
msgBox.exec();
return;
}
//validaçao mac
QRegExp mailREX("[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}");
mailREX.setCaseSensitivity(Qt::CaseInsensitive);
//mailREX.setPatternSyntax(QRegExp::Wildcard);
if(!mailREX.exactMatch(ui->editMac->text())){
QMessageBox msgBox;
msgBox.setWindowTitle("Error");
msgBox.setText("Invalid Mac");
msgBox.exec();
return;
}
pcpp::EthLayer* ethernetLayer;
pcpp::IPv4Layer* ipLayer;
pcpp::TcpLayer* tcpLayer;
pcpp::UdpLayer* udpLayer;
std::string srcMAC;
std::string srcIP;
foreach(QNetworkInterface netInterface, QNetworkInterface::allInterfaces())
{
if (!(netInterface.flags() & QNetworkInterface::IsLoopBack))
{
srcMAC = netInterface.hardwareAddress().toStdString();
break;
}
}
foreach (const QHostAddress &address, QNetworkInterface::allAddresses())
{
if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress(QHostAddress::LocalHost))
{
srcIP = address.toString().toStdString();
break;
}
}
int total = 0;
if(ui->checkTCP->checkState())
{
foreach(pcpp::Packet packet, stats.tcp)
{
ethernetLayer = packet.getLayerOfType<pcpp::EthLayer>();
if(ui->checkMac->isChecked()){
ethernetLayer->setDestMac(pcpp::MacAddress(ui->editMac->text().toStdString()));
}
ethernetLayer->setSourceMac(pcpp::MacAddress(srcMAC));
ipLayer = packet.getLayerOfType<pcpp::IPv4Layer>();
if(ui->checkIP->isChecked()){
ipLayer->setDstIpAddress(pcpp::IPv4Address(ui->editIp->text().toStdString()));
}
ipLayer->setSrcIpAddress(pcpp::IPv4Address(srcIP));
//ipLayer->getIPv4Header()->ipId = htons(4000);
if(ui->checkTtl->isChecked()){
ipLayer->getIPv4Header()->timeToLive = ui->editTTL->text().toInt();
}
tcpLayer = packet.getLayerOfType<pcpp::TcpLayer>();
tcpLayer->getTcpHeader()->portSrc = htons(1337);
if(ui->checkPort->isChecked()){
tcpLayer->getTcpHeader()->portDst = htons(ui->editPort->text().toInt());
}
if(ui->checkFlag->isChecked()){
tcpLayer->getTcpHeader()->urgFlag = ui->comboFlag->currentText().toInt();
}
/*uint16_t mssValue = htons(1460);
tcpLayer->addTcpOptionAfter(pcpp::TCPOPT_MSS, PCPP_TCPOLEN_MSS, (uint8_t*)&mssValue, NULL);*/
packet.computeCalculateFields();
if(writer.writePacket(*packet.getRawPacket()))
{
total ++;
}
}
}
if(ui->checkUDP->checkState())
{
foreach(pcpp::Packet packet, stats.udp)
{
ethernetLayer = packet.getLayerOfType<pcpp::EthLayer>();
if(ui->checkMac->isChecked()){
ethernetLayer->setDestMac(pcpp::MacAddress(ui->editMac->text().toStdString()));
}
ethernetLayer->setSourceMac(pcpp::MacAddress(srcMAC));
ipLayer = packet.getLayerOfType<pcpp::IPv4Layer>();
if(ui->checkIP->isChecked()){
ipLayer->setDstIpAddress(pcpp::IPv4Address(ui->editIp->text().toStdString()));
}
ipLayer->setSrcIpAddress(pcpp::IPv4Address(srcIP));
if(ui->checkTtl->isChecked()){
ipLayer->getIPv4Header()->timeToLive = ui->editTTL->text().toInt();
}
udpLayer = packet.getLayerOfType<pcpp::UdpLayer>();
udpLayer->getUdpHeader()->portSrc = htons(1337);
if(ui->checkPort->isChecked()){
udpLayer->getUdpHeader()->portDst = htons(ui->editPort->text().toInt());
}
packet.computeCalculateFields();
if(writer.writePacket(*packet.getRawPacket()))
{
total++;
}
}
}
writer.close();
QMessageBox msgBox;
msgBox.setWindowTitle("Success");
msgBox.setText(QString::number(total) + " packets saved!");
msgBox.exec();
}
void MainWindow::on_comboBoxInterface_currentTextChanged(const QString &arg1)
{
ui->editIp->setText(arg1.split("- ")[1]);
}
void MainWindow::on_pushButton_clicked()
{
std::string srcMAC;
std::string srcIP;
foreach(QNetworkInterface netInterface, QNetworkInterface::allInterfaces())
{
if (!(netInterface.flags() & QNetworkInterface::IsLoopBack))
{
srcMAC = netInterface.hardwareAddress().toStdString();
break;
}
}
foreach (const QHostAddress &address, QNetworkInterface::allAddresses())
{
if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress(QHostAddress::LocalHost))
{
srcIP = address.toString().toStdString();
break;
}
}
pcpp::EthLayer newEthernetLayer(pcpp::MacAddress(srcMAC), pcpp::MacAddress(ui->editMac->text().toStdString()));
pcpp::IPv4Layer newIPLayer(pcpp::IPv4Address(srcIP), pcpp::IPv4Address(ui->editIp->text().toStdString()));
newIPLayer.getIPv4Header()->ipId = htons(2000);
if(ui->checkTtl->isChecked()){
newIPLayer.getIPv4Header()->timeToLive = ui->editTTL->text().toInt();
}
pcpp::Packet newPacket(100);
pcpp::UdpLayer newUdpLayer(1337, ui->editPort->text().toInt());
newPacket.addLayer(&newEthernetLayer);
newPacket.addLayer(&newIPLayer);
newPacket.addLayer(&newUdpLayer);
newPacket.computeCalculateFields();
stats.consumePacket(newPacket);
ui->textBrowser->clear();
ui->textBrowser->append(stats.returnCount());
}
void MainWindow::on_pushButton_2_clicked()
{
stats.tcp.clear();
stats.udp.clear();
ui->textBrowser->clear();
ui->textBrowser->append("Packets cleared");
}