-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmacmemory.cpp
More file actions
1284 lines (1115 loc) · 53.2 KB
/
macmemory.cpp
File metadata and controls
1284 lines (1115 loc) · 53.2 KB
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// MacMemory - A macOS memory scanner and editor
// Created by: Adrian Maier
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <cstring>
#include <algorithm>
#include <functional>
#include <unordered_map>
#include <unistd.h> // For usleep()
// macOS specific includes
#include <mach/mach.h>
#include <mach/mach_vm.h>
#include <mach/vm_region.h>
#include <mach/vm_map.h>
#include <libproc.h>
#include <sys/sysctl.h>
// ANSI color codes for terminal output
namespace Color {
const std::string RESET = "\033[0m";
const std::string RED = "\033[31m";
const std::string GREEN = "\033[32m";
const std::string YELLOW = "\033[33m";
const std::string BLUE = "\033[34m";
const std::string MAGENTA = "\033[35m";
const std::string CYAN = "\033[36m";
const std::string WHITE = "\033[37m";
const std::string BOLD = "\033[1m";
}
// Enum for value types (using regular enum for compatibility)
enum ValueType {
BYTE,
INT16,
INT32,
INT64,
FLOAT,
DOUBLE,
STRING,
UNKNOWN
};
// String representation of value types
std::map<ValueType, std::string> valueTypeNames;
// Initialize the map in a function
void initValueTypeNames() {
valueTypeNames[BYTE] = "Byte (1 byte)";
valueTypeNames[INT16] = "Short (2 bytes)";
valueTypeNames[INT32] = "Int (4 bytes)";
valueTypeNames[INT64] = "Long (8 bytes)";
valueTypeNames[FLOAT] = "Float (4 bytes)";
valueTypeNames[DOUBLE] = "Double (8 bytes)";
valueTypeNames[STRING] = "String";
valueTypeNames[UNKNOWN] = "Unknown";
}
// Memory region structure
struct MemoryRegion {
mach_vm_address_t start;
mach_vm_size_t size;
vm_prot_t protection;
std::string name;
bool readable;
bool writable;
bool executable;
};
// Memory scan result
struct ScanResult {
mach_vm_address_t address;
ValueType type;
std::vector<uint8_t> value;
std::string description;
};
// Process information
struct ProcessInfo {
pid_t pid;
std::string name;
};
// Main class for memory operations
class MemoryScanner {
private:
task_t targetTask;
pid_t targetPid;
std::string targetName;
std::vector<MemoryRegion> memoryRegions;
std::vector<ScanResult> scanResults;
std::vector<ScanResult> previousScanResults;
bool isAttached;
public:
MemoryScanner() : targetTask(MACH_PORT_NULL), targetPid(0), isAttached(false) {}
~MemoryScanner() {
if (isAttached) {
detachProcess();
}
}
// List all processes
std::vector<ProcessInfo> listProcesses() {
std::vector<ProcessInfo> processes;
int cntp = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0);
std::vector<pid_t> pids(cntp);
proc_listpids(PROC_ALL_PIDS, 0, pids.data(), sizeof(pid_t) * cntp);
for (int i = 0; i < cntp; i++) {
if (pids[i] == 0) continue;
char name[PROC_PIDPATHINFO_MAXSIZE];
if (proc_name(pids[i], name, sizeof(name)) > 0) {
ProcessInfo info;
info.pid = pids[i];
info.name = name;
processes.push_back(info);
}
}
return processes;
}
// Attach to a process
bool attachProcess(pid_t pid) {
kern_return_t kr = task_for_pid(mach_task_self(), pid, &targetTask);
if (kr != KERN_SUCCESS) {
std::cerr << "Failed to attach to process. Error: " << mach_error_string(kr) << std::endl;
std::cerr << "Note: On macOS, this may require running as root or with special permissions." << std::endl;
return false;
}
targetPid = pid;
char name[PROC_PIDPATHINFO_MAXSIZE];
if (proc_name(targetPid, name, sizeof(name)) > 0) {
targetName = name;
} else {
targetName = "Unknown";
}
isAttached = true;
memoryRegions.clear();
scanResults.clear();
previousScanResults.clear();
std::cout << "Successfully attached to process: " << targetName << " (PID: " << targetPid << ")" << std::endl;
// Load memory regions
refreshMemoryRegions();
return true;
}
// Detach from process
void detachProcess() {
if (isAttached) {
mach_port_deallocate(mach_task_self(), targetTask);
targetTask = MACH_PORT_NULL;
targetPid = 0;
targetName = "";
isAttached = false;
memoryRegions.clear();
scanResults.clear();
previousScanResults.clear();
std::cout << "Detached from process" << std::endl;
}
}
// Refresh memory regions
void refreshMemoryRegions() {
memoryRegions.clear();
mach_vm_address_t address = 0;
mach_vm_size_t size = 0;
vm_region_basic_info_data_64_t info;
mach_msg_type_number_t count = VM_REGION_BASIC_INFO_COUNT_64;
mach_port_t object_name;
while (true) {
kern_return_t kr = mach_vm_region(targetTask, &address, &size,
VM_REGION_BASIC_INFO_64,
(vm_region_info_t)&info,
&count, &object_name);
if (kr != KERN_SUCCESS) {
break;
}
MemoryRegion region;
region.start = address;
region.size = size;
region.protection = info.protection;
region.readable = (info.protection & VM_PROT_READ) != 0;
region.writable = (info.protection & VM_PROT_WRITE) != 0;
region.executable = (info.protection & VM_PROT_EXECUTE) != 0;
// Get region name/type
if (info.reserved) {
region.name = "Reserved";
} else if (info.protection == 0) {
region.name = "No access";
} else {
std::stringstream ss;
ss << (region.readable ? "R" : "-")
<< (region.writable ? "W" : "-")
<< (region.executable ? "X" : "-");
region.name = ss.str();
}
memoryRegions.push_back(region);
address += size;
}
std::cout << "Found " << memoryRegions.size() << " memory regions" << std::endl;
}
// Read memory
template <typename T>
bool readMemory(mach_vm_address_t address, T& value) {
mach_vm_size_t size = sizeof(T);
mach_vm_size_t data_size = 0;
kern_return_t kr = mach_vm_read_overwrite(targetTask, address, size,
(mach_vm_address_t)&value, &data_size);
return (kr == KERN_SUCCESS && data_size == size);
}
// Read a block of memory
bool readMemoryBlock(mach_vm_address_t address, void* buffer, size_t size) {
mach_vm_size_t data_size = 0;
kern_return_t kr = mach_vm_read_overwrite(targetTask, address, size,
(mach_vm_address_t)buffer, &data_size);
return (kr == KERN_SUCCESS && data_size == size);
}
// Write memory
template <typename T>
bool writeMemory(mach_vm_address_t address, const T& value) {
kern_return_t kr = mach_vm_write(targetTask, address, (vm_offset_t)&value, sizeof(T));
return (kr == KERN_SUCCESS);
}
// First scan - find values
void firstScan(ValueType type, const std::string& value, const std::string& comparison) {
scanResults.clear();
previousScanResults.clear();
std::cout << "Starting first scan, please wait..." << std::endl;
size_t valueSize = 0;
std::vector<uint8_t> targetValue;
switch (type) {
case ValueType::BYTE: {
uint8_t val = static_cast<uint8_t>(std::stoi(value));
valueSize = sizeof(val);
targetValue.resize(valueSize);
memcpy(targetValue.data(), &val, valueSize);
break;
}
case ValueType::INT16: {
int16_t val = static_cast<int16_t>(std::stoi(value));
valueSize = sizeof(val);
targetValue.resize(valueSize);
memcpy(targetValue.data(), &val, valueSize);
break;
}
case ValueType::INT32: {
int32_t val = static_cast<int32_t>(std::stoi(value));
valueSize = sizeof(val);
targetValue.resize(valueSize);
memcpy(targetValue.data(), &val, valueSize);
break;
}
case ValueType::INT64: {
int64_t val = static_cast<int64_t>(std::stoll(value));
valueSize = sizeof(val);
targetValue.resize(valueSize);
memcpy(targetValue.data(), &val, valueSize);
break;
}
case ValueType::FLOAT: {
float val = std::stof(value);
valueSize = sizeof(val);
targetValue.resize(valueSize);
memcpy(targetValue.data(), &val, valueSize);
break;
}
case ValueType::DOUBLE: {
double val = std::stod(value);
valueSize = sizeof(val);
targetValue.resize(valueSize);
memcpy(targetValue.data(), &val, valueSize);
break;
}
case ValueType::STRING: {
valueSize = value.length();
targetValue.resize(valueSize);
memcpy(targetValue.data(), value.c_str(), valueSize);
break;
}
default:
std::cout << "Unsupported value type" << std::endl;
return;
}
size_t totalRegions = memoryRegions.size();
size_t regionsScanned = 0;
size_t totalHits = 0;
// Iterate through memory regions
for (size_t i = 0; i < memoryRegions.size(); i++) {
const MemoryRegion& region = memoryRegions[i];
regionsScanned++;
// Skip non-readable regions
if (!region.readable) {
continue;
}
// Progress update
if (regionsScanned % 100 == 0) {
float progress = static_cast<float>(regionsScanned) / static_cast<float>(totalRegions) * 100.0f;
std::cout << "\rScanning... " << std::fixed << std::setprecision(1) << progress << "% complete" << std::flush;
}
// Allocate buffer for region data
std::vector<uint8_t> buffer(region.size);
if (!readMemoryBlock(region.start, buffer.data(), region.size)) {
continue;
}
// Scan for values
for (size_t offset = 0; offset <= buffer.size() - valueSize; offset++) {
bool found = false;
if (comparison == "exact") {
found = (memcmp(buffer.data() + offset, targetValue.data(), valueSize) == 0);
} else if (comparison == "greater") {
// Implement comparison logic for each type
switch (type) {
case ValueType::BYTE: {
uint8_t val = *reinterpret_cast<uint8_t*>(buffer.data() + offset);
uint8_t target = *reinterpret_cast<uint8_t*>(targetValue.data());
found = val > target;
break;
}
case ValueType::INT16: {
int16_t val = *reinterpret_cast<int16_t*>(buffer.data() + offset);
int16_t target = *reinterpret_cast<int16_t*>(targetValue.data());
found = val > target;
break;
}
case ValueType::INT32: {
int32_t val = *reinterpret_cast<int32_t*>(buffer.data() + offset);
int32_t target = *reinterpret_cast<int32_t*>(targetValue.data());
found = val > target;
break;
}
case ValueType::INT64: {
int64_t val = *reinterpret_cast<int64_t*>(buffer.data() + offset);
int64_t target = *reinterpret_cast<int64_t*>(targetValue.data());
found = val > target;
break;
}
case ValueType::FLOAT: {
float val = *reinterpret_cast<float*>(buffer.data() + offset);
float target = *reinterpret_cast<float*>(targetValue.data());
found = val > target;
break;
}
case ValueType::DOUBLE: {
double val = *reinterpret_cast<double*>(buffer.data() + offset);
double target = *reinterpret_cast<double*>(targetValue.data());
found = val > target;
break;
}
default:
break;
}
} else if (comparison == "less") {
// Similar logic for less than comparison
switch (type) {
case ValueType::BYTE: {
uint8_t val = *reinterpret_cast<uint8_t*>(buffer.data() + offset);
uint8_t target = *reinterpret_cast<uint8_t*>(targetValue.data());
found = val < target;
break;
}
case ValueType::INT16: {
int16_t val = *reinterpret_cast<int16_t*>(buffer.data() + offset);
int16_t target = *reinterpret_cast<int16_t*>(targetValue.data());
found = val < target;
break;
}
case ValueType::INT32: {
int32_t val = *reinterpret_cast<int32_t*>(buffer.data() + offset);
int32_t target = *reinterpret_cast<int32_t*>(targetValue.data());
found = val < target;
break;
}
case ValueType::INT64: {
int64_t val = *reinterpret_cast<int64_t*>(buffer.data() + offset);
int64_t target = *reinterpret_cast<int64_t*>(targetValue.data());
found = val < target;
break;
}
case ValueType::FLOAT: {
float val = *reinterpret_cast<float*>(buffer.data() + offset);
float target = *reinterpret_cast<float*>(targetValue.data());
found = val < target;
break;
}
case ValueType::DOUBLE: {
double val = *reinterpret_cast<double*>(buffer.data() + offset);
double target = *reinterpret_cast<double*>(targetValue.data());
found = val < target;
break;
}
default:
break;
}
}
if (found) {
ScanResult result;
result.address = region.start + offset;
result.type = type;
result.value.resize(valueSize);
memcpy(result.value.data(), buffer.data() + offset, valueSize);
// Create description string
std::stringstream ss;
switch (type) {
case ValueType::BYTE:
ss << static_cast<int>(*reinterpret_cast<uint8_t*>(result.value.data()));
break;
case ValueType::INT16:
ss << *reinterpret_cast<int16_t*>(result.value.data());
break;
case ValueType::INT32:
ss << *reinterpret_cast<int32_t*>(result.value.data());
break;
case ValueType::INT64:
ss << *reinterpret_cast<int64_t*>(result.value.data());
break;
case ValueType::FLOAT:
ss << *reinterpret_cast<float*>(result.value.data());
break;
case ValueType::DOUBLE:
ss << *reinterpret_cast<double*>(result.value.data());
break;
case ValueType::STRING: {
std::string str(reinterpret_cast<char*>(result.value.data()), result.value.size());
ss << "\"" << str << "\"";
break;
}
default:
ss << "Unknown";
break;
}
result.description = ss.str();
scanResults.push_back(result);
totalHits++;
// Limit results to prevent memory exhaustion
if (totalHits >= 10000) {
std::cout << "\rToo many results (>10000), stopping scan" << std::endl;
break;
}
}
}
if (totalHits >= 10000) {
break;
}
}
std::cout << "\rScan complete. Found " << scanResults.size() << " matches. " << std::endl;
}
// Next scan - filter existing results
void nextScan(ValueType type, const std::string& value, const std::string& comparison) {
if (scanResults.empty()) {
std::cout << "No previous scan results to filter" << std::endl;
return;
}
// Store previous results
previousScanResults = scanResults;
scanResults.clear();
std::cout << "Starting next scan, filtering " << previousScanResults.size() << " addresses..." << std::endl;
// Parse search value
std::vector<uint8_t> targetValue;
size_t valueSize = 0;
switch (type) {
case ValueType::BYTE: {
uint8_t val = static_cast<uint8_t>(std::stoi(value));
valueSize = sizeof(val);
targetValue.resize(valueSize);
memcpy(targetValue.data(), &val, valueSize);
break;
}
case ValueType::INT16: {
int16_t val = static_cast<int16_t>(std::stoi(value));
valueSize = sizeof(val);
targetValue.resize(valueSize);
memcpy(targetValue.data(), &val, valueSize);
break;
}
case ValueType::INT32: {
int32_t val = static_cast<int32_t>(std::stoi(value));
valueSize = sizeof(val);
targetValue.resize(valueSize);
memcpy(targetValue.data(), &val, valueSize);
break;
}
case ValueType::INT64: {
int64_t val = static_cast<int64_t>(std::stoll(value));
valueSize = sizeof(val);
targetValue.resize(valueSize);
memcpy(targetValue.data(), &val, valueSize);
break;
}
case ValueType::FLOAT: {
float val = std::stof(value);
valueSize = sizeof(val);
targetValue.resize(valueSize);
memcpy(targetValue.data(), &val, valueSize);
break;
}
case ValueType::DOUBLE: {
double val = std::stod(value);
valueSize = sizeof(val);
targetValue.resize(valueSize);
memcpy(targetValue.data(), &val, valueSize);
break;
}
case ValueType::STRING: {
valueSize = value.length();
targetValue.resize(valueSize);
memcpy(targetValue.data(), value.c_str(), valueSize);
break;
}
default:
std::cout << "Unsupported value type" << std::endl;
return;
}
size_t totalAddresses = previousScanResults.size();
size_t addressesChecked = 0;
// Check each previous result
for (size_t i = 0; i < previousScanResults.size(); i++) {
const ScanResult& prevResult = previousScanResults[i];
addressesChecked++;
// Progress update
if (addressesChecked % 1000 == 0) {
float progress = static_cast<float>(addressesChecked) / static_cast<float>(totalAddresses) * 100.0f;
std::cout << "\rFiltering... " << std::fixed << std::setprecision(1) << progress << "% complete" << std::flush;
}
// Read current value at address
std::vector<uint8_t> currentValue(valueSize);
if (!readMemoryBlock(prevResult.address, currentValue.data(), valueSize)) {
continue;
}
bool found = false;
if (comparison == "exact") {
found = (memcmp(currentValue.data(), targetValue.data(), valueSize) == 0);
} else if (comparison == "greater") {
switch (type) {
case ValueType::BYTE: {
uint8_t val = *reinterpret_cast<uint8_t*>(currentValue.data());
uint8_t target = *reinterpret_cast<uint8_t*>(targetValue.data());
found = val > target;
break;
}
case ValueType::INT16: {
int16_t val = *reinterpret_cast<int16_t*>(currentValue.data());
int16_t target = *reinterpret_cast<int16_t*>(targetValue.data());
found = val > target;
break;
}
case ValueType::INT32: {
int32_t val = *reinterpret_cast<int32_t*>(currentValue.data());
int32_t target = *reinterpret_cast<int32_t*>(targetValue.data());
found = val > target;
break;
}
case ValueType::INT64: {
int64_t val = *reinterpret_cast<int64_t*>(currentValue.data());
int64_t target = *reinterpret_cast<int64_t*>(targetValue.data());
found = val > target;
break;
}
case ValueType::FLOAT: {
float val = *reinterpret_cast<float*>(currentValue.data());
float target = *reinterpret_cast<float*>(targetValue.data());
found = val > target;
break;
}
case ValueType::DOUBLE: {
double val = *reinterpret_cast<double*>(currentValue.data());
double target = *reinterpret_cast<double*>(targetValue.data());
found = val > target;
break;
}
default:
break;
}
} else if (comparison == "less") {
switch (type) {
case ValueType::BYTE: {
uint8_t val = *reinterpret_cast<uint8_t*>(currentValue.data());
uint8_t target = *reinterpret_cast<uint8_t*>(targetValue.data());
found = val < target;
break;
}
case ValueType::INT16: {
int16_t val = *reinterpret_cast<int16_t*>(currentValue.data());
int16_t target = *reinterpret_cast<int16_t*>(targetValue.data());
found = val < target;
break;
}
case ValueType::INT32: {
int32_t val = *reinterpret_cast<int32_t*>(currentValue.data());
int32_t target = *reinterpret_cast<int32_t*>(targetValue.data());
found = val < target;
break;
}
case ValueType::INT64: {
int64_t val = *reinterpret_cast<int64_t*>(currentValue.data());
int64_t target = *reinterpret_cast<int64_t*>(targetValue.data());
found = val < target;
break;
}
case ValueType::FLOAT: {
float val = *reinterpret_cast<float*>(currentValue.data());
float target = *reinterpret_cast<float*>(targetValue.data());
found = val < target;
break;
}
case ValueType::DOUBLE: {
double val = *reinterpret_cast<double*>(currentValue.data());
double target = *reinterpret_cast<double*>(targetValue.data());
found = val < target;
break;
}
default:
break;
}
} else if (comparison == "changed") {
found = (memcmp(currentValue.data(), prevResult.value.data(), valueSize) != 0);
} else if (comparison == "unchanged") {
found = (memcmp(currentValue.data(), prevResult.value.data(), valueSize) == 0);
}
if (found) {
ScanResult result = prevResult;
result.value = currentValue;
// Update description
std::stringstream ss;
switch (type) {
case ValueType::BYTE:
ss << static_cast<int>(*reinterpret_cast<uint8_t*>(result.value.data()));
break;
case ValueType::INT16:
ss << *reinterpret_cast<int16_t*>(result.value.data());
break;
case ValueType::INT32:
ss << *reinterpret_cast<int32_t*>(result.value.data());
break;
case ValueType::INT64:
ss << *reinterpret_cast<int64_t*>(result.value.data());
break;
case ValueType::FLOAT:
ss << *reinterpret_cast<float*>(result.value.data());
break;
case ValueType::DOUBLE:
ss << *reinterpret_cast<double*>(result.value.data());
break;
case ValueType::STRING: {
std::string str(reinterpret_cast<char*>(result.value.data()), result.value.size());
ss << "\"" << str << "\"";
break;
}
default:
ss << "Unknown";
break;
}
result.description = ss.str();
scanResults.push_back(result);
}
}
std::cout << "\rFiltering complete. Found " << scanResults.size() << " matches. " << std::endl;
}
// Display scan results
void displayResults(size_t limit = 20) {
if (scanResults.empty()) {
std::cout << "No scan results to display" << std::endl;
return;
}
std::cout << Color::BOLD << "Scan Results (" << scanResults.size() << " total):" << Color::RESET << std::endl;
std::cout << "───────────────────────────────────────────────────────────────" << std::endl;
std::cout << Color::BOLD << std::left << std::setw(5) << "ID"
<< std::setw(18) << "Address"
<< std::setw(12) << "Type"
<< "Value" << Color::RESET << std::endl;
std::cout << "───────────────────────────────────────────────────────────────" << std::endl;
size_t count = 0;
for (size_t i = 0; i < scanResults.size() && count < limit; i++) {
const auto& result = scanResults[i];
std::stringstream addr;
addr << "0x" << std::hex << std::setw(16) << std::setfill('0') << result.address;
std::cout << std::left << std::setw(5) << i
<< std::setw(18) << addr.str()
<< std::setw(12) << valueTypeNames[result.type]
<< result.description << std::endl;
count++;
}
if (scanResults.size() > limit) {
std::cout << "... and " << (scanResults.size() - limit) << " more results" << std::endl;
}
std::cout << "───────────────────────────────────────────────────────────────" << std::endl;
}
// Modify a value at a specific address
template <typename T>
bool modifyValue(mach_vm_address_t address, T value) {
return writeMemory(address, value);
}
// Create a watchpoint for an address
void watchAddress(mach_vm_address_t address, ValueType type, size_t updateInterval = 1000) {
if (!isAttached) {
std::cout << "Not attached to any process" << std::endl;
return;
}
size_t valueSize = 0;
switch (type) {
case ValueType::BYTE: valueSize = 1; break;
case ValueType::INT16: valueSize = 2; break;
case ValueType::INT32: valueSize = 4; break;
case ValueType::INT64: valueSize = 8; break;
case ValueType::FLOAT: valueSize = 4; break;
case ValueType::DOUBLE: valueSize = 8; break;
case ValueType::STRING: valueSize = 32; break; // Default string size to watch
default: valueSize = 4; break;
}
std::vector<uint8_t> lastValue(valueSize);
if (!readMemoryBlock(address, lastValue.data(), valueSize)) {
std::cout << "Failed to read initial value at address 0x"
<< std::hex << address << std::dec << std::endl;
return;
}
// Display initial value
std::cout << "Watching address 0x" << std::hex << address << std::dec
<< " (Type: " << valueTypeNames[type] << ")" << std::endl;
std::cout << "Initial value: ";
printValue(lastValue.data(), type);
std::cout << std::endl;
std::cout << "Press Ctrl+C to stop watching" << std::endl;
// Watch loop
int updateCount = 0;
try {
while (true) {
std::vector<uint8_t> currentValue(valueSize);
if (!readMemoryBlock(address, currentValue.data(), valueSize)) {
std::cout << "Failed to read value" << std::endl;
break;
}
// Check if value changed
if (memcmp(lastValue.data(), currentValue.data(), valueSize) != 0) {
updateCount++;
std::cout << "Change detected (#" << updateCount << "): ";
std::cout << "Old: ";
printValue(lastValue.data(), type);
std::cout << " → New: ";
printValue(currentValue.data(), type);
std::cout << std::endl;
// Update last value
lastValue = currentValue;
}
// Sleep - using older sleep method for compatibility
usleep(updateInterval * 1000);
}
} catch (const std::exception& e) {
std::cout << "Error while watching: " << e.what() << std::endl;
}
}
// Print a value based on type
void printValue(const void* data, ValueType type) {
switch (type) {
case ValueType::BYTE:
std::cout << static_cast<int>(*reinterpret_cast<const uint8_t*>(data));
break;
case ValueType::INT16:
std::cout << *reinterpret_cast<const int16_t*>(data);
break;
case ValueType::INT32:
std::cout << *reinterpret_cast<const int32_t*>(data);
break;
case ValueType::INT64:
std::cout << *reinterpret_cast<const int64_t*>(data);
break;
case ValueType::FLOAT:
std::cout << *reinterpret_cast<const float*>(data);
break;
case ValueType::DOUBLE:
std::cout << *reinterpret_cast<const double*>(data);
break;
case ValueType::STRING: {
std::string str(reinterpret_cast<const char*>(data));
std::cout << "\"" << str << "\"";
break;
}
default:
std::cout << "Unknown";
break;
}
}
// Load scanning patterns from file
void loadPatterns(const std::string& filename) {
// Implementation for loading signature patterns
}
// Save scan results to file
void saveResults(const std::string& filename) {
if (scanResults.empty()) {
std::cout << "No results to save" << std::endl;
return;
}
std::ofstream file(filename);
if (!file) {
std::cout << "Failed to open file: " << filename << std::endl;
return;
}
file << "# MacMemory Scan Results" << std::endl;
file << "# Process: " << targetName << " (PID: " << targetPid << ")" << std::endl;
file << "# Timestamp: " << std::time(nullptr) << std::endl;
file << "# Results: " << scanResults.size() << std::endl;
file << "# Format: ID,Address,Type,Value,Description" << std::endl;
for (size_t i = 0; i < scanResults.size(); i++) {
const auto& result = scanResults[i];
file << i << ","
<< "0x" << std::hex << result.address << std::dec << ","
<< static_cast<int>(result.type) << ",";
// Save value as hex bytes
for (size_t j = 0; j < result.value.size(); j++) {
file << std::hex << std::setw(2) << std::setfill('0')
<< static_cast<int>(result.value[j]);
}
file << "," << result.description << std::endl;
}
file.close();
std::cout << "Saved " << scanResults.size() << " results to " << filename << std::endl;
}
// Load scan results from file
void loadResults(const std::string& filename) {
// Implementation for loading saved results
}
// Get current attached process info
void getProcessInfo() {
if (!isAttached) {
std::cout << "Not attached to any process" << std::endl;
return;
}
std::cout << "Process Information:" << std::endl;
std::cout << " Name: " << targetName << std::endl;
std::cout << " PID: " << targetPid << std::endl;
std::cout << " Memory Regions: " << memoryRegions.size() << std::endl;
std::cout << " Current Scan Results: " << scanResults.size() << std::endl;
// Get total memory usage
size_t totalMemory = 0;
for (const auto& region : memoryRegions) {
totalMemory += region.size;
}
std::cout << " Total Memory: " << (totalMemory / (1024 * 1024)) << " MB" << std::endl;
}
// Helper methods
bool isProcessAttached() const { return isAttached; }
std::string getProcessName() const { return targetName; }
pid_t getProcessId() const { return targetPid; }
size_t getResultCount() const { return scanResults.size(); }
};
// Command-line interface class
class CLI {
private:
MemoryScanner scanner;
bool running;
std::unordered_map<std::string, std::function<void(const std::vector<std::string>&)>> commands;
public:
CLI() : running(false) {
initCommands();
}
void initCommands() {
// Core commands
commands["help"] = [this](const std::vector<std::string>& args) { showHelp(args); };
commands["exit"] = [this](const std::vector<std::string>& args) { running = false; };
commands["quit"] = [this](const std::vector<std::string>& args) { running = false; };
// Process commands
commands["ps"] = [this](const std::vector<std::string>& args) { listProcesses(args); };
commands["attach"] = [this](const std::vector<std::string>& args) { attachProcess(args); };
commands["detach"] = [this](const std::vector<std::string>& args) { detachProcess(args); };
commands["info"] = [this](const std::vector<std::string>& args) { processInfo(args); };
// Memory commands
commands["regions"] = [this](const std::vector<std::string>& args) { listRegions(args); };
commands["scan"] = [this](const std::vector<std::string>& args) { scanMemory(args); };
commands["next"] = [this](const std::vector<std::string>& args) { nextScan(args); };
commands["results"] = [this](const std::vector<std::string>& args) { showResults(args); };
commands["read"] = [this](const std::vector<std::string>& args) { readMemory(args); };
commands["write"] = [this](const std::vector<std::string>& args) { writeMemory(args); };
commands["watch"] = [this](const std::vector<std::string>& args) { watchMemory(args); };
// Data management
commands["save"] = [this](const std::vector<std::string>& args) { saveResults(args); };
commands["load"] = [this](const std::vector<std::string>& args) { loadResults(args); };
}
void run() {
running = true;
std::cout << Color::BOLD << Color::CYAN << "MacMemory - Memory Scanner for macOS" << Color::RESET << std::endl;
std::cout << "Type 'help' for a list of commands" << std::endl;
std::cout << Color::BOLD << "Remember: SIP must be disabled for memory access" << Color::RESET << std::endl;
while (running) {
std::string input;
std::vector<std::string> args;
// Display prompt based on attachment status
if (scanner.isProcessAttached()) {
std::cout << Color::GREEN << scanner.getProcessName() << "(" << scanner.getProcessId() << ")> " << Color::RESET;
} else {
std::cout << Color::YELLOW << "MacMemory> " << Color::RESET;
}
std::getline(std::cin, input);
if (input.empty()) {
continue;
}