Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
SecurityTool.cpp
Package: Agent.rar [view]
Upload User: canhn88
Upload Date: 2014-12-28
Package Size: 2438k
Code Size: 3k
Category:
Windows Develop
Development Platform:
C/C++
- //SecurityTool.cpp: implementation of the CSecurityTool class.
- #include "SecurityTool.h"
- #include <tlhelp32.h>
- #include <wtsapi32.h>
- #pragma comment(lib, "Wtsapi32.lib")
- //load dlls tardily.
- #pragma comment(lib, "delayimp.lib")
- #pragma comment(linker, "/DELAYLOAD:"wtsapi32.dll"")
- #define MIN(a,b) ((a)>(b)?(a):(b))
- //current domain-name.
- char CSecurityTool::domain[MAX_USERID_SIZE]="";
- CSecurityTool::CSecurityTool(){
- memset(CSecurityTool::domain, 0x0, sizeof(domain));
- }
- CSecurityTool::~CSecurityTool(){}
- //get current process' user-name.
- BOOL CSecurityTool::GetCurrProcessUser(char *buffer, const int buffersize)
- {
- DWORD dwsize = buffersize;
- memset(buffer, 0x0, buffersize);
- return GetUserName(buffer, &dwsize);
- }
- //get user-name of current logoned on WinXP os.
- BOOL CSecurityTool::GetLogUserXP(char *buffer, const int buffersize)
- {
- memset(buffer, 0x0, buffersize);
- return GetLogUser2K(buffer, buffersize);
- }
- //get user-name of current logoned on Win2000 os.
- BOOL CSecurityTool::GetLogUser2K(char *buffer, const int buffersize)
- {
- HANDLE hsnapshot = NULL;
- memset(buffer, 0x0, buffersize);
- __try
- {
- //get a snapshot of the processes in the system.
- hsnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- if(hsnapshot == NULL) {
- __leave;
- }
- PROCESSENTRY32 pe32;
- pe32.dwSize = sizeof(pe32);
- //find the shell process.
- BOOL hprocess = Process32First(hsnapshot, &pe32);
- while(hprocess) {
- if (stricmp(pe32.szExeFile, "explorer.exe") == 0) {
- if(GetProcessUser(pe32.th32ProcessID, buffer, buffersize)) {
- return TRUE;
- }
- break;
- }
- hprocess = Process32Next(hsnapshot, &pe32);
- }
- }
- __finally
- {
- //cleanup the snapshot.
- if(hsnapshot != NULL) {
- CloseHandle(hsnapshot);
- }
- }
- return FALSE;
- }
- //get specialed process' user-name.
- BOOL CSecurityTool::GetProcessUser(const int dwProcessID,
- char *szUserName,
- const int nNameLen)
- {
- BOOL fResult = FALSE;
- HANDLE hProc = NULL;
- HANDLE hToken = NULL;
- DWORD dwNeedLen = 0;
- TOKEN_USER *pTokenUser = NULL;
- __try
- {
- // Open the process with PROCESS_QUERY_INFORMATION access
- hProc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwProcessID);
- if (hProc == NULL) {
- __leave;
- }
- fResult = OpenProcessToken(hProc, TOKEN_QUERY, &hToken);
- if(!fResult) {
- __leave;
- }
- fResult = GetTokenInformation(hToken,TokenUser, NULL, 0, &dwNeedLen);
- if (dwNeedLen > 0) {
- pTokenUser = (TOKEN_USER*)new BYTE[dwNeedLen];
- fResult = GetTokenInformation(hToken,TokenUser, pTokenUser, dwNeedLen, &dwNeedLen);
- if (!fResult) {
- __leave;
- }
- }
- else {
- __leave;
- }
- SID_NAME_USE sn;
- char szDomainName[MAX_PATH];
- DWORD dwDmLen = sizeof(szDomainName)/sizeof(szDomainName[0]);
- fResult = LookupAccountSid(NULL, pTokenUser->User.Sid, szUserName,
- (LPDWORD)&nNameLen, szDomainName, &dwDmLen, &sn);
- if(fResult) {
- memset(CSecurityTool::domain, 0x0, sizeof(domain));
- strncpy(CSecurityTool::domain, szDomainName,
- MIN(dwDmLen, sizeof(CSecurityTool::domain)));
- }
- }
- __finally
- {
- if (hProc)
- ::CloseHandle(hProc);
- if (hToken)
- ::CloseHandle(hToken);
- if (pTokenUser)
- delete[] (char*)pTokenUser;
- return fResult;
- }
- }