473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

GetNamedSecurityInfo - Read Owner pt II

Hello again,

I am going to re-post a question. I got some excellent suggestions from
Rob and Mattias on this but their ideas did not solve the problem. Here is
the original post:

************************************************** ***
I am looking for a way to 'override' file security and read the Owner of
a file to which I have no access. I am a system administrator, as such I
have administrative rights to all the computers in the company. Some of my
user base has full control rights to their files and have elected to remove
my access to some files. It is possible for an administrator to regain
access, but it is a messy process and can be time consuming. I have had more
than one long night copying data to a larger partition having to wach the
job for files that will not copy and go back to clean it up.

I have written a vb.net program that uses Windows API functions to
automate this. It takes ownership of problem files, grants administrative
access, copies the file or folder plus the security information and then
sets everything back the way it was. There is only one hitch. I have been
unsuccessful reading the owner of a file using Win APIs such as
GetNamedSecurityInfo when I do not have access to the file. I can WRITE a
new owner to such a file, but not read it. I need to be able to do this so I
can subsequently restore the original owner after I copy the file.

My current work around is to make a command shell call to fileacl.exe.
This utility will read a file's owner regardless of permissions if you use
the /force switch. This works, but I am not very happy with it and I would
like to do the whole job with Win APIs. For one thing it makes the program
more portable because I do not need to remember to have the fileacl.exe
utility on every server/computer from which I run this program.

Since the fileacl utility does read the file owner without permissions,
it must be possible. Can anyone give me a hint on how this might be
accomplished?

************************************************** ***

I tried placing myself in the backup operators group and that did not help.
I have tried adjusting my token with the SeRestorePrivilege and
SeBackupPrivilege and that did not help either. (I am not certain I am doing
the latter properly, but the code I wrote does not return any errors,
including dll errors.) Does anyone have any other suggestions? Keep in
mind, I am an administrator on all boxes for which I have tried this.

Dave Coate
Nov 21 '05 #1
3 3967
Take a look at Knowledge Base article Q240184:
INFO: Reading/Modifying DACL of a File or Folder with Backup and Restore
Privilege
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/enu_kbwin32sdk/en-us/win32sdk/Q240184.htm

To use the privileges, check out:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/enu_kbwin32sdk/en-us/win32sdk/Q240184.htm

If you need addtional Win32 security APIs in VB.NET, check out
http://www.DataMarvel.com
It has samples on enabling privileges through a higher level wraper, which
is much simplier than the raw PInvoke Win32 APIs.

"Dave Coate" wrote:
Hello again,

I am going to re-post a question. I got some excellent suggestions from
Rob and Mattias on this but their ideas did not solve the problem. Here is
the original post:

************************************************** ***
I am looking for a way to 'override' file security and read the Owner of
a file to which I have no access. I am a system administrator, as such I
have administrative rights to all the computers in the company. Some of my
user base has full control rights to their files and have elected to remove
my access to some files. It is possible for an administrator to regain
access, but it is a messy process and can be time consuming. I have had more
than one long night copying data to a larger partition having to wach the
job for files that will not copy and go back to clean it up.

I have written a vb.net program that uses Windows API functions to
automate this. It takes ownership of problem files, grants administrative
access, copies the file or folder plus the security information and then
sets everything back the way it was. There is only one hitch. I have been
unsuccessful reading the owner of a file using Win APIs such as
GetNamedSecurityInfo when I do not have access to the file. I can WRITE a
new owner to such a file, but not read it. I need to be able to do this so I
can subsequently restore the original owner after I copy the file.

My current work around is to make a command shell call to fileacl.exe.
This utility will read a file's owner regardless of permissions if you use
the /force switch. This works, but I am not very happy with it and I would
like to do the whole job with Win APIs. For one thing it makes the program
more portable because I do not need to remember to have the fileacl.exe
utility on every server/computer from which I run this program.

Since the fileacl utility does read the file owner without permissions,
it must be possible. Can anyone give me a hint on how this might be
accomplished?

************************************************** ***

I tried placing myself in the backup operators group and that did not help.
I have tried adjusting my token with the SeRestorePrivilege and
SeBackupPrivilege and that did not help either. (I am not certain I am doing
the latter properly, but the code I wrote does not return any errors,
including dll errors.) Does anyone have any other suggestions? Keep in
mind, I am an administrator on all boxes for which I have tried this.

Dave Coate

Nov 21 '05 #2
jzhu,

Thanks for the pointer. This appears to be what I am looking for.
However, I can not quite get it to work. The Security Descriptor pointer
returned by GetKernelObjectSecurity appears to be invalid. At least that is
what 'IsValidSecurityDescriptor' returns. When I try to use it in
'GetSecurityDescriptorOwner' it blows up with 'Object reference not set to
an instance of an object'. Any idea what I am doing wrong?

