source: trunk/DirectShowSpy/TakeRegistryKeyOwnership/TakeRegistryKeyOwnership.cpp @ 155

Last change on this file since 155 was 95, checked in by roman, 10 years ago

DirectShowSpy? moved from Assembla

  • Property svn:keywords set to Id
File size: 5.8 KB
Line 
1////////////////////////////////////////////////////////////
2// Copyright (C) Roman Ryltsov, 2008-2009
3// Created by Roman Ryltsov roman@alax.info
4//
5// $Id: TakeRegistryKeyOwnership.cpp 95 2012-08-17 17:21:13Z roman $
6
7#include "stdafx.h"
8#include <atlsecurity.h>
9#include "roatlbase.h"
10#include "roatlcom.h"
11#include "roatlmisc.h"
12
13inline VOID ATLENSURE_THROW3(BOOL bResult, HRESULT nCode, LPCSTR pszFile, INT nLine)
14{
15        if(!bResult)
16                _tprintf(_T("Warning, %hs(%d): Error 0x%08x\n"), strrchr(pszFile, '\\') + 1, nLine, nCode);
17        ATLENSURE_THROW(bResult, nCode);
18}
19
20inline VOID ATLENSURE_THROW3(const VOID* pvResult, HRESULT nCode, LPCSTR pszFile, INT nLine)
21{
22        return ATLENSURE_THROW3(pvResult != NULL, nCode, pszFile, nLine);
23}
24
25#define ATLENSURE_THROW2(bResult, nCode) ATLENSURE_THROW3(bResult, nCode, __FILE__, __LINE__)
26
27inline VOID ATLENSURE_SUCCEEDED3(HRESULT nResult, LPCSTR pszFile, INT nLine)
28{
29        if(FAILED(nResult))
30                _tprintf(_T("Warning, %hs(%d): Error 0x%08x\n"), strrchr(pszFile, '\\') + 1, nLine, nResult);
31        ATLENSURE_SUCCEEDED(nResult);
32}
33
34#define ATLENSURE_SUCCEEDED2(nResult) ATLENSURE_SUCCEEDED3(nResult, __FILE__, __LINE__)
35
36CString StringFromSecurityDescriptor(CSecurityDesc& SecurityDescriptor)
37{
38        CString sSecurityDescriptorString;
39        ATLENSURE_THROW2(SecurityDescriptor.ToString(&sSecurityDescriptorString), HRESULT_FROM_WIN32(GetLastError()));
40        return sSecurityDescriptorString;
41}
42
43VOID GetRegKeySecurityDescriptor(CRegKey& Key, SECURITY_INFORMATION SecurityInformation, CSecurityDesc& OriginalSecurityDescriptor)
44{
45        DWORD nOwnerSecurityDescriptorSize = 0;
46        Key.GetKeySecurity(SecurityInformation, NULL, &nOwnerSecurityDescriptorSize);
47        CTempBuffer<SECURITY_DESCRIPTOR> pOwnerSecurityDescriptor;
48        ATLVERIFY(pOwnerSecurityDescriptor.AllocateBytes(nOwnerSecurityDescriptorSize));
49        ATLENSURE_SUCCEEDED2(HRESULT_FROM_WIN32(Key.GetKeySecurity(SecurityInformation, pOwnerSecurityDescriptor, &nOwnerSecurityDescriptorSize)));
50        OriginalSecurityDescriptor = *pOwnerSecurityDescriptor;
51}
52
53CString StringFromRegKeySecurityDescriptor(CRegKey& Key, SECURITY_INFORMATION SecurityInformation)
54{
55        CSecurityDesc SecurityDescriptor;
56        GetRegKeySecurityDescriptor(Key, SecurityInformation, SecurityDescriptor);
57        return StringFromSecurityDescriptor(SecurityDescriptor);
58}
59
60int _tmain(int argc, _TCHAR* argv[])
61{
62        _ATLTRY
63        {
64                _ATLTRY
65                {
66                        CAccessToken AccessToken;
67                        ATLENSURE_THROW2(AccessToken.GetProcessToken(TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES), HRESULT_FROM_WIN32(GetLastError()));
68                        ATLENSURE_THROW2(AccessToken.EnablePrivilege(SE_TAKE_OWNERSHIP_NAME), HRESULT_FROM_WIN32(GetLastError()));
69                        ATLENSURE_THROW2(AccessToken.EnablePrivilege(SE_RESTORE_NAME), HRESULT_FROM_WIN32(GetLastError()));
70                }
71                _ATLCATCHALL()
72                {
73                        _tprintf(_T("Warning, could not enable privileges\n"));
74                }
75                static LPCTSTR g_pszClassIdentifier = _T("{E436EBB2-524F-11CE-9F53-0020AF0BA770}");
76                TCHAR pszKeyName[128] = { 0 };
77                _stprintf_s(pszKeyName, _T("CLSID\\%s"), g_pszClassIdentifier);
78                CRegKey Key;
79                ATLENSURE_SUCCEEDED2(HRESULT_FROM_WIN32(Key.Open(HKEY_CLASSES_ROOT, pszKeyName, READ_CONTROL | WRITE_OWNER)));
80                CSecurityDesc OriginalSecurityDescriptor;
81                // NOTE:
82                //   Windows 5.1: CLSID {E436EBB2-524F-11CE-9F53-0020AF0BA770} Key Owner: O:AB (Administrators)
83                //   Windows 6.0: CLSID {E436EBB2-524F-11CE-9F53-0020AF0BA770} Key Owner: O:S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464 (TrustedInstaller)
84                GetRegKeySecurityDescriptor(Key, OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION, OriginalSecurityDescriptor);
85                _tprintf(_T("CLSID %s Key Owner & DACL: %s\n"), g_pszClassIdentifier, StringFromSecurityDescriptor(OriginalSecurityDescriptor));
86                //_tprintf(_T("CLSID %s Key Group: %s\n"), g_pszClassIdentifier, StringFromRegKeySecurityDescriptor(Key, GROUP_SECURITY_INFORMATION));
87                CSecurityDesc AdministratorsOwnerSecurityDescriptor;
88                AdministratorsOwnerSecurityDescriptor.SetOwner(Sids::Admins());
89                ATLENSURE_SUCCEEDED2(HRESULT_FROM_WIN32(Key.SetKeySecurity(OWNER_SECURITY_INFORMATION, const_cast<SECURITY_DESCRIPTOR*>((const SECURITY_DESCRIPTOR*) AdministratorsOwnerSecurityDescriptor))));
90                _tprintf(_T("Owner: %s\n"), StringFromRegKeySecurityDescriptor(Key, OWNER_SECURITY_INFORMATION));
91                ATLENSURE_SUCCEEDED2(HRESULT_FROM_WIN32(Key.Close()));
92                ATLENSURE_SUCCEEDED2(HRESULT_FROM_WIN32(Key.Open(HKEY_CLASSES_ROOT, pszKeyName, READ_CONTROL | WRITE_DAC | WRITE_OWNER)));
93                CSecurityDesc AccessListSecurityDescriptor = OriginalSecurityDescriptor;
94                CDacl AccessList;
95                ATLENSURE_THROW2(AccessListSecurityDescriptor.GetDacl(&AccessList), HRESULT_FROM_WIN32(GetLastError()));
96                ATLVERIFY(AccessList.AddAllowedAce(Sids::Admins(), GENERIC_ALL, CONTAINER_INHERIT_ACE));
97                AccessListSecurityDescriptor.SetDacl(AccessList);
98                ATLENSURE_SUCCEEDED2(HRESULT_FROM_WIN32(Key.SetKeySecurity(DACL_SECURITY_INFORMATION, const_cast<SECURITY_DESCRIPTOR*>((const SECURITY_DESCRIPTOR*) AccessListSecurityDescriptor))));
99                _tprintf(_T("DACL: %s\n"), StringFromRegKeySecurityDescriptor(Key, DACL_SECURITY_INFORMATION));
100#if !defined(_DEBUG)
101                MessageBox(NULL, _T("Before SetKeySecurity(OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION, ...)"), _T("Debug"), MB_OK);
102#endif // !defined(_DEBUG);
103                ATLENSURE_SUCCEEDED2(HRESULT_FROM_WIN32(Key.SetKeySecurity(OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION, const_cast<SECURITY_DESCRIPTOR*>((const SECURITY_DESCRIPTOR*) OriginalSecurityDescriptor))));
104                _tprintf(_T("Owner & DACL: %s\n"), StringFromRegKeySecurityDescriptor(Key, OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION));
105        }
106        _ATLCATCH(Exception)
107        {
108                _tprintf(_T("Fatal Error 0x%08x (%s)\n"), (HRESULT) Exception, AtlFormatSystemMessage(Exception).TrimRight(_T("\t\n\r .")));
109        }
110        _ATLCATCHALL()
111        {
112                _tprintf(_T("Fatal Error\n"));
113        }
114#if !defined(_DEBUG)
115        MessageBox(NULL, _T("After Everything"), _T("Debug"), MB_OK);
116#endif // !defined(_DEBUG);
117        return 0;
118}
119
Note: See TracBrowser for help on using the repository browser.