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.

212 lines
4.3 KiB

  1. /*++
  2. Copyright (c) 1995-1997 Microsoft Corporation
  3. Module Name:
  4. enummod.c
  5. Abstract:
  6. This module implements a remote module enumerator.
  7. Author:
  8. Keith Moore (keithmo) 16-Sep-1997
  9. Revision History:
  10. --*/
  11. #include <nt.h>
  12. #include <ntrtl.h>
  13. #include <nturtl.h>
  14. #include <windows.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <wchar.h>
  19. #include <stdlib.h>
  20. #include "inetdbgp.h"
  21. BOOLEAN
  22. EnumModules(
  23. IN HANDLE ExtensionCurrentProcess,
  24. IN PFN_ENUMMODULES EnumProc,
  25. IN PVOID Param
  26. )
  27. /*++
  28. Routine Description:
  29. Enumerates all loaded modules in the debugee.
  30. Arguments:
  31. EnumProc - An enumeration proc that will be invoked for each module.
  32. Param - An uninterpreted parameter passed to the enumeration proc.
  33. Return Value:
  34. BOOLEAN - TRUE if successful, FALSE otherwise.
  35. --*/
  36. {
  37. PROCESS_BASIC_INFORMATION basicInfo;
  38. NTSTATUS status;
  39. PPEB peb;
  40. PPEB_LDR_DATA ldr = NULL;
  41. PLIST_ENTRY ldrHead, ldrNext;
  42. PLDR_DATA_TABLE_ENTRY ldrEntry;
  43. LDR_DATA_TABLE_ENTRY ldrEntryData;
  44. WCHAR tmpName[MAX_PATH];
  45. MODULE_INFO moduleInfo;
  46. //
  47. // Get the process info.
  48. //
  49. status = NtQueryInformationProcess(
  50. ExtensionCurrentProcess,
  51. ProcessBasicInformation,
  52. &basicInfo,
  53. sizeof(basicInfo),
  54. NULL
  55. );
  56. if( !NT_SUCCESS(status) )
  57. {
  58. return FALSE;
  59. }
  60. peb = basicInfo.PebBaseAddress;
  61. if( peb == NULL )
  62. {
  63. return FALSE;
  64. }
  65. //
  66. // ldr = peb->Ldr
  67. //
  68. if( !ReadProcessMemory(
  69. ExtensionCurrentProcess,
  70. (LPCVOID)&peb->Ldr,
  71. &ldr,
  72. sizeof(ldr),
  73. NULL
  74. ) )
  75. {
  76. return FALSE;
  77. }
  78. ldrHead = &ldr->InMemoryOrderModuleList;
  79. //
  80. // ldrNext = ldrHead->Flink;
  81. //
  82. if( !ReadProcessMemory(
  83. ExtensionCurrentProcess,
  84. (LPCVOID)&ldrHead->Flink,
  85. &ldrNext,
  86. sizeof(ldrNext),
  87. NULL
  88. ) )
  89. {
  90. return FALSE;
  91. }
  92. while( ldrNext != ldrHead )
  93. {
  94. //
  95. // Read the LDR_DATA_TABLE_ENTRY structure and the module name.
  96. //
  97. ldrEntry = CONTAINING_RECORD(
  98. ldrNext,
  99. LDR_DATA_TABLE_ENTRY,
  100. InMemoryOrderLinks
  101. );
  102. if( !ReadProcessMemory(
  103. ExtensionCurrentProcess,
  104. (LPCVOID)ldrEntry,
  105. &ldrEntryData,
  106. sizeof(ldrEntryData),
  107. NULL
  108. ) )
  109. {
  110. return FALSE;
  111. }
  112. if( !ReadProcessMemory(
  113. ExtensionCurrentProcess,
  114. (LPCVOID)ldrEntryData.BaseDllName.Buffer,
  115. tmpName,
  116. ldrEntryData.BaseDllName.MaximumLength,
  117. NULL
  118. ) )
  119. {
  120. return FALSE;
  121. }
  122. #pragma prefast(push)
  123. #pragma prefast(disable:69, "Don't complain about using wsprintf being too slow")
  124. // BaseName and tmpName are both MAX_PATH
  125. wsprintfA(
  126. moduleInfo.BaseName,
  127. "%ws",
  128. tmpName
  129. );
  130. if( !ReadProcessMemory(
  131. ExtensionCurrentProcess,
  132. (LPCVOID)ldrEntryData.FullDllName.Buffer,
  133. tmpName,
  134. ldrEntryData.FullDllName.MaximumLength,
  135. NULL
  136. ) )
  137. {
  138. return FALSE;
  139. }
  140. // FullName and tmpName are both MAX_PATH
  141. wsprintfA(
  142. moduleInfo.FullName,
  143. "%ws",
  144. tmpName
  145. );
  146. #pragma prefast(pop)
  147. moduleInfo.DllBase = (ULONG_PTR)ldrEntryData.DllBase;
  148. moduleInfo.EntryPoint = (ULONG_PTR)ldrEntryData.EntryPoint;
  149. moduleInfo.SizeOfImage = (ULONG)ldrEntryData.SizeOfImage;
  150. //
  151. // Invoke the callback.
  152. //
  153. if( !(EnumProc)(
  154. Param,
  155. &moduleInfo
  156. ) ) {
  157. break;
  158. }
  159. ldrNext = ldrEntryData.InMemoryOrderLinks.Flink;
  160. }
  161. return TRUE;
  162. } // EnumModules