-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexMemory.hpp
More file actions
1331 lines (1042 loc) · 39.7 KB
/
exMemory.hpp
File metadata and controls
1331 lines (1042 loc) · 39.7 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
#pragma once
#include <windows.h>
#include <TlHelp32.h>
#include <Psapi.h>
#include <memory>
#include <vector>
#include <string>
// architecture type helpers
#ifdef _WIN64
typedef unsigned __int64 i64_t;
#else
typedef unsigned int i64_t;
#endif
// fwd declare helpers
inline static std::string ToLower(const std::string& input);
inline static std::string ToUpper(const std::string& input);
inline static std::string ToString(const std::wstring& input);
inline static std::wstring ToWString(const std::string& input);
// general process information
typedef struct PROCESSINFO64
{
bool bAttached; // set when attached to a process
DWORD dwAccessLevel{ 0 }; // access rights to process ( if attached )
HWND hWnd{ 0 }; // handle to process window
HANDLE hProc{ INVALID_HANDLE_VALUE }; // handle to process
DWORD dwPID{ 0 }; // process id
i64_t dwModuleBase{ 0 }; // module base address
std::string mProcName{ "" }; // process name
std::string mProcPath{ "" }; // process path
std::string mWndwTitle{ "" }; // process window title
} PROCESSINFO32 , procInfo_t;
// general module information
typedef struct MODULEINFO64
{
DWORD dwPID{ 0 }; // owning process id
i64_t dwModuleBase{ 0 }; // module base address in process
std::string mModName{ "" }; // module name
} MODULEINFO32 , modInfo_t;
// assembly opcode index for ripping an offset from an instruction in memory
enum class EASM : int
{
ASM_MOV = 0, // mov rax,[proc.exe+offset] ; 0x48 0x8B 0x05 ?? ?? ?? ??
ASM_LEA, // lea rax,[proc.exe+offset] ; 0x48 0x8D 0x05 ?? ?? ?? ??
ASM_CMP, // cmp rax,[proc.exe+offset] ; 0x48 0x3B 0x05 ?? ?? ?? ??
ASM_CALL, // call proc.exe+offset ; 0xE8 ?? ?? ?? ??
ASM_NULL
};
// section headers index
enum class ESECTIONHEADERS : int
{
SECTION_TEXT = 0, // .text
SECTION_DATA, // .data
SECTION_RDATA, // .rdata
SECTION_IMPORT, // IMPORTS TABLE
SECTION_EXPORT, // EXPORTS TABLE
SECTION_NULL
};
// injection type index
enum class EINJECTION : int
{
INJECT_LOADLIBRARY = 0,
INJECT_MANUAL,
INJECT_NULL
};
/*
*
*
*/
class exMemory
{
/*//--------------------------\\
CONSTRUCTORS
*/
public:
explicit inline exMemory() = default; // default constructor | does nothing
explicit inline exMemory(const std::string& name); // attaches to process with all access rights
explicit inline exMemory(const std::string& name, const DWORD& dwAccess); // attaches to process with specified access rights
inline ~exMemory() noexcept; // destructor | detaches from process if attached
/*//--------------------------\\
INSTANCE MEMBERS
*/
public:
bool bAttached; // attached to a process
double mFrequency; // update frequency in ms
protected:
procInfo_t vmProcess; // attached process information
std::vector<procInfo_t> vmProcList; // active process list
std::vector<modInfo_t> vmModList; // module list for attached process
/*//--------------------------\\
INSTANCE METHODS
*/
public:
/* attempts to attach to a process by name
* virtualized to allow for custom behavior in derived classes
*/
virtual inline bool Attach(const std::string& name, const DWORD& dwAccess = PROCESS_ALL_ACCESS);
/* detaches from the attached process
* virtualized to allow for custom behavior in derived classes
*/
virtual inline bool Detach();
/* verifies attached process is active & updates processinfo structure when needed
* virtualized to allow for custom behavior in derived classes
*/
virtual inline void update();
public:
/* returns the process information structure
* see: procInfo_t or PROCESSINFO64
*/
inline const procInfo_t& GetProcessInfo() const { return vmProcess; }
/* returns an updated process list */
inline const std::vector<procInfo_t>& GetProcessList() const { return vmProcList; }
/* returns a list containing all modules in the attached process */
inline const std::vector<modInfo_t>& GetModuleList() const { return vmModList; }
protected:
/* helper method to determine if the current memory instance is attached to a process for handling various memory operations */
inline const bool IsValidInstance() noexcept { return bAttached && vmProcess.bAttached && vmProcess.hProc != INVALID_HANDLE_VALUE; }
public:
/* reads a memory into a buffer at the specified address in the attached process
* returns true if all bytes were read
*/
inline bool ReadMemory(const i64_t& addr, void* buffer, const DWORD& szRead);
/* attempts to write bytes in the attached process
* returns true if all bytes were written successfully
*/
inline bool WriteMemory(const i64_t& addr, const void* buffer, const DWORD& szWrite);
/* reads a continguous string in at the specified address in the attached process
* returns true if the string was successfully read
*/
inline bool ReadString(const i64_t& addr, std::string& string, const DWORD& szString = MAX_PATH);
/* reads a chain of pointers in the attached process to find an address in memory
* returns the address if found
*/
inline i64_t ReadPointerChain(const i64_t& addr, std::vector<unsigned int>& offsets, i64_t* lpResult);
/* attempts to patch a sequence of bytes in the attached process
* returns true if successful
*/
inline bool PatchMemory(const i64_t& addr, const void* buffer, const DWORD& szWrite);
/* gets an address relative to the input named module base address */
inline i64_t GetAddress(const unsigned int& offset, const std::string& modName = "");
inline bool GetAddress(const unsigned int& offset, i64_t* lpResult, const std::string& modName = "");
/* attempts to find a pattern in the attached process
* returns the address of pattern if found
*/
inline i64_t FindPattern(const std::string& signature);
inline i64_t FindPattern(const std::string& signature, int padding);
inline i64_t FindPattern(const std::string& signature, int padding, EASM instruction);
/* attempts to find a section header address in the attached process*/
inline i64_t GetSectionHeader(const ESECTIONHEADERS& section, i64_t* lpResult);
/* attempts to obtain the address of a function located in the atteched processes export directory */
inline i64_t GetProcAddress(const std::string& fnName, i64_t* lpResult);
/* attempts to inject a module from disk into the attached process */
inline bool LoadLibraryInject(const std::string& dllPath);
public:
/* template read memory with szRead parameter
* NOTE: does not work with strings
*/
template<typename T>
auto Read(i64_t addr, DWORD szRead) noexcept -> T
{
T result{};
ReadMemory(addr, &result, szRead);
return result;
}
/* template read memory
* NOTE: does not work with strings
*/
template<typename T>
auto Read(i64_t addr) noexcept -> T
{
T result{};
ReadMemory(addr, &result, sizeof(T));
return result;
}
/* template write memory with szPatch param */
template<typename T>
auto Write(i64_t addr, T patch, DWORD szPatch) noexcept -> bool { return WriteMemory(addr, &patch, szPatch); }
/* template write memory */
template<typename T>
auto Write(i64_t addr, T patch) noexcept -> bool { return WriteMemory(addr, &patch, sizeof(T)); }
/*//--------------------------\\
STATIC METHODS
*/
public: // methods for directly attaching to a process
/* attempts to attach to the named process with desired access level and returns a process information structure */
static inline bool AttachEx(const std::string& name, procInfo_t* lpProcess, const DWORD& dwDesiredAccess);
/* detaches from the attached process by freeing any opened handles to free the process information structure */
static inline bool DetachEx(procInfo_t& pInfo);
public: // methods for retrieving information on a process by name , are somewhat slow and should not be used constantly. consider caching information if needed.
/* attempts to retrieve a process id by name
* utilizes FindProcessEx which iterates through ALL processes information before again searching through the procInfo list to return a match ( if any )
*/
static inline bool GetProcID(const std::string& procName, DWORD* outPID);
/* attempts to obtain the module base address for the specified process name
* utilizes FindProcessEx which iterates through ALL processes information before again searching through the procInfo list to return a match ( if any )
*/
static inline bool GetModuleBaseAddress(const std::string& procName, i64_t* lpResult, const std::string& modName = "");
/* attempts to obtain information on a process without opening a handle to it
* utilizes FindProcessEx which iterates through ALL processes information before again searching through the procInfo list to return a match ( if any )
*/
static inline bool GetProcInfo(const std::string& name, procInfo_t* lpout);
/* determines if the specified name exists in the active process directory
* utilizes FindProcessEx which iterates through ALL processes information before again searching through the procInfo list to return a match ( if any )
*/
static inline bool IsProcessRunning(const std::string& name);
public: // methods for obtaining info on active processes
/* obtains a list of all active processes on the machine that contains basic information on a process without requiring a handle
* ref: https://learn.microsoft.com/en-us/windows/win32/toolhelp/taking-a-snapshot-and-viewing-processes
*/
static inline bool GetActiveProcessesEx(std::vector<procInfo_t>& procList);
/* obtains a list of all modules loaded in the attached process */
static inline bool GetProcessModulesEx(const DWORD& dwPID, std::vector< modInfo_t>& moduleList);
/* gets info on a process by name , can be extended to attach to the process if found
* utilizes GetActiveProcesses method which is somewhat slow as it obtains ALL processes before returning
*/
static inline bool FindProcessEx(const std::string& procName, procInfo_t* procInfo, const bool& bAttach, const DWORD& dwDesiredAccess);
/* attempts to find a module by name located in the attached process and returns it's base address */
static inline bool FindModuleEx(const std::string& procName, const std::string& modName, modInfo_t* lpResult);
public: // basic memory operations
/* attempts to read memory at the specified address from the target process */
static inline bool ReadMemoryEx(const HANDLE& hProc, const i64_t& addr, void* buffer, size_t szRead);
/* attempts to write bytes to the specified address in memory from the target process */
static inline bool WriteMemoryEx(const HANDLE& hProc, const i64_t& addr, LPVOID buffer, DWORD szWrite);
/* attempts to read a string at the specified address in memory from the target process */
static inline bool ReadStringEx(const HANDLE& hProc, const i64_t& addr, const size_t& szString, std::string* lpResult);
/* attempts to return an address located in memory via chain of offsets */
static inline bool ReadPointerChainEx(const HANDLE& hProc, const i64_t& addr, const std::vector<unsigned int>& offsets, i64_t* lpResult);
/* attempts to patch a sequence of bytes in the target process */
static inline bool PatchMemoryEx(const HANDLE& hProc, const i64_t& addr, const void* buffer, const DWORD& szWrite);
public: // advanced methods for obtaining information on a process which requires a handle
/* attempts to find a module by name located in the attached process and returns it's base address */
static inline bool GetModuleAddressEx(const HANDLE& hProc, const std::string& moduleName, i64_t* lpResult);
/* attempts to return the address of a section header by index
* ref: https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-image_nt_headers64
* ref: https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-image_file_header
* ref: https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-image_optional_header64
* ref: https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-image_section_header
*/
static inline bool GetSectionHeaderAddressEx(const HANDLE& hProc, const std::string& moduleName, const ESECTIONHEADERS& section, i64_t* lpResult, size_t* szImage);
static inline bool GetSectionHeaderAddressEx(const HANDLE& hProc, const i64_t& dwModule, const ESECTIONHEADERS& section, i64_t* lpResult, size_t* szImage);
/* attempts to return an address located in memory via pattern scan. can be extended to extract bytes from an instruction
* modifed version of -> https://www.unknowncheats.me/forum/3019469-post2.html
*/
static inline bool FindPatternEx(const HANDLE& hProc, const std::string& moduleName, const std::string& signature, i64_t* lpResult, int padding, EASM instruction);
static inline bool FindPatternEx(const HANDLE& hProc, const i64_t& dwModule, const std::string& signature, i64_t* lpResult, int padding, EASM instruction);
/* attempts to find an exported function by name and return the it's rva
* https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-image_data_directory
*/
static inline bool GetProcAddressEx(const HANDLE& hProc, const std::string& moduleName, const std::string& fnName, i64_t* lpResult);
static inline bool GetProcAddressEx(const HANDLE& hProc, const i64_t& dwModule, const std::string& fnName, i64_t* lpResult);
public: // injection operations
/* injects a module (from disk) into the target process using LoadLibrary */
static inline bool LoadLibraryInjectorEx(const HANDLE& hProc, const std::string& dllPath);
public: // template methods
/* template read memory with szRead parameter
* NOTE: does not work with strings
*/
template<typename T>
static auto ReadEx(const HANDLE& hProc, const i64_t& addr, DWORD szRead) noexcept -> T
{
T result{};
ReadMemoryEx(hProc, addr, &result, szRead);
return result;
}
/* template read memory
* NOTE: does not work with strings
*/
template<typename T>
static auto ReadEx(const HANDLE& hProc, const i64_t& addr) noexcept -> T
{
T result{};
ReadMemoryEx(hProc, addr, &result, sizeof(T));
return result;
}
/* template write memory with szPatch param */
template<typename T>
static auto WriteEx(const HANDLE& hProc, const i64_t& addr, T patch, DWORD szPatch) noexcept -> bool { return WriteMemoryEx(hProc, addr, &patch, szPatch); }
/* template write memory */
template<typename T>
static auto WriteEx(const HANDLE& hProc, const i64_t& addr, T patch) noexcept -> bool { return WriteMemoryEx(hProc, addr, &patch, sizeof(T)); }
/*//--------------------------\\
TOOL METHODS
*/
protected:
struct EnumWindowData
{
unsigned int procId;
HWND hwnd;
};
/* callback for EnumWindows to find the maine process window
* ref: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enumwindows
*/
static inline BOOL CALLBACK GetProcWindowEx(HWND handle, LPARAM lParam);
};
//-------------------------------------------------------------------------------------------------
//
// CONSTRUCTORS
//
//-------------------------------------------------------------------------------------------------
exMemory::exMemory(const std::string& name)
{
bAttached = exMemory::Attach(name, PROCESS_ALL_ACCESS);
}
exMemory::exMemory(const std::string& name, const DWORD& dwAccess)
{
bAttached = exMemory::Attach(name, dwAccess);
}
exMemory::~exMemory()
{
Detach(); // close handles and free resources
}
//-------------------------------------------------------------------------------------------------
//
// INSTANCE METHODS
//
//-------------------------------------------------------------------------------------------------
bool exMemory::Attach(const std::string& name, const DWORD& dwAccess)
{
procInfo_t proc;
if (!AttachEx(name, &proc, dwAccess))
return false;
vmProcess = proc;
return vmProcess.bAttached;
}
bool exMemory::Detach()
{
return DetachEx(vmProcess);
}
void exMemory::update()
{
const bool& bAttched = vmProcess.bAttached; // is instance attached to a process ?
// check if attached process is running
if (!IsProcessRunning(vmProcess.mProcName))
{
Detach(); // close handles and free resources if not already done ( safe to call multiple times if nothing is attached )
return;
}
// attached process is running, update process information
}
//-------------------------------------------------------------------------------------------------
//
// INSTANCE METHODS ( MEMORY OPERATIONS )
//
//-------------------------------------------------------------------------------------------------
bool exMemory::ReadMemory(const i64_t& addr, void* buffer, const DWORD& szRead)
{
if (!IsValidInstance())
return false;
return ReadMemoryEx(vmProcess.hProc, addr, buffer, szRead);
}
bool exMemory::ReadString(const i64_t& addr, std::string& string, const DWORD& szString)
{
if (!IsValidInstance())
return false;
return ReadStringEx(vmProcess.hProc, addr, szString, &string);
}
bool exMemory::WriteMemory(const i64_t& addr, const void* buffer, const DWORD& szWrite)
{
if (!IsValidInstance())
return false;
return WriteMemoryEx(vmProcess.hProc, addr, LPVOID(buffer), szWrite);
}
bool exMemory::PatchMemory(const i64_t& addr, const void* buffer, const DWORD& szWrite)
{
if (!IsValidInstance())
return false;
return PatchMemoryEx(vmProcess.hProc, addr, buffer, szWrite);
}
i64_t exMemory::ReadPointerChain(const i64_t& addr, std::vector<unsigned int>& offsets, i64_t* lpResult)
{
if (!IsValidInstance())
return 0;
if (!ReadPointerChainEx(vmProcess.hProc, addr, offsets, lpResult))
return 0;
return *lpResult;
}
i64_t exMemory::GetAddress(const unsigned int& offset, const std::string& modName)
{
i64_t result = 0;
if (!GetAddress(offset, &result, modName))
return 0;
return result;
}
bool exMemory::GetAddress(const unsigned int& offset, i64_t* lpResult, const std::string& modName)
{
i64_t result = 0;
if (!IsValidInstance())
return 0;
if (modName.empty())
result = vmProcess.dwModuleBase + offset;
else if (!GetModuleAddressEx(vmProcess.hProc, modName, lpResult))
return false;
*lpResult = result;
return result > 0;
}
i64_t exMemory::FindPattern(const std::string& signature)
{
if (!IsValidInstance())
return 0;
i64_t result = 0;
if (!FindPatternEx(vmProcess.hProc, vmProcess.dwModuleBase, signature, &result, 0, EASM::ASM_NULL))
return 0;
return result;
}
i64_t exMemory::FindPattern(const std::string& signature, int padding)
{
if (!IsValidInstance())
return 0;
i64_t result = 0;
if (!FindPatternEx(vmProcess.hProc, vmProcess.dwModuleBase, signature, &result, padding, EASM::ASM_NULL))
return 0;
return result;
}
i64_t exMemory::FindPattern(const std::string& signature, int padding, EASM instruction)
{
if (!IsValidInstance())
return 0;
i64_t result = 0;
if (!FindPatternEx(vmProcess.hProc, vmProcess.dwModuleBase, signature, &result, padding, instruction))
return 0;
return result;
}
i64_t exMemory::GetSectionHeader(const ESECTIONHEADERS& section, i64_t* lpResult)
{
if (!IsValidInstance())
return 0;
if (GetSectionHeaderAddressEx(vmProcess.hProc, vmProcess.dwModuleBase, section, lpResult, nullptr))
return 0;
return *lpResult;
}
i64_t exMemory::GetProcAddress(const std::string& fnName, i64_t* lpResult)
{
if (!IsValidInstance())
return 0;
if (!GetProcAddressEx(vmProcess.hProc, vmProcess.dwModuleBase, fnName, lpResult))
return 0;
return *lpResult;
}
bool exMemory::LoadLibraryInject(const std::string& dllPath)
{
if (!IsValidInstance())
return false;
return LoadLibraryInjectorEx(vmProcess.hProc, dllPath);
}
//-------------------------------------------------------------------------------------------------
//
// STATIC METHODS
//
//-------------------------------------------------------------------------------------------------
bool exMemory::AttachEx(const std::string& name, procInfo_t* lpProcess, const DWORD& dwDesiredAccess)
{
return FindProcessEx(name, lpProcess, true, dwDesiredAccess);
}
bool exMemory::DetachEx(procInfo_t& pInfo)
{
bool result{ true };
if (pInfo.bAttached && pInfo.hProc != INVALID_HANDLE_VALUE)
CloseHandle(pInfo.hProc); // close handle to process
pInfo = procInfo_t(); // clear process information
return result;
}
//-------------------------------------------------------------------------------------------------
//
// STATIC METHODS ( PROCESS INFORMATION )
//
//-------------------------------------------------------------------------------------------------
bool exMemory::GetProcID(const std::string& procName, DWORD* outPID)
{
procInfo_t proc;
if (!GetProcInfo(procName, &proc))
return false;
*outPID = proc.dwPID;
return proc.dwPID > 0;
}
bool exMemory::GetModuleBaseAddress(const std::string& procName, i64_t* lpResult, const std::string& modName)
{
if (!modName.empty())
{
modInfo_t mod;
if (!FindModuleEx(procName, modName, &mod))
return false;
*lpResult = mod.dwModuleBase;
return mod.dwModuleBase > 0;
}
procInfo_t proc;
if (!GetProcInfo(procName, &proc))
return false;
*lpResult = proc.dwModuleBase;
return proc.dwModuleBase > 0;
}
bool exMemory::GetProcInfo(const std::string& name, procInfo_t* lpResult)
{
return FindProcessEx(name, lpResult, false, NULL);
}
bool exMemory::IsProcessRunning(const std::string& name)
{
return FindProcessEx(name, nullptr, false, NULL);
}
//-------------------------------------------------------------------------------------------------
//
// STATIC METHODS ( BASIC MEMORY OPERATIONS )
//
//-------------------------------------------------------------------------------------------------
bool exMemory::ReadMemoryEx(const HANDLE& hProc, const i64_t& addr, void* lpResult, size_t szRead)
{
SIZE_T size_read{};
return ReadProcessMemory(hProc, LPCVOID(addr), lpResult, szRead, &size_read) && szRead == size_read;
}
bool exMemory::WriteMemoryEx(const HANDLE& hProc, const i64_t& addr, LPVOID buffer, DWORD szWrite)
{
SIZE_T size_write{};
return WriteProcessMemory(hProc, LPVOID(addr), buffer, szWrite, &size_write) && szWrite == size_write;
}
bool exMemory::ReadStringEx(const HANDLE& hProc, const i64_t& addr, const size_t& szString, std::string* lpResult)
{
size_t bytes_read{};
char buf[MAX_PATH]{};
if (!ReadMemoryEx(hProc, addr, buf, szString))
return false;
*lpResult = std::string(buf);
return true;
}
bool exMemory::ReadPointerChainEx(const HANDLE& hProc, const i64_t& addr, const std::vector<unsigned int>& offsets, i64_t* lpResult)
{
i64_t result = addr;
for (unsigned int i = 0; i < offsets.size(); ++i)
{
result = ReadEx<i64_t>(hProc, result);
result += offsets[i];
}
*lpResult = result;
return result > 0;
}
bool exMemory::PatchMemoryEx(const HANDLE& hProc, const i64_t& addr, const void* buffer, const DWORD& szWrite)
{
// store original protection & set new protection
DWORD oldprotect;
if (!VirtualProtectEx(hProc, LPVOID(addr), szWrite, PAGE_EXECUTE_READWRITE, &oldprotect))
return false;
bool result = WriteProcessMemory(hProc, LPVOID(addr), buffer, szWrite, nullptr); // write bytes to address
VirtualProtectEx(hProc, LPVOID(addr), szWrite, oldprotect, &oldprotect); // restore memory protection
return result;
}
//-------------------------------------------------------------------------------------------------
//
// STATIC METHODS ( PROCESS & MODULE ENUMERATION )
//
//-------------------------------------------------------------------------------------------------
bool exMemory::GetActiveProcessesEx(std::vector<procInfo_t>& list)
{
// snapshot processes
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnap == INVALID_HANDLE_VALUE)
return FALSE;
PROCESSENTRY32 procEntry;
procEntry.dwSize = sizeof(procEntry);
if (!Process32Next(hSnap, &procEntry))
{
CloseHandle(hSnap);
return FALSE;
}
// iterate through all processes
std::vector<procInfo_t> active_process_list;
do
{
const DWORD procID = procEntry.th32ProcessID;
if (!procID)
continue;
// snapshot modules
HANDLE modSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPPROCESS, procID);
if (modSnap == INVALID_HANDLE_VALUE)
continue;
MODULEENTRY32 modEntry;
modEntry.dwSize = sizeof(modEntry);
if (!Module32First(modSnap, &modEntry))
{
CloseHandle(modSnap);
continue;
}
// iterate through all modules
do
{
// compare module names
if (_wcsicmp(modEntry.szModule, procEntry.szExeFile))
continue;
// module found
procInfo_t proc;
proc.mProcName = ToString(procEntry.szExeFile); // process name
proc.mProcPath = ToString(modEntry.szExePath); // process path
proc.dwPID = procID; // process ID
proc.dwModuleBase = i64_t(modEntry.modBaseAddr); // module base address
// push back process to list
active_process_list.push_back(proc);
break; // get next process information
} while (Module32Next(modSnap, &modEntry));
CloseHandle(modSnap);
} while (Process32Next(hSnap, &procEntry));
CloseHandle(hSnap);
list = active_process_list;
return list.size() > 0;
}
bool exMemory::GetProcessModulesEx(const DWORD& dwPID, std::vector<modInfo_t>& list)
{
// snapshot modules
HANDLE modSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPPROCESS, dwPID);
if (modSnap == INVALID_HANDLE_VALUE)
return false;
MODULEENTRY32 modEntry;
modEntry.dwSize = sizeof(modEntry);
if (!Module32First(modSnap, &modEntry))
{
CloseHandle(modSnap);
return false;
}
// iterate through all modules
std::vector<modInfo_t> active_module_list;
do
{
// module found
modInfo_t mod;
mod.dwPID = dwPID; // process ID
mod.dwModuleBase = i64_t(modEntry.modBaseAddr); // module base address
mod.mModName = ToString(modEntry.szModule); // module name
// push back module to list
active_module_list.push_back(mod);
} while (Module32Next(modSnap, &modEntry));
CloseHandle(modSnap);
list = active_module_list;
return list.size() > 0;
}
bool exMemory::FindProcessEx(const std::string& procName, procInfo_t* procInfo, const bool& bAttach, const DWORD& dwDesiredAccess)
{
bool result = false;
const auto& input = ToLower(procName);
// create process snapshot
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnap == INVALID_HANDLE_VALUE)
return FALSE;
// get first entry
PROCESSENTRY32 procEntry;
procEntry.dwSize = sizeof(procEntry);
if (!Process32Next(hSnap, &procEntry))
{
CloseHandle(hSnap);
return FALSE;
}
// iterate through all processes
do
{
// compare names
if (ToLower(ToString(procEntry.szExeFile)) != input)
continue;
// snapshot modules
HANDLE modSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPPROCESS, procEntry.th32ProcessID);
if (modSnap == INVALID_HANDLE_VALUE)
break;
// get first entry
MODULEENTRY32 modEntry;
modEntry.dwSize = sizeof(modEntry);
if (!Module32First(modSnap, &modEntry))
{
CloseHandle(modSnap);
break;
}
// module found
procInfo_t proc;
proc.mProcName = ToString(procEntry.szExeFile); // process name
proc.mProcPath = ToString(modEntry.szExePath); // process path
proc.dwPID = procEntry.th32ProcessID; // process ID
proc.dwModuleBase = i64_t(modEntry.modBaseAddr); // module base address
proc.dwAccessLevel = dwDesiredAccess; // desired access level
// attempt to get main process window
EnumWindowData eDat;
eDat.procId = proc.dwPID;
if (EnumWindows(GetProcWindowEx, reinterpret_cast<LPARAM>(&eDat)))
proc.hWnd = eDat.hwnd;
// Get window title
char buffer[MAX_PATH];
if (proc.hWnd && GetWindowTextA(proc.hWnd, buffer, MAX_PATH))
proc.mWndwTitle = std::string(buffer);
// open handle to process
if (bAttach && dwDesiredAccess > 0)
{
proc.hProc = OpenProcess(proc.dwAccessLevel, false, proc.dwPID);
proc.bAttached = proc.hProc != INVALID_HANDLE_VALUE;
}
*procInfo = proc;
result = true;
CloseHandle(modSnap);
break;
} while (Process32Next(hSnap, &procEntry));
CloseHandle(hSnap);
return result;
}
bool exMemory::FindModuleEx(const std::string& procName, const std::string& modName, modInfo_t* lpResult)
{
const auto& proc_cmp = ToLower(procName);
const auto& mod_cmp = ToLower(modName);
bool bFound{ false };
modInfo_t modInfo;
// snapshot processes
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnap == INVALID_HANDLE_VALUE)
return FALSE;
PROCESSENTRY32 procEntry;
procEntry.dwSize = sizeof(procEntry);
if (!Process32Next(hSnap, &procEntry))
{
CloseHandle(hSnap);
return FALSE;
}
// iterate through all processes
do
{
// compare process names
if (ToLower(ToString(procEntry.szExeFile)) != proc_cmp)
continue;
// snapshot modules
HANDLE modSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPPROCESS, procEntry.th32ProcessID);
if (modSnap == INVALID_HANDLE_VALUE)
break;
MODULEENTRY32 modEntry;
modEntry.dwSize = sizeof(modEntry);
if (!Module32First(modSnap, &modEntry))
{
CloseHandle(modSnap);
break;
}
// iterate through all modules
do
{
// compare module names
if (ToLower(ToString(modEntry.szModule)) != mod_cmp)
continue;
// module found
bFound = true;
// get module properties
modInfo.dwModuleBase = i64_t(modEntry.modBaseAddr); // module base address
modInfo.dwPID = procEntry.th32ProcessID; // process ID
modInfo.mModName = ToString(modEntry.szModule); // module name
// pass ref
*lpResult = modInfo;
break;
} while (Module32Next(modSnap, &modEntry));
CloseHandle(modSnap);
break;
} while (Process32Next(hSnap, &procEntry));
CloseHandle(hSnap);
return bFound;
}
//-------------------------------------------------------------------------------------------------
//
// STATIC METHODS ( ADVANCED MEMORY OPERATIONS )
//
//-------------------------------------------------------------------------------------------------
bool exMemory::GetModuleAddressEx(const HANDLE& hProc, const std::string& moduleName, i64_t* lpResult)
{
DWORD cbNeeded;
HMODULE modules[1024];
if (!EnumProcessModulesEx(hProc, modules, sizeof(modules), &cbNeeded, LIST_MODULES_ALL))
return false;
const auto szModule = cbNeeded / sizeof(HMODULE);
for (int i = 0; i < szModule; i++)
{
wchar_t modName[MAX_PATH];
if (!GetModuleBaseName(hProc, modules[i], modName, sizeof(modName) / sizeof(wchar_t)))
continue;
if (ToLower(ToString(modName)) != moduleName)
continue;
*lpResult = reinterpret_cast<i64_t>(modules[i]);
return true;
}