-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPackets.cpp
309 lines (226 loc) · 7.58 KB
/
Packets.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
#include "Packets.hpp"
std::queue<PacketWriter*> SendPacketQueue;
std::vector<uint16_t> BlockedList;
std::vector<uint16_t> IgnoredList;
std::vector<ModifyRule*> ModifyList;
std::queue<PacketWriter*> RecvPacketQueue;
std::vector<uint16_t> RecvBlockedList;
std::vector<uint16_t> RecvIgnoredList;
std::vector<ModifyRule*> RecvModifyList;
extern BOOL isDisconnected;
unsigned short Opcode;
BOOL PacketHelper::CreatePacket(LPBYTE data, UINT length) {
memcpy(&Opcode, (void*)&data[0], sizeof(uint16_t));
Opcode = PacketHelper::changeEndianness16(Opcode);
if (Opcode == Opcodes::Send::Ping) //stuff we dont want logged
{
return FALSE;
}
for (auto & element : ModifyList) {
if ((uint16_t)element->opcode == (uint16_t)Opcode) { //found modified
//if (CheckPattern(element->pattern, &packet[2], packetLen - 2) == TRUE) {
// //send a packet which wont cause a recursive send via the modify
// //then return false to block it
// //need createthread
// thread_packet* pak = new thread_packet();
// pak->packet = convertPacketFormatToString(element->replacement, packetLen - 2);
// pak->size = packetLen - 2;
// Thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&_SendPacket, (LPVOID)pak, 0, 0);
// return false;
//}
return TRUE;
}
}
for (auto & element : IgnoredList) {
if ((uint16_t)element == (uint16_t)Opcode) { //found ignored
return TRUE;
}
}
PacketWriter* p = new PacketWriter();
for (int i = 0; i < length; i++) {
p->Write<byte>(data[i]);
}
SendPacketQueue.push(p);
for (auto & element : BlockedList) {
if ((uint16_t)element == (uint16_t)Opcode) { //found blocked, still log it?
return FALSE;
}
}
return TRUE;
}
uint16_t RecvOpcode;
BOOL PacketHelper::CreateRecvPacket(LPBYTE data, DWORD length) {
if (length == 0)
return FALSE;
memcpy(&RecvOpcode, (void*)&data[0], sizeof(uint16_t));
RecvOpcode = PacketHelper::changeEndianness16(RecvOpcode);
for (auto & element : ModifyList) {
if ((uint16_t)element->opcode == (uint16_t)RecvOpcode) { //found modified
//if (CheckPattern(element->pattern, &packet[2], packetLen - 2) == TRUE) {
// //send a packet which wont cause a recursive send via the modify
// //then return false to block it
// //need createthread
// thread_packet* pak = new thread_packet();
// pak->packet = convertPacketFormatToString(element->replacement, packetLen - 2);
// pak->size = packetLen - 2;
// Thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&_SendPacket, (LPVOID)pak, 0, 0);
// return false;
//}
return TRUE;
}
}
for (auto & element : RecvIgnoredList) {
if ((uint16_t)element == (uint16_t)RecvOpcode) { //found ignored
return TRUE;
}
}
PacketWriter* p = new PacketWriter(); //push to queue
for (int i = 0; i < length; i++) {
p->Write<byte>(data[i]);
}
RecvPacketQueue.push(p);
for (auto & element : RecvBlockedList) {
if ((uint16_t)element == (uint16_t)RecvOpcode) { //found blocked, still log it?
return FALSE;
}
}
return FALSE;
}
uint16_t PacketHelper::changeEndianness16(UINT16 val)
{
return (val << 8) | ((val >> 8) & 0x00ff); // right-shift sign-extends, so force to zero
}
int32_t PacketHelper::changeEndianness32(int32_t val)
{
int32_t tmp = (val << 16) |
((val >> 16) & 0x00ffff);
return ((tmp >> 8) & 0x00ff00ff) | ((tmp & 0x00ff00ff) << 8);
}
std::string PacketHelper::randomStr(size_t size)
{
std::string str("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
std::random_device rd;
std::mt19937 generator(rd());
std::shuffle(str.begin(), str.end(), generator);
return str.substr(0, size); // assumes 32 < number of characters in str
}
std::wstring PacketHelper::randomWStr(size_t size)
{
std::wstring str(L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
std::random_device rd;
std::mt19937 generator(rd());
std::shuffle(str.begin(), str.end(), generator);
return str.substr(0, size); // assumes 32 < number of characters in str
}
std::string PacketHelper::ToString(LPBYTE packetStr, int byteLength)
{
if (byteLength <= 0)
return NULL;
std::string newStr = string();
CHAR* convertStr = (CHAR*)malloc(byteLength * 3); //* 3 since 00[ ] an extra 0 with a space for each byte in the str.
for (int i = 0; i < byteLength; i++) {
byte ch = packetStr[i];
sprintf(&convertStr[i], "%.2X", ch);
newStr.append(&convertStr[i]);
newStr.append(" ");
}
free(convertStr);
return newStr;
}
int PacketHelper::GetPacketLength(CHAR* input)
{
int length = 0;
for (int i = 0; input[i] != '\0'; i++) {
if (input[i] == ' ')
length = length;
else
length++;
}
length = length / 2;
return length;
}
int PacketHelper::rand_lim(int limit)
{
int divisor = RAND_MAX / (limit + 1);
int retval;
do {
retval = rand() / divisor;
} while (retval > limit);
return retval;
}
LPBYTE PacketHelper::ToPacketFormat(CHAR* input) //literally the worst old func left still... lmao
{
char fixedString[2048] = { 0 };
//strip spaces, bad text
int i; int j = 0;
for (i = 0; input[i] != '\0'; i++) {
if ((input[i] >= 'A' && input[i] <= 'F') || (input[i] >= '0' && input[i] <= '9') || (input[i] >= 'a' && input[i] <= 'f')) {
fixedString[j] = input[i];
j++;
}
else if (input[i] == ' ') {
}
else if (input[i] == '*') {
int random = rand_lim(15);
if (random >= 0 && random <= 9) {
fixedString[j] = random + 0x30;
}
else if (random >= 9 && random <= 15) {
fixedString[j] = random + 0x55;
}
j++;
}
else {
return NULL;
}
}
LPBYTE bytestring = (unsigned char*)malloc((j * 2) + 1);
j = 0;
for (i = 0; fixedString[i] != '\0'; i++) {
if (i % 2 == 0) { //even number iterator
if (fixedString[i] >= 'a' && fixedString[i] <= 'f') {
bytestring[j] = ((fixedString[i] - 0x57) << 4);
if (fixedString[i + 1] >= 'A' && fixedString[i + 1] <= 'F') {
bytestring[j] = bytestring[j] + (fixedString[i + 1] - 0x37);
}
else if (fixedString[i + 1] >= '0' && fixedString[i + 1] <= '9') {
bytestring[j] = bytestring[j] + (fixedString[i + 1] - 0x30);
}
else if (fixedString[i + 1] >= 'a' && fixedString[i + 1] <= 'f') {
bytestring[j] = bytestring[j] + (fixedString[i + 1] - 0x57);
}
j++;
i++;
}
else if (fixedString[i] >= '0' && fixedString[i] <= '9') {
bytestring[j] = ((fixedString[i] - 0x30) << 4);
if (fixedString[i + 1] >= 'A' && fixedString[i + 1] <= 'F') {
bytestring[j] = bytestring[j] + (fixedString[i + 1] - 0x37);
}
else if (fixedString[i + 1] >= '0' && fixedString[i + 1] <= '9') {
bytestring[j] = bytestring[j] + (fixedString[i + 1] - 0x30);
}
else if (fixedString[i + 1] >= 'a' && fixedString[i + 1] <= 'f') {
bytestring[j] = bytestring[j] + (fixedString[i + 1] - 0x57);
}
j++;
i++;
}
else if (fixedString[i] >= 'A' && fixedString[i] <= 'F') {
bytestring[j] = ((fixedString[i] - 0x37) << 4);
if (fixedString[i + 1] >= 'A' && fixedString[i + 1] <= 'F') {
bytestring[j] = bytestring[j] + (fixedString[i + 1] - 0x37);
}
else if (fixedString[i + 1] >= '0' && fixedString[i + 1] <= '9') {
bytestring[j] = bytestring[j] + (fixedString[i + 1] - 0x30);
}
else if (fixedString[i + 1] >= 'a' && fixedString[i + 1] <= 'f') {
bytestring[j] = bytestring[j] + (fixedString[i + 1] - 0x57);
}
j++;
i++;
}
}
}
return bytestring;
}