Jump to content
Sign in to follow this  
supraspecies

Take ownership under Windows 7

Recommended Posts

supraspecies

Searched the forum, but failed to find a solid answer how to do that. Specifically, I need to take ownership of aero.msstyles file to install a custom Windows 7 theme.

Share this post


Link to post
Share on other sites
Neutro

Hey,

You could give UDF a try :)

; #FUNCTION# ====================================================================================================================
; Name...........: _SetObjectOwner
; Description ...: Sets the owner of an object.
; Syntax.........:  _SetObjectOwner($oName, $_SE_OBJECT_TYPE, $AccountName)
; Parameters ....:  $oName   - The name or handle to the object. This can be a path to a file or folder, a registry key,
;                    + a service name, a process handle, etc. See the comments on the _SE_OBJECT_TYPE enum for more info.
;                    $_SE_OBJECT_TYPE (Optional) - The type of the object to set permissions. This must be one of the values of the
;                    +_SE_OBJECT_TYPE enum. The default is $SE_FILE_OBJECT (a file or folder).
;                    $AccountName (Optional) - The account name or SID string to set. the default is the administrators group.
;                        +Can be any user name or SID string, see GetSidStruct() for more info.
; Return values .: Success      - 1
;                  Failure      - 0 and sets @error
; Author ........: FredAI
; Modified.......:
; Remarks .......:
; Related .......: _GetSidStruct
; Link ..........:
; Example .......: _SetObjectOwner('C:\Example.txt')

Edited by Neutro
  • Like 1

Share this post


Link to post
Share on other sites
supraspecies

@Neutro, thanks a lot! While I was looking, I figured out an easy way:

$STYLEFILE=(@WindowsDir & '\Resources\Themes\Aero\aero.msstyles')
Run(@ComSpec & " /C " & 'TAKEOWN /F ' & $STYLEFILE, '', @SW_HIDE)
Run(@ComSpec & " /C " & 'icacls ' & $STYLEFILE & ' /grant administrators:F /t', '', @SW_HIDE)

But I noticed nobody is using it. Is something wrong with it? Doesn't work under some circumstances?

Edited by supraspecies

Share this post


Link to post
Share on other sites
Neutro

Seems good to me as well :)

Maybe using the Set ACL UDF will be faster since you do not need to call an external software (takeown & icalcs) to do this, but if it's just for a few files then it doesn't really matter i guess!

Share this post


Link to post
Share on other sites
supraspecies

One file. :) But I'll still try out UDF you posted because I myself don't want to call external software unless necessary. Thank you, and topic can be considered closed.

Share this post


Link to post
Share on other sites
careca

Ownership.reg:

RMB over the file/s to own.

Windows Registry Editor Version 5.00

;=============================================================================

;Take ownership Users

[HKEY_CLASSES_ROOT*shellown]
@="Users Ownership"
"NoWorkingDirectory"=""
"Icon"="C:ICOown.ico"

[HKEY_CLASSES_ROOT*shellowncommand]
@="cmd.exe /c takeown /f "%1" /r /d y && icacls "%1" /grant Users:F /t"
"IsolatedCommand"="cmd.exe /c takeown /f "%1" /r /d y && icacls "%1" /grant Users:F /t"

[HKEY_CLASSES_ROOTDirectoryshellown]
@="Users Ownership"
"NoWorkingDirectory"=""
"Icon"="C:ICOown.ico"

[HKEY_CLASSES_ROOTDirectoryshellowncommand]
@="cmd.exe /c takeown /f "%1" /r /d y && icacls "%1" /grant Users:F /t"
"IsolatedCommand"="cmd.exe /c takeown /f "%1" /r /d y && icacls "%1" /grant Users:F /t"

[HKEY_CLASSES_ROOTDriveshellown]
@="Users Ownership"
"NoWorkingDirectory"=""
"Icon"="C:ICOown.ico"

[HKEY_CLASSES_ROOTDriveshellowncommand]
@="cmd.exe /c takeown /f "%1" /r /d y && icacls "%1" /grant Users:F /t"
"IsolatedCommand"="cmd.exe /c takeown /f "%1" /r /d y && icacls "%1" /grant Users:F /t"

