How to take file ownership to the current user using win32 api
Asked Answered
T

0

3

I want to take file ownership using win32 api, and I want my code to work on both xp and win7

anyway, here is what i came up with

Function that changes the ownership of the file

int ChangeFileOwner()
{
        HANDLE token;
        char *filename = "c:\\file1.txt"; //(not owned by the current user)
        DWORD len;
        PSECURITY_DESCRIPTOR security = NULL;
        int retValue = 1;
        PSID sid;

        // Get the privileges you need
        if (OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &token)) {

        if(!SetPrivilege("SeTakeOwnershipPrivilege", 1))retValue=0;
            if(!SetPrivilege("SeSecurityPrivilege", 1))retValue=0;
            if(!SetPrivilege("SeBackupPrivilege", 1))retValue=0;
            if(!SetPrivilege("SeRestorePrivilege", 1))retValue=0;
        } else retValue = 0;

        // Create the security descriptor
        if (retValue) {
            GetFileSecurity(filename, OWNER_SECURITY_INFORMATION, security, 0, &len);
            security = (PSECURITY_DESCRIPTOR)malloc(len);
            if (!InitializeSecurityDescriptor(security,SECURITY_DESCRIPTOR_REVISION))
                retValue = 0;
        }

        // Get the sid for the username
        if (retValue) {
                GetLogonSID(token, &sid) ;
            }
        // Set the sid to be the new owner
        if (retValue && !SetSecurityDescriptorOwner(security, sid, 0))
            retValue = 0;

        // Save the security descriptor
        if (retValue)
            retValue = SetFileSecurity(filename, OWNER_SECURITY_INFORMATION, security);
        if (security) free(security);

        return retValue;
}

Function to get the current user SID

BOOL GetLogonSID (HANDLE hToken, PSID *ppsid) 
{
   BOOL bSuccess = FALSE;
   DWORD dwIndex;
   DWORD dwLength = 0;
   PTOKEN_GROUPS ptg = NULL;
// Get required buffer size and allocate the TOKEN_GROUPS buffer.
   GetTokenInformation(hToken,TokenGroups,(LPVOID) ptg,0,&dwLength) ;

   ptg = (PTOKEN_GROUPS)HeapAlloc(GetProcessHeap(),
         HEAP_ZERO_MEMORY, dwLength);
// Get the token group information from the access token.
   GetTokenInformation(hToken,TokenGroups,(LPVOID) ptg,dwLength,&dwLength) ;
// Loop through the groups to find the logon SID.
   for (dwIndex = 0; dwIndex < ptg->GroupCount; dwIndex++) 
      if ((ptg->Groups[dwIndex].Attributes & SE_GROUP_LOGON_ID)
             ==  SE_GROUP_LOGON_ID) 
      {
      // Found the logon SID; make a copy of it.

         dwLength = GetLengthSid(ptg->Groups[dwIndex].Sid);
         *ppsid = (PSID) HeapAlloc(GetProcessHeap(),
                     HEAP_ZERO_MEMORY, dwLength);
         CopySid(dwLength, *ppsid, ptg->Groups[dwIndex].Sid); 

         break;
      }
return TRUE;

}

Code To Set Privilege

int SetPrivilege(char *privilege, int enable) 
{
    TOKEN_PRIVILEGES tp;
    LUID luid;
    HANDLE token;

    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token)) return 0;
    if (!LookupPrivilegeValue(NULL, privilege, &luid)) return 0; 

    tp.PrivilegeCount = 1;
    tp.Privileges[0].Luid = luid;
    if (enable) tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    else tp.Privileges[0].Attributes = 0;

    // Enable the privilege or disable all privileges.
    return AdjustTokenPrivileges(token, 0, &tp, NULL, NULL, NULL); 
}
Tootsy answered 5/1, 2012 at 15:9 Comment(6)
I can see that it's well indented. If it's working and you just would like comments and suggestions for improvement, it should be migrated to codereview, if it's not working, please tell what's wrong.Slickenside
it just works successfully, but the file ownership stays as it is ..Tootsy
@DanielFischer would u please check itTootsy
Well, has the user running the process the right to take ownership of the file? I think the only users having the right to change ownership of a file are the owner and - what's Windows' near-equivalent of root? - the administrator and I'm not sure about the latter.Slickenside
no, any admin can take owner ship of file if you try the tool that comes with win7 "takeown" like the following "takeown /f c:\windows\system32\osk.exe" it worksTootsy
Okay, so are you running as admin?Slickenside

© 2022 - 2024 — McMap. All rights reserved.