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.

220 lines
5.8 KiB

  1. #include <windows.h>
  2. #include "view.h"
  3. #include "except.h"
  4. #include "thread.h"
  5. #include "dump.h"
  6. #include "memory.h"
  7. #include "profiler.h"
  8. #include "filter.h"
  9. static CRITICAL_SECTION filterCritSec;
  10. static PMODULEFILTER pFilterHead = 0;
  11. static PMODULEFILTER pLBFilterHead = 0;
  12. static char *pszModules[] = {"ntdll.dll",
  13. "kernel32.dll",
  14. "gdi32.dll",
  15. "user32.dll",
  16. "shell32.dll",
  17. "shlwapi.dll",
  18. "msvcrt.dll",
  19. "msvcirt.dll",
  20. "advapi32.dll",
  21. "ddraw.dll",
  22. "dsound.dll",
  23. "ole32.dll",
  24. "rpcrt4.dll",
  25. "oleaut32.dll",
  26. "winmm.dll",
  27. "comctl32.dll",
  28. "comdlg32.dll",
  29. "riched20.dll",
  30. "dinput.dll",
  31. "wdmaud.drv",
  32. NAME_OF_DLL_TO_INJECT};
  33. BOOL
  34. InitializeFilterList(VOID)
  35. {
  36. HMODULE hTemp;
  37. PIMAGE_NT_HEADERS pHeaders;
  38. DWORD dwModStart;
  39. DWORD dwModEnd;
  40. DWORD dwCounter;
  41. DWORD dwCount;
  42. BOOL bResult;
  43. InitializeCriticalSection(&filterCritSec);
  44. dwCount = sizeof(pszModules) / sizeof(char *);
  45. for (dwCounter = 0; dwCounter < dwCount; dwCounter++) {
  46. //
  47. // Build the filter list
  48. //
  49. hTemp = GetModuleHandleA(pszModules[dwCounter]);
  50. if (0 == hTemp) {
  51. bResult = AddModuleToFilterList(pszModules[dwCounter],
  52. 0,
  53. 0,
  54. TRUE);
  55. if (FALSE == bResult) {
  56. return FALSE;
  57. }
  58. }
  59. else {
  60. //
  61. // Dig out the PE information
  62. //
  63. pHeaders = ImageNtHeader2((PVOID)hTemp);
  64. dwModStart = (DWORD)hTemp;
  65. dwModEnd = dwModStart + pHeaders->OptionalHeader.SizeOfImage;
  66. bResult = AddModuleToFilterList(pszModules[dwCounter],
  67. dwModStart,
  68. dwModEnd,
  69. FALSE);
  70. if (FALSE == bResult) {
  71. return FALSE;
  72. }
  73. }
  74. }
  75. return TRUE;
  76. }
  77. BOOL
  78. AddModuleToFilterList(CHAR *pszModuleName,
  79. DWORD dwStartAddress,
  80. DWORD dwEndAddress,
  81. BOOL bLateBound)
  82. {
  83. PMODULEFILTER pModuleFilter;
  84. //
  85. // Allocate entry
  86. //
  87. pModuleFilter = AllocMem(sizeof(MODULEFILTER));
  88. if (0 == pModuleFilter) {
  89. return FALSE;
  90. }
  91. if (pszModuleName) {
  92. strcpy(pModuleFilter->szModuleName, pszModuleName);
  93. }
  94. pModuleFilter->dwModuleStart = dwStartAddress;
  95. pModuleFilter->dwModuleEnd = dwEndAddress;
  96. pModuleFilter->pNextFilter = 0;
  97. EnterCriticalSection(&filterCritSec);
  98. if (FALSE == bLateBound) {
  99. //
  100. // Add DLL to the normal filter list
  101. //
  102. if (0 == pFilterHead) {
  103. pFilterHead = pModuleFilter;
  104. }
  105. else {
  106. pModuleFilter->pNextFilter = pFilterHead;
  107. pFilterHead = pModuleFilter;
  108. }
  109. }
  110. else {
  111. //
  112. // Add DLL to the late bound list
  113. //
  114. if (0 == pLBFilterHead) {
  115. pLBFilterHead = pModuleFilter;
  116. }
  117. else {
  118. pModuleFilter->pNextFilter = pLBFilterHead;
  119. pLBFilterHead = pModuleFilter;
  120. }
  121. }
  122. LeaveCriticalSection(&filterCritSec);
  123. return TRUE;
  124. }
  125. BOOL
  126. IsAddressFiltered(DWORD dwAddress)
  127. {
  128. PMODULEFILTER pModuleFilter;
  129. EnterCriticalSection(&filterCritSec);
  130. //
  131. // Walk both lists and see if we have an address to filter
  132. //
  133. pModuleFilter = pFilterHead;
  134. while (pModuleFilter) {
  135. if ((dwAddress >= pModuleFilter->dwModuleStart) &&
  136. (dwAddress <= pModuleFilter->dwModuleEnd)) {
  137. LeaveCriticalSection(&filterCritSec);
  138. return TRUE;
  139. }
  140. pModuleFilter = pModuleFilter->pNextFilter;
  141. }
  142. pModuleFilter = pLBFilterHead;
  143. while (pModuleFilter) {
  144. if ((dwAddress >= pModuleFilter->dwModuleStart) &&
  145. (dwAddress <= pModuleFilter->dwModuleEnd)) {
  146. LeaveCriticalSection(&filterCritSec);
  147. return TRUE;
  148. }
  149. pModuleFilter = pModuleFilter->pNextFilter;
  150. }
  151. LeaveCriticalSection(&filterCritSec);
  152. return FALSE;
  153. }
  154. VOID
  155. RefreshFilterList(VOID)
  156. {
  157. PMODULEFILTER pModuleFilter;
  158. HMODULE hTemp;
  159. PIMAGE_NT_HEADERS pHeaders;
  160. DWORD dwModStart;
  161. DWORD dwModEnd;
  162. //
  163. // Walk the LB list and refresh the start and end module addresses
  164. //
  165. EnterCriticalSection(&filterCritSec);
  166. pModuleFilter = pLBFilterHead;
  167. while (pModuleFilter) {
  168. //
  169. // Grab the module base address
  170. //
  171. hTemp = GetModuleHandleA(pModuleFilter->szModuleName);
  172. if (hTemp) {
  173. //
  174. // This module is loaded - do refresh
  175. //
  176. pHeaders = ImageNtHeader2((PVOID)hTemp);
  177. dwModStart = (DWORD)hTemp;
  178. dwModEnd = dwModStart + pHeaders->OptionalHeader.SizeOfImage;
  179. pModuleFilter->dwModuleStart = dwModStart;
  180. pModuleFilter->dwModuleEnd = dwModEnd;
  181. }
  182. pModuleFilter = pModuleFilter->pNextFilter;
  183. }
  184. LeaveCriticalSection(&filterCritSec);
  185. }