nt_pviewer.cpp
上传用户:jinandeyu
上传日期:2007-01-05
资源大小:620k
文件大小:13k
源码类别:

远程控制编程

开发平台:

WINDOWS

  1. /*  Back Orifice 2000 - Remote Administration Suite
  2.     Copyright (C) 1999, Cult Of The Dead Cow
  3.     This program is free software; you can redistribute it and/or modify
  4.     it under the terms of the GNU General Public License as published by
  5.     the Free Software Foundation; either version 2 of the License, or
  6.     (at your option) any later version.
  7.     This program is distributed in the hope that it will be useful,
  8.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.     GNU General Public License for more details.
  11.     You should have received a copy of the GNU General Public License
  12.     along with this program; if not, write to the Free Software
  13.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  14. The author of this program may be contacted at dildog@l0pht.com. */
  15. #include <windows.h> 
  16. #include <winperf.h> 
  17. #include <perfdata.h> 
  18. #include <pviewdat.h>
  19. #include <nt_pviewer.h> 
  20. #include <pviewer.h>
  21. #include <string.h> 
  22. #include <stdio.h> 
  23. #define INDEX_STR_LEN       10 
  24. #define MACHINE_NAME_LEN    MAX_COMPUTERNAME_LENGTH+2 
  25. #define MACHINE_NAME_SIZE   MACHINE_NAME_LEN+1 
  26. // Globals 
  27. TCHAR           INDEX_PROCTHRD_OBJ[2*INDEX_STR_LEN]; 
  28. TCHAR           INDEX_COSTLY_OBJ[3*INDEX_STR_LEN]; 
  29. TCHAR           gszMachineName[MACHINE_NAME_SIZE]; 
  30. TCHAR           gszCurrentMachine[MACHINE_NAME_SIZE]; 
  31. DWORD           gPerfDataSize = 50*1024;            // start with 50K 
  32. PPERF_DATA      gpPerfData; 
  33. HKEY            ghPerfKey = HKEY_PERFORMANCE_DATA;  // get perf data from this key 
  34. HKEY            ghMachineKey = HKEY_LOCAL_MACHINE;  // get title index from this key 
  35. //  GetTitleIdx() - Searches Titles[] for Name.  Returns the index found. 
  36. DWORD GetTitleIdx(LPTSTR Title[], DWORD LastIndex, LPTSTR Name) 
  37. DWORD   Index; 
  38. for (Index = 0; Index <= LastIndex; Index++) 
  39. if (Title[Index]) 
  40. if (!lstrcmpi (Title[Index], Name)) 
  41. return Index; 
  42. return 0; 
  43. //  SetPerfIndexes() - Setup the perf data indexes. 
  44. int SetPerfIndexes(void) 
  45. LPTSTR  TitleBuffer; 
  46. LPTSTR  *Title; 
  47. DWORD   Last; 
  48. if(GetPerfTitleSz (ghMachineKey, ghPerfKey, &TitleBuffer, &Title, &Last)!=ERROR_SUCCESS) return -1;
  49. PX_PROCESS                       = GetTitleIdx (Title, Last, PN_PROCESS); 
  50. PX_PROCESS_CPU                   = GetTitleIdx (Title, Last, PN_PROCESS_CPU); 
  51. PX_PROCESS_PRIV                  = GetTitleIdx (Title, Last, PN_PROCESS_PRIV); 
  52. PX_PROCESS_USER                  = GetTitleIdx (Title, Last, PN_PROCESS_USER); 
  53. PX_PROCESS_WORKING_SET           = GetTitleIdx (Title, Last, PN_PROCESS_WORKING_SET); 
  54. PX_PROCESS_PEAK_WS               = GetTitleIdx (Title, Last, PN_PROCESS_PEAK_WS); 
  55. PX_PROCESS_PRIO                  = GetTitleIdx (Title, Last, PN_PROCESS_PRIO); 
  56. PX_PROCESS_ELAPSE                = GetTitleIdx (Title, Last, PN_PROCESS_ELAPSE); 
  57. PX_PROCESS_ID                    = GetTitleIdx (Title, Last, PN_PROCESS_ID); 
  58. PX_PROCESS_PRIVATE_PAGE          = GetTitleIdx (Title, Last, PN_PROCESS_PRIVATE_PAGE); 
  59. PX_PROCESS_VIRTUAL_SIZE          = GetTitleIdx (Title, Last, PN_PROCESS_VIRTUAL_SIZE); 
  60. PX_PROCESS_PEAK_VS               = GetTitleIdx (Title, Last, PN_PROCESS_PEAK_VS); 
  61. PX_PROCESS_FAULT_COUNT           = GetTitleIdx (Title, Last, PN_PROCESS_FAULT_COUNT); 
  62. PX_THREAD                        = GetTitleIdx (Title, Last, PN_THREAD); 
  63. PX_THREAD_CPU                    = GetTitleIdx (Title, Last, PN_THREAD_CPU); 
  64. PX_THREAD_PRIV                   = GetTitleIdx (Title, Last, PN_THREAD_PRIV); 
  65. PX_THREAD_USER                   = GetTitleIdx (Title, Last, PN_THREAD_USER); 
  66. PX_THREAD_START                  = GetTitleIdx (Title, Last, PN_THREAD_START); 
  67. PX_THREAD_SWITCHES               = GetTitleIdx (Title, Last, PN_THREAD_SWITCHES); 
  68. PX_THREAD_PRIO                   = GetTitleIdx (Title, Last, PN_THREAD_PRIO); 
  69. PX_THREAD_BASE_PRIO              = GetTitleIdx (Title, Last, PN_THREAD_BASE_PRIO); 
  70. PX_THREAD_ELAPSE                 = GetTitleIdx (Title, Last, PN_THREAD_ELAPSE); 
  71. PX_THREAD_ID                     = GetTitleIdx (Title, Last, PN_THREAD_ID); 
  72. PX_THREAD_DETAILS                = GetTitleIdx (Title, Last, PN_THREAD_DETAILS); 
  73. PX_THREAD_PC                     = GetTitleIdx (Title, Last, PN_THREAD_PC); 
  74. PX_IMAGE                         = GetTitleIdx (Title, Last, PN_IMAGE); 
  75. PX_IMAGE_NOACCESS                = GetTitleIdx (Title, Last, PN_IMAGE_NOACCESS); 
  76. PX_IMAGE_READONLY                = GetTitleIdx (Title, Last, PN_IMAGE_READONLY); 
  77. PX_IMAGE_READWRITE               = GetTitleIdx (Title, Last, PN_IMAGE_READWRITE); 
  78. PX_IMAGE_WRITECOPY               = GetTitleIdx (Title, Last, PN_IMAGE_WRITECOPY); 
  79. PX_IMAGE_EXECUTABLE              = GetTitleIdx (Title, Last, PN_IMAGE_EXECUTABLE); 
  80. PX_IMAGE_EXE_READONLY            = GetTitleIdx (Title, Last, PN_IMAGE_EXE_READONLY); 
  81. PX_IMAGE_EXE_READWRITE           = GetTitleIdx (Title, Last, PN_IMAGE_EXE_READWRITE); 
  82. PX_IMAGE_EXE_WRITECOPY           = GetTitleIdx (Title, Last, PN_IMAGE_EXE_WRITECOPY); 
  83. PX_PROCESS_ADDRESS_SPACE         = GetTitleIdx (Title, Last, PN_PROCESS_ADDRESS_SPACE); 
  84. PX_PROCESS_PRIVATE_NOACCESS      = GetTitleIdx (Title, Last, PN_PROCESS_PRIVATE_NOACCESS); 
  85. PX_PROCESS_PRIVATE_READONLY      = GetTitleIdx (Title, Last, PN_PROCESS_PRIVATE_READONLY); 
  86. PX_PROCESS_PRIVATE_READWRITE     = GetTitleIdx (Title, Last, PN_PROCESS_PRIVATE_READWRITE); 
  87. PX_PROCESS_PRIVATE_WRITECOPY     = GetTitleIdx (Title, Last, PN_PROCESS_PRIVATE_WRITECOPY); 
  88. PX_PROCESS_PRIVATE_EXECUTABLE    = GetTitleIdx (Title, Last, PN_PROCESS_PRIVATE_EXECUTABLE); 
  89. PX_PROCESS_PRIVATE_EXE_READONLY  = GetTitleIdx (Title, Last, PN_PROCESS_PRIVATE_EXE_READONLY); 
  90. PX_PROCESS_PRIVATE_EXE_READWRITE = GetTitleIdx (Title, Last, PN_PROCESS_PRIVATE_EXE_READWRITE); 
  91. PX_PROCESS_PRIVATE_EXE_WRITECOPY = GetTitleIdx (Title, Last, PN_PROCESS_PRIVATE_EXE_WRITECOPY); 
  92. PX_PROCESS_MAPPED_NOACCESS       = GetTitleIdx (Title, Last, PN_PROCESS_MAPPED_NOACCESS); 
  93. PX_PROCESS_MAPPED_READONLY       = GetTitleIdx (Title, Last, PN_PROCESS_MAPPED_READONLY); 
  94. PX_PROCESS_MAPPED_READWRITE      = GetTitleIdx (Title, Last, PN_PROCESS_MAPPED_READWRITE); 
  95. PX_PROCESS_MAPPED_WRITECOPY      = GetTitleIdx (Title, Last, PN_PROCESS_MAPPED_WRITECOPY); 
  96. PX_PROCESS_MAPPED_EXECUTABLE     = GetTitleIdx (Title, Last, PN_PROCESS_MAPPED_EXECUTABLE); 
  97. PX_PROCESS_MAPPED_EXE_READONLY   = GetTitleIdx (Title, Last, PN_PROCESS_MAPPED_EXE_READONLY); 
  98. PX_PROCESS_MAPPED_EXE_READWRITE  = GetTitleIdx (Title, Last, PN_PROCESS_MAPPED_EXE_READWRITE); 
  99. PX_PROCESS_MAPPED_EXE_WRITECOPY  = GetTitleIdx (Title, Last, PN_PROCESS_MAPPED_EXE_WRITECOPY); 
  100. PX_PROCESS_IMAGE_NOACCESS        = GetTitleIdx (Title, Last, PN_PROCESS_IMAGE_NOACCESS); 
  101. PX_PROCESS_IMAGE_READONLY        = GetTitleIdx (Title, Last, PN_PROCESS_IMAGE_READONLY); 
  102. PX_PROCESS_IMAGE_READWRITE       = GetTitleIdx (Title, Last, PN_PROCESS_IMAGE_READWRITE); 
  103. PX_PROCESS_IMAGE_WRITECOPY       = GetTitleIdx (Title, Last, PN_PROCESS_IMAGE_WRITECOPY); 
  104. PX_PROCESS_IMAGE_EXECUTABLE      = GetTitleIdx (Title, Last, PN_PROCESS_IMAGE_EXECUTABLE); 
  105. PX_PROCESS_IMAGE_EXE_READONLY    = GetTitleIdx (Title, Last, PN_PROCESS_IMAGE_EXE_READONLY); 
  106. PX_PROCESS_IMAGE_EXE_READWRITE   = GetTitleIdx (Title, Last, PN_PROCESS_IMAGE_EXE_READWRITE); 
  107. PX_PROCESS_IMAGE_EXE_WRITECOPY   = GetTitleIdx (Title, Last, PN_PROCESS_IMAGE_EXE_WRITECOPY); 
  108. wsprintf (INDEX_PROCTHRD_OBJ, TEXT("%ld %ld"), PX_PROCESS, PX_THREAD); 
  109. wsprintf (INDEX_COSTLY_OBJ, TEXT("%ld %ld %ld"), 
  110. PX_PROCESS_ADDRESS_SPACE, PX_IMAGE, PX_THREAD_DETAILS); 
  111. LocalFree (TitleBuffer); 
  112. LocalFree (Title); 
  113. return 0;
  114. //  SetLocalMachine() - Set local machine as performance data focus. 
  115. //                      Sets ghPerfKey, ghMachineKey, gszMachineName, gszCurrentMachine 
  116. void SetLocalMachine(void) 
  117. TCHAR   szName[MACHINE_NAME_SIZE]; 
  118. DWORD   dwSize = MACHINE_NAME_SIZE; 
  119. // close remote connections, if any 
  120. if (ghPerfKey!=HKEY_PERFORMANCE_DATA) RegCloseKey(ghPerfKey); 
  121. if (ghMachineKey!=HKEY_LOCAL_MACHINE) RegCloseKey(ghMachineKey); 
  122. // set to registry keys on local machine 
  123. ghPerfKey    = HKEY_PERFORMANCE_DATA; 
  124. ghMachineKey = HKEY_LOCAL_MACHINE; 
  125. // get computer name 
  126. GetComputerName (szName, &dwSize); 
  127. if (szName[0] != '\' || szName[1] != '\') { 
  128.     // must have two '\' 
  129. wsprintf (gszMachineName, TEXT("\\%s"), szName); 
  130. lstrcpy (gszCurrentMachine, gszMachineName); 
  131. } else { 
  132. lstrcpy (gszMachineName, szName); 
  133. lstrcpy (gszCurrentMachine, gszMachineName); 
  134. //  ConnectComputer() - Connect to a computer with name entered in PVIEW_COMPUTER. 
  135. //                      If a new connection is made, then return TRUE else return FALSE. 
  136. //                      Sets gszCurrentMachine, ghPerfKey, and ghMachineKey
  137. int NtProcList_ConnectComputer(char *svName) 
  138. HKEY    hKey; 
  139. TCHAR   szTemp[MACHINE_NAME_SIZE]; 
  140. BOOL    bResult = TRUE; 
  141. if(svName==NULL) {
  142. // Connect to local machine
  143. SetLocalMachine(); 
  144. SetPerfIndexes(); 
  145. return 0;
  146. }
  147. // Connect to remote machine 
  148. lstrcpyn(szTemp,svName,MACHINE_NAME_SIZE);
  149. if(RegConnectRegistry(szTemp, HKEY_PERFORMANCE_DATA, &hKey)!=ERROR_SUCCESS) return -1;
  150. lstrcpy(gszCurrentMachine, szTemp); 
  151. if(ghPerfKey!=HKEY_PERFORMANCE_DATA) RegCloseKey (ghPerfKey); 
  152. ghPerfKey = hKey; 
  153. // we also need to get the remote machine's title indexes. 
  154. if (ghMachineKey != HKEY_LOCAL_MACHINE) RegCloseKey (ghMachineKey); 
  155. if (RegConnectRegistry (gszCurrentMachine, HKEY_LOCAL_MACHINE, &hKey) == ERROR_SUCCESS) 
  156. ghMachineKey = hKey; 
  157. else 
  158. ghMachineKey = HKEY_LOCAL_MACHINE; 
  159. SetPerfIndexes(); 
  160. return 0;
  161. }
  162. // RefreshPerfData() - Get a new set of performance data. pData should be NULL initially. 
  163. PPERF_DATA RefreshPerfData (HKEY        hPerfKey, 
  164.                             LPTSTR      szObjectIndex, 
  165.                             PPERF_DATA  pData, 
  166.                             DWORD       *pDataSize) 
  167.     if (GetPerfData (hPerfKey, szObjectIndex, &pData, pDataSize) == ERROR_SUCCESS) 
  168.         return pData; 
  169.     else 
  170.         return NULL; 
  171.  
  172. // SetProcessListText() - Format the process list text. 
  173. void SetProcessListText (PPERF_INSTANCE pInst, 
  174.                          PPERF_COUNTER  pProcID, 
  175.                          LPTSTR         svProcName,
  176.  DWORD *pdwProcID) 
  177. if(pProcID) { 
  178. *pdwProcID = *(DWORD *)(CounterData (pInst, pProcID)); 
  179.         wsprintf(svProcName, TEXT("%ls"), InstanceName(pInst));
  180.     
  181. PROCESSINFO *NtProcList_BuildSnapShot(void)
  182. {
  183. PROCESSINFO *pProcCur; // linked list
  184. THREADINFO *pThreadCur; // linked list
  185. PPERF_OBJECT pObject; // pointer to an object
  186. PPERF_COUNTER pCounterID;  // pointer to a counter
  187. PPERF_INSTANCE pInstance; // pointer to an instance
  188. int i,InstanceIndex;
  189. PROCESSINFO phd;
  190. // get performance data 
  191. gpPerfData=RefreshPerfData(ghPerfKey, INDEX_PROCTHRD_OBJ, gpPerfData, &gPerfDataSize); 
  192. // Start linked list
  193. phd.next=NULL;
  194. // --------------- Let's get information about processes
  195. pObject=FindObject(gpPerfData, PX_PROCESS); 
  196. // Get Process ID information
  197. pCounterID=FindCounter(pObject, PX_PROCESS_ID);
  198. pProcCur=&phd;
  199. pInstance=FirstInstance(pObject);
  200. InstanceIndex=0;
  201. while(pInstance && (InstanceIndex<pObject->NumInstances)) {
  202. // Add process to the list
  203. pProcCur->next=(PROCESSINFO *)malloc(sizeof(PROCESSINFO));
  204. if(pProcCur->next==NULL) return NULL;
  205. pProcCur=pProcCur->next;
  206. // Fill in info
  207. pProcCur->dwProcID=*(DWORD *)CounterData(pInstance,pCounterID);
  208. wsprintf(pProcCur->svApp,TEXT("%ls"), InstanceName(pInstance));
  209. pProcCur->pThread=NULL;
  210. // Go to next process instance
  211. pInstance=NextInstance(pInstance); 
  212. InstanceIndex++;
  213. }
  214. pProcCur->next=NULL;
  215. // --------------- Let's get information about threads
  216. pObject=FindObject(gpPerfData, PX_THREAD); 
  217. // Get Thread ID information
  218. pCounterID=FindCounter(pObject, PX_THREAD_ID);
  219. pInstance=FirstInstance (pObject);
  220. InstanceIndex=0;
  221. while(pInstance && (InstanceIndex<pObject->NumInstances)) {
  222. // Find process that is this thread's parent
  223. pProcCur=phd.next;
  224. for(i=0;i<(int)pInstance->ParentObjectInstance;i++) {
  225. pProcCur=pProcCur->next;
  226. if(pProcCur==NULL) break;
  227. }
  228. if(pProcCur!=NULL) {
  229. // Allocate thread info struct
  230. pThreadCur=(THREADINFO *)malloc(sizeof(THREADINFO));
  231. if(pThreadCur==NULL) return NULL;
  232. // Link into process info's list
  233. pThreadCur->next=pProcCur->pThread;
  234. pProcCur->pThread=pThreadCur;
  235. // Fill in data
  236. pThreadCur->dwThreadID=*(DWORD *)CounterData(pInstance,pCounterID);
  237. }
  238. pInstance=NextInstance(pInstance);
  239. InstanceIndex++;
  240. }
  241. return phd.next;
  242. }