summaryrefslogtreecommitdiff
path: root/csgo-loader/csgo-client/RemoteCode/RemoteProcess.hpp
blob: b1c716bfecc4a077a4a4d8943ef1e28702c291b8 (plain)
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
#pragma once

#include <windows.h>
#include <psapi.h>

#include <Security/FnvHash.hpp>
#include <Security/SyscallManager.hpp>

namespace RemoteCode {
	// The module wrapper.
	class RemoteProcess;
	class RemoteModule {
		HANDLE  m_Module;
		int32_t m_SizeOfModule;
		
		// All the module data will be read upon class initialisation.
		ByteArray m_ModuleData;

	public:
		// The constructor (reads all module data into m_ModuleData).
		RemoteModule(HANDLE Module, RemoteProcess &Process);

		// TODO: Add support for wild-cards (not currently implemented)
		uintptr_t Scan(ByteArray &Pattern);

		// Allow us to access the module by just passing the
		// handle as a parameter.
		operator HANDLE() { return m_Module; }
		operator HINSTANCE() { return (HINSTANCE)m_Module; }
	};

	// The process wrapper.
	class RemoteProcess {
		HANDLE	m_Process;
		int32_t	m_ProcessId;

		// Exposing the syscalls in a convenient way to use with templating.
		void ReadMemoryWrapper_Internal(void *Address, void *Data, size_t SizeOfData);
		void WriteMemoryWrapper_Internal(void *Address, void *Data, size_t SizeOfData);

	public:
		RemoteProcess() = default;

		// For portability, will ignore exceptions.
		RemoteProcess(const char *ProcessName) {
			Start(ProcessName);
		}

		// Release the handle when the process goes out of scope.
		~RemoteProcess() {
			if(m_Process)
				CloseHandle(m_Process);
		}

		// Find the process ID and initialise the process.
		bool Start(const char *ProcessName);

		// Writes to the process memory.
		template <typename T>
		void Write(void *Address, T Data) {
			WriteMemoryWrapper_Internal(Address, (void *)&Data, sizeof T);
		}

		void Write(void *Address, uint8_t *Data, size_t SizeOfData) {
			WriteMemoryWrapper_Internal(Address, (void *)Data, SizeOfData);
		}
	
		// Reads from the process memory.
		template <typename T>
		T Read(void *Address) {
			T Buffer{};
			ReadMemoryWrapper_Internal(Address, (void *)&Buffer, sizeof T);
		
			return Buffer;
		}

		void Read(void *Address, uint8_t *Data, size_t SizeOfData) {
			ReadMemoryWrapper_Internal(Address, (void *)Data, SizeOfData);
		}

		// Allocates a memory region in the process.
		void *Allocate(size_t AllocationSize);

		// Finds a module in the process.
		RemoteModule FindModule(const char *ModuleName);

		// Allow us to access the process by just passing the
		// handle as a parameter.
		operator HANDLE() { return m_Process; }
	};
}