This repository was archived by the owner on Jul 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathApp.cpp
375 lines (303 loc) · 8.73 KB
/
App.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
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
#include "pch.h"
#include "App.h"
#include "Connection.h"
#include <pcapplusplus/IPv4Layer.h>
#include <pcapplusplus/Packet.h>
#include <pcapplusplus/PcapFileDevice.h>
#include <pcapplusplus/PcapLiveDevice.h>
#include <pcapplusplus/PcapLiveDeviceList.h>
#include <pcapplusplus/TcpLayer.h>
static HANDLE _consoleEventHandle = nullptr;
static BOOL WINAPI _ConsoleHandler(DWORD dwCtrlType);
App::App()
{
// add npcap dir to Dll directory
wchar_t szPath[_MAX_PATH + 1] = {};
GetSystemDirectoryW(szPath, _countof(szPath));
std::wstring szNpcapDir = szPath;
szNpcapDir += L"\\Npcap";
SetDllDirectoryW(szNpcapDir.c_str());
}
int App::run(int argc, const char* argv[])
{
int r = parse_command_line(argc, argv);
if (r != 0)
return r;
if (!_pcapngFile.empty())
r = load_pcapng();
else
r = capture();
if (r != 0)
return r;
r = analyse();
if (r != 0)
return r;
return 0;
}
int App::parse_command_line(int argc, const char* argv[])
{
if (argc < 2)
{
std::cerr << "Missing arguments" << std::endl;
return 1;
}
_pcapngFile.clear();
_interface.clear();
_dstIP.clear();
_dstPortLogin = DEFAULT_PORT_LOGIN;
_dstPortGame = DEFAULT_PORT_GAME;
_logFileName = "packet_log.txt";
_useOldEncryption = false;
_alwaysUsePKWareCompression = false;
for (int i = 1; i < argc;)
{
std::string arg = argv[i];
if (arg == "-i")
{
if (i + 1 >= argc)
{
std::cerr << "Missing argument to " << arg << " [interface IP or name]" << std::endl;
return 2;
}
_interface = argv[i + 1];
i += 2;
continue;
}
if (arg == "-o")
{
if (i + 1 >= argc)
{
std::cerr << "Missing argument to " << arg << " [log filename]" << std::endl;
return 2;
}
_logFileName = argv[i + 1];
i += 2;
continue;
}
if (arg == "-d"
|| arg == "--dst-ip")
{
if (i + 1 >= argc)
{
std::cerr << "Missing argument to " << arg << " [destination IP]" << std::endl;
return 2;
}
_dstIP = argv[i + 1];
i += 2;
continue;
}
if (arg == "-g"
|| arg == "--game-port")
{
if (i + 1 >= argc)
{
std::cerr << "Missing argument to " << arg << " [game port]" << std::endl;
return 2;
}
_dstPortGame = (uint16_t) atoi(argv[i + 1]);
i += 2;
continue;
}
if (arg == "-l"
|| arg == "--login-port")
{
if (i + 1 >= argc)
{
std::cerr << "Missing argument to " << arg << " [login port]" << std::endl;
return 2;
}
_dstPortLogin = (uint16_t) atoi(argv[i + 1]);
i += 2;
continue;
}
if (arg == "-old"
|| arg == "--old-encryption")
{
_useOldEncryption = true;
++i;
continue;
}
if (arg == "-pkware"
|| arg == "--old-compression")
{
_alwaysUsePKWareCompression = true;
++i;
continue;
}
if (arg == "-pcapng")
{
if (i + 1 >= argc)
{
std::cerr << "Missing argument to " << arg << " [pcapng filename]" << std::endl;
return 2;
}
_pcapngFile = argv[i + 1];
i += 2;
continue;
}
++i;
}
size_t extPos = _logFileName.rfind('.');
if (extPos != std::string::npos)
{
_logFileExt = _logFileName.substr(extPos);
_logFileName = _logFileName.substr(0, extPos);
}
else
{
_logFileExt = ".txt";
}
std::cout
<< "Using old encryption headers: "
<< (_useOldEncryption ? "TRUE" : "FALSE")
<< std::endl;
std::cout
<< "Always use old (PKWARE) compression: "
<< (_alwaysUsePKWareCompression ? "TRUE" : "FALSE")
<< std::endl << std::endl;
return 0;
}
int App::load_pcapng()
{
pcpp::PcapNgFileReaderDevice reader(_pcapngFile);
if (!reader.open())
{
std::cerr << "Error opening the PCAPNG capture file: " << _pcapngFile << std::endl;
return 3;
}
reader.getNextPackets(_capturedPackets);
reader.close();
return 0;
}
int App::capture()
{
bool isLoopbackCapture = false;
if (_interface == "127.0.0.1"
|| _interface == "localhost"
|| _interface == "::1")
{
isLoopbackCapture = true;
_interface = "\\Device\\NPF_Loopback";
}
auto dev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIpOrName(_interface);
if (dev == nullptr)
{
std::cerr << "Cannot find interface for address or name " << _interface << std::endl;
return 3;
}
if (isLoopbackCapture)
_interfaceIP = "127.0.0.1";
else
_interfaceIP = dev->getIPv4Address().toString();
std::cout
<< "Interface info:" << std::endl
<< " Interface IP: " << _interfaceIP << std::endl
<< " Interface name: " << dev->getName() << std::endl
<< " Interface description: " << dev->getDesc() << std::endl
<< " MAC address: " << dev->getMacAddress() << std::endl
<< " Default gateway: " << dev->getDefaultGateway() << std::endl
<< " Interface MTU: " << dev->getMtu() << std::endl;
if (!dev->getDnsServers().empty())
std::cout << " DNS server: " << dev->getDnsServers().at(0) << std::endl;
std::cout << std::endl;
if (!dev->open())
{
std::cerr << "Cannot open device" << std::endl;
return 4;
}
std::cout << "Starting capture..." << std::endl;
std::cout << "Press CTRL+C to stop capturing." << std::endl;
SetConsoleCtrlHandler(_ConsoleHandler, TRUE);
_consoleEventHandle = CreateEventA(nullptr, FALSE, FALSE, nullptr);
if (_consoleEventHandle == nullptr)
{
std::cerr << "Failed to create listen event for waiting, stopping capture..." << std::endl;
dev->close();
return 5;
}
// Build filters
// port == _dstPortGame || port == _dstPortLogin
pcpp::PortFilter portFilterGame(_dstPortGame, pcpp::SRC_OR_DST);
pcpp::PortFilter portFilterLogin(_dstPortLogin, pcpp::SRC_OR_DST);
pcpp::OrFilter portFilter;
portFilter.addFilter(&portFilterGame);
portFilter.addFilter(&portFilterLogin);
// proto == tcp
pcpp::ProtoFilter protocolFilter(pcpp::TCP);
// Merge filters
// (port == _dstPortGame || port == _dstPortLogin) && (proto == tcp)
pcpp::AndFilter andFilter;
andFilter.addFilter(&portFilter);
andFilter.addFilter(&protocolFilter);
if (!_dstIP.empty())
{
pcpp::IPFilter ipFilter(_dstIP, pcpp::SRC_OR_DST);
andFilter.addFilter(&ipFilter);
}
dev->setFilter(andFilter);
dev->startCapture(_capturedPackets);
WaitForSingleObject(_consoleEventHandle, INFINITE);
std::cout << "Stopping capture..." << std::endl;
dev->stopCapture();
CloseHandle(_consoleEventHandle);
std::string pcapngFilename = "capture_" + std::to_string(time(nullptr)) + ".pcapng";
std::cout << "Writing " << _capturedPackets.size() << " captured packets to " << pcapngFilename << std::endl;
pcpp::PcapNgFileWriterDevice pcapNgWriter(pcapngFilename);
if (!pcapNgWriter.open())
{
std::cerr << "Cannot open " << pcapngFilename << " for writing" << std::endl;
return 0; // normally we'd error, but this is more of a warning than an error so well let it slide
}
pcapNgWriter.writePackets(_capturedPackets);
pcapNgWriter.close();
return 0;
}
int App::analyse()
{
std::cout << "Analysing " << _capturedPackets.size() << " captured packets...\n";
size_t i = 0;
for (const auto rawPacket : _capturedPackets)
{
std::cout << "Parsing raw packet " << (++i) << " / " << _capturedPackets.size() << std::endl;
pcpp::Packet parsedPacket(rawPacket);
auto tcpLayer = parsedPacket.getLayerOfType<pcpp::TcpLayer>();
if (tcpLayer == nullptr)
continue;
auto ipv4Layer = parsedPacket.getLayerOfType<pcpp::IPv4Layer>();
if (ipv4Layer == nullptr)
continue;
auto pktDstIP = ipv4Layer->getDstIPv4Address();
auto pktSrcIP = ipv4Layer->getSrcIPv4Address();
time_t local_tv_sec = rawPacket->getPacketTimeStamp().tv_sec;
// Ensure we create the connection on the first packet.
// This should be the client performing the initial SYN request, so we know which side is the client.
auto connection = Connection::find_or_create(
this,
pktSrcIP.toString(),
tcpLayer->getSrcPort(),
pktDstIP.toString(),
tcpLayer->getDstPort(),
local_tv_sec);
connection->process_raw_packet(parsedPacket);
}
return 0;
}
void App::print_usage()
{
std::cerr << "Usage: SimpleKOLogger -i [capture interface IP or name]" << std::endl;
std::cerr << "OR: SimpleKOLogger -pcapng [pcapng capture log filename]" << std::endl;
std::cerr << "Optional arguments:" << std::endl;
std::cerr << "-o [log filename, defaults to packet_log.txt]" << std::endl;
std::cerr << "-d or --dst-ip [destination/server IP address]" << std::endl;
std::cerr << "-g or --game-port [game port, defaults to 15001]" << std::endl;
std::cerr << "-l or --login-port [login port, defaults to 15100]" << std::endl;
std::cerr << "-old or --old-encryption [if supplied, old encryption headers are used]" << std::endl;
std::cerr << "-pkware or --old-compression [if supplied, forces PKWARE to always be used, rather than as fallback]" << std::endl;
system("pause");
}
static BOOL WINAPI _ConsoleHandler(DWORD dwCtrlType)
{
SetEvent(_consoleEventHandle);
Sleep(10'000); // Win7 onwards allows 10 seconds before it'll forcibly terminate
return TRUE;
}