Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.0 KiB

  1. #include "nt.h"
  2. #include "ntrtl.h"
  3. #include "nturtl.h"
  4. #include "windows.h"
  5. #include "imagehlp.h"
  6. #include "winthrow.h"
  7. #include "fusionbuffer.h"
  8. #include "sxshimlib.h"
  9. #include "fusionhandle.h"
  10. #include "sxsmain.h"
  11. #include "lhport.h"
  12. void __stdcall ThrowLastError(DWORD error = ::GetLastError())
  13. {
  14. ::RaiseException(error, 0, 0, NULL);
  15. //throw HRESULT_FROM_WIN32(error);
  16. }
  17. typedef HANDLE (WINAPI * PFN_CREATEFILEW)(
  18. IN LPCWSTR lpFileName,
  19. IN DWORD dwDesiredAccess,
  20. IN DWORD dwShareMode,
  21. IN LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  22. IN DWORD dwCreationDisposition,
  23. IN DWORD dwFlagsAndAttributes,
  24. IN HANDLE hTemplateFile
  25. );
  26. // This is exported and gets the previous value of CreateFileW.
  27. PFN_CREATEFILEW My2OriginalCreateFileW;
  28. //extern "C" {
  29. HANDLE
  30. WINAPI
  31. My2CreateFileW(
  32. IN LPCWSTR lpFileName,
  33. IN DWORD dwDesiredAccess,
  34. IN DWORD dwShareMode,
  35. IN LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  36. IN DWORD dwCreationDisposition,
  37. IN DWORD dwFlagsAndAttributes,
  38. IN HANDLE hTemplateFile
  39. )
  40. {
  41. printf("%s\n", __FUNCTION__);
  42. if (My2OriginalCreateFileW == NULL)
  43. {
  44. ::SetLastError(ERROR_INTERNAL_ERROR);
  45. return INVALID_HANDLE_VALUE;
  46. }
  47. return My2OriginalCreateFileW(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
  48. }
  49. // This is exported and gets the previous value of CreateFileW.
  50. PFN_CREATEFILEW MyOriginalCreateFileW;
  51. HANDLE
  52. WINAPI
  53. MyCreateFileW(
  54. IN LPCWSTR lpFileName,
  55. IN DWORD dwDesiredAccess,
  56. IN DWORD dwShareMode,
  57. IN LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  58. IN DWORD dwCreationDisposition,
  59. IN DWORD dwFlagsAndAttributes,
  60. IN HANDLE hTemplateFile
  61. )
  62. {
  63. printf("%s\n", __FUNCTION__);
  64. if (MyOriginalCreateFileW == NULL)
  65. {
  66. ::SetLastError(ERROR_INTERNAL_ERROR);
  67. return INVALID_HANDLE_VALUE;
  68. }
  69. return MyOriginalCreateFileW(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
  70. }
  71. //}
  72. int ShimExe(int argc, wchar_t** argv)
  73. {
  74. HMODULE ExeHandle = ::GetModuleHandleW(NULL);
  75. if (!SxspDllMainOnDemandProcessAttachCalledByExports(ExeHandle, DLL_PROCESS_ATTACH, NULL))
  76. ThrowLastError();
  77. SXPE_APPLY_SHIMS_IN ShimIn = {sizeof(ShimIn)};
  78. SXPE_APPLY_SHIMS_OUT ShimOut = {sizeof(ShimOut)};
  79. ShimIn.DllToRedirectFrom = ExeHandle;
  80. ShimIn.DllToRedirectTo.DllHandle = ExeHandle;
  81. ::CreateFileW(0, 0, 0, 0, 0, 0, 0);
  82. ::MyCreateFileW(0, 0, 0, 0, 0, 0, 0);
  83. ::My2CreateFileW(0, 0, 0, 0, 0, 0, 0);
  84. SxPepApplyShims(&ShimIn, &ShimOut);
  85. ::CreateFileW(0, 0, 0, 0, 0, 0, 0);
  86. ShimIn.Prefix = "My2";
  87. SxPepApplyShims(&ShimIn, &ShimOut);
  88. ::CreateFileW(0, 0, 0, 0, 0, 0, 0);
  89. //SxPepRevokeShims(ExeHandle);
  90. //::CreateFileW(0, 0, 0, 0, 0, 0, 0);
  91. return 0;
  92. }