Results 1 to 11 of 11

Thread: [RESOLVED] GEt unc path gives access denied error for user without admin permissions

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2013
    Posts
    285

    Resolved [RESOLVED] GEt unc path gives access denied error for user without admin permissions

    I am using code given in this link to get UNC path of a file.

    It works only if a user has admin Admin Privileges. Otherwise it gives access denied error. What is the reason for it? Is there any alternative or solutions?

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: GEt unc path gives access denied error for user without admin permissions

    Is there any alternative
    file system object can return the sharename of a mapped drive
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: GEt unc path gives access denied error for user without admin permissions

    Spelunking for shares on the local PC is secured because a malware "agent" running locally could use this to find such shares and report them to external machines for exploitation. Since a local process never needs to use a share to reach a local file (i.e. mapped drive letters or UNC names) this isn't a burden.

    Thus enumerating shares on the local machine requires elevation.

    However it is simple for a local process to convert a remote path based on a mapped drive letter to a UNC name for local use. This doesn't need to be secured.

    So if you taker Karl's code (at your link) and strip out 90% of it you'll have working code that doesn't require elevation. Or just write your own, for example:

    Code:
    Option Explicit
    
    Private Const UNIVERSAL_NAME_INFO_LEVEL As Long = 1
    
    Private Const NO_ERROR As Long = 0
    Private Const ERROR_MORE_DATA As Long = 234
    Private Const ERROR_NO_NET_OR_BAD_PATH = 1203
    Private Const ERROR_NOT_CONNECTED As Long = 2250
    
    Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyW" ( _
        ByVal lpStringDest As Long, _
        ByVal lpStringSource As Long) As Long
    
    Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenW" ( _
        ByVal lpString As Long) As Long
    
    Private Declare Function WNetGetUniversalName Lib "mpr" _
        Alias "WNetGetUniversalNameW" ( _
        ByVal lpLocalPath As Long, _
        ByVal dwInfoLevel As Long, _
        ByRef Buffer As Long, _
        ByRef BufferSize As Long) As Long
    
    Private Function UncNameOf(ByVal DriveLetterPath As String, ByRef UncPath As String) As Long
        Dim BufferSize As Long
        Dim Buffer() As Long
    
        ReDim Buffer(0)
        UncNameOf = WNetGetUniversalName(StrPtr(DriveLetterPath), _
                                         UNIVERSAL_NAME_INFO_LEVEL, _
                                         Buffer(0), _
                                         BufferSize)
        If UncNameOf = ERROR_MORE_DATA Then
            ReDim Buffer((BufferSize + 3) \ 4 - 1)
            UncNameOf = WNetGetUniversalName(StrPtr(DriveLetterPath), _
                                             UNIVERSAL_NAME_INFO_LEVEL, _
                                             Buffer(0), _
                                             BufferSize)
            If UncNameOf = NO_ERROR Then
                UncPath = Space$(lstrlen(Buffer(0)))
                lstrcpy StrPtr(UncPath), Buffer(0)
            End If
        End If
    End Function
    Last edited by dilettante; May 22nd, 2014 at 09:33 AM.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2013
    Posts
    285

    Re: GEt unc path gives access denied error for user without admin permissions

    The code given by you only works for mapped drive. What about other folders?
    Last edited by IT researcher; May 23rd, 2014 at 06:14 AM.

  5. #5
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: GEt unc path gives access denied error for user without admin permissions

    Yes, it accepts a full drive-letter based path and translates that by putting the server & share names in, creating a UNC-format name for the path.

    It will not do this for paths on local drives, because that is an administration function and not a legitimate application function. To do that is a little like trying to unscramble an egg anyway: any given file could be located within any number of overlapping shares.

  6. #6
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,654

    Re: GEt unc path gives access denied error for user without admin permissions

    Perhaps I'm not understanding what kind of shares we're talking about; but if we're talking about things you see when you select Network in an Explorer window or Mapped Drives in My Computer, both of those can be enumerated with IShellFolder without any elevation.

    See http://btmtz.mvps.org/enumdeskvb for a basic shell browser that will enumerate all mapped drives and network locations. No ocx or dll required.

    Here what a treeview in my project looks like with My Computer and Network opened. JON-PC is the local machine:

    Last edited by fafalone; May 23rd, 2014 at 04:53 PM.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2013
    Posts
    285

    Re: GEt unc path gives access denied error for user without admin permissions

    Quote Originally Posted by dilettante View Post
    Yes, it accepts a full drive-letter based path and translates that by putting the server & share names in, creating a UNC-format name for the path.

    It will not do this for paths on local drives, because that is an administration function and not a legitimate application function. To do that is a little like trying to unscramble an egg anyway: any given file could be located within any number of overlapping shares.
    But Path copy copy works even in user and without admin privileges. Its able to provide UNC path for all files, also of local drive.

    How does it do?( The code is in C++)

  8. #8
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,654

    Re: GEt unc path gives access denied error for user without admin permissions

    One of the ways it does it is to get a list of all the shares and search them for the path, then replaces the drive letter with the share name. See the PluginUtils::GetNetworkShareFilePath function. It calls that when its function using WGetUniversalNameW fails, so I'd guess it has the same permission issues. Wouldn't be hard to do the same thing in VB; especially since there's a registry key that has all the shares that can be read (SYSTEM\\CurrentControlSet\\Services\\Lanmanserver\\Shares)

    Interesting program.
    Last edited by fafalone; May 24th, 2014 at 07:57 AM.

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: GEt unc path gives access denied error for user without admin permissions

    you can also try
    Code:
    Dim fso As FileSystemObject, d As Drive
    Set fso = CreateObject("scripting.filesystemobject")
     fil = "y:\compex\usb_lan.exe"  ' change to suit
     Set f = fso.GetFile(fil).Drive
     MsgBox Replace(fil, f.Path, f.ShareName)
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    May 2013
    Posts
    285

    Re: GEt unc path gives access denied error for user without admin permissions

    Quote Originally Posted by fafalone View Post
    One of the ways it does it is to get a list of all the shares and search them for the path, then replaces the drive letter with the share name. See the PluginUtils::GetNetworkShareFilePath function. It calls that when its function using WGetUniversalNameW fails, so I'd guess it has the same permission issues. Wouldn't be hard to do the same thing in VB; especially since there's a registry key that has all the shares that can be read (SYSTEM\\CurrentControlSet\\Services\\Lanmanserver\\Shares)

    Interesting program.
    ok. By using registry SYSTEM\\CurrentControlSet\\Services\\Lanmanserver\\Shares we can get share name. I think it will work in user also. Is there any readily available code to get share name from the registry?

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    May 2013
    Posts
    285

    Re: GEt unc path gives access denied error for user without admin permissions

    Got the solution here

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width