;=============================================================================

Edited by careca

Spoiler

Paster - Main function is to paste text, but has more functions. (No longer mantained, switched to String Trigger)

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Firefox Profile Backup - Backup/restore previously saved profile.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Share this post


Link to post
Share on other sites
supraspecies

@ careca

Already have that on my system, has nothing to do with AutoIT. I wanted my script to take ownership on its own. And even though I could make it click on mouse context menu option, it wouldn't be proper because it would work only for those who actually have that option in context menu.

Share this post


Link to post
Share on other sites
UEZ

Maybe this helps, too. 

 

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
supraspecies

I'm testing that UDF on a random system file, and no go.

#include <Permissions.au3>
#RequireAdmin
_GrantAllAccess(@WindowsDir & '\System32\advapi32.dll')

It works for non-system files (user-created), but on system files it does nothing for me. It reports success, but nothing changes. And yes, I'm running it as admin. I even have UAC turned off. Any ideas?

Actually - I'm wrong. Depends on files. Seems to work for Windows theme files, but DLLs in System32 - nope. I don't need my script to access them at the moment, but someday i might... So I'm asking.

P.S. If you're wondering what is all this for - customizing Windows 7 installation. Unattended.

 

@ UEZ

Appreciate it, trying now.

Edited by supraspecies

Share this post


Link to post
Share on other sites
careca

@ careca

Already have that on my system, has nothing to do with AutoIT. I wanted my script to take ownership on its own. And even though I could make it click on mouse context menu option, it wouldn't be proper because it would work only for those who actually have that option in context menu.

Yeah, but you could use the same commands in autoit.


Spoiler

Paster - Main function is to paste text, but has more functions. (No longer mantained, switched to String Trigger)

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Firefox Profile Backup - Backup/restore previously saved profile.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Share this post


Link to post
Share on other sites
spudw2k

Share this post


Link to post
Share on other sites
supraspecies

Actually - I'm wrong. Depends on files. Seems to work for Windows theme files, but DLLs in System32 - nope. I don't need my script to access them at the moment, but someday i might... So I'm asking.

 

That day has come. I need to replace shellstyle.dll in System32 on first Windows 7 launch (post-install). Permission script above and takeown do not work with files located in System32. How can my script gain permissions needed to replace files in System32 folder?

In case youre wondering - not using it for any malicious purpose. I have customized my Windows and made an unattended install for it. This file contains modded Classic Theme for Windows, and I need my Windows to install with this theme by default.

Edited by supraspecies

Share this post


Link to post
Share on other sites
careca

safe mode?


Spoiler

Paster - Main function is to paste text, but has more functions. (No longer mantained, switched to String Trigger)

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Firefox Profile Backup - Backup/restore previously saved profile.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Share this post


Link to post
Share on other sites
MHz

In Windows XP, it was known as Windows File Protection. Later it become Windows Resource Protection. This is perhaps the problem.

In XP you could replace the file in the dllcache folder and then the file in the system32 folder. In Vista+, I am not sure how it is done at this moment.

Share this post


Link to post
Share on other sites
careca

Spoiler

Paster - Main function is to paste text, but has more functions. (No longer mantained, switched to String Trigger)

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Firefox Profile Backup - Backup/restore previously saved profile.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Share this post


Link to post
Share on other sites
supraspecies

Solved it (with a help from a friend). If anyone needs to know how to replace files in System32 (including x64 edition of Windows), here is the way:

#requireadmin
DllCall("kernel32.dll", "int", "Wow64DisableWow64FsRedirection", "int", 1)
$STYLEFILE=(@WindowsDir & '\System32\shellstyle.dll')
Run(@ComSpec & " /c " & 'TAKEOWN /F ' & $STYLEFILE, '', @SW_HIDE)
Run(@ComSpec & " /c " & 'cacls.exe ' & $STYLEFILE & ' /E /P BUILTIN\Administrators:F', '', @SW_HIDE)
DllCall("kernel32.dll", "int", "Wow64EnableWow64FsRedirection", "int", 1)
 

This will grant you all permissions to do whatever you wish.

Edited by supraspecies
  • Like 1

Share this post


Link to post
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
Sign in to follow this  

×