SecurityTool.cpp
Upload User: canhn88
Upload Date: 2014-12-28
Package Size: 2438k
Code Size: 3k
Category:

Windows Develop

Development Platform:

C/C++

  1. //SecurityTool.cpp: implementation of the CSecurityTool class.
  2. #include "SecurityTool.h"
  3. #include <tlhelp32.h>
  4. #include <wtsapi32.h>
  5. #pragma comment(lib, "Wtsapi32.lib")
  6. //load dlls tardily.
  7. #pragma comment(lib, "delayimp.lib")
  8. #pragma comment(linker, "/DELAYLOAD:"wtsapi32.dll"")
  9. #define MIN(a,b) ((a)>(b)?(a):(b))
  10. //current domain-name.
  11. char CSecurityTool::domain[MAX_USERID_SIZE]="";
  12. CSecurityTool::CSecurityTool(){
  13. memset(CSecurityTool::domain, 0x0, sizeof(domain));
  14. }
  15. CSecurityTool::~CSecurityTool(){}
  16. //get current process' user-name.
  17. BOOL CSecurityTool::GetCurrProcessUser(char *buffer, const int buffersize)
  18. {
  19. DWORD dwsize = buffersize;
  20. memset(buffer, 0x0, buffersize);
  21. return GetUserName(buffer, &dwsize);
  22. }
  23. //get user-name of current logoned on WinXP os.
  24. BOOL CSecurityTool::GetLogUserXP(char *buffer, const int buffersize)
  25. {
  26. memset(buffer, 0x0, buffersize);
  27. return GetLogUser2K(buffer, buffersize);
  28. }
  29. //get user-name of current logoned on Win2000 os.
  30. BOOL CSecurityTool::GetLogUser2K(char *buffer, const int buffersize)
  31. {
  32. HANDLE hsnapshot = NULL;
  33. memset(buffer, 0x0, buffersize);
  34.     __try
  35. {
  36. //get a snapshot of the processes in the system.
  37.         hsnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  38.         if(hsnapshot == NULL) {
  39. __leave;
  40. }
  41.         PROCESSENTRY32 pe32;
  42.         pe32.dwSize = sizeof(pe32);
  43.         //find the shell process.
  44.         BOOL hprocess = Process32First(hsnapshot, &pe32);
  45.         while(hprocess) {
  46. if (stricmp(pe32.szExeFile, "explorer.exe") == 0) {
  47. if(GetProcessUser(pe32.th32ProcessID, buffer, buffersize)) {
  48. return TRUE;
  49. }
  50. break;
  51. }
  52. hprocess = Process32Next(hsnapshot, &pe32);
  53. }
  54. }
  55.     __finally
  56. {
  57. //cleanup the snapshot.
  58. if(hsnapshot != NULL) {
  59. CloseHandle(hsnapshot);
  60. }
  61.     }
  62. return FALSE;
  63. }
  64. //get specialed process' user-name.
  65. BOOL CSecurityTool::GetProcessUser(const int dwProcessID,
  66.    char *szUserName, 
  67.    const int nNameLen)
  68. {
  69. BOOL fResult  = FALSE;
  70.     HANDLE hProc  = NULL;
  71. HANDLE hToken = NULL;
  72. DWORD  dwNeedLen = 0;
  73. TOKEN_USER *pTokenUser = NULL;
  74. __try
  75. {
  76.         // Open the process with PROCESS_QUERY_INFORMATION access
  77.         hProc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwProcessID);
  78.         if (hProc == NULL) {
  79. __leave;
  80. }
  81.         fResult = OpenProcessToken(hProc, TOKEN_QUERY, &hToken);
  82.         if(!fResult) {
  83. __leave;
  84. }
  85. fResult = GetTokenInformation(hToken,TokenUser, NULL, 0, &dwNeedLen);
  86. if (dwNeedLen > 0) {
  87. pTokenUser = (TOKEN_USER*)new BYTE[dwNeedLen];
  88. fResult = GetTokenInformation(hToken,TokenUser, pTokenUser, dwNeedLen, &dwNeedLen);
  89. if (!fResult) {
  90. __leave;
  91. }
  92. }
  93. else {
  94. __leave;
  95. }
  96. SID_NAME_USE sn;
  97. char szDomainName[MAX_PATH];
  98. DWORD dwDmLen = sizeof(szDomainName)/sizeof(szDomainName[0]);
  99. fResult = LookupAccountSid(NULL, pTokenUser->User.Sid, szUserName, 
  100. (LPDWORD)&nNameLen, szDomainName, &dwDmLen, &sn);
  101. if(fResult) {
  102. memset(CSecurityTool::domain, 0x0, sizeof(domain));
  103. strncpy(CSecurityTool::domain, szDomainName, 
  104. MIN(dwDmLen, sizeof(CSecurityTool::domain)));
  105. }
  106. }
  107. __finally
  108. {
  109. if (hProc)
  110. ::CloseHandle(hProc);
  111. if (hToken)
  112. ::CloseHandle(hToken);
  113. if (pTokenUser)
  114. delete[] (char*)pTokenUser;
  115. return fResult;
  116. }
  117. }