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.

138 lines
3.9 KiB

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "imagehlp.h"
  5. #include "restok.h"
  6. //... PROTOTYPES
  7. USHORT ChkSum(
  8. DWORD PartialSum,
  9. PUSHORT Source,
  10. DWORD Length);
  11. static PIMAGE_NT_HEADERS MyRtlImageNtHeader(
  12. PVOID pBaseAddress);
  13. //...........................................................................
  14. DWORD FixCheckSum( LPSTR ImageName)
  15. {
  16. HANDLE FileHandle;
  17. HANDLE MappingHandle;
  18. PIMAGE_NT_HEADERS NtHeaders;
  19. PVOID BaseAddress;
  20. FileHandle = CreateFileA( ImageName,
  21. GENERIC_READ | GENERIC_WRITE,
  22. FILE_SHARE_READ,
  23. NULL,
  24. OPEN_EXISTING,
  25. 0,
  26. NULL);
  27. if ( FileHandle == INVALID_HANDLE_VALUE )
  28. {
  29. QuitA( IDS_ENGERR_01, "image", ImageName);
  30. }
  31. MappingHandle = CreateFileMapping( FileHandle,
  32. NULL,
  33. PAGE_READWRITE,
  34. 0,
  35. 0,
  36. NULL);
  37. if ( MappingHandle == NULL )
  38. {
  39. CloseHandle( FileHandle );
  40. QuitA( IDS_ENGERR_22, ImageName, NULL);
  41. }
  42. else
  43. {
  44. BaseAddress = MapViewOfFile( MappingHandle,
  45. FILE_MAP_READ | FILE_MAP_WRITE,
  46. 0,
  47. 0,
  48. 0);
  49. CloseHandle( MappingHandle );
  50. if ( BaseAddress == NULL )
  51. {
  52. CloseHandle( FileHandle );
  53. QuitA( IDS_ENGERR_23, ImageName, NULL);
  54. }
  55. else
  56. {
  57. DWORD dwFileLength = 0;
  58. //
  59. // Get the length of the file in bytes and compute the checksum.
  60. //
  61. dwFileLength = GetFileSize( FileHandle, NULL );
  62. //
  63. // Obtain a pointer to the header information.
  64. //
  65. NtHeaders = MyRtlImageNtHeader( BaseAddress);
  66. if ( NtHeaders == NULL )
  67. {
  68. CloseHandle( FileHandle );
  69. UnmapViewOfFile( BaseAddress );
  70. QuitA( IDS_ENGERR_17, ImageName, NULL);
  71. }
  72. else
  73. {
  74. DWORD dwHeaderSum = 0;
  75. DWORD dwCheckSum = 0;
  76. DWORD dwOldCheckSum = 0;
  77. //
  78. // Recompute and reset the checksum of the modified file.
  79. //
  80. dwOldCheckSum = NtHeaders->OptionalHeader.CheckSum;
  81. (VOID) MapFileAndCheckSumA( ImageName,
  82. &dwHeaderSum,
  83. &dwCheckSum);
  84. NtHeaders->OptionalHeader.CheckSum = dwCheckSum;
  85. if ( ! FlushViewOfFile( BaseAddress, dwFileLength) )
  86. {
  87. QuitA( IDS_ENGERR_24, ImageName, NULL);
  88. }
  89. if ( NtHeaders->OptionalHeader.CheckSum != dwOldCheckSum )
  90. {
  91. if ( ! TouchFileTimes( FileHandle, NULL) )
  92. {
  93. QuitA( IDS_ENGERR_25, ImageName, NULL);
  94. }
  95. }
  96. UnmapViewOfFile( BaseAddress );
  97. CloseHandle( FileHandle );
  98. }
  99. }
  100. }
  101. return( 0);
  102. }
  103. //.........................................................................
  104. static PIMAGE_NT_HEADERS MyRtlImageNtHeader( PVOID pBaseAddress)
  105. {
  106. IMAGE_DOS_HEADER *pDosHeader = (IMAGE_DOS_HEADER *)pBaseAddress;
  107. return( pDosHeader->e_magic == IMAGE_DOS_SIGNATURE
  108. ? (PIMAGE_NT_HEADERS)(((PBYTE)pBaseAddress) + pDosHeader->e_lfanew)
  109. : NULL);
  110. }