Parent Directory
|
Revision Log
Revision 1999 - (view) (download)
1 : | Isibaar | 1999 | #include <windows.h> |
2 : | #include <aclapi.h> | ||
3 : | #include <stdio.h> | ||
4 : | |||
5 : | BOOL SetPrivilege( | ||
6 : | HANDLE hToken, // access token handle | ||
7 : | LPCTSTR lpszPrivilege, // name of privilege to enable/disable | ||
8 : | BOOL bEnablePrivilege // to enable or disable privilege | ||
9 : | ) | ||
10 : | { | ||
11 : | TOKEN_PRIVILEGES tp; | ||
12 : | LUID luid; | ||
13 : | |||
14 : | if ( !LookupPrivilegeValue( | ||
15 : | NULL, // lookup privilege on local system | ||
16 : | lpszPrivilege, // privilege to lookup | ||
17 : | &luid ) ) // receives LUID of privilege | ||
18 : | { | ||
19 : | OutputDebugString(L"LookupPrivilegeValue error"); | ||
20 : | return FALSE; | ||
21 : | } | ||
22 : | |||
23 : | tp.PrivilegeCount = 1; | ||
24 : | tp.Privileges[0].Luid = luid; | ||
25 : | if (bEnablePrivilege) | ||
26 : | tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; | ||
27 : | else | ||
28 : | tp.Privileges[0].Attributes = 0; | ||
29 : | |||
30 : | // Enable the privilege or disable all privileges. | ||
31 : | |||
32 : | if ( !AdjustTokenPrivileges( | ||
33 : | hToken, | ||
34 : | FALSE, | ||
35 : | &tp, | ||
36 : | sizeof(TOKEN_PRIVILEGES), | ||
37 : | (PTOKEN_PRIVILEGES) NULL, | ||
38 : | (PDWORD) NULL) ) | ||
39 : | { | ||
40 : | OutputDebugString(L"AdjustTokenPrivileges error"); | ||
41 : | return FALSE; | ||
42 : | } | ||
43 : | |||
44 : | if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) | ||
45 : | |||
46 : | { | ||
47 : | OutputDebugString(L"The token does not have the specified privilege."); | ||
48 : | return FALSE; | ||
49 : | } | ||
50 : | |||
51 : | return TRUE; | ||
52 : | } | ||
53 : | |||
54 : | BOOL TakeOwnership(LPTSTR lpszRegKey) | ||
55 : | { | ||
56 : | BOOL bRetval = FALSE; | ||
57 : | |||
58 : | HANDLE hToken = NULL; | ||
59 : | PSECURITY_DESCRIPTOR pSecurityDescriptor = NULL; | ||
60 : | PSID pSIDAdmin = NULL; | ||
61 : | PACL pOldACL = NULL; | ||
62 : | PACL pNewACL = NULL; | ||
63 : | SID_IDENTIFIER_AUTHORITY SIDAuthNT = SECURITY_NT_AUTHORITY; | ||
64 : | EXPLICIT_ACCESS ea; | ||
65 : | DWORD dwRes; | ||
66 : | |||
67 : | // Create a SID for the BUILTIN\Administrators group. | ||
68 : | if (!AllocateAndInitializeSid(&SIDAuthNT, 2, | ||
69 : | SECURITY_BUILTIN_DOMAIN_RID, | ||
70 : | DOMAIN_ALIAS_RID_ADMINS, | ||
71 : | 0, 0, 0, 0, 0, 0, | ||
72 : | &pSIDAdmin)) | ||
73 : | { | ||
74 : | OutputDebugString(L"AllocateAndInitializeSid (Admin) error"); | ||
75 : | goto Cleanup; | ||
76 : | } | ||
77 : | |||
78 : | // Get DACL of current object. | ||
79 : | dwRes = GetNamedSecurityInfo( | ||
80 : | lpszRegKey, | ||
81 : | SE_REGISTRY_KEY, | ||
82 : | DACL_SECURITY_INFORMATION, | ||
83 : | NULL, | ||
84 : | NULL, | ||
85 : | &pOldACL, | ||
86 : | NULL, | ||
87 : | &pSecurityDescriptor); | ||
88 : | |||
89 : | if (ERROR_SUCCESS != dwRes) | ||
90 : | { | ||
91 : | bRetval = FALSE; | ||
92 : | OutputDebugString(L"GetNamedSecurityInfo failed."); | ||
93 : | goto Cleanup; | ||
94 : | } | ||
95 : | |||
96 : | ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS)); | ||
97 : | |||
98 : | // Set full control for Administrators. | ||
99 : | ea.grfAccessPermissions = GENERIC_ALL; | ||
100 : | ea.grfAccessMode = SET_ACCESS; | ||
101 : | ea.grfInheritance = NO_INHERITANCE; | ||
102 : | ea.Trustee.TrusteeForm = TRUSTEE_IS_SID; | ||
103 : | ea.Trustee.TrusteeType = TRUSTEE_IS_GROUP; | ||
104 : | ea.Trustee.ptstrName = (LPTSTR) pSIDAdmin; | ||
105 : | |||
106 : | if (ERROR_SUCCESS != SetEntriesInAcl(1,&ea,pOldACL,&pNewACL)) | ||
107 : | { | ||
108 : | OutputDebugString(L"Failed SetEntriesInAcl\n"); | ||
109 : | goto Cleanup; | ||
110 : | } | ||
111 : | |||
112 : | // Try to modify the object's DACL. | ||
113 : | dwRes = SetNamedSecurityInfo( | ||
114 : | lpszRegKey, // name of the object | ||
115 : | SE_REGISTRY_KEY, // type of object | ||
116 : | DACL_SECURITY_INFORMATION, // change only the object's DACL | ||
117 : | NULL, NULL, // do not change owner or group | ||
118 : | pNewACL, // DACL specified | ||
119 : | NULL); // do not change SACL | ||
120 : | |||
121 : | if (ERROR_SUCCESS == dwRes) | ||
122 : | { | ||
123 : | OutputDebugString(L"Successfully changed DACL\n"); | ||
124 : | bRetval = TRUE; | ||
125 : | // No more processing needed. | ||
126 : | goto Cleanup; | ||
127 : | } | ||
128 : | if (dwRes != ERROR_ACCESS_DENIED) | ||
129 : | { | ||
130 : | OutputDebugString(L"First SetNamedSecurityInfo call failed"); | ||
131 : | goto Cleanup; | ||
132 : | } | ||
133 : | |||
134 : | // If the preceding call failed because access was denied, | ||
135 : | // enable the SE_TAKE_OWNERSHIP_NAME privilege, create a SID for | ||
136 : | // the Administrators group, take ownership of the object, and | ||
137 : | // disable the privilege. Then try again to set the object's DACL. | ||
138 : | |||
139 : | // Open a handle to the access token for the calling process. | ||
140 : | if (!OpenProcessToken(GetCurrentProcess(), | ||
141 : | TOKEN_ADJUST_PRIVILEGES, | ||
142 : | &hToken)) | ||
143 : | { | ||
144 : | OutputDebugString(L"OpenProcessToken failed"); | ||
145 : | goto Cleanup; | ||
146 : | } | ||
147 : | |||
148 : | // Enable the SE_TAKE_OWNERSHIP_NAME privilege. | ||
149 : | if (!SetPrivilege(hToken, SE_TAKE_OWNERSHIP_NAME, TRUE)) | ||
150 : | { | ||
151 : | OutputDebugString(L"You must be logged on as Administrator.\n"); | ||
152 : | goto Cleanup; | ||
153 : | } | ||
154 : | |||
155 : | // Set the owner in the object's security descriptor. | ||
156 : | dwRes = SetNamedSecurityInfo( | ||
157 : | lpszRegKey, // name of the object | ||
158 : | SE_REGISTRY_KEY, // type of object | ||
159 : | OWNER_SECURITY_INFORMATION, // change only the object's owner | ||
160 : | pSIDAdmin, // SID of Administrator group | ||
161 : | NULL, | ||
162 : | NULL, | ||
163 : | NULL); | ||
164 : | |||
165 : | if (dwRes != ERROR_SUCCESS) | ||
166 : | { | ||
167 : | OutputDebugString(L"Could not set owner. Error: %u\n"); | ||
168 : | goto Cleanup; | ||
169 : | } | ||
170 : | |||
171 : | // Disable the SE_TAKE_OWNERSHIP_NAME privilege. | ||
172 : | if (!SetPrivilege(hToken, SE_TAKE_OWNERSHIP_NAME, FALSE)) | ||
173 : | { | ||
174 : | OutputDebugString(L"Failed SetPrivilege call unexpectedly.\n"); | ||
175 : | goto Cleanup; | ||
176 : | } | ||
177 : | |||
178 : | // Try again to modify the object's DACL, | ||
179 : | // now that we are the owner. | ||
180 : | dwRes = SetNamedSecurityInfo( | ||
181 : | lpszRegKey, // name of the object | ||
182 : | SE_REGISTRY_KEY, // type of object | ||
183 : | DACL_SECURITY_INFORMATION, // change only the object's DACL | ||
184 : | NULL, NULL, // do not change owner or group | ||
185 : | pNewACL, // DACL specified | ||
186 : | NULL); // do not change SACL | ||
187 : | |||
188 : | if (dwRes == ERROR_SUCCESS) | ||
189 : | { | ||
190 : | OutputDebugString(L"Successfully changed DACL\n"); | ||
191 : | bRetval = TRUE; | ||
192 : | } | ||
193 : | else | ||
194 : | { | ||
195 : | OutputDebugString(L"Second SetNamedSecurityInfo call failed"); | ||
196 : | } | ||
197 : | |||
198 : | Cleanup: | ||
199 : | |||
200 : | if (pSecurityDescriptor) | ||
201 : | LocalFree(pSecurityDescriptor); | ||
202 : | |||
203 : | if (pSIDAdmin) | ||
204 : | FreeSid(pSIDAdmin); | ||
205 : | |||
206 : | if (pNewACL) | ||
207 : | LocalFree(pNewACL); | ||
208 : | |||
209 : | if (hToken) | ||
210 : | CloseHandle(hToken); | ||
211 : | |||
212 : | return bRetval; | ||
213 : | |||
214 : | } | ||
215 : | |||
216 : | int CALLBACK WinMain( | ||
217 : | __in HINSTANCE hInstance, | ||
218 : | __in HINSTANCE hPrevInstance, | ||
219 : | __in LPSTR lpCmdLine, | ||
220 : | __in int nCmdShow | ||
221 : | ) | ||
222 : | { | ||
223 : | BOOL bRetval; | ||
224 : | |||
225 : | bRetval = TakeOwnership(L"MACHINE\\SOFTWARE\\Classes\\MediaFoundation\\MapVideo4cc"); // target reg-key | ||
226 : | |||
227 : | bRetval = TakeOwnership(L"MACHINE\\SOFTWARE\\Microsoft\\DirectShow\\Preferred"); // target reg-key | ||
228 : | |||
229 : | return bRetval; | ||
230 : | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |