/
CDisplayAdapterHelper.cpp
198 lines (167 loc) · 4.8 KB
/
CDisplayAdapterHelper.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
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
#include "stdafx.h"
#include <assert.h>
#include "injectHelper.h"
#include "CDisplayAdapterHelper.h"
#include "Helper.h"
#include "CNameEvent.h"
BOOL CDisplayAdapterHelper::GetProcessAdapterInfoByName(LPSTR lpAppName, BOOL bInjectSafeMode)
{
return InjectSpecificDllAndProcessByNameInner(lpAppName, HOOKDLLNAME, bInjectSafeMode);
}
BOOL CDisplayAdapterHelper::GetProcessAdapterInfoById(DWORD ProcessID, BOOL bInjectSafeMode)
{
return InjectSpecificDllAndProcessByIdInner(ProcessID, HOOKDLLNAME, bInjectSafeMode);
}
BOOL CDisplayAdapterHelper::InjectSpecificDllAndProcessByNameInner(LPSTR lpAppName, LPSTR lpDllName, BOOL bInjectSafeMode)
{
assert(ISNOTNULL(lpAppName));
if (ISNULL(lpAppName))
{
DOLOG("AppName should not to be null !");
return FALSE;
}
DWORD procid = GetSpecificProcIDByNameEx(lpAppName);
if (ISZERO(procid))
{
DOLOG("GetSpecificProcIDByNameEx Failed");
return FALSE;
}
return InjectSpecificDllAndProcessByIdInner(procid, lpDllName, bInjectSafeMode);
}
BOOL CDisplayAdapterHelper::AdjustDllforProcess(HANDLE hProcess, LPSTR lpDllPath)
{
BOOL bIs32Bit = FALSE;
//#ifdef _WIN64
// BOOL myselfBitWide = TRUE;
//#else
// BOOL mySelfBitWide = FALSE;
//#endif
// BOOL bSelfIsWow64 = FALSE;
// IsWow64Process(GetCurrentProcess(), &bSelfIsWow64);
IsWow64Process(hProcess, &bIs32Bit);
if (!bIs32Bit)
{
CHAR tempStrBuf[MAX_PATH] = { 0 };
if (ISTRUE(AdjustDllfor64bit(tempStrBuf, MAX_PATH, lpDllPath)))
{
memcpy(lpDllPath, tempStrBuf, strlen(tempStrBuf) + 1);
}
}
return TRUE;
}
BOOL CDisplayAdapterHelper::AdjustDllfor64bit(CHAR* cbTargetBuf, UINT uSize, LPSTR lpDllPath)
{
if (ISNULL(cbTargetBuf))
{
DOLOG("cbTargetBuf Can't be null");
return FALSE;
}
if (ISNULL(lpDllPath))
{
DOLOG("lpDllpath Can't be null !");
return FALSE;
}
LPSTR lpDotPos = strrchr(lpDllPath, '.');
UINT uPos = lpDotPos - lpDllPath;
return InsertStrInStrSpecifyPosA(cbTargetBuf, uSize, lpDllPath, BIT64, uPos);
}
BOOL CDisplayAdapterHelper::InjectSpecificDllAndProcessByIdInner(DWORD dwProcessID, LPSTR lpDllName, BOOL bInjectSafeMode)
{
#if defined _M_X64 && _MSC_VER == 1800
//workaround AVX2 bug in VS2013, http://connect.microsoft.com/VisualStudio/feedback/details/811093
_set_FMA3_enable(0);
#endif
assert(dwProcessID != 0);
if (ISZERO(dwProcessID))
{
DOLOG("id of target process can't be zero !");
return FALSE;
}
assert(ISNOTNULL(lpDllName));
if(ISNULL(lpDllName))
{
DOLOG("name of dll can't not be null !");
return FALSE;
}
CHAR cbDllPath[MAX_PATH] = { 0 };
//if (ISZERO(GetCurrentDirectoryA(dirLen, cbDllPath)))
if(ISZERO(GetModuleFileNameA(GetModuleHandle(NULL), cbDllPath, MAX_PATH)))
{
DOLOG("GetCurrentDirectory Failed ! " + GetLastError());
return FALSE;
}
CHAR* lpFullPath = strrchr(cbDllPath, '\\');
lpFullPath[1] = '\0';
UINT dirLen = strlen(cbDllPath);
const size_t fileNameLen = strlen(lpDllName);
size_t len = dirLen + fileNameLen + 1;
cbDllPath[dirLen - 1] = '\\';
strncpy_s(cbDllPath + dirLen, len - dirLen, lpDllName, fileNameLen);
BOOL result = TRUE;
LoadSeDebugPrivilege();
CNameEvent nameEvent;
nameEvent.Init(EVENTNAME, TRUE);
CNameShareMemory shareMemInst;
shareMemInst.Init(SHAREMEMNAME, MAX_PATH, TRUE);
char* lpInfo = shareMemInst.GetBuffer();
ZeroMemory(lpInfo, MAX_PATH);
if (!bInjectSafeMode)
{
OPPROC pOpenProcess;
HANDLE hProcess;
char pOPStr[12];
int i;
memcpy(pOPStr, "NpflUvhel{x", 12); //OpenProcess obfuscated
for (i = 0; i<11; i++) pOPStr[i] ^= i ^ 1;
pOpenProcess = (OPPROC)GetProcAddress(GetModuleHandle(TEXT("KERNEL32")), pOPStr);
if (ISNULL(pOpenProcess))
{
DOLOG("GetProcAddress failed ! " + GetLastError());
return FALSE;
}
hProcess = (*pOpenProcess)(PROCESS_ALL_ACCESS, FALSE, dwProcessID);
if (ISNULL(hProcess))
{
DOLOG("OpenProcess Failed ! " + GetLastError());
return FALSE;
}
if (ISFALSE(AdjustDllforProcess(hProcess, cbDllPath)))
{
DOLOG("AdjustDllforProcess Failed ! " + GetLastError());
return FALSE;
}
if (ISNOTTRUE(InjectLibraryA(hProcess, cbDllPath, (DWORD)(len - 1))))
{
result = FALSE;
DOLOG("Inject Library failed ! " + GetLastError());
}
CloseHandle(hProcess);
}
else
{
#ifdef _WIN64
CHAR cbTempDllPath[MAX_PATH] = { 0 };
if (ISTRUE(AdjustDllfor64bit(cbTempDllPath, MAX_PATH, cbDllPath)))
{
memcpy(cbDllPath, cbTempDllPath, strlen(cbTempDllPath) + 1);
}
#endif
if (!InjectLibrarySafeA(dwProcessID, cbDllPath, (DWORD)(len - 1)))
{
DOLOG("InjectLibrarySafeA failed ! "+ GetLastError());
result = FALSE;
}
}
if(ISTRUE(nameEvent.Wait()))
{
lpInfo = shareMemInst.GetBuffer();
if (ISNOTNULL(lpInfo))
{
DOLOG(" µÃµ½µÄÐÅÏ¢ÊÇ>>>>>>" + lpInfo);
strcpy(m_cbVideoAdapterName, lpInfo);
}
}
nameEvent.Close();
shareMemInst.Close();
return result ;
}