File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11#pragma once
22#include " Common.hpp"
33
4+ enum class DllValidityError
5+ {
6+ VALID ,
7+ ACCESS_FAILURE ,
8+ TOO_SMALL ,
9+ ALLOCATION_FAILURE ,
10+ NOT_A_DLL ,
11+ INVALID_PLATFORM
12+ };
13+
414namespace Util
515{
616 inline const wchar_t * CharToWchar (const char * a)
@@ -11,4 +21,68 @@ namespace Util
1121
1222 return wchar;
1323 }
24+
25+ inline DllValidityError CheckIfFileIsValidDll (const std::filesystem::path& dllFile)
26+ {
27+ std::ifstream fileStream (dllFile, std::ios::binary | std::ios::ate);
28+
29+ if (fileStream.fail ())
30+ {
31+ fileStream.close ();
32+
33+ return DllValidityError::ACCESS_FAILURE ;
34+ }
35+
36+ const auto fileSize = fileStream.tellg ();
37+ if (fileSize < 0x1000 )
38+ {
39+ fileStream.close ();
40+
41+ return DllValidityError::TOO_SMALL ;
42+ }
43+
44+ auto * pSrcData = new uint8_t [static_cast <uintptr_t >(fileSize)];
45+ if (!pSrcData)
46+ {
47+ fileStream.close ();
48+
49+ return DllValidityError::ALLOCATION_FAILURE ;
50+ }
51+
52+ fileStream.seekg (0 , std::ios::beg);
53+ fileStream.read (reinterpret_cast <char *>(pSrcData), fileSize);
54+ fileStream.close ();
55+
56+ if (reinterpret_cast <IMAGE_DOS_HEADER *>(pSrcData)->e_magic != 0x5A4D )
57+ {
58+ delete[] pSrcData;
59+
60+ return DllValidityError::NOT_A_DLL ;
61+ }
62+
63+ const auto * pOldNtHeader = reinterpret_cast <IMAGE_NT_HEADERS *>(pSrcData + reinterpret_cast <IMAGE_DOS_HEADER *>(pSrcData)->e_lfanew );
64+ const auto * pOldFileHeader = &pOldNtHeader->FileHeader ;
65+
66+ #ifdef _WIN64
67+ if (pOldFileHeader->Machine != IMAGE_FILE_MACHINE_AMD64 )
68+ {
69+ delete[] pSrcData;
70+ delete pOldFileHeader;
71+ delete pOldNtHeader;
72+
73+ return DllValidityError::INVALID_PLATFORM ;
74+ }
75+ #else
76+ if (pOldFileHeader->Machine != IMAGE_FILE_MACHINE_I386 )
77+ {
78+ delete[] pSrcData;
79+ delete pOldFileHeader;
80+ delete pOldNtHeader;
81+
82+ return DllValidityError::INVALID_PLATFORM ;
83+ }
84+ #endif
85+
86+ return DllValidityError::VALID ;
87+ }
1488}
Original file line number Diff line number Diff line change @@ -59,6 +59,34 @@ int main(int argc, const char** argv)
5959 if (!dllFile.is_absolute ())
6060 dllFile = std::filesystem::absolute (dllFile);
6161 LOG (INFO ) << " Starting injection for " << dllFile.filename ().string ();
62+
63+ switch (Util::CheckIfFileIsValidDll (dllFile))
64+ {
65+ case DllValidityError::ACCESS_FAILURE :
66+ LOG (WARNING ) << " Failed to access DLL on disk." ;
67+
68+ return 1 ;
69+ case DllValidityError::TOO_SMALL :
70+ LOG (WARNING ) << " DLL file seems inconceivably small, request to inject ignored." ;
71+
72+ return 1 ;
73+ case DllValidityError::ALLOCATION_FAILURE :
74+ LOG (WARNING ) << " Failed to allocate memory when checking DLL file." ;
75+
76+ return 1 ;
77+ case DllValidityError::NOT_A_DLL :
78+ LOG (WARNING ) << " The file given does not appear to be a valid DLL." ;
79+
80+ return 1 ;
81+ case DllValidityError::INVALID_PLATFORM :
82+ LOG (WARNING ) << " The DLL given did not match the target platform the injector." ;
83+
84+ return 1 ;
85+ case DllValidityError::VALID :
86+ LOG (INFO ) << " DLL seems valid, proceeding with injection." ;
87+
88+ break ;
89+ }
6290
6391 const int processId = Injector::GetProcessId (targetApplication.data ());
6492 if (processId == -1 )
You can’t perform that action at this time.
0 commit comments