【VBS】获取指定文件夹的用户权限

递归获取指定文件夹下的所有用户权限,代码如下:

rootPath = "D:\File Data\Shared"
forbiddenFile = "D:\forbidden_result.txt"
resultFile = "D:\security_result.txt"

Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(forbiddenFile) Then
    fso.DeleteFile forbiddenFile
End If
If fso.FileExists(resultFile) Then
    fso.DeleteFile resultFile
End If
Set treeList = CreateObject("Scripting.Dictionary")
FolderTree rootPath, "", "", treeList
Set fso = Nothing

Set stream = CreateObject("ADODB.Stream")
stream.Type = 2
stream.Open
stream.Charset = "UTF-8"
For Each key In treeList.Keys
    Set folder = treeList.Item(key)
    shortPath = folder.Item("shortPath")
    path = folder.Item("path")
    parent = folder.Item("parent")
    Set securityList = GetFolderSecurityDescriptor(shortPath, path, stream)
    Set parentSecurityList = CreateObject("Scripting.Dictionary")
    If parent <> "" Then
        Set parentSecurityList = treeList.Item(parent).Item("securityList")
    End If
    For Each itemKey in securityList.Keys
        Set item = securityList.Item(itemKey)
        sid = item.Item("sid")
        If parentSecurityList.Exists(sid) Then
            accessMask = item.Item("accessMask")
            Set parentItem = parentSecurityList.Item(sid)
            parentAccessMask = parentItem.Item("accessMask")
            If accessMask = parentAccessMask Then
                item.Item("inherit") = 1
            End If
        End If
    Next
    folder.Remove "securityList"
    folder.Add "securityList", securityList
Next
stream.SaveToFile forbiddenFile, 2
stream.Close
Set stream = Nothing

Set stream = CreateObject("ADODB.Stream")
stream.Type = 2
stream.Open
stream.Charset = "UTF-8"
stream.Position = stream.Size
header = "folder" & vbTab & _
        "domain" & vbTab & _
        "username" & vbTab & _
        "access mask" & vbTab &_
        "inherit" & vbTab & _
        "allow full control" & vbTab & _
        "allow modify" & vbTab & _
        "allow read and execute" & vbTab & _
        "allow list" & vbTab & _
        "allow read" & vbTab & _
        "allow write" & vbTab & _
        "allow special" & vbTab & _
        "deny full control" & vbTab & _
        "deny modify" & vbTab & _
        "deny read and execute" & vbTab & _
        "deny list" & vbTab & _
        "deny read" & vbTab & _
        "deny write" & vbTab & _
        "deny special" & vbNewLine
stream.WriteText header
For Each key In treeList.Keys
    Set folder = treeList.Item(key)
    path = folder.Item("path")
    Set securityList = folder.Item("securityList")
    For Each itemKey In securityList.Keys
        Set item = securityList.Item(itemKey)
        flag = ""
        If item.Item("inherit") > 0 Then flag = "Y"
        line = path & vbTab & _
                item.Item("domain") & vbTab &_
                item.Item("name") & vbTab & _
                item.Item("accessMask") & vbTab & _
                flag & vbTab &_
                GetAccessFlags(item.Item("accessMask")) & vbNewLine
        stream.WriteText line
    Next
Next
stream.SaveToFile resultFile, 2
stream.Close
Set treeList = Nothing
Set stream = Nothing
Wscript.Echo "Finished"


Function FolderTree(path, shortPath, parent, list)
    Set fso = CreateObject("Scripting.FileSystemObject")
    If shortPath <> "" Then
        Set folder = fso.GetFolder(shortPath)
    Else
        Set folder = fso.GetFolder(path)
    End If
    Err.Clear
    shortPath = folder.ShortPath
    If fso.FolderExists(folder) = True Then
        Set item = CreateObject("Scripting.Dictionary")
        Set securityList = CreateObject("Scripting.Dictionary")
        item.Add "path", path
        item.Add "parent", parent
        item.Add "shortPath", shortPath
        item.Add "securityList", securityList
        list.Add path, item
        Set subFolders = folder.SubFolders
        On Error Resume Next
        For Each subFolder In subFolders
            fullPath = path & "\" & subFolder.Name
            FolderTree fullPath, subFolder.shortPath, path, list
        Next
        On Error GoTo 0
        Set subFolders = Nothing
    End If
    Set fso = Nothing
    Set folder = Nothing
End Function


