Guest User

Untitled

a guest
Jan 23rd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 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. using namespace std;
  103.  
  104. void ErrorMes(LPTSTR lpszFunction)
  105. {
  106. // Retrieve the system error message for the last-error code
  107.  
  108. LPVOID lpMsgBuf;
  109. LPVOID lpDisplayBuf;
  110. DWORD dw = GetLastError();
  111.  
  112. FormatMessage(
  113. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  114. FORMAT_MESSAGE_FROM_SYSTEM |
  115. FORMAT_MESSAGE_IGNORE_INSERTS,
  116. NULL,
  117. dw,
  118. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  119. (LPTSTR) &lpMsgBuf,
  120. 0, NULL );
  121.  
  122. // Display the error message
  123.  
  124. lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
  125. (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
  126. wprintf(L"%s failed with error %d: %s",
  127. lpszFunction, dw, lpMsgBuf);
  128.  
  129. LocalFree(lpMsgBuf);
  130. LocalFree(lpDisplayBuf);
  131.  
  132. }
  133.  
  134.  
  135. int main()
  136. {
  137. HANDLE hProcess=NULL;
  138. HANDLE hToken;
  139. HANDLE snapshot;
  140. TCHAR filename[MAX_PATH];
  141. DWORD charsCarried = MAX_PATH;
  142. PROCESSENTRY32 process;
  143. int count = 0;
  144.  
  145. setlocale(LC_ALL,"Russian");
  146.  
  147. snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  148. process.dwSize = sizeof(PROCESSENTRY32);
  149.  
  150. if (snapshot != INVALID_HANDLE_VALUE)
  151. {
  152. if (Process32First(snapshot, &process))
  153. {
  154. do
  155. {
  156. wprintf(L"PID: %d, Name: %s, CountThreads: %dn",
  157. process.th32ProcessID,
  158. process.szExeFile,
  159. process.cntThreads);
  160.  
  161. /*get process handle*/
  162. hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, process.th32ProcessID);
  163. if(hProcess==NULL){
  164. ErrorMes(L"OpenProcess");
  165. wprintf(L"n");
  166. continue;
  167. }
  168.  
  169. charsCarried=MAX_PATH;
  170.  
  171. /* get executable name*/
  172. if(QueryFullProcessImageName(hProcess,0,filename,&charsCarried)!=FALSE)
  173. {
  174. wprintf(L"%sn",
  175. filename);
  176. }
  177. else
  178. {
  179. ErrorMes(L"QueryFullProcessImageName");
  180. wprintf(L"n");
  181. }
  182. wprintf(L"n");
  183.  
  184. CloseHandle(hProcess);
  185. hProcess = NULL;
  186. }
  187. while (Process32Next(snapshot, &process));
  188. }
  189. }
  190. else
  191. {
  192. ErrorMes(L"CreateToolhelp32Snapshot");
  193. }
  194.  
  195. if(hProcess!=NULL)CloseHandle(hProcess);
  196. CloseHandle(snapshot);
  197. system("PAUSE");
  198. return 0;
  199. }
Add Comment
Please, Sign In to add comment