-
Notifications
You must be signed in to change notification settings - Fork 1
/
sig.cpp
148 lines (124 loc) · 3.4 KB
/
sig.cpp
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
#include "sig.h"
#include "module.h"
#include "xorstr.h"
#include "macro.h"
// Forward declarations
extern Module* g_pDx9;
// No Sig constructor as Sig class may be initialized before class Module and m_moduleBase be null. Also, xorstr messes with member variables in a constructor
Sig* g_pDx9Sig = new Sig();
//Sig functions
Sig::Sig()
{
}
Sig::Sig(uintptr_t moduleBase, char* pattern, char* mask, int index, int vSize)
: m_moduleBase(moduleBase), m_pattern(pattern), m_mask(mask), m_index(index), m_vSize(vSize)
{
m_textSize = GetBaseOfData(m_moduleBase);
PBYTE sig = (PBYTE)FindSignature((PBYTE)m_moduleBase, m_textSize, (PCHAR)m_pattern, (PCHAR)m_mask);
memcpy(&m_pSig, (uintptr_t *)(sig + m_index), m_vSize);
}
Sig::~Sig()
{
}
int Sig::InitDx9Sig()
{
// "shaderapidx9.dll" | "8B 77 1C B9 ?? ?? ?? ??"
m_moduleBase = g_pDx9->GetImage();
m_pattern = xorstr("\x8B\x77\x1C\xB9\x00\x00\x00\x00").crypt_get();
m_mask = xorstr("xxxx????").crypt_get();
m_index = 4;
m_vSize = 4;
m_textSize = GetBaseOfData(m_moduleBase);
PBYTE sig = (PBYTE)FindSignature((PBYTE)m_moduleBase, m_textSize, (PCHAR)m_pattern, (PCHAR)m_mask);
memcpy(&m_pSig, (uintptr_t *)(sig + m_index), m_vSize);
return 1;
}
uintptr_t Sig::SigScan(uintptr_t moduleBase, char* pattern, char* mask, int index, int vSize, uintptr_t offset)
{
uintptr_t * pSig;
size_t textSize = GetBaseOfData(moduleBase);
PBYTE sig = (PBYTE)FindSignature((PBYTE)moduleBase, textSize, (PCHAR)pattern, (PCHAR)mask);
memcpy(&pSig, (uintptr_t *)(sig + index), vSize);
if (offset != 0)
{
return ((uintptr_t)pSig - moduleBase + offset);
}
else
{
return ((uintptr_t)pSig - moduleBase);
}
}
uintptr_t Sig::NetvarsScan(uintptr_t moduleBase, char* pattern, char* mask, int index, int vSize, uintptr_t offset)
{
uintptr_t * pSig;
size_t textSize = GetBaseOfData(moduleBase);
PBYTE sig = (PBYTE)FindSignature((PBYTE)moduleBase, textSize, (PCHAR)pattern, (PCHAR)mask);
memcpy(&pSig, (uintptr_t *)(sig + index), vSize);
if (offset != 0)
{
return ((uintptr_t)pSig + offset);
}
else
{
return ((uintptr_t)pSig);
}
}
// Base of Data = size of (Headers + .text)
size_t Sig::GetBaseOfData(uintptr_t pImageBase)
{
if (!pImageBase)
return 0;
PIMAGE_DOS_HEADER DosHeader = (PIMAGE_DOS_HEADER)pImageBase;
PIMAGE_NT_HEADERS Ntheaders = (PIMAGE_NT_HEADERS)((LPBYTE)pImageBase + DosHeader->e_lfanew);
return (size_t)Ntheaders->OptionalHeader.BaseOfData;
}
int Sig::CompareByteArray(PBYTE ByteArray1, PCHAR ByteArray2, PCHAR Mask, size_t Length)
{
size_t nextStart = 0;
char start = ByteArray2[0];
ROLLED;
for (size_t i = 0; i < Length; i++)
{
ROLLED;
if (Mask[i] == '?')
{
continue;
}
ROLLED;
if (ByteArray1[i] == start)
{
nextStart = i;
}
ROLLED;
if (ByteArray1[i] != (BYTE)ByteArray2[i])
{
return nextStart;
}
}
return -1;
}
PBYTE Sig::FindSignature(LPVOID BaseAddress, size_t ImageSize, PCHAR Signature, PCHAR Mask)
{
PBYTE Address = NULL;
PBYTE Buffer = (PBYTE)BaseAddress;
ROLLED;
size_t Length = strlen(Mask);
ROLLED;
for (size_t i = 0; i < (ImageSize - Length); i++)
{
ROLLED;
int result = CompareByteArray((Buffer + i), Signature, Mask, Length);
ROLLED;
if (result < 0)
{
ROLLED;
Address = (PBYTE)BaseAddress + i;
break;
}
else
{
i += result;
}
}
return Address;
}