《windows核心编程》笔记(三)

命名内核对象有一种问题:任何程序都可以创建一个命名对象,这样如果某个程序要实现单例运行而创建了一个内核对象,这种情况下另一程序也创建了同名的内核对象时,该单例程序就无法正常运行了。这是DoS攻击的一种。<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Vista中有一种机制使得用户创建的命名内核对象永远不会和其它程序创建的对象冲突,要使用定制的前缀并把它作为人的私有命名空间,如GlobalLocal,服务进程会确保为内核对象定义一边界描述符来保护命名空间。
下面是检查实例的代码:

None.gif void CheckInstances()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {//检查实例
InBlock.gif
//Createtheboundarydescriptor
InBlock.gif
g_hBoundary=CreateBoundaryDescriptor(g_szBoundary,0);
InBlock.gif
InBlock.gif
//CreateaSIDcorrespondingtotheLocalAdministratorgroup
InBlock.gif
BYTElocalAdminSID[SECURITY_MAX_SID_SIZE];
InBlock.gifPSIDpLocalAdminSID
=&localAdminSID;
InBlock.gifDWORDcbSID
=sizeof(localAdminSID);
InBlock.gif
if(!CreateWellKnownSid(
InBlock.gifWinBuiltinAdministratorsSid,NULL,pLocalAdminSID,
&cbSID)
ExpandedSubBlockStart.gifContractedSubBlock.gif)
dot.gif{
InBlock.gifAddText(TEXT(
"AddSIDToBoundaryDescriptorfailed:%u/r/n"),
InBlock.gifGetLastError());
InBlock.gif
return;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
//AssociatetheLocalAdminSIDtotheboundarydescriptor
InBlock.gif
//-->onlyapplicationsrunningunderanadministratoruser
InBlock.gif
//willbeabletoaccessthekernelobjectsinthesamenamespace
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(!AddSIDToBoundaryDescriptor(&g_hBoundary,pLocalAdminSID))dot.gif{
InBlock.gifAddText(TEXT(
"AddSIDToBoundaryDescriptorfailed:%u/r/n"),
InBlock.gifGetLastError());
InBlock.gif
return;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
//CreatethenamespaceforLocalAdministratorsonly
InBlock.gif
SECURITY_ATTRIBUTESsa;
InBlock.gifsa.nLength
=sizeof(sa);
InBlock.gifsa.bInheritHandle
=FALSE;
InBlock.gif
if(!ConvertStringSecurityDescriptorToSecurityDescriptor(
InBlock.gifTEXT(
"D:(A;;GA;;;BA)"),
ExpandedSubBlockStart.gifContractedSubBlock.gifSDDL_REVISION_1,
&sa.lpSecurityDescriptor,NULL))dot.gif{
InBlock.gifAddText(TEXT(
"SecurityDescriptorcreationfailed:%u/r/n"),GetLastError());
InBlock.gif
return;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gifg_hNamespace
=
InBlock.gifCreatePrivateNamespace(
&sa,g_hBoundary,g_szNamespace);
InBlock.gif
InBlock.gif
//Don'tforgettoreleasememoryforthesecuritydescriptor
InBlock.gif
LocalFree(sa.lpSecurityDescriptor);
InBlock.gif
InBlock.gif
InBlock.gif
//Checktheprivatenamespacecreationresult
InBlock.gif
DWORDdwLastError=GetLastError();
InBlock.gif
if(g_hNamespace==NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//Nothingtodoifaccessisdenied
InBlock.gif
//-->thiscodemustrununderaLocalAdministratoraccount
InBlock.gif
if(dwLastError==ERROR_ACCESS_DENIED)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifAddText(TEXT(
"Accessdeniedwhencreatingthenamespace./r/n"));
InBlock.gifAddText(TEXT(
"YoumustberunningasAdministrator./r/n/r/n"));
InBlock.gif
return;
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(dwLastError==ERROR_ALREADY_EXISTS)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//Ifanotherinstancehasalreadycreatedthenamespace,
InBlock.gif
//weneedtoopenitinstead.
InBlock.gif
AddText(TEXT("CreatePrivateNamespacefailed:%u/r/n"),dwLastError);
InBlock.gifg_hNamespace
=OpenPrivateNamespace(g_hBoundary,g_szNamespace);
InBlock.gif
if(g_hNamespace==NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifAddText(TEXT(
"andOpenPrivateNamespacefailed:%u/r/n"),
InBlock.gifdwLastError);
InBlock.gif
return;
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifg_bNamespaceOpened
=TRUE;
InBlock.gifAddText(TEXT(
"butOpenPrivateNamespacesucceeded/r/n/r/n"));
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifAddText(TEXT(
"Unexpectederroroccured:%u/r/n/r/n"),dwLastError);
InBlock.gif
return;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
//Trytocreatethemutexobjectwithaname
InBlock.gif
//basedontheprivatenamespace
InBlock.gif
TCHARszMutexName[64];
InBlock.gifStringCchPrintf(szMutexName,_countof(szMutexName),TEXT(
"%s//%s"),g_szNamespace,TEXT("Singleton"));
InBlock.gif
InBlock.gifg_hSingleton
=CreateMutex(NULL,FALSE,szMutexName);//创建互斥量
InBlock.gif
if(GetLastError()==ERROR_ALREADY_EXISTS)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//ThereisalreadyaninstanceofthisSingletonobject
InBlock.gif
AddText(TEXT("AnotherinstanceofSingletonisrunning:/r/n"));
InBlock.gifAddText(TEXT(
"-->Impossibletoaccessapplicationfeatures./r/n"));
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//FirsttimetheSingletonobjectiscreated
InBlock.gif
AddText(TEXT("FirstinstanceofSingleton:/r/n"));
InBlock.gifAddText(TEXT(
"-->Accessapplicationfeaturesnow./r/n"));
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif


ExpandedBlockStart.gif ContractedBlock.gif void AddText(PCTSTRpszFormat,dot.gif) dot.gif {
InBlock.gif
InBlock.gifva_listargList;
InBlock.gifva_start(argList,pszFormat);
InBlock.gif
InBlock.gifTCHARsz[
20*1024];
InBlock.gif
InBlock.gifEdit_GetText(DETAILS_CTRL,sz,_countof(sz));
InBlock.gif_vstprintf_s(
InBlock.gif_tcschr(sz,TEXT(
'/0')),_countof(sz)-_tcslen(sz),
InBlock.gifpszFormat,argList);
InBlock.gifEdit_SetText(DETAILS_CTRL,sz);
InBlock.gifva_end(argList);
ExpandedBlockEnd.gif}

None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值