-
Notifications
You must be signed in to change notification settings - Fork 4
/
ProcessManager.cs
435 lines (355 loc) · 15.9 KB
/
ProcessManager.cs
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
using libdebug;
using System;
using System.Collections.Generic;
using System.Text;
namespace PS4_Cheat_Engine {
public class MappedSection {
public MappedSection() => ResultList = null;
public bool Check { set; get; }
public int Length { get; set; }
public string Name { get; set; }
public uint Prot { get; set; }
public ResultList ResultList { get; set; }
public ulong Start { get; set; }
public void PointerSearchInit(ProcessManager processManager, MemoryHelper memoryHelper, PointerList pointerList) {
ulong address = this.Start;
int length = this.Length;
const int buffer_length = 1024 * 1024 * 128;
while (length != 0) {
int cur_length = buffer_length;
if (cur_length > length) {
cur_length = length;
length = 0;
}
else {
length -= cur_length;
}
byte[] buffer = memoryHelper.ReadMemory(address, (int)cur_length);
memoryHelper.CompareWithMemoryBufferPointerScanner(processManager, buffer, pointerList, address);
address += (ulong)cur_length;
}
}
public void UpdateResultList(ProcessManager processManager, MemoryHelper memoryHelper, string default_value_0_str, string default_value_1_str, bool is_hex, bool newScan, int thread_id) {
if (!Check) {
ResultList = null;
return;
}
ResultList new_result_list = new ResultList(memoryHelper.Length, memoryHelper.Alignment);
ulong address = this.Start;
uint base_address = 0;
int length = this.Length;
const int buffer_length = 1024 * 1024 * 128;
while (length != 0) {
int cur_length = buffer_length;
if (cur_length > length) {
cur_length = length;
length = 0;
}
else {
length -= cur_length;
}
byte[] buffer = memoryHelper.ReadMemory(address, (int)cur_length, thread_id);
byte[] default_value_0 = null;
if (memoryHelper.ParseFirstValue) {
if (is_hex) {
default_value_0 = memoryHelper.HexStringToBytes(default_value_0_str);
}
else {
default_value_0 = memoryHelper.StringToBytes(default_value_0_str);
}
}
byte[] default_value_1 = null;
if (memoryHelper.ParseSecondValue) {
if (is_hex) {
default_value_1 = memoryHelper.HexStringToBytes(default_value_1_str);
}
else {
default_value_1 = memoryHelper.StringToBytes(default_value_1_str);
}
}
if (newScan) {
memoryHelper.CompareWithMemoryBufferNewScanner(default_value_0, default_value_1, buffer, new_result_list, base_address);
}
else {
memoryHelper.CompareWithMemoryBufferNextScanner(default_value_0, default_value_1, buffer, ResultList, new_result_list);
}
address += (ulong)cur_length;
base_address += (uint)cur_length;
}
ResultList = new_result_list;
}
}
public class MappedSectionList {
private List<MappedSection> mapped_section_list = new List<MappedSection>();
public MappedSectionList() {}
public int Count { get { return mapped_section_list.Count; } }
public ulong TotalMemorySize { get; set; }
public MappedSection this[int index] {
get { return mapped_section_list[index]; }
}
public void ClearResultList() {
for (int idx = 0; idx < mapped_section_list.Count; ++idx) {
if (mapped_section_list[idx].ResultList != null) {
mapped_section_list[idx].ResultList.Clear();
}
}
}
public MappedSection GetMappedSection(ulong address) {
int sectionID = GetMappedSectionID(address);
if (sectionID < 0) return null;
return mapped_section_list[sectionID];
}
public int GetMappedSectionID(ulong address) {
ulong start = 0;
ulong end = 0;
if (mapped_section_list.Count > 0) {
start = mapped_section_list[0].Start;
end = mapped_section_list[mapped_section_list.Count - 1].Start + (ulong)mapped_section_list[mapped_section_list.Count - 1].Length;
}
if (start > address || end < address) {
return -1;
}
return FindSectionID(address);
}
public List<MappedSection> GetMappedSectionList(string name, int prot) {
List<MappedSection> result_list = new List<MappedSection>();
for (int idx = 0; idx < mapped_section_list.Count; ++idx) {
if (mapped_section_list[idx].Prot == prot &&
mapped_section_list[idx].Name.StartsWith(name)) {
result_list.Add(mapped_section_list[idx]);
}
}
return result_list;
}
public string GetSectionName(int section_idx) {
if (section_idx < 0) return $"Section Index {i} wrong!";
MappedSection sectionInfo = mapped_section_list[section_idx];
StringBuilder section_name = new StringBuilder();
section_name.Append(sectionInfo.Name + "-");
section_name.Append(String.Format("{0:X}", sectionInfo.Prot) + "-");
section_name.Append(String.Format("{0:X}", sectionInfo.Start) + "-");
section_name.Append((sectionInfo.Length / 1024).ToString() + "KB");
return section_name.ToString();
}
public void InitMemorySectionList(ProcessMap pm) {
mapped_section_list.Clear();
TotalMemorySize = 0;
for (int i = 0; i < pm.entries.Length; i++) {
MemoryEntry entry = pm.entries[i];
if ((entry.prot & 0x1) == 0x1) {
ulong length = entry.end - entry.start;
ulong start = entry.start;
string name = entry.name;
int idx = 0;
ulong buffer_length = 1024 * 1024 * 128;
//Executable section
if ((entry.prot & 0x5) == 0x5) {
buffer_length = length;
}
while (length != 0) {
ulong cur_length = buffer_length;
if (cur_length > length) {
cur_length = length;
length = 0;
}
else {
length -= cur_length;
}
MappedSection mappedSection = new MappedSection();
mappedSection.Start = start;
mappedSection.Length = (int)cur_length;
mappedSection.Name = entry.name + "[" + idx + "]";
mappedSection.Check = false;
mappedSection.Prot = entry.prot;
mapped_section_list.Add(mappedSection);
start += cur_length;
++idx;
}
}
}
}
public void SectionCheck(int idx, bool _checked) {
mapped_section_list[idx].Check = _checked;
if (mapped_section_list[idx].Check) {
TotalMemorySize += (ulong)mapped_section_list[idx].Length;
}
else {
TotalMemorySize -= (ulong)mapped_section_list[idx].Length;
}
}
public ulong TotalResultCount() {
ulong totalResultCount = 0;
foreach (var section in mapped_section_list) {
if (section.Check && section.ResultList != null)
totalResultCount += (ulong)section.ResultList.Count;
}
return totalResultCount;
}
private int FindSectionID(ulong address) {
int low = 0;
int high = mapped_section_list.Count - 1;
int middle;
while (low <= high) {
middle = (low + high) / 2;
var m_section = mapped_section_list[middle];
if (address >= m_section.Start + (ulong)(m_section.Length)) {
low = middle + 1; // Search last half of the array
}
else if (address < m_section.Start) {
high = middle - 1; // Search first half of the array
}
else {
return middle; // Found, returning index
}
}
return -1;
}
}
public class ProcessManager {
public ProcessManager() => MappedSectionList = new MappedSectionList();
public MappedSectionList MappedSectionList { get; }
public ProcessInfo? GetProcessInfo(string process_name) {
ProcessList processList = MemoryHelper.GetProcessList();
ProcessInfo? processInfo = null;
foreach (Process process in processList.processes) {
if (process.name == process_name) {
processInfo = MemoryHelper.GetProcessInfo(process.pid);
break;
}
}
return processInfo;
}
public string GetProcessName(int idx) {
ProcessList processList = MemoryHelper.GetProcessList();
return processList.processes[idx].name;
}
}
public class ResultList {
private const int BIT_MAP_SIZE = 8;
private const int buffer_size = 4096 * 16;
private const int OFFSET_SIZE = 4;
private int buffer_id = 0;
private List<byte[]> buffer_list = new List<byte[]>();
private int buffer_tag_elem_count = 0;
private int buffer_tag_offset = 0;
private int count = 0;
private int element_alignment = 1;
private int element_size = 0;
private int iterator = 0;
public ResultList(int element_size, int element_alignment) {
buffer_list.Add(new byte[buffer_size]);
this.element_size = element_size;
this.element_alignment = element_alignment;
}
public int Count {
get { return count; }
}
public void Add(uint memoryAddressOffset, byte[] memoryValue) {
if (memoryValue.Length != element_size)
throw new Exception("Invalid address!");
byte[] dense_buffer = buffer_list[buffer_id];
uint tag_address_offset_base = BitConverter.ToUInt32(dense_buffer, buffer_tag_offset);
ulong bit_map = BitConverter.ToUInt64(dense_buffer, buffer_tag_offset + OFFSET_SIZE);
if (tag_address_offset_base > memoryAddressOffset)
throw new Exception("Invalid address!");
if (bit_map == 0) {
tag_address_offset_base = memoryAddressOffset;
Buffer.BlockCopy(BitConverter.GetBytes(memoryAddressOffset), 0, dense_buffer, buffer_tag_offset, OFFSET_SIZE);
}
int offset_in_bit_map = (int)(memoryAddressOffset - tag_address_offset_base) / element_alignment;
if (offset_in_bit_map < 64) {
dense_buffer[buffer_tag_offset + OFFSET_SIZE + offset_in_bit_map / 8] |= (byte)(1 << (offset_in_bit_map % 8)); //bit map
Buffer.BlockCopy(memoryValue, 0, dense_buffer, buffer_tag_offset + OFFSET_SIZE + BIT_MAP_SIZE + element_size * buffer_tag_elem_count, element_size);//value
++buffer_tag_elem_count;
}
else {
buffer_tag_offset += OFFSET_SIZE + BIT_MAP_SIZE + element_size * buffer_tag_elem_count;
//Alloc new page
if (buffer_tag_offset + OFFSET_SIZE + BIT_MAP_SIZE + element_size * 64 >= buffer_size) {
buffer_list.Add(new byte[buffer_size]);
++buffer_id;
buffer_tag_offset = 0;
buffer_tag_elem_count = 0;
dense_buffer = buffer_list[buffer_id];
}
Buffer.BlockCopy(BitConverter.GetBytes(memoryAddressOffset), 0, dense_buffer, buffer_tag_offset, OFFSET_SIZE); //tag address base
dense_buffer[buffer_tag_offset + OFFSET_SIZE] = (byte)1; //bit map
Buffer.BlockCopy(memoryValue, 0, dense_buffer, buffer_tag_offset + OFFSET_SIZE + BIT_MAP_SIZE, element_size); //value
buffer_tag_elem_count = 1;
}
count++;
}
public void Begin() {
iterator = 0;
buffer_tag_offset = 0;
buffer_tag_elem_count = 0;
buffer_id = 0;
}
public void Clear() {
count = 0;
buffer_tag_offset = 0;
buffer_tag_elem_count = 0;
buffer_id = 0;
buffer_list.Clear();
buffer_list.Add(new byte[buffer_size]);
}
public bool End() => iterator == count;
public void Get(ref uint memoryAddressOffset, ref byte[] memoryValue) {
byte[] dense_buffer = buffer_list[buffer_id];
memoryValue = new byte[element_size];
uint offset_base = BitConverter.ToUInt32(dense_buffer, buffer_tag_offset);
ulong bit_map = BitConverter.ToUInt64(dense_buffer, buffer_tag_offset + OFFSET_SIZE);
memoryAddressOffset = (uint)(bit_position(bit_map, buffer_tag_elem_count) * element_alignment) + offset_base;
Buffer.BlockCopy(dense_buffer, buffer_tag_offset + OFFSET_SIZE + BIT_MAP_SIZE + element_size * buffer_tag_elem_count, memoryValue, 0, element_size);
}
public void Next() {
++iterator;
byte[] dense_buffer = buffer_list[buffer_id];
uint base_offset = BitConverter.ToUInt32(dense_buffer, buffer_tag_offset);
ulong bit_map = BitConverter.ToUInt64(dense_buffer, buffer_tag_offset + 4);
++buffer_tag_elem_count;
if (bit_count(bit_map, 63) <= buffer_tag_elem_count) {
buffer_tag_offset += OFFSET_SIZE + BIT_MAP_SIZE + element_size * buffer_tag_elem_count;
if (buffer_tag_offset + OFFSET_SIZE + BIT_MAP_SIZE + element_size * 64 >= buffer_size) {
++buffer_id;
buffer_tag_offset = 0;
buffer_tag_elem_count = 0;
}
else {
buffer_tag_elem_count = 0;
}
}
}
public void Set(byte[] memoryValue) {
byte[] dense_buffer = buffer_list[buffer_id];
Buffer.BlockCopy(
memoryValue,
0,
dense_buffer,
buffer_tag_offset + OFFSET_SIZE + BIT_MAP_SIZE + element_size * buffer_tag_elem_count,
element_size
);
}
private int bit_count(ulong data, int end) {
int sum = 0;
for (int i = 0; i <= end; ++i) {
if ((data & (1ul << i)) != 0) {
++sum;
}
}
return sum;
}
private int bit_position(ulong data, int pos) {
int sum = 0;
for (int i = 0; i <= 63; ++i) {
if ((data & (1ul << i)) != 0) {
if (sum == pos) {
return i;
}
++sum;
}
}
return -1;
}
}
}