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.

215 lines
3.9 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. hash.c
  5. Abstract:
  6. This module compiles into a program hash.exe
  7. that is used to compute the import table hash.
  8. This program should be in the path for the perl
  9. tool webblade.pl to work.
  10. Author:
  11. Vishnu Patankar (VishnuP) 04-June-2001
  12. Revision History:
  13. --*/
  14. #include <nt.h>
  15. #include <ntrtl.h>
  16. #include <nturtl.h>
  17. #include <windows.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. VOID
  21. PrintHash(
  22. BYTE *Hash
  23. );
  24. VOID PrintNibble(
  25. BYTE OneByte
  26. );
  27. VOID
  28. PrintError(
  29. DWORD rc
  30. );
  31. #define WEB_BLADE_MAX_HASH_SIZE 16
  32. VOID __cdecl
  33. main(int argc, char *argv[])
  34. {
  35. char * lpFileName;
  36. HANDLE hFile;
  37. SECURITY_ATTRIBUTES sa;
  38. BYTE Hash[WEB_BLADE_MAX_HASH_SIZE];
  39. NTSTATUS Status = STATUS_SUCCESS;
  40. //
  41. // Get the name of the passed .exe
  42. //
  43. if (argc != 2) {
  44. printf("Usage: hash <filename>\n");
  45. exit(0);
  46. }
  47. lpFileName = argv[1];
  48. //
  49. // Map the image of the exe
  50. //
  51. sa.nLength = sizeof( SECURITY_ATTRIBUTES );
  52. sa.lpSecurityDescriptor = NULL;
  53. sa.bInheritHandle = FALSE;
  54. hFile = CreateFile(
  55. lpFileName,
  56. GENERIC_READ,
  57. 0,
  58. &sa,
  59. OPEN_EXISTING,
  60. FILE_ATTRIBUTE_NORMAL,
  61. NULL
  62. );
  63. if (hFile == INVALID_HANDLE_VALUE) {
  64. PrintError(GetLastError());
  65. return;
  66. }
  67. #define ITH_REVISION_1 1
  68. Status = RtlComputeImportTableHash( hFile, Hash, ITH_REVISION_1 );
  69. if (NT_SUCCESS(Status)) {
  70. wprintf(L"Hashing Succeeded:");
  71. PrintHash( Hash );
  72. }
  73. else {
  74. PrintError(RtlNtStatusToDosError(Status));
  75. }
  76. CloseHandle(hFile);
  77. return;
  78. }
  79. VOID
  80. PrintHash(
  81. BYTE *Hash
  82. )
  83. {
  84. DWORD dwIndex;
  85. if (Hash == NULL) {
  86. return;
  87. }
  88. for (dwIndex=0; dwIndex < WEB_BLADE_MAX_HASH_SIZE; dwIndex++ ) {
  89. PrintNibble( (Hash[dwIndex] & 0xF0) >> 4 );
  90. PrintNibble( Hash[dwIndex] & 0xF );
  91. }
  92. printf("\n");
  93. }
  94. VOID PrintNibble(
  95. BYTE OneByte
  96. )
  97. {
  98. DWORD dwIndex;
  99. switch (OneByte) {
  100. case 0x0:
  101. printf("0");
  102. break;
  103. case 0x1:
  104. printf("1");
  105. break;
  106. case 0x2:
  107. printf("2");
  108. break;
  109. case 0x3:
  110. printf("3");
  111. break;
  112. case 0x4:
  113. printf("4");
  114. break;
  115. case 0x5:
  116. printf("5");
  117. break;
  118. case 0x6:
  119. printf("6");
  120. break;
  121. case 0x7:
  122. printf("7");
  123. break;
  124. case 0x8:
  125. printf("8");
  126. break;
  127. case 0x9:
  128. printf("9");
  129. break;
  130. case 0xa:
  131. printf("A");
  132. break;
  133. case 0xb:
  134. printf("B");
  135. break;
  136. case 0xc:
  137. printf("C");
  138. break;
  139. case 0xd:
  140. printf("D");
  141. break;
  142. case 0xe:
  143. printf("E");
  144. break;
  145. case 0xf:
  146. printf("F");
  147. break;
  148. default:
  149. break;
  150. }
  151. }
  152. VOID
  153. PrintError(
  154. DWORD rc
  155. )
  156. {
  157. LPVOID lpMsgBuf=NULL;
  158. wprintf(L"Hashing Failed:");
  159. FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  160. NULL,
  161. rc,
  162. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  163. (LPTSTR)&lpMsgBuf,
  164. 0,
  165. NULL
  166. );
  167. if (lpMsgBuf) {
  168. wprintf(L"%s", lpMsgBuf);
  169. LocalFree(lpMsgBuf);
  170. }
  171. }