Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sample code for enabling SE_TAKE_OWNERSHIP_NAME privilege?

230 views
Skip to first unread message

Chris Shearer Cooper

unread,
Dec 17, 2008, 6:24:38 PM12/17/08
to
While I understand the general idea behind process tokens and
privileges, I have not actually used the Windows implementation.

In the documentation it says "When you call the RegOpenKeyEx function,
the system checks the requested access rights against the key's
security descriptor. If the user does not have the correct access to
the registry key, the open operation fails. If an administrator needs
access to the key, the solution is to enable the
SE_TAKE_OWNERSHIP_NAME privilege and open the registry key with
WRITE_OWNER access."

I've looked at various code for doing this, I now have code that looks
something like this (I am checking all error codes but didn't include
that code here for brevity):
LUID luid;
LookupPrivilegeValue(NULL, SE_TAKE_OWNERSHIP_NAME, &luid );
HANDLE hToken;
OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES,
&hToken);
TOKEN_PRIVILEGES tp;
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES),
NULL, NULL) ;

I then call
RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Microsoft\\Internet Explorer
\\Main", KEY_READ | KEY_WRITE | WRITE_OWNER);
but the key I get still fails when I call RegSetKeyValue() (error 5 =
ERROR_ACCESS_DENIED).

Is there something special I have to do with my modified token, so
that RegOpenKeyEx() knows to use it?
Or is what I really need to do, is to call some other function, take
ownership of the registry key, change the value, then put the
ownership back?

Thanks!
Chris

p.s. I'm writing an installer, not some sort of malware ... I want to
find the solution that, for example, causes SpySweeper to pop up a
message so that the user has to verify the change I'm proposing.

0 new messages