天天看點

《windows核心程式設計》筆記(三)

命名核心對象有一種問題:任何程式都可以建立一個命名對象,這樣如果某個程式要實作單例運作而建立了一個核心對象,這種情況下另一程式也建立了同名的核心對象時,該單例程式就無法正常運作了。這是DoS攻擊的一種。

在Vista中有一種機制使得使用者建立的命名核心對象永遠不會和其它程式建立的對象沖突,要使用定制的字首并把它作為人的私有命名空間,如Global和Local,服務程序會確定為核心對象定義一邊界描述符來保護命名空間。

下面是檢查執行個體的代碼:

void CheckInstances() 

{//檢查執行個體

   // Create the boundary descriptor

   g_hBoundary = CreateBoundaryDescriptor(g_szBoundary, 0);

   // Create a SID corresponding to the Local Administrator group

   BYTE localAdminSID[SECURITY_MAX_SID_SIZE];

   PSID pLocalAdminSID = &localAdminSID;

   DWORD cbSID = sizeof(localAdminSID);

   if (!CreateWellKnownSid(

      WinBuiltinAdministratorsSid, NULL, pLocalAdminSID, &cbSID)

      ) {

      AddText(TEXT("AddSIDToBoundaryDescriptor failed: %u\r\n"), 

         GetLastError());

      return;

   }

   // Associate the Local Admin SID to the boundary descriptor

   // --> only applications running under an administrator user

   //     will be able to access the kernel objects in the same namespace

   if (!AddSIDToBoundaryDescriptor(&g_hBoundary, pLocalAdminSID)) {

   // Create the namespace for Local Administrators only

   SECURITY_ATTRIBUTES sa;

   sa.nLength = sizeof(sa);

   sa.bInheritHandle = FALSE;

   if (!ConvertStringSecurityDescriptorToSecurityDescriptor(

      TEXT("D:(A;;GA;;;BA)"), 

      SDDL_REVISION_1, &sa.lpSecurityDescriptor, NULL)) {

      AddText(TEXT("Security Descriptor creation failed: %u\r\n"), GetLastError());

   g_hNamespace = 

      CreatePrivateNamespace(&sa, g_hBoundary, g_szNamespace);

   // Don't forget to release memory for the security descriptor

   LocalFree(sa.lpSecurityDescriptor);

   // Check the private namespace creation result

   DWORD dwLastError = GetLastError();

   if (g_hNamespace == NULL) 

   {

      // Nothing to do if access is denied

      // --> this code must run under a Local Administrator account

      if (dwLastError == ERROR_ACCESS_DENIED) 

      {

         AddText(TEXT("Access denied when creating the namespace.\r\n"));

         AddText(TEXT("   You must be running as Administrator.\r\n\r\n"));

         return;

      }

      else 

      { 

         if (dwLastError == ERROR_ALREADY_EXISTS) 

         {

         // If another instance has already created the namespace, 

         // we need to open it instead. 

            AddText(TEXT("CreatePrivateNamespace failed: %u\r\n"), dwLastError);

            g_hNamespace = OpenPrivateNamespace(g_hBoundary, g_szNamespace);

            if (g_hNamespace == NULL) 

            {

               AddText(TEXT("   and OpenPrivateNamespace failed: %u\r\n"), 

               dwLastError);

               return;

            } 

            else 

               g_bNamespaceOpened = TRUE;

               AddText(TEXT("   but OpenPrivateNamespace succeeded\r\n\r\n"));

            }

         } 

         else

            AddText(TEXT("Unexpected error occured: %u\r\n\r\n"),dwLastError);

            return;

         }

   // Try to create the mutex object with a name 

   // based on the private namespace 

   TCHAR szMutexName[64];

   StringCchPrintf(szMutexName, _countof(szMutexName), TEXT("%s\\%s"), g_szNamespace, TEXT("Singleton"));

   g_hSingleton = CreateMutex(NULL, FALSE, szMutexName);//建立互斥量

   if (GetLastError() == ERROR_ALREADY_EXISTS) 

      // There is already an instance of this Singleton object

      AddText(TEXT("Another instance of Singleton is running:\r\n"));

      AddText(TEXT("--> Impossible to access application features.\r\n"));

   else  

      // First time the Singleton object is created

      AddText(TEXT("First instance of Singleton:\r\n"));

      AddText(TEXT("--> Access application features now.\r\n"));

}

void AddText(PCTSTR pszFormat, ) {

   va_list argList;

   va_start(argList, pszFormat);

   TCHAR sz[20 * 1024];

   Edit_GetText(DETAILS_CTRL, sz, _countof(sz));

   _vstprintf_s(

      _tcschr(sz, TEXT('\0')), _countof(sz) - _tcslen(sz), 

      pszFormat, argList);

   Edit_SetText(DETAILS_CTRL, sz);

   va_end(argList);

本文轉自Phinecos(洞庭散人)部落格園部落格,原文連結:http://www.cnblogs.com/phinecos/archive/2008/06/08/1216009.html,如需轉載請自行聯系原作者

繼續閱讀