Skip to content

Commit ba4c2d5

Browse files
committed
fixed potential bug problem with pe header faking, added proper string checks when dealing with file names
1 parent aef733b commit ba4c2d5

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

GH Injector Library/Error.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#define INJ_ERR_INVALID_PEB_DATA 0x0000002C //internal error : - : peb data required to erase/fake header or unlike the module from the peb wasn't findable
6666
#define INJ_ERR_UPDATE_PROTECTION_FAILED 0x0000002D //NtProtectVirtualMemory : NTSTATUS : updating the page protection of the pe header failed
6767
#define INJ_ERR_WOW64_NTDLL_MISSING 0x0000002E //internal error : - : can't resolve address of the wow64 ntdll
68+
#define INJ_ERR_INVALID_PATH_SEPERATOR 0x0000002F //internal error : - : can't find '\' in a path. '/' as seperators aren't supported.
6869

6970

7071
///////////////////

GH Injector Library/Injection.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,27 @@ DWORD __stdcall InjectW(INJECTIONDATAW * pData)
232232
return InitErrorStruct(szDllPath, pData, -1, INJ_ERR_STRINGC_XXX_FAIL, error_data);
233233
}
234234

235-
wchar_t * pFileName = wcsrchr(pData->szDllPath, '\\') + 1;
236-
memcpy(pFileName, new_name, sizeof(new_name));
235+
wchar_t * pFileName = wcsrchr(pData->szDllPath, '\\');
236+
if (!pFileName)
237+
{
238+
INIT_ERROR_DATA(error_data, (DWORD)hr);
239+
240+
return INJ_ERR_INVALID_PATH_SEPERATOR;
241+
}
242+
else
243+
{
244+
++pFileName;
245+
}
246+
247+
auto size_delta = pFileName - pData->szDllPath;
248+
249+
hr = StringCbCopyW(pFileName, sizeof(pData->szDllPath) - size_delta, new_name);
250+
if (FAILED(hr))
251+
{
252+
INIT_ERROR_DATA(error_data, (DWORD)hr);
253+
254+
return INJ_ERR_STRINGC_XXX_FAIL;
255+
}
237256

238257
auto ren_ret = _wrename(OldFilePath, pData->szDllPath);
239258
if (ren_ret)

GH Injector Library/Manual Mapping WOW64.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,17 @@ DWORD MMAP_WOW64::ManualMap_WOW64(const wchar_t * szDllFile, HANDLE hTargetProc,
3535
return INJ_ERR_STRINGC_XXX_FAIL;
3636
}
3737

38-
const wchar_t * pDllName = wcsrchr(szDllFile, '\\') + 1;
38+
const wchar_t * pDllName = wcsrchr(szDllFile, '\\');
39+
if (!pDllName)
40+
{
41+
INIT_ERROR_DATA(error_data, (DWORD)hr);
42+
43+
return INJ_ERR_INVALID_PATH_SEPERATOR;
44+
}
45+
else
46+
{
47+
++pDllName;
48+
}
3949

4050
hr = StringCbLengthW(pDllName, sizeof(data.szNameBuffer), &len);
4151
if (FAILED(hr))

GH Injector Library/Manual Mapping.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,17 @@ DWORD MMAP_NATIVE::ManualMap(const wchar_t * szDllFile, HANDLE hTargetProc, LAUN
4040
return INJ_ERR_STRINGC_XXX_FAIL;
4141
}
4242

43-
const wchar_t * pDllName = wcsrchr(szDllFile, '\\') + 1;
43+
const wchar_t * pDllName = wcsrchr(szDllFile, '\\');
44+
if (!pDllName)
45+
{
46+
INIT_ERROR_DATA(error_data, (DWORD)hr);
47+
48+
return INJ_ERR_INVALID_PATH_SEPERATOR;
49+
}
50+
else
51+
{
52+
++pDllName;
53+
}
4454

4555
hr = StringCbLengthW(pDllName, sizeof(data.szNameBuffer), &len);
4656
if (FAILED(hr))

0 commit comments

Comments
 (0)