Dave

My Code:
*****************************************
'Structures and Functions
<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Public Structure LUID
Public LowPart As Integer
Public HighPart As Integer
End Structure

<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Public Structure LUID_AND_ATTRIBUTES
Public pLuid As LUID
Public Attributes As Integer
End Structure

<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Public Structure TOKEN_PRIVILEGES
Public PrivilegeCount As Integer
Public Privileges As LUID_AND_ATTRIBUTES
End Structure

Private Const TOKEN_ADJUST_PRIVILEGES As Integer = &H20
Private Const TOKEN_QUERY As Integer = &H8
Private Const SE_PRIVILEGE_ENABLED As Integer = &H2
Private Const FORMAT_MESSAGE_FROM_SYSTEM As Integer = &H1000
Private Const EWX_FORCE As Integer = 4
Private Declare Ansi Function OpenProcessToken Lib "advapi32" ( _
ByVal ProcessHandle As IntPtr, _
ByVal DesiredAccess As Integer, _
ByRef TokenHandle As IntPtr) As Integer

Private Declare Ansi Function LookupPrivilegeValue Lib "advapi32" _
Alias "LookupPrivilegeValueA" ( _
ByVal lpSystemName As String, _
ByVal lpName As String, _
ByRef lpLuid As LUID) As Integer

Private Declare Ansi Function AdjustTokenPrivileges Lib "advapi32" ( _
ByVal TokenHandle As IntPtr, _
ByVal DisableAllPrivileges As Integer, _
ByRef NewState As TOKEN_PRIVILEGES, _
ByVal BufferLength As Integer, _
ByRef PreviousState As TOKEN_PRIVILEGES, _
ByRef ReturnLength As Integer) As Integer

Private Declare Function CreateFile Lib "kernel32.dll" _
Alias "CreateFileA" ( _
ByVal sFileName As String, _
ByVal iDesiredAccess As Integer, _
ByVal iSharedMode As Integer, _
ByVal iSecurityAttributes As Integer, _
ByVal iCreationDisposition As Integer, _
ByVal iFlagsAndAttributes As Integer, _
ByVal hTemplateFile As Integer) As IntPtr

Private Declare Function GetKernelObjectSecurity Lib "advapi32.dll" ( _
ByVal hFileHandle As IntPtr, _
ByVal SecurityInformation As SECURITY_INFORMATION, _
ByRef pSD As IntPtr, _
ByVal iLength As Integer, _
ByRef iLengthRequired As Integer) As Integer

Private Declare Function GetSecurityDescriptorOwner Lib "advapi32.dll"
( _
ByVal pSD As IntPtr, _
ByRef pOwner As IntPtr, _
ByRef pOwnerDefaulted As Boolean) As Integer

Private Declare Function IsValidSecurityDescriptor Lib "advapi32.dll"
( _
ByVal pSD As IntPtr) As Integer

*****************************************
Calling Code

Dim tokenHandle As IntPtr
Dim privilegeLUID As LUID
Dim newPrivileges As TOKEN_PRIVILEGES
Dim tokenPrivileges As TOKEN_PRIVILEGES
Dim bSuccess As Integer

If OpenProcessToken(Process.GetCurrentProcess.Handle, _
TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, tokenHandle) = 0 Then
MsgBox(Err.LastDllError)
End If

If LookupPrivilegeValue("", privilege, privilegeLUID) = 0 Then
MsgBox(Err.LastDllError)
End If

tokenPrivileges.PrivilegeCount = 1
tokenPrivileges.Privileges.Attributes = SE_PRIVILEGE_ENABLED
tokenPrivileges.Privileges.pLuid = privilegeLUID
bSuccess = AdjustTokenPrivileges(tokenHandle, 0, tokenPrivileges, _
4 + (12 * tokenPrivileges.PrivilegeCount), _
newPrivileges, 4 + (12 * newPrivileges.PrivilegeCount))
If bSuccess = 0 Then
MsgBox(Err.LastDllError)
End If

Dim hFileHandle As IntPtr

hFileHandle = CreateFile(sPath, READ_CONTROL, _
FILE_SHARE_READ, Nothing, OPEN_EXISTING, _
FILE_FLAG_BACKUP_SEMANTICS, Nothing)
If hFileHandle.ToString = INVALID_HANDLE_VALUE Then
MsgBox("CreateFile Failed")
Else
MsgBox("CreateFile Succeeded")
End If

Dim pSD As IntPtr
Dim iLen As Integer
Dim iLenReq As Integer