Function GetFolderSecurityDescriptor(path, fullPath, currentStream)
    On Error Resume Next
    Set list = CreateObject("Scripting.Dictionary")
    pathExpr = "winmgmts:Win32_LogicalFileSecuritySetting.path"
    Set wmiFileSecSetting = GetObject(pathExpr & "=""" & replace(path, "\", "\\") & """")
    wmiFileSecSetting.GetSecurityDescriptor wmiSecurityDescriptor
    DACL = wmiSecurityDescriptor.DACL
    On Error GoTo 0
    If VarType(DACL) <> 0 Then
        For Each wmiAce In DACL
            Set item = CreateObject("Scripting.Dictionary")
            Set trustee = wmiAce.Trustee
            sid = trustee.SIDString
            domain = trustee.Domain
            name = Trustee.Name
            accessMask = wmiAce.AceType & " " & wmiAce.AccessMask
            If list.Exists(sid) Then
                Set item = list.Item(sid)
                accessMask = item.Item("accessMask") & "," & accessMask
                item.Item("accessMask") = accessMask
            Else
                item.Add "sid", sid
                item.Add "domain", domain
                item.Add "name", name
                item.Add "accessMask", accessMask
                item.Add "inherit", 0
                list.Add sid, item
            End If
        Next
    Else
        currentStream.Position = currentStream.Size
        currentStream.WriteText fullPath & vbNewLine
    End If
    Set wmiSecurityDescriptor = Nothing
    Set wmiFileSecSetting = Nothing
    Set GetFolderSecurityDescriptor = list
End Function


Function GetAccessFlags(accessMask)
    Dim arr(14)
    For i = 0 To 13
        arr(i) = ""
    Next
    list = Split(accessMask, ",")
    For i = 0 To UBound(list)
        list2 = Split(list(i), " ")
        aceType = Int(list2(0))
        mask = Int(list2(1))
        flag = GetAccessFlag(mask)
        list2 = Split(flag, ",")
        startIndex = 0
        endIndex = 6
        If aceType = 1 Then
            startIndex = 7
            endIndex = 13
        End If
        For j = startIndex To endIndex
            value = ""
            If aceType = 1 Then
                value = list2(j - 7)
            Else
                value = list2(j)
            End If
            If arr(j) = "" And value <> "" Then arr(j) = value
        Next
    Next
    GetAccessFlags = Join(arr, vbTab)
End Function


Function GetAccessFlag(accessMask)
    FILE_READ_DATA = 1
    FILE_LIST_DIRECTORY = 1
    FILE_WRITE_DATA = 2
    FILE_ADD_FILE = 2
    FILE_APPEND_DATA = 4
    FILE_ADD_SUBDIRECTORY = 4
    FILE_READ_EA = 8
    FILE_WRITE_EA = 16
    FILE_EXECUTE = 32
    FILE_TRAVERSE = 32
    FILE_DELETE_CHILD = 64
    FILE_READ_ATTRIBUTES = 128
    FILE_WRITE_ATTRIBUTES = 256
    DELETE = 65536
    READ_CONTROL = 131072
    WRITE_DAC = 262144
    WRITE_OWNER = 524288
    SYNCHRONIZE = 1048576
    acRead = FILE_READ_DATA Or FILE_READ_ATTRIBUTES Or FILE_READ_EA Or READ_CONTROL Or SYNCHRONIZE
    acWrite = FILE_WRITE_DATA Or FILE_APPEND_DATA Or FILE_WRITE_ATTRIBUTES Or FILE_WRITE_EA Or SYNCHRONIZE
    acReadAndExecute = acRead Or FILE_EXECUTE
    acList = acReadAndExecute
    acModify = acReadAndExecute Or acWrite Or DELETE
    acFullControl = acModify Or FILE_DELETE_CHILD Or WRITE_DAC Or WRITE_OWNER
    Dim arr(7)
    For i = 0 To 6
        arr(i) = ""
    Next
    If (accessMask And acFullControl) = acFullControl Then arr(0) = "Y"
    If (accessMask And acModify) = acModify Then arr(1) = "Y"
    If (accessMask And acReadAndExecute) = acReadAndExecute Then arr(2) = "Y"
    If (accessMask And acList) = acList Then arr(3) = "Y"
    If (accessMask And acRead) = acRead Then arr(4) = "Y"
    If (accessMask And acWrite) = acWrite Then arr(5) = "Y"
    If arr(0) = "" And arr(1) = "" And arr(2) = "" And _
            arr(3) = "" And arr(4) = "" And arr(5) = "" Then
        arr(6) = "Y"
    End If
    GetAccessFlag = Join(arr, ",")
End Function

程序执行完后会放到将结果保存到 D:\security_result.txt 中,此时复制该文件中的文本,粘贴到Excel中即可。D:\forbidden_result.txt保存当前用户无法访问的目录列表。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,133评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,682评论 3 390
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,784评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,508评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,603评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,607评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,604评论 3 415
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,359评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,805评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,121评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,280评论 1 344
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,959评论 5 339
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,588评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,206评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,442评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,193评论 2 367
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,144评论 2 352