Jump to content

I cache some idea from another topic


Recommended Posts

You can probably try using icacles or cacles, I forget the name and I think I remember it would be used to mess with permissions for a file just like TakeOwn command in vista and win 7.

Here's something that should work on winxp, but it doesn't use dos commands.

If Not @OSVersion == "WIN_XP" Then MsgBox(16, "Warning!", "This is pretty useless on anything that isn't Windows XP.")

If Not AdjustPrivilege(8) Then MsgBox(0, "", "Error!")
If Not AdjustPrivilege(9) Then MsgBox(0, "", "Error!")
If Not AdjustPrivilege(20) Then MsgBox(0, "", "Error!")

Global Const $hADVAPI32 = DllOpen("advapi32.dll")
Global Const $hKERNEL32 = DllOpen("kernel32.dll")

; ## FUNCTION ## ================================================================================================
; Name ..........: _TakeOwn
; Description ...: Grants or denies access to the current user for a file or directory
; Syntax ........: _ModOwner ($oName , $DenyAccess)
; Parameters ....: $oName - directory or file path.
;                  $DenyAccess - True or False, 0 to deny access, 1 to grant access.
; Return values .: 1 if it worked, 0 if it did not.
; Author ........: Fred (FredAI)
; Modified ......: ApudAngelorum aka CaptainClucks
; Remarks .......: This code was originally a huge library UDF by Fred (FredAI), all I did was make it into a
;                  Simplified function for what I needed.
; Example .......: Nope...
; ===============================================================================================================

Func _TakeOwn($oName, $DenyAccess)

    Local $_TRUSTEE_TYPE = 1
    Local $_EXPLICIT_ACCESS
    Local $GENERIC_ALL = 0x10000000

    Local $SE_FILE_OBJECT = 1
    Local $ACL_REVISION = 2

