@@ -31,7 +31,7 @@ using tstring = std::basic_string<TCHAR, std::char_traits<TCHAR>, std::allocator
31
31
32
32
// General definitions
33
33
34
- #define HIJACK_VERSION " 1.1.1 "
34
+ #define HIJACK_VERSION " 1.1.2 "
35
35
36
36
#define ProcessDebugFlags static_cast <PROCESSINFOCLASS>(0x1F )
37
37
#define SafeCloseHandle (x ) if ((x) && (x != INVALID_HANDLE_VALUE)) { CloseHandle (x); }
@@ -72,7 +72,7 @@ bool ReLaunchAsAdmin(bool bAllowCancel = false) {
72
72
LPCTSTR szCommandLine = GetCommandLine ();
73
73
LPCTSTR szArguments = _tcsstr (szCommandLine, _T (" " ));
74
74
if (!szArguments) {
75
- _tprintf_s (_T (" ERROR: _tcsstr (Error = 0x%08X) \n " ), GetLastError ( ));
75
+ _tprintf_s (_T (" ERROR: _tcsstr\n " ));
76
76
return false ;
77
77
}
78
78
@@ -168,8 +168,9 @@ tstring GetProcessDirectory(HANDLE hProcess) {
168
168
}
169
169
170
170
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);
173
174
return _T (" " );
174
175
}
175
176
@@ -189,8 +190,9 @@ tstring GetProcessName(HANDLE hProcess) {
189
190
}
190
191
191
192
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);
194
196
return _T (" " );
195
197
}
196
198
@@ -263,8 +265,9 @@ tstring GetFileNameFromHandle(HANDLE hFile) {
263
265
}
264
266
265
267
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);
268
271
return _T (" " );
269
272
}
270
273
@@ -393,8 +396,9 @@ tstring GetProcessHiJackLibraryName(HANDLE hProcess) {
393
396
}
394
397
395
398
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);
398
402
return _T (" " );
399
403
}
400
404
@@ -619,7 +623,7 @@ void OnLoadModuleEvent(DWORD ProcessId, LPVOID ImageBase, HANDLE hFile) {
619
623
620
624
DWORD dwAttrib = GetFileAttributes (ProcessHiJackLibraryPath.c_str ());
621
625
if (!((dwAttrib != INVALID_FILE_ATTRIBUTES) && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY))) {
622
- return ; // Not exist file
626
+ return ; // File not exist
623
627
}
624
628
625
629
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[]) {
1088
1092
1089
1093
TCHAR szKey[MAX_PATH] = {};
1090
1094
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 ());
1092
1096
return EXIT_FAILURE;
1093
1097
}
1094
1098
@@ -1265,6 +1269,86 @@ int _tmain(int argc, PTCHAR argv[], PTCHAR envp[]) {
1265
1269
}
1266
1270
1267
1271
#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
+
1268
1352
if (pTempNTHs->FileHeader .Machine != IMAGE_FILE_MACHINE_AMD64) {
1269
1353
_tprintf_s (_T (" ERROR: This process cannot be run in 64 bit!\n " ));
1270
1354
UnmapViewOfFile (pMap);
@@ -1311,11 +1395,12 @@ int _tmain(int argc, PTCHAR argv[], PTCHAR envp[]) {
1311
1395
1312
1396
TCHAR szDrive[_MAX_DRIVE] = {}, szDir[_MAX_DIR] = {}, szName[_MAX_FNAME] = {}, szExt[_MAX_EXT] = {};
1313
1397
#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));
1315
1399
#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));
1317
1401
#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);
1319
1404
CloseHandle (hJob);
1320
1405
return EXIT_FAILURE;
1321
1406
}
@@ -1328,7 +1413,7 @@ int _tmain(int argc, PTCHAR argv[], PTCHAR envp[]) {
1328
1413
1329
1414
TCHAR szProcessPath[MAX_PATH] = {};
1330
1415
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 () );
1332
1417
CloseHandle (hJob);
1333
1418
return EXIT_FAILURE;
1334
1419
}
0 commit comments