-//
-// This wrapper around loadlibrary appends the system folder (usually c:\windows\system32)
-// to the relative path of the DLL, so that the DLL is always loaded from an absolute path
-// (It's no longer possible to load airpcap.dll from the application folder).
-// This solves the DLL Hijacking issue discovered in August 2010
-// http://blog.metasploit.com/2010/08/exploiting-dll-hijacking-flaws.html
-//
-HMODULE LoadLibrarySafe(LPCTSTR lpFileName)
-{
- TCHAR path[MAX_PATH];
- TCHAR fullFileName[MAX_PATH];
- UINT res;
- HMODULE hModule = NULL;
- do
- {
- res = GetSystemDirectory(path, MAX_PATH);
-
- if (res == 0)
- {
- //
- // some bad failure occurred;
- //
- break;
- }
-
- if (res > MAX_PATH)
- {
- //
- // the buffer was not big enough
- //
- SetLastError(ERROR_INSUFFICIENT_BUFFER);
- break;
- }
-
- if (res + 1 + _tcslen(lpFileName) + 1 < MAX_PATH)
- {
- memcpy(fullFileName, path, res * sizeof(TCHAR));
- fullFileName[res] = _T('\\');
- memcpy(&fullFileName[res + 1], lpFileName, (_tcslen(lpFileName) + 1) * sizeof(TCHAR));
-
- hModule = LoadLibrary(fullFileName);
- }
- else
- {
- SetLastError(ERROR_INSUFFICIENT_BUFFER);
- }
-
- }while(FALSE);
-
- return hModule;
-}
-