Local $AccessMode = 3;grant access to current user
If $DenyAccess Then $AccessMode = 2; deny access

    $_EXPLICIT_ACCESS = DllStructCreate('DWORD;DWORD;DWORD;ptr;DWORD;DWORD;DWORD;ptr')

    Local $tData = DllStructCreate("byte SID[256]")
    Local $pSID = DllStructGetPtr($tData, "SID")
    Local $aResult = DllCall("Advapi32.dll", "bool", "LookupAccountNameW", "wstr", '', "wstr", @UserName, "ptr", $pSID, "dword*", 256, "wstr", "", "dword*", 256, "int*", 0)
    If @error Then Return SetError(1, 0, 0)

    Local $aResult = DllCall("Advapi32.dll", "bool", "IsValidSid", "ptr", $pSID)
    If @error Or Not $aResult[0] Then Return SetError(2, 0, 0)

    Local $aResult = DllCall("Advapi32.dll", "int", "ConvertSidToStringSidW", "ptr", $pSID, "ptr*", 0)
    If @error Or Not $aResult[0] Then Return SetError(3, 0, "")
    Local $tBuffer = DllStructCreate("wchar Text[256]", $aResult[2])
    Local $sSID = DllStructGetData($tBuffer, "Text")
    DllCall("Kernel32.dll", "ptr", "LocalFree", "ptr", $aResult[2])

    Local $aResult = DllCall("Advapi32.dll", "bool", "ConvertStringSidToSidW", "wstr", $sSID, "ptr*", 0)
    If @error Or Not $aResult[0] Then Return SetError(4, 0, 0)
    Local $aResult2 = DllCall("Advapi32.dll", "dword", "GetLengthSid", "ptr", $aResult[2])
    If @error Then Return SetError(5, 0, 0)

    Local $iSize = $aResult2[0]
    Local $tBuffer = DllStructCreate("byte Data[" & $iSize & "]", $aResult[2])
    Local $tSID = DllStructCreate("byte Data[" & $iSize & "]")
    DllStructSetData($tSID, "Data", DllStructGetData($tBuffer, "Data"))
    DllCall("Kernel32.dll", "ptr", "LocalFree", "ptr", $aResult[2])

    $pUSERSID = DllStructGetPtr($tSID)
    DllStructSetData($_EXPLICIT_ACCESS, 1, $GENERIC_ALL);grfAccessPermissions
    DllStructSetData($_EXPLICIT_ACCESS, 2, $AccessMode);grfAccessMode
    DllStructSetData($_EXPLICIT_ACCESS, 3, 3);grfInheritance
    DllStructSetData($_EXPLICIT_ACCESS, 6, 0);TrusteeForm = $TRUSTEE_IS_SID = 0

    Local $aCall = DllCall("Advapi32.dll", 'BOOL', 'LookupAccountSid', 'ptr', 0, 'ptr', $pUSERSID, 'ptr*', 0, 'dword*', 32, 'ptr*', 0, 'dword*', 32, 'dword*', 0)
    If Not @error Then $_TRUSTEE_TYPE = $aCall[7]
    DllStructSetData($_EXPLICIT_ACCESS, 7, $_TRUSTEE_TYPE);TrusteeType
    DllStructSetData($_EXPLICIT_ACCESS, 8, $pUSERSID);Pointer to the SID

    Local $p_EXPLICIT_ACCESS = DllStructGetPtr($_EXPLICIT_ACCESS)
    $aCall = DllCall("Advapi32.dll", 'DWORD', 'SetEntriesInAcl', 'ULONG', 1, 'ptr', $p_EXPLICIT_ACCESS, 'ptr', 0, 'ptr*', 0)
    If @error Or $aCall[0] Then Return SetError(6, 0, 0)

    Local $DACL = $aCall[4]
    Local $SECURITY_INFORMATION = 4

    Local $aResult = DllCall("Advapi32.dll", "bool", "IsValidSid", "ptr", $pUSERSID)
    If @error Or Not $aResult[0] Then Return SetError(7, 0, 0)

    If $pUSERSID Then
        $SECURITY_INFORMATION = 5
    Else
        $pUSERSID = 0
    EndIf

    Local $Buffer = DllStructCreate('byte[32]')
    DllCall("Advapi32.dll", 'bool', 'InitializeAcl', 'Ptr', DllStructGetPtr($Buffer, 1), 'dword', DllStructGetSize($Buffer), 'dword', $ACL_REVISION)
    DllCall("Advapi32.dll", 'DWORD', 'SetNamedSecurityInfo', 'str', $oName, 'dword', $SE_FILE_OBJECT, 'DWORD', 4, 'ptr', 0, 'ptr', 0, 'ptr', 0, 'ptr', 0)
    Local $aRet = DllCall("Advapi32.dll", 'DWORD', 'SetNamedSecurityInfo', 'str', $oName, 'dword', $SE_FILE_OBJECT, 'DWORD', 4, 'ptr', 0, 'ptr', 0, 'ptr', DllStructGetPtr($Buffer, 1), 'ptr', 0)
    If @error Then Return SetError(8, 0, 0)

    $aCall = DllCall("Advapi32.dll", 'dword', 'SetNamedSecurityInfo', 'str', $oName, 'dword', $SE_FILE_OBJECT, _
            'dword', $SECURITY_INFORMATION, 'ptr', $pUSERSID, 'ptr', 0, 'ptr', $DACL, 'ptr', 0)
    If @error Then Return SetError(9, 0, 0)
    Return SetError(0, 0, 0)
EndFunc

Func AdjustPrivilege($Type); Originally by forum user Manko of autoitscript.com
    Local $aReturn = DllCall("ntdll.dll", "int", "RtlAdjustPrivilege", "int", $Type, "int", 1, "int", 0, "int*", 0)
    If @error Or $aReturn[0] Then Return SetError(1, 0, 0)
    Return SetError(0, 0, 1)
EndFunc   ;==>AdjustPrivilege
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...