forked from faggotman69/Prometheus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtilities.h
More file actions
162 lines (135 loc) · 5.11 KB
/
Utilities.h
File metadata and controls
162 lines (135 loc) · 5.11 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
#pragma once
// Includes
#include "CommonIncludes.h"
#include <time.h>
#include <locale>
#include <codecvt>
#include <sstream>
#include <chrono>
// Colors for the console
//Define extra colours
#define FOREGROUND_WHITE (FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN)
#define FOREGROUND_YELLOW (FOREGROUND_RED | FOREGROUND_GREEN)
#define FOREGROUND_CYAN (FOREGROUND_BLUE | FOREGROUND_GREEN)
#define FOREGROUND_MAGENTA (FOREGROUND_RED | FOREGROUND_BLUE)
#define FOREGROUND_BLACK 0
#define FOREGROUND_INTENSE_RED (FOREGROUND_RED | FOREGROUND_INTENSITY)
#define FOREGROUND_INTENSE_GREEN (FOREGROUND_GREEN | FOREGROUND_INTENSITY)
#define FOREGROUND_INTENSE_BLUE (FOREGROUND_BLUE | FOREGROUND_INTENSITY)
#define FOREGROUND_INTENSE_WHITE (FOREGROUND_WHITE | FOREGROUND_INTENSITY)
#define FOREGROUND_INTENSE_YELLOW (FOREGROUND_YELLOW | FOREGROUND_INTENSITY)
#define FOREGROUND_INTENSE_CYAN (FOREGROUND_CYAN | FOREGROUND_INTENSITY)
#define FOREGROUND_INTENSE_MAGENTA (FOREGROUND_MAGENTA | FOREGROUND_INTENSITY)
#define INRANGE(x,a,b) (x >= a && x <= b)
#define getBits( x ) (INRANGE((x&(~0x20)),'A','F') ? ((x&(~0x20)) - 'A' + 0xa) : (INRANGE(x,'0','9') ? x - '0' : 0))
#define getByte( x ) (getBits(x[0]) << 4 | getBits(x[1]))
// U Namespace
// Purpose: Contains misc functionality for memory related functionality
namespace U
{
// Debug console controls
void OpenConsole(std::string Title);
void CloseConsole();
void Log(const char *fmt, ...);
void EnableLogFile(std::string filename);
void SetConsoleColor(WORD color);
// Misc Shizz
std::string GetTimeString();
// Memory utils
// Purpose: Provides memeory related functionality (pattern scanning ect)
// Waits for a module to be available, before returning it's base address
DWORD WaitOnModuleHandle(std::string moduleName);
uintptr_t FindSig(std::string moduleName, std::string pattern);
uintptr_t FindPatternNew(const char * module, const char * pattern_string, const char * mask);
// Attempts to locate the given signature and mask in a memory range
// Returns the address at which it has been found
DWORD FindPattern(std::string moduleName, BYTE* Mask, char* szMask);
DWORD_PTR GetPatternOffset(std::string moduleName, PBYTE pattern, std::string mask, DWORD_PTR codeBase, DWORD_PTR codeSize);
// Attempts to locate the given text in a memory range
// Returns the address at which it has been found
DWORD FindTextPattern(std::string moduleName, char* string);
std::uint8_t* pattern_scan(void* module, const char* signature);
std::wstring StringToWstring(std::string str);
//clantag + namespammer shit
long GetEpochTime();
const char * PadStringRight(std::string text, size_t value);
template <typename T>
T* CaptureInterface(std::string strModule, std::string strInterface)
{
typedef T* (*CreateInterfaceFn)(const char* szName, int iReturn);
CreateInterfaceFn CreateInterface = (CreateInterfaceFn)GetProcAddress(GetModuleHandleA(strModule.c_str()), "CreateInterface");
return CreateInterface(strInterface.c_str(), 0);
}
bool bin_match(uint8_t *code, std::vector< uint8_t > &pattern);
template< typename T = uintptr_t > static T first_match(uintptr_t start, std::string sig, size_t len);
template< typename T = uintptr_t > static T first_code_match(HMODULE start, std::string sig);
};
namespace detail
{
class protect_guard
{
public:
protect_guard(void* base, size_t len, std::uint32_t flags)
{
_base = base;
_length = len;
if (!VirtualProtect(base, len, flags, (PDWORD)&_old))
throw std::runtime_error("Failed to protect region.");
}
~protect_guard()
{
VirtualProtect(_base, _length, _old, (PDWORD)&_old);
}
private:
void* _base;
size_t _length;
std::uint32_t _old;
};
}
class vfunc_hook
{
public:
vfunc_hook();
vfunc_hook(void* base);
~vfunc_hook();
bool setup(void* class_base = nullptr);
template<typename T>
void hook_index(int index, T fun)
{
assert(index >= 0 && index <= (int)vftbl_len);
new_vftbl[index + 1] = reinterpret_cast<std::uintptr_t>(fun);
}
void unhook_index(int index)
{
new_vftbl[index] = old_vftbl[index];
}
void unhook_all()
{
try {
if (old_vftbl != nullptr) {
auto guard = detail::protect_guard{ class_base, sizeof(std::uintptr_t), PAGE_READWRITE };
*(std::uintptr_t**)class_base = old_vftbl;
old_vftbl = nullptr;
}
}
catch (...) {
}
}
template<typename T>
T get_original(int index)
{
return (T)old_vftbl[index];
}
private:
static inline std::size_t estimate_vftbl_length(std::uintptr_t* vftbl_start);
void* class_base;
std::size_t vftbl_len;
std::uintptr_t* new_vftbl;
std::uintptr_t* old_vftbl;
};
inline bool Compare(const uint8_t* data, const uint8_t* pattern, const char* mask) {
for (; *mask; ++mask, ++data, ++pattern)
if (*mask == 'x' && *data != *pattern)
return false;
return (*mask) == 0;
}