SafeDLLPath.pas 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. unit SafeDLLPath;
  2. {
  3. Inno Setup
  4. Copyright (C) 1997-2024 Jordan Russell
  5. Portions by Martijn Laan
  6. For conditions of distribution and use, see LICENSE.TXT.
  7. To provide protection against "DLL preloading" attacks, this unit calls
  8. SetDefaultDllDirectories. SetDefaultDllDirectories is available on Windows 8
  9. and newer, and on previous versions that have the KB2533623 update installed
  10. which was released in July 2011.
  11. It also calls SetSearchPathMode to enable "safe search mode", which causes
  12. SearchPath, and callers of SearchPath such as CreateProcess, to search the
  13. current directory after the system directories (rather than before).
  14. Finally, it calls SetProcessDEPPolicy (where available) to enable DEP for
  15. the lifetime of the process. (This has nothing to do with search paths;
  16. it's just convenient to put the call here.)
  17. This unit should be listed at the top of the program's "uses" clause to
  18. ensure that it runs prior to any LoadLibrary calls that other units might
  19. make during their initialization. (The System unit will always initialize
  20. first, though.)
  21. }
  22. interface
  23. implementation
  24. uses
  25. Windows;
  26. const
  27. LOAD_LIBRARY_SEARCH_SYSTEM32 = $00000800;
  28. BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE = $00000001;
  29. BASE_SEARCH_PATH_PERMANENT = $00008000;
  30. var
  31. KernelModule: HMODULE;
  32. SetDefaultDllDirectoriesFunc: function(DirectoryFlags: DWORD): BOOL; stdcall;
  33. SetSearchPathModeFunc: function(Flags: DWORD): BOOL; stdcall;
  34. SetProcessDEPPolicyFunc: function(dwFlags: DWORD): BOOL; stdcall;
  35. initialization
  36. KernelModule := GetModuleHandle(kernel32);
  37. SetDefaultDllDirectoriesFunc := GetProcAddress(KernelModule, PAnsiChar('SetDefaultDllDirectories'));
  38. if Assigned(SetDefaultDllDirectoriesFunc) then
  39. SetDefaultDllDirectoriesFunc(LOAD_LIBRARY_SEARCH_SYSTEM32);
  40. SetSearchPathModeFunc := GetProcAddress(KernelModule, PAnsiChar('SetSearchPathMode'));
  41. if Assigned(SetSearchPathModeFunc) then
  42. SetSearchPathModeFunc(BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE or
  43. BASE_SEARCH_PATH_PERMANENT);
  44. SetProcessDEPPolicyFunc := GetProcAddress(KernelModule, PAnsiChar('SetProcessDEPPolicy'));
  45. if Assigned(SetProcessDEPPolicyFunc) then
  46. SetProcessDEPPolicyFunc(PROCESS_DEP_ENABLE);
  47. end.