admin管理员组

文章数量:1516870

一、获取SSID


(1)在 stdafx.h 头文件声明


#define _WIN32_WINNT 0x0501
#define _WINVER 0x0501


(2)

#include <Tlhelp32.h>
#include <Sddl.h>

#pragma  comment(lib, "Advapi32.lib")


void GetProgramName(TCHAR *ProgramFullName, TCHAR *ProgramName)
{
TCHAR sDrive[_MAX_DRIVE];
TCHAR sDir[_MAX_DIR];
TCHAR sFname[_MAX_FNAME];
TCHAR sExt[_MAX_EXT];

_tsplitpath(ProgramFullName, sDrive, sDir, sFname, sExt);

lstrcpy(ProgramName,sFname);
lstrcat(ProgramName,sExt);

return;
}

DWORD GetProcessIDFromName(const char *argv1)
{
BOOL bWin9x = FALSE;
OSVERSIONINFO VerInfo;
VerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&VerInfo);

bWin9x = (VerInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);

HANDLE         hProcessSnap = NULL;
PROCESSENTRY32 pe32      = {0};
BOOL                nResult;
//  Take a snapshot of all processes in the system.

hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

if (hProcessSnap == INVALID_HANDLE_VALUE)
return 0;

//  Fill in the size of the structure before using it.

pe32.dwSize = sizeof(PROCESSENTRY32);

//  Walk the snapshot of the processes, and for each process,
//  display information.

if (Process32First(hProcessSnap, &pe32))
{
do
{
TCHAR ProgName[MAX_PATH] = {0};
if( bWin9x )
{
GetProgramName(pe32.szExeFile,ProgName);
}
else
{
_tcscpy(ProgName,pe32.szExeFile);
}
USES_CONVERSION;
nResult=stricmp(argv1, CT2CA(ProgName));
if (nResult==0)
{
DWORD dwProcessID=pe32.th32ProcessID;
CloseHandle (hProcessSnap);
return dwProcessID;
}
}while (Process32Next(hProcessSnap, &pe32));
}
CloseHandle (hProcessSnap);
return 0;
}

BOOL GetMySSID(char *szSSID)
{
////////////////////////////////
//    CCommonFunc cf;

szSSID[0] = 0;
DWORD dwProcessID = GetProcessIDFromName("explorer.exe");
if( dwProcessID == 0 )
{
// Can't Find Explorer.exe
return FALSE;
}

HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwProcessID);
if( hProcess == NULL )
{
return FALSE;
}

HANDLE hToken;
if( !OpenProcessToken(hProcess, TOKEN_ALL_ACCESS, &hToken) )
{
CloseHandle(hProcess);
return FALSE;
}

DWORD dwLength;
TOKEN_USER* account = NULL;
if (GetTokenInformation(hToken, TokenUser, NULL, 0, &dwLength) == FALSE &&
GetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
CloseHandle(hToken);
CloseHandle(hProcess);
return FALSE;
}

account = (TOKEN_USER*)malloc(dwLength);
if (NULL == account)
{
CloseHandle(hToken);
CloseHandle(hProcess);
return FALSE;
}

if (GetTokenInformation(hToken, TokenUser, (LPVOID)account, dwLength, &dwLength))
{
char* lpszSid = NULL;

if (ConvertSidToStringSidA(account->User.Sid, &lpszSid))// 第一步的声明
{
strcpy(szSSID, lpszSid);
LocalFree(lpszSid);
}
}

CloseHandle(hToken);
CloseHandle(hProcess);
free(account);
return (0 == szSSID[0] ? FALSE : TRUE);
}

二、将账户名转换为字符串类型的SSID

void   GetSidString(PSID   pSid,   char*   szBuffer)
{
SID_IDENTIFIER_AUTHORITY   *psia   =   GetSidIdentifierAuthority(   pSid   );
DWORD   dwTopAuthority   =   psia-> Value[5];
sprintf(szBuffer,   "S-1-%lu",   dwTopAuthority);

char   szTemp[32];
int   iSubAuthorityCount   =   *(GetSidSubAuthorityCount(pSid));
for   (int   i   =   0;   i <iSubAuthorityCount;   i++)
{
DWORD   dwSubAuthority   =   *(GetSidSubAuthority(pSid,   i));
sprintf(szTemp,   "%lu",   dwSubAuthority);
strcat(szBuffer,   "-");
strcat(szBuffer,   szTemp);
}
}

void ConvertAccountNameToStringSid (const std::string& strAccountName, std::string& strSid )
{
BYTE   sidBuffer[MAX_PATH] = {0};
PSID   pSid = (PSID)&sidBuffer;
DWORD   sidBufferSize   =   MAX_PATH;
char   domainBuffer[MAX_PATH];
DWORD   domainBufferSize   =   MAX_PATH;
SID_NAME_USE   snu;
BOOL bRet = FALSE;
//获取指定用户的SID
bRet = LookupAccountNameA(NULL, strAccountName.c_str(),  pSid,&sidBufferSize, domainBuffer,&domainBufferSize,&snu);
//format the string sid
SID_IDENTIFIER_AUTHORITY   *psia   =   GetSidIdentifierAuthority(pSid);
DWORD   dwTopAuthority   =   psia-> Value[5];
char szSid[MAX_PATH] = {0};
sprintf(szSid,   "S-1-%lu",   dwTopAuthority);

char   szTemp[32];
int   iSubAuthorityCount   =   *(GetSidSubAuthorityCount(pSid));
for   (int   i   =   0;   i <iSubAuthorityCount;   i++)
{
DWORD   dwSubAuthority   =   *(GetSidSubAuthority(pSid,   i));
sprintf(szTemp,   "%lu",   dwSubAuthority);
strcat(szSid,   "-");
strcat(szSid,   szTemp);
}
strSid = szSid;
}


本文标签: 读秀从获取轻松玩转