-
Notifications
You must be signed in to change notification settings - Fork 1
/
linuxHeapDump.sp
412 lines (326 loc) · 10.2 KB
/
linuxHeapDump.sp
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
406
407
408
409
410
411
412
#include <MemoryEx/Stocks>
public Plugin myinfo =
{
name = "[SPHack] Dumper",
author = "Rostu [vk.com/rostu13 | Rostu#7917]",
version = "1.0",
url = "http://vk.com/rostu13"
};
static const char g_sSMXMagic[] = "FFPS";
static const char outPutDir[] = "/memoryDump";
static const float g_fTimerDelay = 0.2;
ArrayList g_hHeapList;
enum struct HeapInfo // very bad. same as in DynamicLibrary.
{
Address base;
Address end;
int GetSize()
{
return view_as<int>(this.end - this.base);
}
void FillFromBuffer(const char[] sBuffer)
{
char sBaseAddress[16];
char sEndAddress[16];
FormatEx(sBaseAddress, 9, "%s", sBuffer);
FormatEx(sEndAddress, 9, "%s", sBuffer[9]);
this.base = view_as<Address>(HexToDec(sBaseAddress));
this.end = view_as<Address>(HexToDec(sEndAddress));
}
}
//https://github.com/alliedmodders/sourcepawn/blob/c44a169030f09df9a72506242d09d5eb76c13534/include/smx/smx-headers.h
enum struct VM_SMXHeader
{
Address addr;
int size;
/*
int section;
int stringTab;
int dataoffs;*/
bool IsValid(Address pBase)
{
int version = LoadFromAddress(pBase + view_as<Address>(0x04), NumberType_Int16);
if(version != 0x0102) // 0x0200 - Used by spcomp2 - not supported
{
return false;
}
int compress = LoadFromAddress(pBase + view_as<Address>(0x06), NumberType_Int8);
if(compress != 0 && compress != 1)
{
return false;
}
return true;
}
void FillInfo(Address pBase)
{
this.addr = pBase;
this.size = LoadFromAddress(pBase + view_as<Address>(0x0B), NumberType_Int32);
/*
this.section = LoadFromAddress(pBase + view_as<Address>(0x0F), NumberType_Int8);
this.stringTab = LoadFromAddress(pBase + view_as<Address>(0x10), NumberType_Int32);
this.dataoffs = LoadFromAddress(pBase + view_as<Address>(0x14), NumberType_Int32);*/
}
}
public void OnPluginStart()
{
g_hHeapList = new ArrayList(sizeof(HeapInfo));
RegServerCmd("sm_dump_smx", Cmd_Dump_Smx);
RegServerCmd("sm_dump_heaps", Cmd_Dump_Heaps);
}
public Action Cmd_Dump_Smx(int iArgs)
{
PrintToServer("----------Start Dump SMX from Memory----------");
DumpPlugins();
PrintToServer("----------End Dump SMX from Memory----------");
}
public Action Cmd_Dump_Heaps(int iArgs)
{
PrintToServer("----------Start Dump Heaps from Memory----------");
ArrayList heaps = DumpHeaps(true);
HeapInfo memInfo;
Address offsetSize;
int iSize;
int iCurrentSize;
static const int iMaxHeapSize = 0x1312D00; // 20 MB
PrintToServer("Heaps count: %d", heaps.Length);
g_hHeapList.Clear();
HeapInfo memTemp;
for(int x = 0; x < heaps.Length; x++)
{
heaps.GetArray(x, memInfo, sizeof(HeapInfo));
iSize = memInfo.GetSize();
PrintToServer("Dump Heaps: Start: 0x%X | End: 0x%X | Size: 0x%X", memInfo.base, memInfo.end, memInfo.GetSize());
if(iSize >= iMaxHeapSize)
{
offsetSize = Address_Null;
while(iSize > 0)
{
iCurrentSize = iSize >= iMaxHeapSize ? iMaxHeapSize : iSize;
PrintToServer("\tDump Part heap[0x%X-0x%X] Start: 0x%X | End: 0x%X | Size: 0x%X|0x%X [> 0x%X[%d]]",
memInfo.base, memInfo.end, memInfo.base + offsetSize, memInfo.base + offsetSize + view_as<Address>(iCurrentSize), iCurrentSize, iSize, iMaxHeapSize, iSize >= iMaxHeapSize);
memTemp.base = memInfo.base + offsetSize;
memTemp.end = memInfo.base + offsetSize + view_as<Address>(iCurrentSize);
g_hHeapList.PushArray(memTemp, sizeof(HeapInfo));
offsetSize += view_as<Address>(iCurrentSize);
iSize -= iCurrentSize;
}
}
else
{
g_hHeapList.PushArray(memInfo, sizeof(HeapInfo));
}
}
CreateTimer(g_fTimerDelay, HeapDumpFrame_, _, TIMER_FLAG_NO_MAPCHANGE);
}
public Action HeapDumpFrame_(Handle hTimer, any data)
{
HeapInfo heap;
if(g_hHeapList.Length)
{
g_hHeapList.GetArray(0, heap, sizeof(HeapInfo));
g_hHeapList.Erase(0);
CreateHeapMemoryDump(heap.base, heap.GetSize());
}
else
{
PrintToServer("----------End Dump Heaps from Memory----------");
}
}
void DumpPlugins()
{
ArrayList plugins = VM_GetValidPlugins();
VM_SMXHeader smx;
char sName[8];
for(int x = 0; x < plugins.Length; x++)
{
plugins.GetArray(x, smx, sizeof(VM_SMXHeader));
FormatEx(sName, sizeof sName, "%d.smx", x);
CreateMemoryDump(sName, smx.addr, smx.size);
}
}
void CreateHeapMemoryDump(Address pBase, any iSize)
{
char sNewPath[PLATFORM_MAX_PATH];
if(!DirExists(outPutDir))
{
CreateDirectory(outPutDir, 511);
}
FormatEx(sNewPath, sizeof sNewPath, "%s/%X-%X.txt", outPutDir, pBase, pBase + view_as<Address>(iSize));
if(FileExists(sNewPath))
{
if(!DeleteFile(sNewPath))
{
SetFailState("CreateMemoryDump couldn't delete file %s", sNewPath);
}
}
File outPut = OpenFile(sNewPath, "ab");
if(outPut == null)
{
SetFailState("file == null");
}
static const int iMaxColum = 0x10;
char sLine[512];
int offset;
int[] bytes = new int[iMaxColum];
Address end = pBase + view_as<Address>(iSize);
while(pBase < end)
{
int iByte = LoadFromAddress(pBase, NumberType_Int8);
if(offset >= iMaxColum)
{
offset = 0x00;
outPut.WriteLine("%s %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c", sLine, bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7], bytes[8],
bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]);
//LogError("%s", sLine);
}
if(iByte <= 0x20)
{
bytes[offset] = 0x2E;
}
else
{
bytes[offset] = iByte;
}
if(!offset)
{
FormatEx(sLine, sizeof sLine, "[0x%08X]", pBase);
}
Format(sLine, sizeof sLine, "%s [0x%02X]", sLine, iByte);
offset++;
pBase++;
}
delete outPut;
PrintToServer("CreateHeapMemoryDump: %X-%X - complete %d...", pBase - view_as<Address>(iSize), end, g_hHeapList.Length);
CreateTimer(g_fTimerDelay, HeapDumpFrame_, _, TIMER_FLAG_NO_MAPCHANGE);
}
void CreateMemoryDump(const char[] sFileName, Address pBase, any iSize)
{
char sNewPath[PLATFORM_MAX_PATH];
if(!DirExists(outPutDir))
{
CreateDirectory(outPutDir, 511);
}
FormatEx(sNewPath, sizeof sNewPath, "%s/%s", outPutDir, sFileName);
if(FileExists(sNewPath))
{
if(!DeleteFile(sNewPath))
{
SetFailState("CreateMemoryDump couldn't delete file %s", sNewPath);
}
}
File outPut = OpenFile(sNewPath, "ab");
if(outPut == null)
{
SetFailState("file == null");
}
// Исправление smx header
outPut.WriteString(g_sSMXMagic, false);
outPut.WriteInt16(LoadFromAddress(pBase + view_as<Address>(0x04), NumberType_Int16));
outPut.WriteInt8(0x00);
outPut.WriteInt32(iSize);
pBase += view_as<Address>(0x0B);
Address end = pBase + view_as<Address>(iSize);
while(pBase < end) // Очень плохая реализация
{
outPut.WriteInt8(LoadFromAddress(pBase, NumberType_Int8));
pBase++;
}
delete outPut;
}
ArrayList VM_GetValidPlugins()
{
ArrayList plugins = VM_GetPluginsBase();
ArrayList res = new ArrayList(sizeof(VM_SMXHeader));
VM_SMXHeader smx;
Address pBase;
for(int x = 0; x < plugins.Length; x++)
{
pBase = view_as<Address>(plugins.Get(x));
if(!smx.IsValid(pBase))
{
continue;
}
smx.FillInfo(pBase);
res.PushArray(smx, sizeof(VM_SMXHeader));
}
delete plugins;
return res;
}
ArrayList VM_GetPluginsBase()
{
ArrayList heaps = DumpHeaps(true);
HeapInfo memInfo;
ArrayList hPlugins = new ArrayList();
PrintToServer("Heaps: %d", heaps.Length);
for(int x = 0; x < heaps.Length; x++)
{
heaps.GetArray(x, memInfo, sizeof(HeapInfo));
PrintToServer("VM_GetPluginsBase: region %X-%X | %X", memInfo.base, memInfo.end, memInfo.GetSize());
ArrayList res = FindAllStrings(memInfo.base, memInfo.GetSize(), g_sSMXMagic);
for(int y = 0; y < res.Length; y++)
{
hPlugins.Push(res.Get(y));
}
delete res;
}
return hPlugins;
}
ArrayList DumpHeaps(bool bRefresh = false)
{
static ArrayList list;
if(list == null)
{
list = new ArrayList(sizeof(HeapInfo));
}
else if(bRefresh)
{
list.Clear();
}
else if(list.Length != 0)
{
return list;
}
char sName[64];
char sBuffer[1024];
int iLength;
int iOffset;
File file = OpenFile("file:///proc/self/maps", "rt");
HeapInfo info;
while(file.ReadLine(sBuffer, sizeof sBuffer))
{
TrimString(sBuffer);
iLength = strlen(sBuffer);
LogError(sBuffer);
if(sBuffer[iLength - 1] == '0' && sBuffer[iLength - 2] == ' ')
{
if(sBuffer[18] == 'r') // read
{
info.FillFromBuffer(sBuffer);
list.PushArray(info, sizeof(HeapInfo));
}
}
else if(sBuffer[iLength - 1] == ']')
{
iOffset = 0;
strcopy(sName, sizeof sName, "");
//LogError(sBuffer);
for(int x = iLength - 2; x >= 0; x--, iOffset++)
{
if(sBuffer[x] == '[')
{
strcopy(sName, iOffset + 1, sBuffer[x + 1]);
PrintToServer(sName);
if(!strcmp(sName, "heap"))
{
info.FillFromBuffer(sBuffer);
list.PushArray(info, sizeof(HeapInfo));
}
break;
}
}
}
}
//LogError("name %s base %s|0x%X end %s|0x%X size 0x%X", sName, sBaseAddress, info.base, sEndAddress, info.end, info.GetSize());
delete file;
return list;
}