Skip to content

Commit 4ef161d

Browse files
committed
Fixes
1 parent 566398b commit 4ef161d

File tree

3 files changed

+104
-18
lines changed

3 files changed

+104
-18
lines changed

HiJack/HiJack.cpp

+101-16
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ using tstring = std::basic_string<TCHAR, std::char_traits<TCHAR>, std::allocator
3131

3232
// General definitions
3333

34-
#define HIJACK_VERSION "1.1.1"
34+
#define HIJACK_VERSION "1.1.2"
3535

3636
#define ProcessDebugFlags static_cast<PROCESSINFOCLASS>(0x1F)
3737
#define SafeCloseHandle(x) if ((x) && (x != INVALID_HANDLE_VALUE)) { CloseHandle(x); }
@@ -72,7 +72,7 @@ bool ReLaunchAsAdmin(bool bAllowCancel = false) {
7272
LPCTSTR szCommandLine = GetCommandLine();
7373
LPCTSTR szArguments = _tcsstr(szCommandLine, _T(" "));
7474
if (!szArguments) {
75-
_tprintf_s(_T("ERROR: _tcsstr (Error = 0x%08X)\n"), GetLastError());
75+
_tprintf_s(_T("ERROR: _tcsstr\n"));
7676
return false;
7777
}
7878

@@ -168,8 +168,9 @@ tstring GetProcessDirectory(HANDLE hProcess) {
168168
}
169169

170170
TCHAR szDrive[_MAX_DRIVE] = {}, szDir[_MAX_DIR] = {};
171-
if (_tsplitpath_s(ProcessPath.c_str(), szDrive, _countof(szDrive), szDir, _countof(szDir), nullptr, 0, nullptr, 0) != 0) {
172-
_tprintf_s(_T("ERROR: _tsplitpath_s (Error = 0x%08X)\n"), GetLastError());
171+
errno_t err = _tsplitpath_s(ProcessPath.c_str(), szDrive, _countof(szDrive), szDir, _countof(szDir), nullptr, 0, nullptr, 0);
172+
if (err != 0) {
173+
_tprintf_s(_T("ERROR: _tsplitpath_s (Error = %i)\n"), err);
173174
return _T("");
174175
}
175176

@@ -189,8 +190,9 @@ tstring GetProcessName(HANDLE hProcess) {
189190
}
190191

191192
TCHAR szName[_MAX_FNAME] = {}, szExt[_MAX_EXT] = {};
192-
if (_tsplitpath_s(ProcessPath.c_str(), nullptr, 0, nullptr, 0, szName, _countof(szName), szExt, _countof(szExt)) != 0) {
193-
_tprintf_s(_T("ERROR: _tsplitpath_s (Error = 0x%08X)\n"), GetLastError());
193+
errno_t err = _tsplitpath_s(ProcessPath.c_str(), nullptr, 0, nullptr, 0, szName, _countof(szName), szExt, _countof(szExt));
194+
if (err != 0) {
195+
_tprintf_s(_T("ERROR: _tsplitpath_s (Error = %i)\n"), err);
194196
return _T("");
195197
}
196198

@@ -263,8 +265,9 @@ tstring GetFileNameFromHandle(HANDLE hFile) {
263265
}
264266

265267
TCHAR szName[_MAX_FNAME] = {}, szExt[_MAX_EXT] = {};
266-
if (_tsplitpath_s(szFileName, nullptr, 0, nullptr, 0, szName, _countof(szName), szExt, _countof(szExt)) != 0) {
267-
_tprintf_s(_T("ERROR: _tsplitpath_s (Error = 0x%08X)\n"), GetLastError());
268+
errno_t err = _tsplitpath_s(szFileName, nullptr, 0, nullptr, 0, szName, _countof(szName), szExt, _countof(szExt));
269+
if (err != 0) {
270+
_tprintf_s(_T("ERROR: _tsplitpath_s (Error = %i)\n"), err);
268271
return _T("");
269272
}
270273

@@ -393,8 +396,9 @@ tstring GetProcessHiJackLibraryName(HANDLE hProcess) {
393396
}
394397

395398
TCHAR szName[_MAX_FNAME] = {};
396-
if (_tsplitpath_s(ProcessName.c_str(), nullptr, 0, nullptr, 0, szName, _countof(szName), nullptr, 0) != 0) {
397-
_tprintf_s(_T("ERROR: _tsplitpath_s (Error = 0x%08X)\n"), GetLastError());
399+
errno_t err = _tsplitpath_s(ProcessName.c_str(), nullptr, 0, nullptr, 0, szName, _countof(szName), nullptr, 0);
400+
if (err != 0) {
401+
_tprintf_s(_T("ERROR: _tsplitpath_s (Error = %i)\n"), err);
398402
return _T("");
399403
}
400404

@@ -619,7 +623,7 @@ void OnLoadModuleEvent(DWORD ProcessId, LPVOID ImageBase, HANDLE hFile) {
619623

620624
DWORD dwAttrib = GetFileAttributes(ProcessHiJackLibraryPath.c_str());
621625
if (!((dwAttrib != INVALID_FILE_ATTRIBUTES) && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY))) {
622-
return; // Not exist file
626+
return; // File not exist
623627
}
624628

625629
HANDLE hProcessFile = CreateFile(ProcessHiJackLibraryPath.c_str(), GENERIC_READ, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
@@ -1088,7 +1092,7 @@ int _tmain(int argc, PTCHAR argv[], PTCHAR envp[]) {
10881092

10891093
TCHAR szKey[MAX_PATH] = {};
10901094
if (_stprintf_s(szKey, _countof(szKey), _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\%s"), argv[2]) < 0) {
1091-
_tprintf_s(_T("ERROR: RegCreateKeyEx (Error = 0x%08X)\n"), GetLastError());
1095+
_tprintf_s(_T("ERROR: _stprintf_s (Error = 0x%08X)\n"), GetLastError());
10921096
return EXIT_FAILURE;
10931097
}
10941098

@@ -1265,6 +1269,86 @@ int _tmain(int argc, PTCHAR argv[], PTCHAR envp[]) {
12651269
}
12661270

12671271
#ifdef _WIN64
1272+
if (pTempNTHs->FileHeader.Machine == IMAGE_FILE_MACHINE_I386) {
1273+
UnmapViewOfFile(pMap);
1274+
CloseHandle(hMapFile);
1275+
CloseHandle(hProcessFile);
1276+
1277+
PWSTR szSelfProcessPath = NtCurrentTeb()->ProcessEnvironmentBlock->ProcessParameters->ImagePathName.Buffer;
1278+
if (!szSelfProcessPath) {
1279+
_tprintf_s(_T("ERROR: PEB\n"));
1280+
CloseHandle(hJob);
1281+
return EXIT_FAILURE;
1282+
}
1283+
1284+
#ifndef _UNICODE
1285+
UNICODE_STRING us = {};
1286+
RtlInitUnicodeString(&us, szSelfProcessPath);
1287+
1288+
ANSI_STRING as = {};
1289+
NTSTATUS nStatus = RtlUnicodeStringToAnsiString(&as, &us, TRUE);
1290+
if (!NT_SUCCESS(nStatus)) {
1291+
_tprintf_s(_T("ERROR: RtlUnicodeStringToAnsiString (Error = 0x%08X)\n"), nStatus);
1292+
CloseHandle(hJob);
1293+
return EXIT_FAILURE;
1294+
}
1295+
#endif // !_UNICODE
1296+
1297+
TCHAR szDrive[_MAX_DRIVE] = {}, szDir[_MAX_DIR] = {}, szName[_MAX_FNAME] = {}, szExt[_MAX_EXT] = {};
1298+
#ifdef _UNICODE
1299+
errno_t err = _tsplitpath_s(szSelfProcessPath, szDrive, _countof(szDrive), szDir, _countof(szDir), szName, _countof(szName), szExt, _countof(szExt));
1300+
#else
1301+
errno_t err = _tsplitpath_s(as.Buffer, szDrive, _countof(szDrive), szDir, _countof(szDir), szName, _countof(szName), szExt, _countof(szExt));
1302+
#endif
1303+
if (err != 0) {
1304+
_tprintf_s(_T("ERROR: _tsplitpath_s (Error = %i)\n"), err);
1305+
CloseHandle(hJob);
1306+
return EXIT_FAILURE;
1307+
}
1308+
1309+
TCHAR szProcessPath[MAX_PATH] = {};
1310+
if (_stprintf_s(szProcessPath, _countof(szProcessPath), _T("%s%s%s32%s"), szDrive, szDir, szName, szExt) < 0) {
1311+
_tprintf_s(_T("ERROR: _stprintf_s (Error = 0x%08X)\n"), GetLastError());
1312+
CloseHandle(hJob);
1313+
return EXIT_FAILURE;
1314+
}
1315+
1316+
DWORD dwAttrib = GetFileAttributes(szProcessPath);
1317+
if (!((dwAttrib != INVALID_FILE_ATTRIBUTES) && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY))) {
1318+
_tprintf_s(_T("ERROR: This process cannot be run in 32 bit!\n"));
1319+
CloseHandle(hJob);
1320+
return EXIT_FAILURE;
1321+
}
1322+
1323+
STARTUPINFO si = {};
1324+
PROCESS_INFORMATION pi = {};
1325+
si.cb = sizeof(si);
1326+
1327+
if (!CreateProcess(szProcessPath, GetCommandLine(), nullptr, nullptr, TRUE, 0, nullptr, nullptr, &si, &pi)) {
1328+
_tprintf_s(_T("ERROR: Failed to launch 64-bit version (Error = 0x%08X)\n"), GetLastError());
1329+
CloseHandle(hJob);
1330+
return EXIT_FAILURE;
1331+
}
1332+
1333+
if (WaitForSingleObject(pi.hProcess, INFINITE) != WAIT_OBJECT_0) {
1334+
_tprintf_s(_T("ERROR: WaitForSingleObject (Error = 0x%08X)\n"), GetLastError());
1335+
TerminateProcess(pi.hProcess, EXIT_FAILURE);
1336+
CloseHandles(pi);
1337+
CloseHandle(hJob);
1338+
return EXIT_FAILURE;
1339+
}
1340+
1341+
DWORD unExitCode = EXIT_FAILURE;
1342+
if (!GetExitCodeProcess(pi.hProcess, &unExitCode)) {
1343+
_tprintf_s(_T("ERROR: GetExitCodeProcess (Error = 0x%08X)\n"), GetLastError());
1344+
CloseHandles(pi);
1345+
CloseHandle(hJob);
1346+
return EXIT_FAILURE;
1347+
}
1348+
1349+
return unExitCode;
1350+
}
1351+
12681352
if (pTempNTHs->FileHeader.Machine != IMAGE_FILE_MACHINE_AMD64) {
12691353
_tprintf_s(_T("ERROR: This process cannot be run in 64 bit!\n"));
12701354
UnmapViewOfFile(pMap);
@@ -1311,11 +1395,12 @@ int _tmain(int argc, PTCHAR argv[], PTCHAR envp[]) {
13111395

13121396
TCHAR szDrive[_MAX_DRIVE] = {}, szDir[_MAX_DIR] = {}, szName[_MAX_FNAME] = {}, szExt[_MAX_EXT] = {};
13131397
#ifdef _UNICODE
1314-
if (_tsplitpath_s(ProcessPath.c_str(), szDrive, _countof(szDrive), szDir, _countof(szDir), szName, _countof(szName), szExt, _countof(szExt)) != 0) {
1398+
errno_t err = _tsplitpath_s(szSelfProcessPath, szDrive, _countof(szDrive), szDir, _countof(szDir), szName, _countof(szName), szExt, _countof(szExt));
13151399
#else
1316-
if (_tsplitpath_s(as.Buffer, szDrive, _countof(szDrive), szDir, _countof(szDir), szName, _countof(szName), szExt, _countof(szExt)) != 0) {
1400+
errno_t err = _tsplitpath_s(as.Buffer, szDrive, _countof(szDrive), szDir, _countof(szDir), szName, _countof(szName), szExt, _countof(szExt));
13171401
#endif
1318-
_tprintf_s(_T("ERROR: _tsplitpath_s (Error = 0x%08X)\n"), nStatus);
1402+
if (err != 0) {
1403+
_tprintf_s(_T("ERROR: _tsplitpath_s (Error = %i)\n"), err);
13191404
CloseHandle(hJob);
13201405
return EXIT_FAILURE;
13211406
}
@@ -1328,7 +1413,7 @@ int _tmain(int argc, PTCHAR argv[], PTCHAR envp[]) {
13281413

13291414
TCHAR szProcessPath[MAX_PATH] = {};
13301415
if (_stprintf_s(szProcessPath, _countof(szProcessPath), _T("%s%s%s%s"), szDrive, szDir, szName, szExt) < 0) {
1331-
_tprintf_s(_T("ERROR: _stprintf_s (Error = 0x%08X)\n"), nStatus);
1416+
_tprintf_s(_T("ERROR: _stprintf_s (Error = 0x%08X)\n"), GetLastError());
13321417
CloseHandle(hJob);
13331418
return EXIT_FAILURE;
13341419
}

HiJack/HiJack.vcxproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@
3030
<ConfigurationType>Application</ConfigurationType>
3131
<UseDebugLibraries>true</UseDebugLibraries>
3232
<PlatformToolset>v143</PlatformToolset>
33-
<CharacterSet>MultiByte</CharacterSet>
33+
<CharacterSet>Unicode</CharacterSet>
3434
</PropertyGroup>
3535
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
3636
<ConfigurationType>Application</ConfigurationType>
3737
<UseDebugLibraries>false</UseDebugLibraries>
3838
<PlatformToolset>v143</PlatformToolset>
3939
<WholeProgramOptimization>true</WholeProgramOptimization>
40-
<CharacterSet>MultiByte</CharacterSet>
40+
<CharacterSet>Unicode</CharacterSet>
4141
</PropertyGroup>
4242
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
4343
<ConfigurationType>Application</ConfigurationType>

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ This will enable HiJack to intercept the process creation of `hello.exe` and inj
2020
- Use the 32-bit version for 32-bit processes.
2121
- Use the 64-bit version for 64-bit processes.
2222
* The 32-bit version of HiJack can utilize the 64-bit version if both executables are placed in the same directory.
23+
* The 64-bit version of HiJack can utilize the 32-bit version if both executables are placed in the same directory.

0 commit comments

Comments
 (0)