能力值:
( LV2,RANK:10 )
6 楼
bool runExeObject::runExe(LPCTSTR userName, int sessionID, LPCTSTR exePath)
{
bool forReturn=false;
PROCESSENTRY32 pe32;
//在使用这个结构前,先设置它的大小
pe32.dwSize = sizeof(pe32);
//给系统内所有的进程拍个快照
HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if (hProcessSnap == INVALID_HANDLE_VALUE)
{
return false;
}
//遍历进程快照,轮流显示每个进程的信息
BOOL bMore = ::Process32First(hProcessSnap,&pe32);
while (bMore)
{
//printf("进程名称:%s\n",pe32.szExeFile);
//printf("进程ID:%u\n\n",pe32.th32ProcessID);
/////////////////////////////////////////////////////////////////////////////////////////////////get user name
HANDLE hProcess=OpenProcess(PROCESS_QUERY_INFORMATION,FALSE,pe32.th32ProcessID);
if( hProcess==NULL )
{
bMore = ::Process32Next(hProcessSnap,&pe32);
continue;
}
HANDLE hToken =NULL;
BOOL bResult =FALSE;
DWORD dwSize =0;
static TCHAR szUserName[256]={0};
TCHAR szDomain[256]={0};
DWORD dwDomainSize=256;
DWORD dwNameSize=256;
SID_NAME_USE SNU;
PTOKEN_USER pTokenUser=NULL;
__try
{
if( !OpenProcessToken(hProcess,TOKEN_QUERY,&hToken) )
{
bResult = FALSE;
__leave;
}
if( !GetTokenInformation(hToken,TokenUser,pTokenUser,dwSize,&dwSize) )
{
if( GetLastError() != ERROR_INSUFFICIENT_BUFFER )
{
bResult = FALSE ;
__leave;
}
}
pTokenUser = NULL;
pTokenUser = (PTOKEN_USER)malloc(dwSize);
if( pTokenUser == NULL )
{
bResult = FALSE;
__leave;
}
if( !GetTokenInformation(hToken,TokenUser,pTokenUser,dwSize,&dwSize) )
{
bResult = FALSE;
__leave;
}
if( LookupAccountSid(NULL,pTokenUser->User.Sid,szUserName,&dwNameSize,szDomain,&dwDomainSize,&SNU) != 0 )
{
//TCHAR sdfsdfsdf[255]=(TCHAR)*userName;
//CString sdsddssssd=(CString)*userName;
if(!strcmp((const char *)&szUserName,(const char *)userName)) //用户名通过
{
///////////////////////////////////////////////////////////接下来判断session id是否为指定的
dwSize=0;
int sessionID2=999;
if( !GetTokenInformation(hToken,TokenSessionId,(LPVOID)&sessionID2,dwSize,&dwSize) )
{
if( GetLastError() != ERROR_INSUFFICIENT_BUFFER )
{
bResult = FALSE ;
__leave;
}
}
if( !GetTokenInformation(hToken,TokenSessionId,(LPVOID)&sessionID2,sizeof(int),&dwSize) )
{
bResult = FALSE;
__leave;
}
if(sessionID2==sessionID)//找到符合要求的token
{
//dup token
HANDLE hDuplicatedToken = NULL;
if (DuplicateTokenEx(hToken, TOKEN_ALL_ACCESS, NULL, SecurityImpersonation, TokenPrimary, &hDuplicatedToken) == FALSE)
{
int eee=GetLastError();
bResult = FALSE ;
__leave;
}
//利用这个duptoken创建指定进程
PROCESS_INFORMATION pi;
STARTUPINFO si;// = { sizeof(STARTUPINFO),NULL,(LPWSTR)"",NULL,0,0,0,0,0,0,0,STARTF_USESHOWWINDOW,0,0,NULL,0,0,0};
ZeroMemory( &si, sizeof(si) );
si.wShowWindow = SW_SHOW;
si.lpDesktop = NULL;
si.cb=sizeof(STARTUPINFO);
si.dwFlags=STARTF_USESHOWWINDOW;
//LPCTSTR s1=(LPCTSTR)"E:\\";
//si.lpDesktop=TEXT("winsta0\\default");
ZeroMemory( &pi, sizeof(pi) );
bool abc=CreateProcessAsUser( hDuplicatedToken ,(LPCTSTR)exePath,NULL ,NULL,NULL,FALSE ,CREATE_DEFAULT_ERROR_MODE| CREATE_UNICODE_ENVIRONMENT ,NULL,NULL,&si,&pi );//| CREATE_NEW_CONSOLE
if(abc)
forReturn=true;
int er=GetLastError();
//NORMAL_PRIORITY_CLASS| CREATE_NEW_CONSOLE //CREATE_DEFAULT_ERROR_MODE| CREATE_UNICODE_ENVIRONMENT
//if(::CreateProcessWithTokenW(hToken,LOGON_WITH_PROFILE,(LPCWSTR)exePath,NULL,NORMAL_PRIORITY_CLASS,NULL,NULL,&si,&pi))
// forReturn=true;
//int er=GetLastError();
/*
strDesktop = "WinSta0\Default"
si.lpDesktop = strDesktop
si.cb = Len(si)
WriteToSpecialLogfile "Application=" & Application
WriteToSpecialLogfile "Parameters=" & Parameters
Result = CreateProcessAsUser(hToken, Application, Parameters, 0&, 0&, False, _
CREATE_DEFAULT_ERROR_MODE Or CREATE_UNICODE_ENVIRONMENT, lpEnv, CurrentDirectory, si, PI)
*/
break;
}//end 创建进程
/////////////////////////////////////////////////////////////end 判断 session id
}//end user name ok
}
}
__finally
{
if( pTokenUser!=NULL )
free(pTokenUser);
}
//////////////////////////////////////////////////////////////////////////////////////////////////end get user name
bMore = ::Process32Next(hProcessSnap,&pe32);
}
//不要忘记清除掉snapshot对象
::CloseHandle(hProcessSnap);
return forReturn;
}