-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
lsobj.go
106 lines (100 loc) · 2.19 KB
/
lsobj.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// lsobj is a console-based WinObj-like tool
// See https://technet.microsoft.com/en-us/sysinternals/winobj
package main
import (
"errors"
"fmt"
"os"
"unsafe"
"github.com/hillu/go-ntdll"
)
var skipDir = errors.New("skip this directory")
type walkFunc func(string, string) error
func walk(entry string, fn walkFunc) error {
var h ntdll.Handle
if st := ntdll.NtOpenDirectoryObject(&h, ntdll.STANDARD_RIGHTS_READ|ntdll.DIRECTORY_QUERY,
ntdll.NewObjectAttributes(entry, 0, 0, nil),
); st != 0 {
return st.Error()
}
defer ntdll.NtClose(h)
var context uint32
for {
var buf [32768]byte
var length uint32
switch st := ntdll.NtQueryDirectoryObject(
h,
&buf[0],
uint32(len(buf)),
true,
context == 0,
&context,
&length,
); st {
case ntdll.STATUS_SUCCESS:
case ntdll.STATUS_NO_MORE_ENTRIES:
return nil
default:
return st.Error()
}
odi := (*ntdll.ObjectDirectoryInformationT)(unsafe.Pointer(&buf[0]))
var path string
if entry == `\` {
path = `\` + odi.Name.String()
} else {
path = entry + `\` + odi.Name.String()
}
switch typ := odi.TypeName.String(); typ {
case "Directory":
if err := walk(path, fn); err != nil {
return err
}
default:
switch err := fn(path, typ); err {
case skipDir:
return nil
case nil:
continue
default:
return err
}
}
}
}
func main() {
var arg string
if len(os.Args) > 1 {
arg = os.Args[1]
} else {
arg = "\\"
}
walk(arg, func(path, typ string) error {
switch typ {
case "SymbolicLink":
var h ntdll.Handle
if st := ntdll.NtOpenSymbolicLinkObject(
&h,
ntdll.STANDARD_RIGHTS_READ|ntdll.DIRECTORY_QUERY,
ntdll.NewObjectAttributes(path, 0, 0, nil),
); st != 0 {
fmt.Printf("%s %s -> <NtOpenSymbolicLinkObject error %08x>\n", path, typ, st)
return nil
}
defer ntdll.NtClose(h)
target := ntdll.NewEmptyUnicodeString(1024)
var length uint32
if st := ntdll.NtQuerySymbolicLinkObject(
h,
target,
&length,
); st != 0 {
fmt.Printf("%s %s -> <NtQuerySymbolicLinkObject error %08x>\n", path, typ, st)
return nil
}
fmt.Printf("%s %s -> %s\n", path, typ, target)
default:
fmt.Printf("%s %s\n", path, typ)
}
return nil
})
}