12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870 |
- #include "StdAfx.h"
- #include "TWProcHelper.h"
- CTWProcHelper::CTWProcHelper(void)
- :m_hJob(NULL)
- {
- }
- CTWProcHelper::CTWProcHelper(HANDLE hJob)
- :m_hJob(hJob)
- {
- }
- CTWProcHelper::~CTWProcHelper(void)
- {
- if (m_hJob != NULL)
- {
- CloseHandle(m_hJob);
- m_hJob = NULL;
- }
-
- }
- // PrintError support function.
- // Simple wrapper function for error output.
- void CTWProcHelper::PrintError(LPCTSTR errDesc)
- {
- DWORD dwErrCode = GetLastError();
- LPCTSTR errMsg = ErrorMessage(dwErrCode);
- Dbg(TEXT("** ERROR ** %s: %s(%d)."), errDesc, errMsg, dwErrCode);
- LocalFree((LPVOID)errMsg);
- }
- void CTWProcHelper::PrintInfo(LPCTSTR lpszDesc)
- {
- Dbg("ProcHelper: %s.", lpszDesc);
- return;
- }
- // ErrorMessage support function.
- // Retrieves the system error message for the GetLastError() code.
- // Note: caller must use LocalFree() on the returned LPCTSTR buffer.
- LPCTSTR CTWProcHelper::ErrorMessage(DWORD error)
- {
- LPVOID lpMsgBuf;
- FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
- | FORMAT_MESSAGE_FROM_SYSTEM
- | FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- error,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPTSTR) &lpMsgBuf,
- 0,
- NULL);
- return((LPCTSTR)lpMsgBuf);
- }
- //===== Below is Functions about Job ========================
- BOOL CTWProcHelper::Create(PSECURITY_ATTRIBUTES psa, PCTSTR pszName)
- {
- m_hJob = CreateJobObject(psa, pszName);
- return(m_hJob != NULL);
- }
- BOOL CTWProcHelper::Open(PCTSTR pszName, DWORD dwDesiredAccess, BOOL fInheritHandle)
- {
- m_hJob = OpenJobObject(dwDesiredAccess, fInheritHandle, pszName);
- return(m_hJob != NULL);
- }
- BOOL CTWProcHelper::AssignProcess(HANDLE hProcess)
- {
- return(AssignProcessToJobObject(m_hJob, hProcess));
- }
- BOOL CTWProcHelper::AssignProcess(DWORD dwProcessId)
- {
- HANDLE hProcess = OpenProcess(
- PROCESS_SET_QUOTA | PROCESS_TERMINATE, FALSE, dwProcessId);
- if(hProcess == NULL)
- {
- PrintError("The hProcess is null which get from ProcessId");
- return FALSE;
- }
- return (AssignProcess(hProcess));
- }
- BOOL CTWProcHelper::SetBasicLimitInfo(PJOBOBJECT_BASIC_LIMIT_INFORMATION pjobli)
- {
- return(SetInformationJobObject(m_hJob,
- JobObjectBasicLimitInformation, pjobli, sizeof(*pjobli)));
- }
- BOOL CTWProcHelper::SetExtendedLimitInfo(PJOBOBJECT_EXTENDED_LIMIT_INFORMATION pjobeli)
- {
- return(SetInformationJobObject(m_hJob,
- JobObjectExtendedLimitInformation, pjobeli, sizeof(*pjobeli)));
- }
- BOOL CTWProcHelper::SetBasicUIRestrictions(DWORD fdwLimits)
- {
- JOBOBJECT_BASIC_UI_RESTRICTIONS jobuir = { fdwLimits };
- return(SetInformationJobObject(m_hJob,
- JobObjectBasicUIRestrictions, &jobuir, sizeof(jobuir)));
- }
- BOOL CTWProcHelper::QueryEndOfJobTimeInfo(PDWORD pfdwEndOfJobTimeInfo)
- {
- JOBOBJECT_END_OF_JOB_TIME_INFORMATION joeojti;
- BOOL bRet = QueryInformationJobObject(m_hJob, JobObjectBasicUIRestrictions,
- &joeojti, sizeof(joeojti), NULL);
- if (bRet)
- *pfdwEndOfJobTimeInfo = joeojti.EndOfJobTimeAction;
- return(bRet);
- }
- BOOL CTWProcHelper::SetSecurityLimitInfo(PJOBOBJECT_SECURITY_LIMIT_INFORMATION pjobsli)
- {
- return(SetInformationJobObject(m_hJob,
- JobObjectSecurityLimitInformation, pjobsli, sizeof(*pjobsli)));
- }
- BOOL CTWProcHelper::SetEndOfJobInfo(DWORD fdwEndOfJobInfo)
- {
- JOBOBJECT_END_OF_JOB_TIME_INFORMATION joeojti;
- joeojti.EndOfJobTimeAction = fdwEndOfJobInfo;
- return(SetInformationJobObject(m_hJob,
- JobObjectEndOfJobTimeInformation, &joeojti, sizeof(joeojti)));
- }
- BOOL CTWProcHelper::QueryBaseAndIOInfo(PJOBOBJECT_BASIC_AND_IO_ACCOUNTING_INFORMATION pjobai)
- {
- return(QueryInformationJobObject(m_hJob,
- JobObjectBasicAndIoAccountingInformation, pjobai, sizeof(*pjobai),
- NULL));
- }
- BOOL CTWProcHelper::QueryExtendLimitInfo(PJOBOBJECT_EXTENDED_LIMIT_INFORMATION pjobeli)
- {
- return(QueryInformationJobObject(m_hJob,
- JobObjectExtendedLimitInformation, pjobeli, sizeof(*pjobeli),
- NULL));
- }
- BOOL CTWProcHelper::QueryBasicProcessIdList(DWORD dwMaxProcesses, PDWORD pdwProcessIdList, PDWORD pdwProcessesReturned)
- {
- // Calculate the # of bytes necessary
- DWORD cb = sizeof(JOBOBJECT_BASIC_PROCESS_ID_LIST) +
- (sizeof(DWORD) * (dwMaxProcesses - 1));
- // Allocate those bytes from the stack
- PJOBOBJECT_BASIC_PROCESS_ID_LIST pjobProIdList =
- (PJOBOBJECT_BASIC_PROCESS_ID_LIST) _alloca(cb);
- if(pjobProIdList == NULL)
- return FALSE;
- pjobProIdList->NumberOfProcessIdsInList = dwMaxProcesses;
- BOOL bRet = QueryInformationJobObject(m_hJob,
- JobObjectBasicProcessIdList, pjobProIdList, cb, NULL);
- if(bRet)
- {
- if(pdwProcessesReturned != NULL)
- {
- *pdwProcessesReturned = pjobProIdList->NumberOfProcessIdsInList;
- }
- CopyMemory(pdwProcessIdList, pjobProIdList->ProcessIdList,
- sizeof(DWORD) * pjobProIdList->NumberOfProcessIdsInList);
- }
- return bRet;
- }
- BOOL CTWProcHelper::QueryBasicUIRestrictions(PDWORD pfdwRestrictions)
- {
- JOBOBJECT_BASIC_UI_RESTRICTIONS jobuir;
- BOOL bRet = QueryInformationJobObject(m_hJob, JobObjectBasicUIRestrictions,
- &jobuir, sizeof(jobuir), NULL);
- if (bRet)
- *pfdwRestrictions = jobuir.UIRestrictionsClass;
- return(bRet);
- }
- BOOL CTWProcHelper::QuerySecurityLimitInfo(
- PJOBOBJECT_SECURITY_LIMIT_INFORMATION pjosli)
- {
- return(QueryInformationJobObject(m_hJob, JobObjectSecurityLimitInformation,
- pjosli, sizeof(*pjosli), NULL));
- }
- BOOL CTWProcHelper::Terminate(UINT uExitCode)
- {
- return(TerminateJobObject(m_hJob, uExitCode));
- }
- BOOL CTWProcHelper::AssociateCompletionPort(HANDLE hIOCP, ULONG_PTR CompKey)
- {
- JOBOBJECT_ASSOCIATE_COMPLETION_PORT joacp = { (PVOID) CompKey, hIOCP };
- return(SetInformationJobObject(m_hJob,
- JobObjectAssociateCompletionPortInformation, &joacp, sizeof(joacp)));
- }
- BOOL CTWProcHelper::QueryAssociatedCompletionPort
- (PJOBOBJECT_ASSOCIATE_COMPLETION_PORT pjoacp)
- {
- return(QueryInformationJobObject(m_hJob,
- JobObjectAssociateCompletionPortInformation, pjoacp, sizeof(*pjoacp),
- NULL));
- }
- void CTWProcHelper::GetProcessName(DWORD PID, PTSTR szProcessName, size_t cchSize)
- {
- HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
- FALSE, PID);
- if (hProcess == NULL) {
- _tcscpy_s(szProcessName, cchSize, TEXT("???"));
- return;
- }
- if (GetModuleFileNameEx(hProcess, (HMODULE)0, szProcessName, cchSize)
- == 0) {
- // GetModuleFileNameEx could fail when the address space
- // is not completely initialized. This occurs when the job
- // notification happens.
- // Hopefully, GetProcessImageFileNameW still works even though
- // the obtained path is more complication to decipher
- // \Device\HarddiskVolume1\Windows\System32\notepad.exe
- //if (!GetProcessImageFileName(hProcess, szProcessName, cchSize)) {
- // _tcscpy_s(szProcessName, cchSize, TEXT("???"));
- //}
- DWORD dwSize = (DWORD) cchSize;
- if(QueryFullProcessImageName(hProcess, 0, szProcessName, &dwSize) == 0)
- {
- if (!GetProcessImageFileName(hProcess, szProcessName, cchSize)) {
- _tcscpy_s(szProcessName, cchSize, TEXT("???"));
- }
- }
- }
- // but it is easier to call this function instead that works fine
- // in all situations.
- // Don't forget to close the process handle
- CloseHandle(hProcess);
- }
- BOOL CTWProcHelper::DestoryProcess(DWORD dwProcessId)
- {
- HANDLE hProcess = OpenProcess(
- PROCESS_SET_QUOTA | PROCESS_TERMINATE, FALSE, dwProcessId);
- if(hProcess == NULL)
- {
- PrintError("The hProcess is null which get from ProcessId");
- return FALSE;
- }
- BOOL bRet = TerminateProcess(hProcess, 0);
- if(!bRet)
- {
- PrintError("TerminateProcess failed");
- }
- return bRet;
- }
- //===== Above is Functions about Job ========================
- DWORD CTWProcHelper::ModifyDefaultDacl(HANDLE hProcess)
- {
- int i;
- ACL_SIZE_INFORMATION asi;
- ACCESS_ALLOWED_ACE *pTempAce;
- DWORD dwNewAclSize;
- DWORD dwSize = 0;
- DWORD dwTokenInfoLength = 0;
- DWORD dwResult = -1;
- HANDLE hToken = NULL;
- PACL pNewAcl = NULL;
- PSID pEveryoneSID = NULL;
- SID_IDENTIFIER_AUTHORITY sidAuthWorld = SECURITY_WORLD_SID_AUTHORITY;
- TOKEN_DEFAULT_DACL tddNew;
- TOKEN_DEFAULT_DACL *ptdd = NULL;
- TOKEN_INFORMATION_CLASS tic = TokenDefaultDacl;
- __try
- {
- //获取与进程关联的令牌
- if (!OpenProcessToken(hProcess, TOKEN_QUERY |
- TOKEN_ADJUST_DEFAULT, &hToken))
- {
- dwResult = GetLastError();
- __leave;
- }
- if (!GetTokenInformation(hToken, tic, (LPVOID)NULL,
- dwTokenInfoLength, &dwSize))
- {
- if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
- {
- ptdd = (TOKEN_DEFAULT_DACL *) LocalAlloc(LPTR, dwSize);
- if (ptdd == NULL)
- {
- dwResult = GetLastError();
- __leave;
- }
- //直接获取令牌的缺省 DACL
- if (!GetTokenInformation(hToken, tic, (LPVOID)ptdd, dwSize, &dwSize))
- {
- dwResult = GetLastError();
- __leave;
- }
- }
- else
- {
- dwResult = GetLastError();
- __leave;
- }
- }
- //获取访问控制列表信息
- if (!GetAclInformation(ptdd->DefaultDacl, (LPVOID)&asi,
- (DWORD)sizeof(ACL_SIZE_INFORMATION),
- AclSizeInformation))
- {
- dwResult = GetLastError();
- __leave;
- }
- //
- // Create a well-known SID for the Everyone group.
- //
- if (!AllocateAndInitializeSid(&sidAuthWorld, 1, SECURITY_WORLD_RID,
- 0, 0, 0, 0, 0, 0, 0,
- &pEveryoneSID))
- {
- dwResult = GetLastError();
- __leave;
- }
- //
- // Compute the size of the new ACL.
- //
- dwNewAclSize = asi.AclBytesInUse + sizeof(ACCESS_ALLOWED_ACE) +
- GetLengthSid(pEveryoneSID) - sizeof(DWORD);
- //
- // Allocate buffer for the new ACL.
- //
- pNewAcl = (PACL) LocalAlloc(LPTR, dwNewAclSize);
- if (pNewAcl == NULL)
- {
- dwResult = GetLastError();
- __leave;
- }
- //
- // Intialize the ACL.
- //
- if (!InitializeAcl(pNewAcl, dwNewAclSize, ACL_REVISION))
- {
- dwResult = GetLastError();
- __leave;
- }
- //
- // Loop through all the ACEs.
- // 轮询所有的访问控制项
- //
- for (i = 0; i < (int) asi.AceCount; i++)
- {
- //
- // Get current ACE.
- //
- if (!GetAce(ptdd->DefaultDacl, i, (LPVOID *)&pTempAce))
- {
- dwResult = GetLastError();
- __leave;
- }
- //
- // 添加访问控制项到指定的访问控制列表
- //
- if (!AddAce(pNewAcl, ACL_REVISION, MAXDWORD, pTempAce,
- ((PACE_HEADER)pTempAce)->AceSize))
- {
- dwResult = GetLastError();
- __leave;
- }
- }
- //
- // 添加任意访问控制项到访问控制项列表的末尾
- //
- if (!AddAccessAllowedAce(pNewAcl, ACL_REVISION, GENERIC_ALL,/*访问权限掩码指定ACE控制的访问权限*/
- pEveryoneSID))
- {
- dwResult = GetLastError();
- __leave;
- }
- //
- // Set the new Default DACL.
- //
- tddNew.DefaultDacl = pNewAcl;
- if (!SetTokenInformation(hToken, tic, (LPVOID)&tddNew,
- dwNewAclSize))
- {
- dwResult = GetLastError();
- __leave;
- }
- dwResult = 0;
- }
- __finally
- {
- //
- // Free the buffer for the sid.
- //
- if (pEveryoneSID)
- {
- FreeSid(pEveryoneSID);
- }
- //
- // Free the buffers.
- //
- if (pNewAcl)
- {
- LocalFree((HLOCAL)pNewAcl);
- }
- if (ptdd)
- {
- LocalFree((HLOCAL)ptdd);
- }
- //
- // Close the access token.
- //
- if (hToken)
- {
- CloseHandle(hToken);
- }
- }
- return dwResult;
- }
- BOOL CTWProcHelper::SetPrivilege(LPCTSTR lpszPrivilege, BOOL bEnablePrivilege)
- {
- HANDLE hToken = NULL;
- if (!OpenProcessToken(GetCurrentProcess(),
- TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
- {
- PrintError("OpenProcessToken failed");
- return FALSE;
- }
- return SetPrivilege(hToken, lpszPrivilege, bEnablePrivilege);
- }
- BOOL CTWProcHelper::SetPrivilege(HANDLE hToken, LPCTSTR lpszPrivilege, BOOL bEnablePrivilege)
- {
- TOKEN_PRIVILEGES tp;
- LUID luid;
- if ( !LookupPrivilegeValue(
- NULL, // lookup privilege on local system
- lpszPrivilege, // privilege to lookup
- &luid ) ) // receives LUID of privilege
- {
- Dbg("LookupPrivilegeValue error: %u\n", GetLastError() );
- return FALSE;
- }
- tp.PrivilegeCount = 1;
- tp.Privileges[0].Luid = luid;
- if (bEnablePrivilege)
- tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
- else
- tp.Privileges[0].Attributes = 0;
- // Enable the privilege or disable all privileges.
- if ( !AdjustTokenPrivileges(
- hToken,
- FALSE,
- &tp,
- sizeof(TOKEN_PRIVILEGES),
- (PTOKEN_PRIVILEGES) NULL,
- (PDWORD) NULL) )
- {
- Dbg("AdjustTokenPrivileges error: %u\n", GetLastError() );
- return FALSE;
- }
- if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
- {
- Dbg("The token does not have the specified privilege. \n");
- return FALSE;
- }
- return TRUE;
- }
- BOOL CTWProcHelper::CreateLowerProcess(LPTSTR lpCommandLine)
- {
- BOOL bResult = 0;
- HANDLE hToken = NULL;
- HANDLE hNewToken = NULL;
- // Low integrity SID
- CHAR szIntegritySid[20] = "S-1-16-4096";
- PSID pIntegritySid = NULL;
- TOKEN_MANDATORY_LABEL tml = {0};
- PROCESS_INFORMATION pi = {0};
- STARTUPINFO si = {0};
- ULONG exitCode = 0;
- __try
- {
- if (!OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED, &hToken))
- __leave;
- if (DuplicateTokenEx(hToken, MAXIMUM_ALLOWED, NULL,
- SecurityImpersonation, TokenPrimary, &hNewToken))
- {
- if (ConvertStringSidToSid(szIntegritySid, &pIntegritySid))
- {
- tml.Label.Attributes = SE_GROUP_INTEGRITY;
- tml.Label.Sid = pIntegritySid;
- // Set the process integrity level
- if (SetTokenInformation(hNewToken, TokenIntegrityLevel, &tml,
- sizeof(TOKEN_MANDATORY_LABEL) + GetLengthSid(pIntegritySid)))
- {
- bResult = CreateProcessAsUser(
- hNewToken, // access token
- NULL, // file to execute
- lpCommandLine, // command line
- NULL, // pointer to process SECURITY_ATTRIBUTES
- NULL, // pointer to thread SECURITY_ATTRIBUTES
- FALSE, // handles are not inheritable
- 0, // creation flags
- NULL, // pointer to new environment block
- NULL, // name of current directory
- &si, // pointer to STARTUPINFO structure
- &pi // receives information about new process
- );
- //DWORD dw=GetLastError();
- //CHAR ch[100];
- //sDbg(ch, "CreateProcessAsUser getlasterror =%d",dw);
- //MessageBox(NULL,ch,"13",MB_OK);
- RevertToSelf();
- if(bResult)
- {
- if (pi.hProcess != INVALID_HANDLE_VALUE)
- {
- //WaitForSingleObject(pi.hProcess, INFINITE);
- CloseHandle(pi.hProcess);
- }
- if (pi.hThread != INVALID_HANDLE_VALUE)
- CloseHandle(pi.hThread);
- }
- }
- }
- }
- }
- __finally
- {
- if(pIntegritySid)
- {
- LocalFree(pIntegritySid);
- }
- //
- // Close the access token.
- //
- if (hToken)
- {
- CloseHandle(hToken);
- }
- if(hNewToken)
- {
- CloseHandle(hNewToken);
- }
- }
- return bResult;
- }
- BOOL CTWProcHelper::LowerThreadIntegirtyLevel()
- {
- if(!ImpersonateSelf(SecurityIdentification))
- {
- PrintError("ImpersonateSelf(SecurityIdentification)");
- return FALSE;
- }
- return TRUE;
- HANDLE hToken = NULL;
- HANDLE hNewToken = NULL;
- BOOL bResult = FALSE;
- PSID pEveryoneSID = NULL;
- SID_IDENTIFIER_AUTHORITY sidManLabel = SECURITY_MANDATORY_LABEL_AUTHORITY;
- PSID pIntegritySid = NULL;
- TOKEN_MANDATORY_LABEL tml = { 0 };
- __try
- {
- //获取与进程关联的令牌
- if (!OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE | TOKEN_QUERY
- | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT | TOKEN_ASSIGN_PRIMARY,
- &hToken))
- {
- PrintError("OpenProcessToken failed");
- __leave;
- }
- // Duplicate the primary token of the current process.
- if (!DuplicateTokenEx(hToken, 0, NULL, SecurityImpersonation,
- TokenPrimary, &hNewToken))
- {
- PrintError("DuplicateTokenEx failed");
- __leave;
- }
- // Create the low integrity SID.
- if (!AllocateAndInitializeSid(&sidManLabel, 1, SECURITY_MANDATORY_LOW_RID,
- 0, 0, 0, 0, 0, 0, 0, &pIntegritySid))
- {
- PrintError("AllocateAndInitializeSid failed");
- __leave;
- }
- tml.Label.Attributes = SE_GROUP_INTEGRITY;
- tml.Label.Sid = pIntegritySid;
- // Set the integrity level in the access token to low.
- if (!SetTokenInformation(hNewToken, TokenIntegrityLevel, &tml,
- (sizeof(TOKEN_MANDATORY_LABEL) + GetLengthSid(pIntegritySid))))
- {
- PrintError("SetTokenInformation failed");
- __leave;
- }
- HANDLE hThread = GetCurrentThread();
- if (!SetThreadToken(&hThread, hNewToken))
- {
- PrintError("SetThreadToken failed");
- __leave;
- }
- RevertToSelf();
- bResult = TRUE;
- }
- __finally
- {
- if(pIntegritySid)
- {
- LocalFree(pIntegritySid);
- }
- //
- // Close the access token.
- //
- if (hToken)
- {
- CloseHandle(hToken);
- }
- if(hNewToken)
- {
- CloseHandle(hNewToken);
- }
- }
- return bResult;
- }
- void CTWProcHelper::SetLowLabelToFile(LPCTSTR lpszFileName)
- {
- if(lpszFileName == NULL || strlen(lpszFileName) <= 0)
- return;
- // The LABEL_SECURITY_INFORMATION SDDL SACL to be set for low integrity
- #define LOW_INTEGRITY_SDDL_SACL_W "S:(ML;;NW;;;LW)"
- DWORD dwErr = ERROR_SUCCESS;
- PSECURITY_DESCRIPTOR pSD = NULL;
- //SECURITY_MANDATORY_LOW_RID
- PACL pSacl = NULL; // not allocated
- BOOL fSaclPresent = FALSE;
- BOOL fSaclDefaulted = FALSE;
- CHAR szPath[MAX_PATH] = {0};
- _tcscpy_s(szPath, lpszFileName);
- Dbg("szPath:%s", szPath);
- if (ConvertStringSecurityDescriptorToSecurityDescriptorA(
- LOW_INTEGRITY_SDDL_SACL_W, SDDL_REVISION_1, &pSD, NULL))
- {
- //Dbg("enter 1");
- if (GetSecurityDescriptorSacl(pSD, &fSaclPresent, &pSacl,
- &fSaclDefaulted))
- {
- //Dbg("enter 2");
- // Note that psidOwner, psidGroup, and pDacl are
- // all NULL and set the new LABEL_SECURITY_INFORMATION
- dwErr = SetNamedSecurityInfoA((LPSTR)szPath,
- SE_FILE_OBJECT, LABEL_SECURITY_INFORMATION,
- NULL, NULL, NULL, pSacl);
- //Dbg("enter 3: %d.", dwErr);
- }
- LocalFree(pSD);
- }
- }
- BOOL CTWProcHelper::GetProcessIntegrityLevel(HANDLE hProcess, PDWORD pIntegrityLevel,
- PDWORD pPolicy, PDWORD pResourceIntegrityLevel, PDWORD pResourcePolicy)
- {
- HANDLE hToken = NULL;
- if (!OpenProcessToken(hProcess, TOKEN_READ, &hToken))
- {
- return(FALSE);
- }
- BOOL bReturn = FALSE;
- // First, compute the size of the buffer to get the Integrity level
- DWORD dwNeededSize = 0;
- if (!GetTokenInformation(hToken, TokenIntegrityLevel, NULL, 0, &dwNeededSize))
- {
- PTOKEN_MANDATORY_LABEL pTokenInfo = NULL;
- if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
- {
- pTokenInfo = (PTOKEN_MANDATORY_LABEL)LocalAlloc(0, dwNeededSize);
- if (pTokenInfo != NULL)
- {
- if (GetTokenInformation(hToken, TokenIntegrityLevel, pTokenInfo,
- dwNeededSize, &dwNeededSize))
- {
- *pIntegrityLevel =
- *GetSidSubAuthority(
- pTokenInfo->Label.Sid,
- (*GetSidSubAuthorityCount(pTokenInfo->Label.Sid)-1)
- );
- bReturn = TRUE;
- }
- LocalFree(pTokenInfo);
- }
- }
- }
- // Try to get the policy if the integrity level was available
- if (bReturn)
- {
- *pPolicy = TOKEN_MANDATORY_POLICY_OFF;
- dwNeededSize = sizeof(DWORD);
- GetTokenInformation(hToken, TokenMandatoryPolicy, pPolicy,
- dwNeededSize, &dwNeededSize);
- }
- // Look for the resource policy
- *pResourceIntegrityLevel = 0; // 0 means none explicitely set
- *pResourcePolicy = 0;
- PACL pSACL = NULL;
- PSECURITY_DESCRIPTOR pSD = NULL;
- DWORD dwResult = ERROR_SUCCESS;
- // Look for the no-read-up/no-write-up policy in the SACL
- if (hToken != NULL)
- {
- dwResult = GetSecurityInfo(
- hProcess, SE_KERNEL_OBJECT,
- LABEL_SECURITY_INFORMATION,
- NULL, NULL, NULL,
- &pSACL, &pSD
- );
- if (dwResult == ERROR_SUCCESS)
- {
- if (pSACL != NULL)
- {
- SYSTEM_MANDATORY_LABEL_ACE* pACE = NULL;
- if ((pSACL->AceCount > 0) && (GetAce(pSACL, 0, (PVOID*)&pACE)))
- {
- if (pACE != NULL)
- {
- SID* pSID = (SID*)(&pACE->SidStart);
- *pResourceIntegrityLevel = pSID->SubAuthority[0];
- *pResourcePolicy = pACE->Mask;
- }
- }
- }
- }
- // Cleanup memory allocated on our behalf
- if (pSD != NULL)
- LocalFree(pSD);
- }
- // Don't forget to close the token handle.
- CloseHandle(hToken);
- return(bReturn);
- }
- BOOL CTWProcHelper::GetProcessIntegrityLevel(DWORD PID, PDWORD pIntegrityLevel,
- PDWORD pPolicy, PDWORD pResourceIntegrityLevel, PDWORD pResourcePolicy)
- {
- // Sanity checks
- if ((PID <= 0) || (pIntegrityLevel == NULL))
- return(FALSE);
- // Check if we can get information for this process
- HANDLE hProcess = OpenProcess(
- READ_CONTROL | PROCESS_QUERY_INFORMATION,
- FALSE, PID);
- if (hProcess == NULL)
- return(FALSE);
- BOOL bReturn = GetProcessIntegrityLevel(hProcess, pIntegrityLevel,
- pPolicy, pResourceIntegrityLevel, pResourcePolicy);
- // Don't forget to release the process handle
- CloseHandle(hProcess);
- return(bReturn);
- }
- BOOL CTWProcHelper::CreateSecurityDescriptor()
- {
- BOOL bRet = FALSE;
- DWORD dwRes, dwDisposition;
- PSID pEveryoneSID = NULL, pAdminSID = NULL;
- PACL pACL = NULL;
- PSECURITY_DESCRIPTOR pSD = NULL;
- EXPLICIT_ACCESS ea[2];
- SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY;
- SID_IDENTIFIER_AUTHORITY SIDAuthNT = SECURITY_NT_AUTHORITY;
- SECURITY_ATTRIBUTES sa;
- LONG lRes;
- HKEY hkSub = NULL;
- // Create a well-known SID for the Everyone group.
- if(!AllocateAndInitializeSid(&SIDAuthWorld, 1,
- SECURITY_WORLD_RID,
- 0, 0, 0, 0, 0, 0, 0,
- &pEveryoneSID))
- {
- Dbg(_T("AllocateAndInitializeSid Error %u\n"), GetLastError());
- goto Cleanup;
- }
- // Initialize an EXPLICIT_ACCESS structure for an ACE.
- // The ACE will allow Everyone read access to the key.
- ZeroMemory(&ea, 2 * sizeof(EXPLICIT_ACCESS));
- ea[0].grfAccessPermissions = KEY_READ;
- ea[0].grfAccessMode = SET_ACCESS;
- ea[0].grfInheritance= NO_INHERITANCE;
- ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
- ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
- ea[0].Trustee.ptstrName = (LPTSTR) pEveryoneSID;
- // Create a SID for the BUILTIN\Administrators group.
- if(! AllocateAndInitializeSid(&SIDAuthNT, 2,
- SECURITY_BUILTIN_DOMAIN_RID,
- DOMAIN_ALIAS_RID_ADMINS,
- 0, 0, 0, 0, 0, 0,
- &pAdminSID))
- {
- Dbg(_T("AllocateAndInitializeSid Error %u\n"), GetLastError());
- goto Cleanup;
- }
- // Initialize an EXPLICIT_ACCESS structure for an ACE.
- // The ACE will allow the Administrators group full access to
- // the key.
- ea[1].grfAccessPermissions = KEY_ALL_ACCESS;
- ea[1].grfAccessMode = SET_ACCESS;
- ea[1].grfInheritance= NO_INHERITANCE;
- ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
- ea[1].Trustee.TrusteeType = TRUSTEE_IS_GROUP;
- ea[1].Trustee.ptstrName = (LPTSTR) pAdminSID;
- // Create a new ACL that contains the new ACEs.
- dwRes = SetEntriesInAcl(2, ea, NULL, &pACL);
- if (ERROR_SUCCESS != dwRes)
- {
- Dbg(_T("SetEntriesInAcl Error %u\n"), GetLastError());
- goto Cleanup;
- }
- // Initialize a security descriptor.
- pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR,
- SECURITY_DESCRIPTOR_MIN_LENGTH);
- if (NULL == pSD)
- {
- Dbg(_T("LocalAlloc Error %u\n"), GetLastError());
- goto Cleanup;
- }
- if (!InitializeSecurityDescriptor(pSD,
- SECURITY_DESCRIPTOR_REVISION))
- {
- Dbg(_T("InitializeSecurityDescriptor Error %u\n"),
- GetLastError());
- goto Cleanup;
- }
- // Add the ACL to the security descriptor.
- if (!SetSecurityDescriptorDacl(pSD,
- TRUE, // bDaclPresent flag
- pACL,
- FALSE)) // not a default DACL
- {
- Dbg(_T("SetSecurityDescriptorDacl Error %u\n"),
- GetLastError());
- goto Cleanup;
- }
- // Initialize a security attributes structure.
- sa.nLength = sizeof (SECURITY_ATTRIBUTES);
- sa.lpSecurityDescriptor = pSD;
- sa.bInheritHandle = FALSE;
- // Use the security attributes to set the security descriptor
- // when you create a key.
- lRes = RegCreateKeyEx(HKEY_CURRENT_USER, _T("TWKey"), 0, _T(""), 0,
- KEY_READ | KEY_WRITE, &sa, &hkSub, &dwDisposition);
- Dbg(_T("RegCreateKeyEx result %u\n"), lRes );
- /*
-
- // 运行进程并等待其正常结束
- PROCESS_INFORMATION ProcessInfo;
- STARTUPINFO StartupInfo;
- ZeroMemory(&StartupInfo, sizeof(StartupInfo));
- StartupInfo.cb = sizeof(StartupInfo);
- if (CreateProcess("c:\\winnt\\notepad.exe", NULL,
- &sa, NULL, FALSE, 0, NULL,
- NULL, &StartupInfo, &ProcessInfo))
- {
- WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
- CloseHandle(ProcessInfo.hThread);
- CloseHandle(ProcessInfo.hProcess);
- }
- else
- {
- Dbg("CreateProcess failed!\n");
- goto Cleanup;
- }
- */
- if(lRes == ERROR_SUCCESS)
- bRet = TRUE;
- Cleanup:
- if (pEveryoneSID)
- FreeSid(pEveryoneSID);
- if (pAdminSID)
- FreeSid(pAdminSID);
- if (pACL)
- LocalFree(pACL);
- if (pSD)
- LocalFree(pSD);
- if (hkSub)
- RegCloseKey(hkSub);
- return bRet;
- }
- DWORD CTWProcHelper::AddAceToObjectsSecurityDescriptor(
- LPTSTR pszObjName,
- SE_OBJECT_TYPE ObjectType,
- LPTSTR pszTrustee,
- TRUSTEE_FORM TrusteeForm,
- DWORD dwAccessRights,
- ACCESS_MODE AccessMode,
- DWORD dwInheritance)
- {
- DWORD dwRes = 0;
- PACL pOldDACL = NULL, pNewDACL = NULL;
- PSECURITY_DESCRIPTOR pSD = NULL;
- EXPLICIT_ACCESS ea;
- if (NULL == pszObjName)
- return ERROR_INVALID_PARAMETER;
- // Get a pointer to the existing DACL.
- dwRes = GetNamedSecurityInfo(pszObjName, ObjectType,
- DACL_SECURITY_INFORMATION,
- NULL, NULL, &pOldDACL, NULL, &pSD);
- if (ERROR_SUCCESS != dwRes)
- {
- Dbg( "GetNamedSecurityInfo Error %u\n", dwRes );
- goto Cleanup;
- }
- // Initialize an EXPLICIT_ACCESS structure for the new ACE.
- ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS));
- ea.grfAccessPermissions = dwAccessRights;
- ea.grfAccessMode = AccessMode;
- ea.grfInheritance= dwInheritance;
- ea.Trustee.TrusteeForm = TrusteeForm;
- ea.Trustee.ptstrName = pszTrustee;
- // Create a new ACL that merges the new ACE
- // into the existing DACL.
- dwRes = SetEntriesInAcl(1, &ea, pOldDACL, &pNewDACL);
- if (ERROR_SUCCESS != dwRes)
- {
- Dbg( "SetEntriesInAcl Error %u\n", dwRes );
- goto Cleanup;
- }
- // Attach the new ACL as the object's DACL.
- dwRes = SetNamedSecurityInfo(pszObjName, ObjectType,
- DACL_SECURITY_INFORMATION,
- NULL, NULL, pNewDACL, NULL);
- if (ERROR_SUCCESS != dwRes)
- {
- Dbg( "SetNamedSecurityInfo Error %u\n", dwRes );
- goto Cleanup;
- }
- Cleanup:
- if(pSD != NULL)
- LocalFree((HLOCAL) pSD);
- if(pNewDACL != NULL)
- LocalFree((HLOCAL) pNewDACL);
- return dwRes;
- }
- BOOL CTWProcHelper::TakeOwnership(LPTSTR lpszOwnFile)
- {
- BOOL bRetval = FALSE;
- HANDLE hToken = NULL;
- PSID pSIDAdmin = NULL;
- PSID pSIDEveryone = NULL;
- PACL pACL = NULL;
- SID_IDENTIFIER_AUTHORITY SIDAuthWorld =
- SECURITY_WORLD_SID_AUTHORITY;
- SID_IDENTIFIER_AUTHORITY SIDAuthNT = SECURITY_NT_AUTHORITY;
- const int NUM_ACES = 2;
- EXPLICIT_ACCESS ea[NUM_ACES];
- DWORD dwRes;
- // Specify the DACL to use.
- // Create a SID for the Everyone group.
- if (!AllocateAndInitializeSid(&SIDAuthWorld, 1,
- SECURITY_WORLD_RID,
- 0,
- 0, 0, 0, 0, 0, 0,
- &pSIDEveryone))
- {
- Dbg("AllocateAndInitializeSid (Everyone) error %u\n",
- GetLastError());
- goto Cleanup;
- }
- // Create a SID for the BUILTIN\Administrators group.
- if (!AllocateAndInitializeSid(&SIDAuthNT, 2,
- SECURITY_BUILTIN_DOMAIN_RID,
- DOMAIN_ALIAS_RID_ADMINS,
- 0, 0, 0, 0, 0, 0,
- &pSIDAdmin))
- {
- Dbg("AllocateAndInitializeSid (Admin) error %u\n",
- GetLastError());
- goto Cleanup;
- }
- ZeroMemory(&ea, NUM_ACES * sizeof(EXPLICIT_ACCESS));
- // Set read access for Everyone.
- ea[0].grfAccessPermissions = GENERIC_READ; //指定ACE允许,否认或审查受托人权限的位标志
- ea[0].grfAccessMode = SET_ACCESS; //对于DACL, 指定ACL是否允许或者拒绝指定访问权限
- ea[0].grfInheritance = NO_INHERITANCE;
- ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
- ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
- ea[0].Trustee.ptstrName = (LPTSTR) pSIDEveryone;
- // Set full control for Administrators.
- ea[1].grfAccessPermissions = GENERIC_ALL;
- ea[1].grfAccessMode = SET_ACCESS;
- ea[1].grfInheritance = NO_INHERITANCE;
- ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
- ea[1].Trustee.TrusteeType = TRUSTEE_IS_GROUP;
- ea[1].Trustee.ptstrName = (LPTSTR) pSIDAdmin;
- if (ERROR_SUCCESS != SetEntriesInAcl(NUM_ACES, ea, NULL, &pACL))
- {
- Dbg("Failed SetEntriesInAcl\n");
- goto Cleanup;
- }
- // Try to modify the object's DACL.
- dwRes = SetNamedSecurityInfo(
- lpszOwnFile, // name of the object
- SE_FILE_OBJECT, // type of object
- DACL_SECURITY_INFORMATION, // change only the object's DACL
- NULL, NULL, // do not change owner or group
- pACL, // DACL specified
- NULL); // do not change SACL
- if (ERROR_SUCCESS == dwRes)
- {
- Dbg("Successfully changed DACL\n");
- bRetval = TRUE;
- // No more processing needed.
- goto Cleanup;
- }
- if (dwRes != ERROR_ACCESS_DENIED)
- {
- Dbg("First SetNamedSecurityInfo call failed: %u\n", dwRes);
- goto Cleanup;
- }
- // If the preceding call failed because access was denied,
- // enable the SE_TAKE_OWNERSHIP_NAME privilege, create a SID for
- // the Administrators group, take ownership of the object, and
- // disable the privilege. Then try again to set the object's DACL.
- // Open a handle to the access token for the calling process.
- if (!OpenProcessToken(GetCurrentProcess(),
- TOKEN_ADJUST_PRIVILEGES,
- &hToken))
- {
- Dbg("OpenProcessToken failed: %u\n", GetLastError());
- goto Cleanup;
- }
- // Enable the SE_TAKE_OWNERSHIP_NAME privilege.
- if (!SetPrivilege(hToken, SE_TAKE_OWNERSHIP_NAME, TRUE))
- {
- Dbg("You must be logged on as Administrator.\n");
- goto Cleanup;
- }
- // Set the owner in the object's security descriptor.
- dwRes = SetNamedSecurityInfo(
- lpszOwnFile, // name of the object
- SE_FILE_OBJECT, // type of object
- OWNER_SECURITY_INFORMATION, // change only the object's owner
- pSIDAdmin, // SID of Administrator group
- NULL,
- NULL,
- NULL);
- if (dwRes != ERROR_SUCCESS)
- {
- Dbg("Could not set owner. Error: %u\n", dwRes);
- goto Cleanup;
- }
- // Disable the SE_TAKE_OWNERSHIP_NAME privilege.
- if (!SetPrivilege(hToken, SE_TAKE_OWNERSHIP_NAME, FALSE))
- {
- Dbg("Failed SetPrivilege call unexpectedly.\n");
- goto Cleanup;
- }
- // Try again to modify the object's DACL,
- // now that we are the owner.
- dwRes = SetNamedSecurityInfo(
- lpszOwnFile, // name of the object
- SE_FILE_OBJECT, // type of object
- DACL_SECURITY_INFORMATION, // change only the object's DACL
- NULL, NULL, // do not change owner or group
- pACL, // DACL specified
- NULL); // do not change SACL
- if (dwRes == ERROR_SUCCESS)
- {
- Dbg("Successfully changed DACL\n");
- bRetval = TRUE;
- }
- else
- {
- Dbg("Second SetNamedSecurityInfo call failed: %u\n", dwRes);
- }
- Cleanup:
- if (pSIDAdmin)
- FreeSid(pSIDAdmin);
- if (pSIDEveryone)
- FreeSid(pSIDEveryone);
- if (pACL)
- LocalFree(pACL);
- if (hToken)
- CloseHandle(hToken);
- return bRetval;
- }
- BOOL CTWProcHelper::FindOwnerOfFile(LPCTSTR lpszOwnFile, LPTSTR lpszOwnerAccount)
- {
- DWORD dwRtnCode = 0;
- PSID pSidOwner = NULL;
- BOOL bRtnBool = TRUE;
- LPTSTR AcctName = NULL;
- LPTSTR DomainName = NULL;
- DWORD dwAcctName = 1, dwDomainName = 1;
- SID_NAME_USE eUse = SidTypeUnknown;
- HANDLE hFile;
- PSECURITY_DESCRIPTOR pSD = NULL;
- // Get the handle of the file object.
- hFile = CreateFile(
- lpszOwnFile,
- GENERIC_READ,
- FILE_SHARE_READ,
- NULL,
- OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL,
- NULL);
- // Check GetLastError for CreateFile error code.
- if (hFile == INVALID_HANDLE_VALUE)
- {
- DWORD dwErrorCode = 0;
- dwErrorCode = GetLastError();
- Dbg(TEXT("CreateFile error = %d\n"), dwErrorCode);
- return FALSE;
- }
- // Get the owner SID of the file.
- dwRtnCode = GetSecurityInfo(
- hFile,
- SE_FILE_OBJECT,
- OWNER_SECURITY_INFORMATION,
- &pSidOwner,
- NULL,
- NULL,
- NULL,
- &pSD);
- // Check GetLastError for GetSecurityInfo error condition.
- if (dwRtnCode != ERROR_SUCCESS)
- {
- DWORD dwErrorCode = 0;
- dwErrorCode = GetLastError();
- Dbg(TEXT("GetSecurityInfo error = %d\n"), dwErrorCode);
- return FALSE;
- }
- // First call to LookupAccountSid to get the buffer sizes.
- bRtnBool = LookupAccountSid(
- NULL, // local computer
- pSidOwner,
- AcctName,
- (LPDWORD)&dwAcctName,
- DomainName,
- (LPDWORD)&dwDomainName,
- &eUse);
- // Reallocate memory for the buffers.
- AcctName = (LPTSTR)GlobalAlloc(
- GMEM_FIXED,
- dwAcctName);
- // Check GetLastError for GlobalAlloc error condition.
- if (AcctName == NULL)
- {
- DWORD dwErrorCode = 0;
- dwErrorCode = GetLastError();
- Dbg(TEXT("GlobalAlloc error = %d\n"), dwErrorCode);
- return FALSE;
- }
- DomainName = (LPTSTR)GlobalAlloc(GMEM_FIXED, dwDomainName);
- // Check GetLastError for GlobalAlloc error condition.
- if (DomainName == NULL)
- {
- DWORD dwErrorCode = 0;
- dwErrorCode = GetLastError();
- Dbg(TEXT("GlobalAlloc error = %d\n"), dwErrorCode);
- return FALSE;
- }
- // Second call to LookupAccountSid to get the account name.
- bRtnBool = LookupAccountSid(
- NULL, // name of local or remote computer
- pSidOwner, // security identifier
- AcctName, // account name buffer
- (LPDWORD)&dwAcctName, // size of account name buffer
- DomainName, // domain name
- (LPDWORD)&dwDomainName, // size of domain name buffer
- &eUse); // SID type
- // Check GetLastError for LookupAccountSid error condition.
- if (bRtnBool == FALSE)
- {
- DWORD dwErrorCode = 0;
- dwErrorCode = GetLastError();
- if (dwErrorCode == ERROR_NONE_MAPPED)
- Dbg(TEXT("Account owner not found for specified SID.\n"));
- else
- Dbg(TEXT("Error in LookupAccountSid.\n"));
- return FALSE;
- } else if (bRtnBool == TRUE)
- // Print the account name.
- sprintf(lpszOwnerAccount, "Account owner = %s", AcctName);
- return TRUE;
- }
- BOOL CTWProcHelper::CustomAccessCheck(LPTSTR lpszFileName)
- {
- BOOL bRet = FALSE;
- DWORD dwAccessDesired = GENERIC_READ | GENERIC_WRITE;
- GENERIC_MAPPING GenericMapping;
- PSECURITY_DESCRIPTOR pSD = NULL;
- if(!GetSecurityDescriptorOfFile(lpszFileName, &pSD))
- {
- PrintError("GetSecurityDescriptorOfFile failed.");
- if(pSD)
- TWHeapFree(pSD);
- return FALSE;
- }
- //mapping of generic access types to specific and standard access types
- //将通用访问类型映射到特定的标准访问类型
- //将通用权限控制标志和特定类型对象权限控制标志挂钩,即通用读写的映射,对于文件来说就是FILE_***
- GenericMapping.GenericRead = FILE_GENERIC_READ;
- GenericMapping.GenericWrite = FILE_GENERIC_WRITE;
- GenericMapping.GenericExecute = FILE_GENERIC_EXECUTE;
- GenericMapping.GenericAll = FILE_ALL_ACCESS;
- DWORD dwAccessAllow;
- bRet = ImpersonateAndCheckAccess(pSD, dwAccessDesired, &GenericMapping, &dwAccessAllow);
-
- if(pSD) TWHeapFree(pSD);
- return bRet;
- }
- BOOL CTWProcHelper::GetSecurityDescriptorOfFile(LPCSTR lpszFileName, PSECURITY_DESCRIPTOR *ppSD)
- {
- BOOL bRes = FALSE;
- DWORD dwNeeded = 0;
- //请求的安全信息类型
- SECURITY_INFORMATION si = OWNER_SECURITY_INFORMATION |
- GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION;
- bRes = GetFileSecurity(lpszFileName, si, *ppSD, 0, &dwNeeded);
- if(!bRes)
- {
- if(GetLastError() == ERROR_INSUFFICIENT_BUFFER)
- {
- *ppSD = (PSECURITY_DESCRIPTOR *)TWHeapAlloc(dwNeeded);
- if(!GetFileSecurity(lpszFileName, si, *ppSD, dwNeeded, &dwNeeded))
- {
- PrintError("GetFileSecurity failed.");
- return FALSE;
- }
- return TRUE;
- }
- }
- return FALSE;
- }
- BOOL CTWProcHelper::ImpersonateAndCheckAccess(
- PSECURITY_DESCRIPTOR pSD, // security descriptor to check
- DWORD dwAccessDesired, // access rights to check
- PGENERIC_MAPPING pGeneric, // generic mapping for object
- PDWORD pdwAccessAllowed // returns allowed access rights
- )
- {
- HANDLE hToken;
- HANDLE hImpersonatedToken = NULL;
- DWORD dwResult = -1;
- PRIVILEGE_SET PrivilegeSet;
- DWORD dwPrivSetSize = sizeof( PRIVILEGE_SET );
- BOOL fAccessGranted=FALSE;
- *pdwAccessAllowed = 0;
- // Get an impersonation token with the client's security context.
- if (!OpenThreadToken(GetCurrentThread(), TOKEN_DUPLICATE|TOKEN_QUERY, TRUE, &hToken))
- {
- PrintError("Warning: OpenProcessToken for GetCurrentThread failed.");
- if (GetLastError() != ERROR_NO_TOKEN)
- {
- goto Cleanup;
- }
- if (!OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &hToken))
- {
- dwResult = GetLastError();
- PrintError("OpenProcessToken for GetCurrentProcess failed.");
- goto Cleanup;
- }
- }
- if(!DuplicateToken(hToken, SecurityImpersonation, &hImpersonatedToken))
- {
- dwResult = GetLastError();
- PrintError("DuplicateToken failed.");
- goto Cleanup;
- }
- // Use the GENERIC_MAPPING structure to convert any
- // generic access rights to object-specific access rights.
- // 函数返回后 dwAccessDesired 中无通用占用位和未定义位集
- MapGenericMask(&dwAccessDesired, pGeneric);
- // Check the client's access rights.
- if( !AccessCheck(
- pSD, // security descriptor to check
- hImpersonatedToken, // impersonation token
- dwAccessDesired, // requested access rights
- pGeneric, // pointer to GENERIC_MAPPING
- &PrivilegeSet, // receives privileges used in check
- &dwPrivSetSize, // size of PrivilegeSet buffer
- pdwAccessAllowed, // receives mask of allowed access rights
- &fAccessGranted )) // receives results of access check
- {
- dwResult = GetLastError();
- if(dwResult == ERROR_INVALID_SECURITY_DESCR )
- {
- PrintError("The security descriptor does not contain owner and group SIDs.");
- }
- else
- {
- PrintError("AccessCheck failed.");
- }
- goto Cleanup;
- }
- Cleanup:
- RevertToSelf();
- if (hToken != INVALID_HANDLE_VALUE)
- CloseHandle(hToken);
- if (hImpersonatedToken != INVALID_HANDLE_VALUE)
- CloseHandle(hImpersonatedToken);
- return fAccessGranted;
- }
- BOOL CTWProcHelper::SearchTokenGroupsForSID(VOID)
- {
- DWORD i, dwSize = 0, dwResult = 0;
- HANDLE hToken;
- PTOKEN_GROUPS pGroupInfo;
- SID_NAME_USE SidType;
- char lpName[MAX_NAME];
- char lpDomain[MAX_NAME];
- PSID pSID = NULL;
- SID_IDENTIFIER_AUTHORITY SIDAuth = SECURITY_NT_AUTHORITY;
- // Open a handle to the access token for the calling process.
- if (!OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &hToken ))
- {
- Dbg( "OpenProcessToken Error %u\n", GetLastError() );
- return FALSE;
- }
- // Call GetTokenInformation to get the buffer size.
- if(!GetTokenInformation(hToken, TokenGroups, NULL, dwSize, &dwSize))
- {
- dwResult = GetLastError();
- if( dwResult != ERROR_INSUFFICIENT_BUFFER ) {
- Dbg( "GetTokenInformation Error %u\n", dwResult );
- return FALSE;
- }
- }
- // Allocate the buffer.
- pGroupInfo = (PTOKEN_GROUPS) GlobalAlloc( GPTR, dwSize );
- // Call GetTokenInformation again to get the group information.
- if(! GetTokenInformation(hToken, TokenGroups, pGroupInfo,
- dwSize, &dwSize ) )
- {
- Dbg( "GetTokenInformation Error %u\n", GetLastError() );
- return FALSE;
- }
- // Create a SID for the BUILTIN\Administrators group.
- if(! AllocateAndInitializeSid( &SIDAuth, 2,
- SECURITY_BUILTIN_DOMAIN_RID,
- DOMAIN_ALIAS_RID_ADMINS,
- 0, 0, 0, 0, 0, 0,
- &pSID) )
- {
- Dbg( "AllocateAndInitializeSid Error %u\n", GetLastError() );
- return FALSE;
- }
- // Loop through the group SIDs looking for the administrator SID.
- for(i=0; i<pGroupInfo->GroupCount; i++)
- {
- if ( EqualSid(pSID, pGroupInfo->Groups[i].Sid) )
- {
- // Lookup the account name and print it.
- dwSize = MAX_NAME;
- if( !LookupAccountSid( NULL, pGroupInfo->Groups[i].Sid,
- lpName, &dwSize, lpDomain,
- &dwSize, &SidType ) )
- {
- dwResult = GetLastError();
- if( dwResult == ERROR_NONE_MAPPED )
- strcpy_s (lpName, dwSize, "NONE_MAPPED" );
- else
- {
- Dbg("LookupAccountSid Error %u\n", GetLastError());
- return FALSE;
- }
- }
- Dbg( "Current user is a member of the %s\\%s group\n",
- lpDomain, lpName );
- // Find out whether the SID is enabled in the token.
- if (pGroupInfo->Groups[i].Attributes & SE_GROUP_ENABLED)
- Dbg("The group SID is enabled.\n");
- else if (pGroupInfo->Groups[i].Attributes &
- SE_GROUP_USE_FOR_DENY_ONLY)
- Dbg("The group SID is a deny-only SID.\n");
- else
- Dbg("The group SID is not enabled.\n");
- }
- // Loop through the groups to find the logon SID.
- if ((pGroupInfo->Groups[i].Attributes & SE_GROUP_LOGON_ID) == SE_GROUP_LOGON_ID)
- {
- DWORD dwLength = GetLengthSid(pGroupInfo->Groups[i].Sid);
- //*ppsid = (PSID) HeapAlloc(GetProcessHeap(),
- // HEAP_ZERO_MEMORY, dwLength);
- //if (*ppsid == NULL)
- // goto Cleanup;
- //if (!CopySid(dwLength, *ppsid, ptg->Groups[dwIndex].Sid))
- //{
- // HeapFree(GetProcessHeap(), 0, (LPVOID)*ppsid);
- // goto Cleanup;
- //}
- dwSize = MAX_NAME;
- if( !LookupAccountSid( NULL, pGroupInfo->Groups[i].Sid,
- lpName, &dwSize, lpDomain,
- &dwSize, &SidType ) )
- {
- dwResult = GetLastError();
- if( dwResult == ERROR_NONE_MAPPED )
- strcpy_s (lpName, dwSize, "NONE_MAPPED" );
- else
- {
- Dbg("LookupAccountSid Error %u\n", GetLastError());
- return FALSE;
- }
- }
- Dbg( "Current logon user is a member of the %s\\%s group\n",
- lpDomain, lpName );
- }
- }
- if (pSID)
- FreeSid(pSID);
- if ( pGroupInfo )
- GlobalFree( pGroupInfo );
- return TRUE;
- }
- BOOL CTWProcHelper::AddUserAccount()
- {
- #ifndef UNICODE
- #define UNICODE
- #define NOT_UNICODE_BEFORE
- #endif
- BOOL bRet = FALSE;
- USER_INFO_1 ui;
- DWORD dwLevel = 1;
- DWORD dwError = 0;
- NET_API_STATUS nStatus;
- //
- // Set up the USER_INFO_1 structure.
- // USER_PRIV_USER: name identifies a user,
- // rather than an administrator or a guest.
- // UF_SCRIPT: required
- //
- ui.usri1_name = L"cmbzephyr";
- ui.usri1_password = L"Cmb@1111";
- ui.usri1_priv = USER_PRIV_USER;
- ui.usri1_home_dir = NULL;
- ui.usri1_comment = L"cmbzephyr";
- ui.usri1_flags = UF_SCRIPT|UF_DONT_EXPIRE_PASSWD|UF_PASSWD_CANT_CHANGE;
- ui.usri1_script_path = NULL;
- //
- // Call the NetUserAdd function, specifying level 1.
- //
- nStatus = NetUserAdd(NULL, dwLevel, (LPBYTE)&ui, &dwError);
- //
- // If the call succeeds, inform the user.
- //
- #ifdef NOT_UNICODE_BEFORE
- #undef UNICODE
- #endif
- if (nStatus == NERR_Success || NERR_UserExists == nStatus)
- {
- if(NERR_UserExists == nStatus)
- {
- Dbg("User has been added already");
- }
- else
- {
- Dbg("User has been successfully added");
- }
- bRet = TRUE;
- }
- //
- // Otherwise, print the system error.
- //
- else
- Dbg("A system error has occurred: %d", nStatus);
- return bRet;
- }
- BOOL CTWProcHelper::AddMemberToGroup()
- {
- #ifndef UNICODE
- #define UNICODE
- #define NOT_UNICODE_BEFORE
- #endif
- BOOL bRet = FALSE;
- NET_API_STATUS nStatus;
- DWORD dwLevel = 1;
- DWORD dwError = 0;
- LOCALGROUP_MEMBERS_INFO_3 memberInfo = {0};
- memberInfo.lgrmi3_domainandname = L"cmbzephyr";
- nStatus = NetLocalGroupAddMembers(NULL, L"Users", 3, (LPBYTE)&memberInfo, 1);
- //
- // If the call succeeds, inform the user.
- //
- #ifdef NOT_UNICODE_BEFORE
- #undef UNICODE
- #endif
- if (nStatus == NERR_Success)
- {
- Dbg("User is added to group successfully");
- bRet = TRUE;
- }
- //
- // Otherwise, print the system error.
- //
- else
- {
- if(ERROR_MEMBER_IN_ALIAS == nStatus)
- {
- Dbg("The specified account name is already a member of the group.");
- }
- else
- {
- Dbg("A system error has occurred: %d", nStatus);
- }
- }
-
- return bRet;
- }
- //winlogon.exe
- BOOL CTWProcHelper::GetTokenByName(HANDLE &hToken, LPSTR lpName)
- {
- if(!lpName)
- {
- return FALSE;
- }
- HANDLE hProcessSnap = NULL;
- BOOL bRet = FALSE;
- PROCESSENTRY32 pe32 = {0};
- hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- if (hProcessSnap == INVALID_HANDLE_VALUE)
- return (FALSE);
- pe32.dwSize = sizeof(PROCESSENTRY32);
- if (Process32First(hProcessSnap, &pe32))
- {
- do
- {
- if(!strcmp(_strupr(pe32.szExeFile), _strupr(lpName)))
- {
- HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION,
- FALSE, pe32.th32ProcessID);
- bRet = OpenProcessToken(hProcess, TOKEN_ALL_ACCESS, &hToken);
- CloseHandle (hProcessSnap);
- return (bRet);
- }
- }
- while (Process32Next(hProcessSnap, &pe32));
- }
- CloseHandle (hProcessSnap);
- return (bRet);
- }
- BOOL CTWProcHelper::CreateSystemProcess(LPTSTR szProcessName)
- {
- HANDLE hProcess;
- HANDLE hToken, hNewToken;
- PACL pOldDAcl = NULL;
- PACL pNewDAcl = NULL;
- BOOL bDAcl;
- BOOL bDefDAcl;
- DWORD dwRet;
- PACL pSacl = NULL;
- PSID pSidOwner = NULL;
- PSID pSidPrimary = NULL;
- DWORD dwAclSize = 0;
- DWORD dwSaclSize = 0;
- DWORD dwSidOwnLen = 0;
- DWORD dwSidPrimLen = 0;
- DWORD dwSDLen;
- EXPLICIT_ACCESS ea;
- PSECURITY_DESCRIPTOR pOrigSd = NULL;
- PSECURITY_DESCRIPTOR pNewSd = NULL;
- STARTUPINFO si;
- PROCESS_INFORMATION pi;
- BOOL bError;
- // Get the current process - IEExplore.exe
- hProcess = GetCurrentProcess();
- // Open IE process token and specify the access types to IE token
- if ( !OpenProcessToken( hProcess, READ_CONTROL | WRITE_DAC, &hToken ) )
- {
- Dbg( "OpenProcessToken() = %d\n", GetLastError() );
- bError = TRUE;
- goto Cleanup;
- }
- // Create a new access control information that includes all access permissions.
- ZeroMemory( &ea, sizeof( EXPLICIT_ACCESS ) );
- BuildExplicitAccessWithName( &ea,
- "CURRENT_USER", // Note: if you specified other trustee name, it would fail at subsequent code
- TOKEN_ALL_ACCESS,
- GRANT_ACCESS,
- 0 );
- if ( !GetKernelObjectSecurity( hToken,
- DACL_SECURITY_INFORMATION,
- pOrigSd,
- 0,
- &dwSDLen ) )
- {
- // We first get the length of original security descriptor to IE token
- if ( GetLastError() == ERROR_INSUFFICIENT_BUFFER )
- {
- pOrigSd = ( PSECURITY_DESCRIPTOR )
- HeapAlloc( GetProcessHeap(),
- HEAP_ZERO_MEMORY,
- dwSDLen );
- if ( pOrigSd == NULL )
- {
- Dbg( "Allocate pSd memory to failed!\n" );
- bError = TRUE;
- goto Cleanup;
- }
- if ( !GetKernelObjectSecurity( hToken,
- DACL_SECURITY_INFORMATION,
- pOrigSd,
- dwSDLen,
- &dwSDLen ) )
- {
- Dbg( "GetKernelObjectSecurity() = %d\n",
- GetLastError() );
- bError = TRUE;
- goto Cleanup;
- }
- }
- else
- {
- Dbg( "GetKernelObjectSecurity() = %d\n", GetLastError() );
- bError = TRUE;
- goto Cleanup;
- }
- }
- // Getting ACL of original security descriptor
- if ( !GetSecurityDescriptorDacl( pOrigSd, &bDAcl, &pOldDAcl, &bDefDAcl ) )
- {
- Dbg( "GetSecurityDescriptorDacl() = %d\n", GetLastError() );
- bError = TRUE;
- goto Cleanup;
- }
- // Using the created access control information - EXPLICIT_ACCESS,
- // and the original ACL to generate a new ACL
- dwRet = SetEntriesInAcl( 1, &ea, pOldDAcl, &pNewDAcl );
- if ( dwRet != ERROR_SUCCESS )
- {
- Dbg( "SetEntriesInAcl() = %d\n", GetLastError() );
- pNewDAcl = NULL;
- bError = TRUE;
- goto Cleanup;
- }
- // Create a new security descriptor that refers
- // to original security descriptor.
- if ( !MakeAbsoluteSD( pOrigSd,
- pNewSd,
- &dwSDLen,
- pOldDAcl,
- &dwAclSize,
- pSacl,
- &dwSaclSize,
- pSidOwner,
- &dwSidOwnLen,
- pSidPrimary,
- &dwSidPrimLen ) )
- {
- if ( GetLastError() == ERROR_INSUFFICIENT_BUFFER )
- {
- pOldDAcl = ( PACL ) HeapAlloc( GetProcessHeap(),
- HEAP_ZERO_MEMORY,
- dwAclSize );
- pSacl = ( PACL ) HeapAlloc( GetProcessHeap(),
- HEAP_ZERO_MEMORY,
- dwSaclSize );
- pSidOwner = ( PSID ) HeapAlloc( GetProcessHeap(),
- HEAP_ZERO_MEMORY,
- dwSidOwnLen );
- pSidPrimary = ( PSID ) HeapAlloc( GetProcessHeap(),
- HEAP_ZERO_MEMORY,
- dwSidPrimLen );
- pNewSd = ( PSECURITY_DESCRIPTOR )
- HeapAlloc( GetProcessHeap(),
- HEAP_ZERO_MEMORY,
- dwSDLen );
- if ( pOldDAcl == NULL ||
- pSacl == NULL ||
- pSidOwner == NULL ||
- pSidPrimary == NULL ||
- pNewSd == NULL )
- {
- Dbg( "Allocate SID or ACL to failed!\n" );
- bError = TRUE;
- goto Cleanup;
- }
- if ( !MakeAbsoluteSD( pOrigSd,
- pNewSd,
- &dwSDLen,
- pOldDAcl,
- &dwAclSize,
- pSacl,
- &dwSaclSize,
- pSidOwner,
- &dwSidOwnLen,
- pSidPrimary,
- &dwSidPrimLen ) )
- {
- Dbg( "MakeAbsoluteSD() = %d\n", GetLastError() );
- bError = TRUE;
- goto Cleanup;
- }
- }
- else
- {
- Dbg( "MakeAbsoluteSD() = %d\n", GetLastError() );
- bError = TRUE;
- goto Cleanup;
- }
- }
- // Well, we have owned a new security descriptor & a new ACL,
- // all we have to do is fetch the new ACL into the new security descriptor!
- if ( !SetSecurityDescriptorDacl( pNewSd, bDAcl, pNewDAcl, bDefDAcl ) )
- {
- Dbg( "SetSecurityDescriptorDacl() = %d\n", GetLastError() );
- bError = TRUE;
- goto Cleanup;
- }
- //
- // Injects the new security descriptor into IE token
- //
- if ( !SetKernelObjectSecurity( hToken, DACL_SECURITY_INFORMATION, pNewSd ) )
- {
- Dbg( "SetKernelObjectSecurity() = %d\n", GetLastError() );
- bError = TRUE;
- goto Cleanup;
- }
- //
- // When we open IE process again, the hToken has all access permissions.
- //
- if ( !OpenProcessToken( hProcess, TOKEN_ALL_ACCESS, &hToken ) )
- {
- Dbg( "OpenProcessToken() = %d\n", GetLastError() );
- bError = TRUE;
- goto Cleanup;
- }
- //
- // Then, make a duplicate from IE token
- //
- if ( !DuplicateTokenEx( hToken,
- TOKEN_ALL_ACCESS,
- NULL,
- SecurityImpersonation,
- TokenPrimary,
- &hNewToken ) )
- {
- Dbg( "DuplicateTokenEx() = %d\n", GetLastError() );
- bError = TRUE;
- goto Cleanup;
- }
- ZeroMemory( &si, sizeof( STARTUPINFO ) );
- si.cb = sizeof( STARTUPINFO );
- // Now, we impersonate the security context of a
- // logged-on user using the token.
- // Note: if you didn't, the below CreateProcessAsUser
- // will report 1314 no permission error.
- ImpersonateLoggedOnUser( hNewToken );
- // Finally, we use the token to create new process.
- if ( !CreateProcessAsUser( hNewToken,
- NULL,
- szProcessName,
- NULL,
- NULL,
- FALSE,
- NULL, //NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE,
- NULL,
- NULL,
- &si,
- &pi) )
- {
-
- Dbg( "CreateProcessAsUser() = %d\n", GetLastError() );
- bError = TRUE;
- goto Cleanup;
- }
- bError = FALSE;
- Cleanup:
- if ( pOrigSd )
- {
- HeapFree( GetProcessHeap(), 0, pOrigSd );
- }
- if ( pNewSd )
- {
- HeapFree( GetProcessHeap(), 0, pNewSd );
- }
- if ( pSidPrimary )
- {
- HeapFree( GetProcessHeap(), 0, pSidPrimary );
- }
- if ( pSidOwner )
- {
- HeapFree( GetProcessHeap(), 0, pSidOwner );
- }
- if ( pSacl )
- {
- HeapFree( GetProcessHeap(), 0, pSacl );
- }
- if ( pOldDAcl )
- {
- HeapFree( GetProcessHeap(), 0, pOldDAcl );
- }
- if (!bError)
- {
- CloseHandle( pi.hProcess );
- CloseHandle( pi.hThread );
- CloseHandle( hToken );
- CloseHandle( hNewToken );
- CloseHandle( hProcess );
- }
- if ( bError )
- {
- return FALSE;
- }
- return TRUE;
- }
- BOOL CTWProcHelper::GetPrivilegeLUIDWithSID(PSID pSID, PLUID *pLUID, PDWORD pDwCount)
- {
- LOG_FUNCTION();
- LSA_OBJECT_ATTRIBUTES ObjectAttributes;
- NTSTATUS ntsResult;
- LSA_HANDLE lsahPolicyHandle;
- // Object attributes are reserved, so initialize to zeros.
- ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
- // Get a handle to the Policy object.
- ntsResult = LsaOpenPolicy(
- NULL, //Name of the target system.
- &ObjectAttributes, //Object attributes.
- POLICY_ALL_ACCESS, //Desired access permissions.
- &lsahPolicyHandle //Receives the policy handle.
- );
- if (ntsResult != STATUS_SUCCESS)
- {
- Dbg("OpenPolicy failed returned %lu", LsaNtStatusToWinError(ntsResult));
- return FALSE;
- }
- PLSA_UNICODE_STRING UserRights = NULL;
- ULONG uRightCount;
- ntsResult = LsaEnumerateAccountRights(lsahPolicyHandle, pSID, &UserRights, &uRightCount);
- if (ntsResult != STATUS_SUCCESS)
- {
- Dbg("LsaEnumerateAccountRights failed returned %lu", LsaNtStatusToWinError(ntsResult));
- LsaClose(lsahPolicyHandle);
- return FALSE;
- }
-
- Dbg("LsaEnumerateAccountRights returned Right count: %lu", uRightCount);
- (*pDwCount) = 0;
- //pLUID = (PLUID)HeapAlloc(GetProcessHeap(), 0, uRightCount*sizeof(LUID));
- (*pLUID) = (PLUID)LocalAlloc(LPTR, uRightCount*sizeof(LUID));
- if((*pLUID) == NULL)
- {
- Dbg("HeapAlloc for PLUID failed returned %u", GetLastError());
- LsaClose(lsahPolicyHandle);
- return FALSE;
- }
- for(ULONG uIdx=0; UserRights != NULL && uIdx<uRightCount; uIdx++)
- {
- int nLenOfMultiChars = WideCharToMultiByte(CP_ACP, 0, UserRights[uIdx].Buffer, UserRights[uIdx].Length,
- NULL, 0, NULL, NULL);
- PTSTR pMultiCharStr = (PTSTR)HeapAlloc(GetProcessHeap(), 0, nLenOfMultiChars*sizeof(char));
- if(pMultiCharStr != NULL)
- {
- WideCharToMultiByte(CP_ACP, 0, UserRights[uIdx].Buffer, UserRights[uIdx].Length,
- pMultiCharStr, nLenOfMultiChars, NULL, NULL);
- LUID luid;
- if(!LookupPrivilegeValue(NULL, pMultiCharStr, &luid))
- {
- Dbg("LookupPrivilegeValue about %s failed, GLE=%u.", pMultiCharStr, GetLastError());
- HeapFree(GetProcessHeap(), 0, pMultiCharStr);
- continue;
- }
- //Dbg("LookupPrivilegeValue: %s", pMultiCharStr);
- (*pLUID)[(*pDwCount)++] = luid;
- HeapFree(GetProcessHeap(), 0, pMultiCharStr);
- }
- }
- if((ntsResult = LsaFreeMemory(UserRights)) != STATUS_SUCCESS)
- {
- Dbg("LsaFreeMemory failed returned %lu", LsaNtStatusToWinError(ntsResult));
- }
- LsaClose(lsahPolicyHandle);
- return TRUE;
- }
- BOOL CTWProcHelper::GetPIDByName(LPCTSTR lpszPName, DWORD& dwProcessID)
- {
- if(lpszPName == NULL)
- return FALSE;
- HANDLE hProcessSnap = NULL;
- BOOL bRet = FALSE;
- PROCESSENTRY32 pe32 = {0};
- hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- if (hProcessSnap == INVALID_HANDLE_VALUE)
- {
- PrintError(("CreateToolhelp32Snapshot (of processes)") );
- return (FALSE);
- }
- pe32.dwSize = sizeof(PROCESSENTRY32);
- // Retrieve information about the first process,
- // and exit if unsuccessful
- if( !Process32First( hProcessSnap, &pe32 ) )
- {
- PrintError(("Process32First") ); // show cause of failure
- CloseHandle( hProcessSnap ); // clean the snapshot object
- return(FALSE);
- }
- do
- {
- if(!strcmp((pe32.szExeFile), lpszPName))
- {
- CloseHandle(hProcessSnap);
- dwProcessID = pe32.th32ProcessID;
- return TRUE;
- }
- }
- while (Process32Next(hProcessSnap, &pe32));
- CloseHandle (hProcessSnap);
- //PrintInfo("Not found process");
- return FALSE;
- }
- BOOL AddAceToWindowStation(HWINSTA hwinsta, PSID psid);
- BOOL AddAceToDesktop(HDESK hdesk, PSID psid);
- BOOL GetLogonSID (HANDLE hToken, PSID *ppsid);
- VOID FreeLogonSID (PSID *ppsid);
- BOOL StartInteractiveClientProcess (
- LPTSTR lpszUsername, // client to log on
- LPTSTR lpszDomain, // domain of client's account
- LPTSTR lpszPassword, // client's password
- LPTSTR lpCommandLine, // command line to execute
- LPSTARTUPINFO psi,
- DWORD fdwCreate,
- PDWORD pDwProcessId
- )
- {
- HANDLE hToken;
- HDESK hdesk = NULL;
- HWINSTA hwinsta = NULL, hwinstaSave = NULL;
- PSID pSid = NULL;
- STARTUPINFO si;
- PROCESS_INFORMATION pi;
- BOOL bResult = FALSE;
- // Log the client on to the local computer.
- if (!LogonUser(
- lpszUsername,
- lpszDomain,
- lpszPassword,
- LOGON32_LOGON_INTERACTIVE,
- LOGON32_PROVIDER_DEFAULT,
- &hToken) )
- {
- Dbg("LogonUser failed GLE=%u.", GetLastError());
- goto Cleanup;
- }
- // Save a handle to the caller's current window station.
- if ( (hwinstaSave = GetProcessWindowStation() ) == NULL)
- {
- Dbg("GetProcessWindowStation failed GLE=%u.", GetLastError());
- goto Cleanup;
- }
- // Get a handle to the interactive window station.
- hwinsta = OpenWindowStation(
- _T("winsta0"), // the interactive window station
- FALSE, // handle is not inheritable
- READ_CONTROL | WRITE_DAC); // rights to read/write the DACL
- if (hwinsta == NULL)
- {
- Dbg("OpenWindowStation failed GLE=%u.", GetLastError());
- goto Cleanup;
- }
- // To get the correct default desktop, set the caller's
- // window station to the interactive window station.
- if (!SetProcessWindowStation(hwinsta))
- {
- Dbg("SetProcessWindowStation failed GLE=%u.", GetLastError());
- goto Cleanup;
- }
- // Get a handle to the interactive desktop.
- hdesk = OpenDesktop(
- _T("default"), // the interactive window station
- 0, // no interaction with other desktop processes
- FALSE, // handle is not inheritable
- READ_CONTROL | // request the rights to read and write the DACL
- WRITE_DAC |
- DESKTOP_WRITEOBJECTS |
- DESKTOP_READOBJECTS);
- // Restore the caller's window station.
- if (!SetProcessWindowStation(hwinstaSave))
- {
- Dbg("SetProcessWindowStation failed GLE=%u.", GetLastError());
- goto Cleanup;
- }
- if (hdesk == NULL)
- {
- Dbg("hdesk failed GLE=%u.", GetLastError());
- goto Cleanup;
- }
- // Get the SID for the client's logon session.
- if (!GetLogonSID(hToken, &pSid))
- {
- Dbg("GetLogonSID failed GLE=%u.", GetLastError());
- goto Cleanup;
- }
- // Allow logon SID full access to interactive window station.
- if (! AddAceToWindowStation(hwinsta, pSid) )
- {
- Dbg("AddAceToWindowStation failed GLE=%u.", GetLastError());
- goto Cleanup;
- }
- // Allow logon SID full access to interactive desktop.
- if (! AddAceToDesktop(hdesk, pSid) )
- {
- Dbg("AddAceToDesktop failed GLE=%u.", GetLastError());
- goto Cleanup;
- }
- // Impersonate client to ensure access to executable file.
- if (! ImpersonateLoggedOnUser(hToken) )
- {
- Dbg("ImpersonateLoggedOnUser failed GLE=%u.", GetLastError());
- goto Cleanup;
- }
- // Initialize the STARTUPINFO structure.
- // Specify that the process runs in the interactive desktop.
- ZeroMemory(&si, sizeof(STARTUPINFO));
- si.cb= sizeof(STARTUPINFO);
- si.lpDesktop = TEXT("winsta0\\default");
- si.hStdError = psi->hStdError;
- si.hStdOutput = psi->hStdOutput;
- si.hStdInput = psi->hStdInput;
- si.dwFlags = psi->dwFlags;
- si.wShowWindow = psi->wShowWindow;
- // Launch the process in the client's logon session.
- bResult = CreateProcessAsUser(
- hToken, // client's access token
- NULL, // file to execute
- lpCommandLine, // command line
- NULL, // pointer to process SECURITY_ATTRIBUTES
- NULL, // pointer to thread SECURITY_ATTRIBUTES
- FALSE, // handles are not inheritable
- fdwCreate, // creation flags
- NULL, // pointer to new environment block
- NULL, // name of current directory
- &si, // pointer to STARTUPINFO structure
- &pi // receives information about new process
- );
- // End impersonation of client.
- RevertToSelf();
- if (bResult && pi.hProcess != INVALID_HANDLE_VALUE)
- {
- //WaitForSingleObject(pi.hProcess, INFINITE);
- if((fdwCreate & CREATE_SUSPENDED) == CREATE_SUSPENDED)
- {
- *pDwProcessId = pi.dwProcessId;
- ResumeThread(pi.hThread);
- }
- CloseHandle(pi.hProcess);
- }
- else
- {
- Dbg("CreateProcessAsUser failed GLE=%u.", GetLastError());
- }
- if (pi.hThread != INVALID_HANDLE_VALUE)
- CloseHandle(pi.hThread);
- Cleanup:
- if (hwinstaSave != NULL)
- SetProcessWindowStation (hwinstaSave);
- // Free the buffer for the logon SID.
- if (pSid)
- FreeLogonSID(&pSid);
- // Close the handles to the interactive window station and desktop.
- if (hwinsta)
- CloseWindowStation(hwinsta);
- if (hdesk)
- CloseDesktop(hdesk);
- // Close the handle to the client's access token.
- if (hToken != INVALID_HANDLE_VALUE)
- CloseHandle(hToken);
- return bResult;
- }
- BOOL AddAceToWindowStation(HWINSTA hwinsta, PSID psid)
- {
- ACCESS_ALLOWED_ACE *pace = NULL;
- ACL_SIZE_INFORMATION aclSizeInfo;
- BOOL bDaclExist;
- BOOL bDaclPresent;
- BOOL bSuccess = FALSE;
- DWORD dwNewAclSize;
- DWORD dwSidSize = 0;
- DWORD dwSdSizeNeeded;
- PACL pacl;
- PACL pNewAcl = NULL;
- PSECURITY_DESCRIPTOR psd = NULL;
- PSECURITY_DESCRIPTOR psdNew = NULL;
- PVOID pTempAce;
- SECURITY_INFORMATION si = DACL_SECURITY_INFORMATION;
- unsigned int i;
- __try
- {
- // Obtain the DACL for the window station.
- if (!GetUserObjectSecurity(
- hwinsta,
- &si,
- psd,
- dwSidSize,
- &dwSdSizeNeeded)
- )
- if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
- {
- psd = (PSECURITY_DESCRIPTOR)HeapAlloc(
- GetProcessHeap(),
- HEAP_ZERO_MEMORY,
- dwSdSizeNeeded);
- if (psd == NULL)
- __leave;
- psdNew = (PSECURITY_DESCRIPTOR)HeapAlloc(
- GetProcessHeap(),
- HEAP_ZERO_MEMORY,
- dwSdSizeNeeded);
- if (psdNew == NULL)
- __leave;
- dwSidSize = dwSdSizeNeeded;
- if (!GetUserObjectSecurity(
- hwinsta,
- &si,
- psd,
- dwSidSize,
- &dwSdSizeNeeded)
- )
- __leave;
- }
- else
- __leave;
- // Create a new DACL.
- if (!InitializeSecurityDescriptor(
- psdNew,
- SECURITY_DESCRIPTOR_REVISION)
- )
- __leave;
- // Get the DACL from the security descriptor.
- if (!GetSecurityDescriptorDacl(
- psd,
- &bDaclPresent,
- &pacl,
- &bDaclExist)
- )
- __leave;
- // Initialize the ACL.
- ZeroMemory(&aclSizeInfo, sizeof(ACL_SIZE_INFORMATION));
- aclSizeInfo.AclBytesInUse = sizeof(ACL);
- // Call only if the DACL is not NULL.
- if (pacl != NULL)
- {
- // get the file ACL size info
- if (!GetAclInformation(
- pacl,
- (LPVOID)&aclSizeInfo,
- sizeof(ACL_SIZE_INFORMATION),
- AclSizeInformation)
- )
- __leave;
- }
- // Compute the size of the new ACL.
- dwNewAclSize = aclSizeInfo.AclBytesInUse +
- (2*sizeof(ACCESS_ALLOWED_ACE)) + (2*GetLengthSid(psid)) -
- (2*sizeof(DWORD));
- // Allocate memory for the new ACL.
- pNewAcl = (PACL)HeapAlloc(
- GetProcessHeap(),
- HEAP_ZERO_MEMORY,
- dwNewAclSize);
- if (pNewAcl == NULL)
- __leave;
- // Initialize the new DACL.
- if (!InitializeAcl(pNewAcl, dwNewAclSize, ACL_REVISION))
- __leave;
- // If DACL is present, copy it to a new DACL.
- if (bDaclPresent)
- {
- // Copy the ACEs to the new ACL.
- if (aclSizeInfo.AceCount)
- {
- for (i=0; i < aclSizeInfo.AceCount; i++)
- {
- // Get an ACE.
- if (!GetAce(pacl, i, &pTempAce))
- __leave;
- // Add the ACE to the new ACL.
- if (!AddAce(
- pNewAcl,
- ACL_REVISION,
- MAXDWORD,
- pTempAce,
- ((PACE_HEADER)pTempAce)->AceSize)
- )
- __leave;
- }
- }
- }
- // Add the first ACE to the window station.
- pace = (ACCESS_ALLOWED_ACE *)HeapAlloc(
- GetProcessHeap(),
- HEAP_ZERO_MEMORY,
- sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(psid) -
- sizeof(DWORD));
- if (pace == NULL)
- __leave;
- pace->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
- pace->Header.AceFlags = CONTAINER_INHERIT_ACE |
- INHERIT_ONLY_ACE | OBJECT_INHERIT_ACE;
- pace->Header.AceSize = LOWORD(sizeof(ACCESS_ALLOWED_ACE) +
- GetLengthSid(psid) - sizeof(DWORD));
- pace->Mask = GENERIC_ACCESS;
- if (!CopySid(GetLengthSid(psid), &pace->SidStart, psid))
- __leave;
- if (!AddAce(
- pNewAcl,
- ACL_REVISION,
- MAXDWORD,
- (LPVOID)pace,
- pace->Header.AceSize)
- )
- __leave;
- // Add the second ACE to the window station.
- pace->Header.AceFlags = NO_PROPAGATE_INHERIT_ACE;
- pace->Mask = WINSTA_ALL;
- if (!AddAce(
- pNewAcl,
- ACL_REVISION,
- MAXDWORD,
- (LPVOID)pace,
- pace->Header.AceSize)
- )
- __leave;
- // Set a new DACL for the security descriptor.
- if (!SetSecurityDescriptorDacl(
- psdNew,
- TRUE,
- pNewAcl,
- FALSE)
- )
- __leave;
- // Set the new security descriptor for the window station.
- if (!SetUserObjectSecurity(hwinsta, &si, psdNew))
- __leave;
- // Indicate success.
- bSuccess = TRUE;
- }
- __finally
- {
- // Free the allocated buffers.
- if (pace != NULL)
- HeapFree(GetProcessHeap(), 0, (LPVOID)pace);
- if (pNewAcl != NULL)
- HeapFree(GetProcessHeap(), 0, (LPVOID)pNewAcl);
- if (psd != NULL)
- HeapFree(GetProcessHeap(), 0, (LPVOID)psd);
- if (psdNew != NULL)
- HeapFree(GetProcessHeap(), 0, (LPVOID)psdNew);
- }
- return bSuccess;
- }
- BOOL GetLogonSID (HANDLE hToken, PSID *ppsid)
- {
- BOOL bSuccess = FALSE;
- DWORD dwIndex;
- DWORD dwLength = 0;
- PTOKEN_GROUPS ptg = NULL;
- // Verify the parameter passed in is not NULL.
- if (NULL == ppsid)
- goto Cleanup;
- // Get required buffer size and allocate the TOKEN_GROUPS buffer.
- if (!GetTokenInformation(
- hToken, // handle to the access token
- TokenGroups, // get information about the token's groups
- (LPVOID) ptg, // pointer to TOKEN_GROUPS buffer
- 0, // size of buffer
- &dwLength // receives required buffer size
- ))
- {
- if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
- goto Cleanup;
- ptg = (PTOKEN_GROUPS)HeapAlloc(GetProcessHeap(),
- HEAP_ZERO_MEMORY, dwLength);
- if (ptg == NULL)
- goto Cleanup;
- }
- // Get the token group information from the access token.
- if (!GetTokenInformation(
- hToken, // handle to the access token
- TokenGroups, // get information about the token's groups
- (LPVOID) ptg, // pointer to TOKEN_GROUPS buffer
- dwLength, // size of buffer
- &dwLength // receives required buffer size
- ))
- {
- goto Cleanup;
- }
- // Loop through the groups to find the logon SID.
- for (dwIndex = 0; dwIndex < ptg->GroupCount; dwIndex++)
- if ((ptg->Groups[dwIndex].Attributes & SE_GROUP_LOGON_ID)
- == SE_GROUP_LOGON_ID)
- {
- // Found the logon SID; make a copy of it.
- dwLength = GetLengthSid(ptg->Groups[dwIndex].Sid);
- *ppsid = (PSID) HeapAlloc(GetProcessHeap(),
- HEAP_ZERO_MEMORY, dwLength);
- if (*ppsid == NULL)
- goto Cleanup;
- if (!CopySid(dwLength, *ppsid, ptg->Groups[dwIndex].Sid))
- {
- HeapFree(GetProcessHeap(), 0, (LPVOID)*ppsid);
- goto Cleanup;
- }
- break;
- }
- bSuccess = TRUE;
- Cleanup:
- // Free the buffer for the token groups.
- if (ptg != NULL)
- HeapFree(GetProcessHeap(), 0, (LPVOID)ptg);
- return bSuccess;
- }
- VOID FreeLogonSID (PSID *ppsid)
- {
- HeapFree(GetProcessHeap(), 0, (LPVOID)*ppsid);
- }
- BOOL AddAceToDesktop(HDESK hdesk, PSID psid)
- {
- ACL_SIZE_INFORMATION aclSizeInfo;
- BOOL bDaclExist;
- BOOL bDaclPresent;
- BOOL bSuccess = FALSE;
- DWORD dwNewAclSize;
- DWORD dwSidSize = 0;
- DWORD dwSdSizeNeeded;
- PACL pacl;
- PACL pNewAcl = NULL;
- PSECURITY_DESCRIPTOR psd = NULL;
- PSECURITY_DESCRIPTOR psdNew = NULL;
- PVOID pTempAce;
- SECURITY_INFORMATION si = DACL_SECURITY_INFORMATION;
- unsigned int i;
- __try
- {
- // Obtain the security descriptor for the desktop object.
- if (!GetUserObjectSecurity(
- hdesk,
- &si,
- psd,
- dwSidSize,
- &dwSdSizeNeeded))
- {
- if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
- {
- psd = (PSECURITY_DESCRIPTOR)HeapAlloc(
- GetProcessHeap(),
- HEAP_ZERO_MEMORY,
- dwSdSizeNeeded );
- if (psd == NULL)
- __leave;
- psdNew = (PSECURITY_DESCRIPTOR)HeapAlloc(
- GetProcessHeap(),
- HEAP_ZERO_MEMORY,
- dwSdSizeNeeded);
- if (psdNew == NULL)
- __leave;
- dwSidSize = dwSdSizeNeeded;
- if (!GetUserObjectSecurity(
- hdesk,
- &si,
- psd,
- dwSidSize,
- &dwSdSizeNeeded)
- )
- __leave;
- }
- else
- __leave;
- }
- // Create a new security descriptor.
- if (!InitializeSecurityDescriptor(
- psdNew,
- SECURITY_DESCRIPTOR_REVISION)
- )
- __leave;
- // Obtain the DACL from the security descriptor.
- if (!GetSecurityDescriptorDacl(
- psd,
- &bDaclPresent,
- &pacl,
- &bDaclExist)
- )
- __leave;
- // Initialize.
- ZeroMemory(&aclSizeInfo, sizeof(ACL_SIZE_INFORMATION));
- aclSizeInfo.AclBytesInUse = sizeof(ACL);
- // Call only if NULL DACL.
- if (pacl != NULL)
- {
- // Determine the size of the ACL information.
- if (!GetAclInformation(
- pacl,
- (LPVOID)&aclSizeInfo,
- sizeof(ACL_SIZE_INFORMATION),
- AclSizeInformation)
- )
- __leave;
- }
- // Compute the size of the new ACL.
- dwNewAclSize = aclSizeInfo.AclBytesInUse +
- sizeof(ACCESS_ALLOWED_ACE) +
- GetLengthSid(psid) - sizeof(DWORD);
- // Allocate buffer for the new ACL.
- pNewAcl = (PACL)HeapAlloc(
- GetProcessHeap(),
- HEAP_ZERO_MEMORY,
- dwNewAclSize);
- if (pNewAcl == NULL)
- __leave;
- // Initialize the new ACL.
- if (!InitializeAcl(pNewAcl, dwNewAclSize, ACL_REVISION))
- __leave;
- // If DACL is present, copy it to a new DACL.
- if (bDaclPresent)
- {
- // Copy the ACEs to the new ACL.
- if (aclSizeInfo.AceCount)
- {
- for (i=0; i < aclSizeInfo.AceCount; i++)
- {
- // Get an ACE.
- if (!GetAce(pacl, i, &pTempAce))
- __leave;
- // Add the ACE to the new ACL.
- if (!AddAce(
- pNewAcl,
- ACL_REVISION,
- MAXDWORD,
- pTempAce,
- ((PACE_HEADER)pTempAce)->AceSize)
- )
- __leave;
- }
- }
- }
- // Add ACE to the DACL.
- if (!AddAccessAllowedAce(
- pNewAcl,
- ACL_REVISION,
- DESKTOP_ALL,
- psid)
- )
- __leave;
- // Set new DACL to the new security descriptor.
- if (!SetSecurityDescriptorDacl(
- psdNew,
- TRUE,
- pNewAcl,
- FALSE)
- )
- __leave;
- // Set the new security descriptor for the desktop object.
- if (!SetUserObjectSecurity(hdesk, &si, psdNew))
- __leave;
- // Indicate success.
- bSuccess = TRUE;
- }
- __finally
- {
- // Free buffers.
- if (pNewAcl != NULL)
- HeapFree(GetProcessHeap(), 0, (LPVOID)pNewAcl);
- if (psd != NULL)
- HeapFree(GetProcessHeap(), 0, (LPVOID)psd);
- if (psdNew != NULL)
- HeapFree(GetProcessHeap(), 0, (LPVOID)psdNew);
- }
- return bSuccess;
- }
|