Guest User

Untitled

a guest
Jan 23rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <windows.h>
  4. #include <tlhelp32.h>
  5. #include <psapi.h>
  6. using namespace std;
  7.  
  8. BOOL SetPrivilege(
  9. HANDLE hToken, // access token handle
  10. LPCTSTR lpszPrivilege, // name of privilege to enable/disable
  11. BOOL bEnablePrivilege // to enable or disable privilege
  12. )
  13. {
  14. TOKEN_PRIVILEGES tp;
  15. LUID luid;
  16.  
  17. if (!LookupPrivilegeValue(
  18. NULL, // lookup privilege on local system
  19. lpszPrivilege, // privilege to lookup
  20. &luid)) // receives LUID of privilege
  21. {
  22. return FALSE;
  23. }
  24.  
  25. tp.PrivilegeCount = 1;
  26. tp.Privileges[0].Luid = luid;
  27. if (bEnablePrivilege)
  28. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  29. else
  30. tp.Privileges[0].Attributes = 0;
  31.  
  32. // Enable the privilege or disable all privileges.
  33.  
  34. if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL))
  35. {
  36. return FALSE;
  37. }
  38.  
  39. if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
  40. {
  41. return FALSE;
  42. }
  43.  
  44. return TRUE;
  45. }
  46.  
  47. int main()
  48. {
  49. HANDLE hProcess;
  50. HANDLE hToken;
  51. HANDLE snapshot;
  52. TCHAR filename[MAX_PATH];
  53. DWORD charsCarried = MAX_PATH;
  54. PROCESSENTRY32 process;
  55. int count = 0;
  56.  
  57. snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  58. process.dwSize = sizeof(PROCESSENTRY32);
  59.  
  60. if (snapshot != INVALID_HANDLE_VALUE)
  61. {
  62. if (Process32First(snapshot, &process))
  63. {
  64. do
  65. {
  66. count++;
  67. OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
  68. SetPrivilege(hToken, SE_DEBUG_NAME, TRUE);
  69. hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, process.th32ProcessID);
  70.  
  71. if (hProcess != NULL)
  72. {
  73. GetModuleFileNameEx(hProcess, NULL, filename, MAX_PATH);
  74. printf("%d - PID: %d, Name: %ls, CountThread: %dn",
  75. count,
  76. process.th32ProcessID,
  77. filename,
  78. process.cntThreads);
  79. }
  80. else
  81. {
  82. printf("Err: %dn", GetLastError());
  83. }
  84. SetPrivilege(hToken, SE_DEBUG_NAME, FALSE);
  85. }
  86. while (Process32Next(snapshot, &process));
  87. }
  88. }
  89.  
  90. CloseHandle(hProcess);
  91. CloseHandle(snapshot);
  92. return 0;
  93. }
  94.  
  95. #include <stdlib.h>
  96. #include <locale.h>
  97. #include <stdio.h>
  98. #include <tchar.h>
  99. #include <windows.h>
  100. #include <tlhelp32.h>
  101. #include <psapi.h>
  102.  
  103. void ErrorMes(LPTSTR lpszFunction)
  104. {
  105. // Retrieve the system error message for the last-error code
  106.  
  107. LPVOID lpMsgBuf;
  108. LPVOID lpDisplayBuf;
  109. DWORD dw = GetLastError();
  110.  
  111. FormatMessage(
  112. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  113. FORMAT_MESSAGE_FROM_SYSTEM |
  114. FORMAT_MESSAGE_IGNORE_INSERTS,
  115. NULL,
  116. dw,
  117. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  118. (LPTSTR) &lpMsgBuf,
  119. 0, NULL );
  120.  
  121. // Display the error message
  122.  
  123. lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
  124. (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
  125. wprintf(L"%s failed with error %d: %s",
  126. lpszFunction, dw, lpMsgBuf);
  127.  
  128. LocalFree(lpMsgBuf);
  129. LocalFree(lpDisplayBuf);
  130.  
  131. }
  132.  
  133.  
  134. int main()
  135. {
  136. HANDLE hProcess=NULL;
  137. HANDLE hToken;
  138. HANDLE snapshot;
  139. TCHAR filename[MAX_PATH];
  140. DWORD charsCarried = MAX_PATH;
  141. PROCESSENTRY32 process;
  142. int count = 0;
  143.  
  144. setlocale(LC_ALL,"Russian");
  145.  
  146. snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  147. process.dwSize = sizeof(PROCESSENTRY32);
  148.  
  149. if (snapshot != INVALID_HANDLE_VALUE)
  150. {
  151. if (Process32First(snapshot, &process))
  152. {
  153. do
  154. {
  155. wprintf(L"PID: %d, Name: %s, CountThreads: %dn",
  156. process.th32ProcessID,
  157. process.szExeFile,
  158. process.cntThreads);
  159.  
  160. /*get process handle*/
  161. hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, process.th32ProcessID);
  162. if(hProcess==NULL){
  163. ErrorMes(L"OpenProcess");
  164. wprintf(L"n");
  165. continue;
  166. }
  167.  
  168. charsCarried=MAX_PATH;
  169.  
  170. /* get executable name*/
  171. if(QueryFullProcessImageName(hProcess,0,filename,&charsCarried)!=FALSE)
  172. {
  173. wprintf(L"%sn",
  174. filename);
  175. }
  176. else
  177. {
  178. ErrorMes(L"QueryFullProcessImageName");
  179. wprintf(L"n");
  180. }
  181. wprintf(L"n");
  182.  
  183. CloseHandle(hProcess);
  184. hProcess = NULL;
  185. }
  186. while (Process32Next(snapshot, &process));
  187. }
  188. }
  189. else
  190. {
  191. ErrorMes(L"CreateToolhelp32Snapshot");
  192. }
  193.  
  194. if(hProcess!=NULL)CloseHandle(hProcess);
  195. CloseHandle(snapshot);
  196. system("PAUSE");
  197. return 0;
  198. }
Add Comment
Please, Sign In to add comment