bSuccess = GetKernelObjectSecurity(hFileHandle, _
SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION, pSD, iLen,
iLenReq)

bSuccess = GetKernelObjectSecurity(hFileHandle, _
SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION, pSD, iLenReq,
0)

If bSuccess = 0 Then
MsgBox("GetKernelErr " & Err.LastDllError)
Else
MsgBox("GetKernelObjSec Suceeded")
End If

MsgBox(IsValidSecurityDescriptor(pSD))

Dim pOwner As IntPtr
Dim dft As Boolean

bSuccess = GetSecurityDescriptorOwner(pSD, pOwner, dft)
*****************************************


"jzhu" <jz**@discussions.microsoft.com> wrote in message
news:19**********************************@microsof t.com...
Take a look at Knowledge Base article Q240184:
INFO: Reading/Modifying DACL of a File or Folder with Backup and Restore
Privileges
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/enu_kbwin32sdk/en-us/win32sdk
/Q240184.htm
To use the privileges, check out:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/enu_kbwin32sdk/en-us/win32sdk
/Q240184.htm
If you need addtional Win32 security APIs in VB.NET, check out
http://www.DataMarvel.com
It has samples on enabling privileges through a higher level wraper, which
is much simplier than the raw PInvoke Win32 APIs.

"Dave Coate" wrote:
Hello again,

I am going to re-post a question. I got some excellent suggestions from Rob and Mattias on this but their ideas did not solve the problem. Here is the original post:

************************************************** ***
I am looking for a way to 'override' file security and read the Owner of a file to which I have no access. I am a system administrator, as such I
have administrative rights to all the computers in the company. Some of my user base has full control rights to their files and have elected to remove my access to some files. It is possible for an administrator to regain
access, but it is a messy process and can be time consuming. I have had more than one long night copying data to a larger partition having to wach the job for files that will not copy and go back to clean it up.

I have written a vb.net program that uses Windows API functions to
automate this. It takes ownership of problem files, grants administrative access, copies the file or folder plus the security information and then
sets everything back the way it was. There is only one hitch. I have been unsuccessful reading the owner of a file using Win APIs such as
GetNamedSecurityInfo when I do not have access to the file. I can WRITE a new owner to such a file, but not read it. I need to be able to do this so I can subsequently restore the original owner after I copy the file.

My current work around is to make a command shell call to fileacl.exe. This utility will read a file's owner regardless of permissions if you use the /force switch. This works, but I am not very happy with it and I would like to do the whole job with Win APIs. For one thing it makes the program more portable because I do not need to remember to have the fileacl.exe
utility on every server/computer from which I run this program.

Since the fileacl utility does read the file owner without permissions, it must be possible. Can anyone give me a hint on how this might be
accomplished?

************************************************** ***

I tried placing myself in the backup operators group and that did not help. I have tried adjusting my token with the SeRestorePrivilege and
SeBackupPrivilege and that did not help either. (I am not certain I am doing the latter properly, but the code I wrote does not return any errors,
including dll errors.) Does anyone have any other suggestions? Keep in
mind, I am an administrator on all boxes for which I have tried this.

Dave Coate

Nov 21 '05 #3
Dave,

Did you see my last reply to your old thread?

http://groups.google.com/groups?thre...TNGP15.phx.gbl

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
0
by: Majordomo | last post by:
-- >>>> --36742377 **** Command '--36742377' not recognized. >>>> Content-Type: text/plain; charset=us-ascii **** Command 'content-type:' not recognized. >>>> Content-Transfer-Encoding: 7bit...
1
by: isaac rainsford | last post by:
how on earth do i call and use GetNamedSecurityInfo from Advapi32.dll in VB.NET? i can make it work on VB5/6... any tips much appreciated, thanks in advance...
7
by: Gene | last post by:
I have a number of aspx pages on which a single user control appears. All of the aspx pages and the user control make user of code-behind modules. I need for logic in the user control's code-behind...
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
7
by: Dave Coate | last post by:
Hi everyone, I am looking for a way to 'override' file security and read the Owner of a file to which I have no access. I am a system administrator, as such I have administrative rights to all...
2
by: dba123 | last post by:
I need help in coding the following or if you can just point me in the right direction: 1) Reading the follwoing XML document 2) Hook up a GridView to the data received from the XML document. ...
3
by: eholz1 | last post by:
Hello PHP Group, I am having trouble setting permissions correctly so that the magickwand api (php 5.2) can read and write images. I usually read a file from one directory, create a magickwand...
1
by: leiger | last post by:
Hi, I need help with this problem as soon as possible (within a couple of days). This is the first time I have ever used Access 2007 and therefore I am having some problems - especially as I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.