forked from RedTeamPentesting/adauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtarget_test.go
172 lines (142 loc) · 4.16 KB
/
target_test.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package adauth_test
import (
"context"
"net"
"strconv"
"testing"
"github.com/RedTeamPentesting/adauth"
)
func TestNewTarget(t *testing.T) {
targetIP := net.ParseIP("10.0.0.1")
targetPort := "1234"
targetHostname := "computer.tld"
resolver := &testResolver{
HostToAddr: map[string][]net.IP{
targetHostname: {targetIP},
},
AddrToHost: map[string][]string{
targetIP.String(): {targetHostname},
},
}
t.Run("hostname_from_ip", func(t *testing.T) {
target := adauth.NewTarget("", targetIP.String())
target.Resolver = resolver
if target.Address() != targetIP.String() {
t.Errorf("target address is %q instead of %q", target.Address(), targetIP.String())
}
hostname, err := target.Hostname(context.Background())
if err != nil {
t.Errorf("get hostname: %v", err)
}
if hostname != targetHostname {
t.Errorf("hostname is %q instead of %q", hostname, targetHostname)
}
})
t.Run("ip_from_hostname", func(t *testing.T) {
target := adauth.NewTarget("", targetHostname)
target.Resolver = resolver
if target.Address() != targetHostname {
t.Errorf("target address is %q instead of %q", target.Address(), targetIP.String())
}
ip, err := target.IP(context.Background())
if err != nil {
t.Errorf("get hostname: %v", err)
}
if ip.String() != targetIP.String() {
t.Errorf("hostname is %q instead of %q", ip.String(), targetIP.String())
}
})
t.Run("hostname_and_port", func(t *testing.T) {
target := adauth.NewTarget("", net.JoinHostPort(targetHostname, targetPort))
target.Resolver = resolver
if target.Port != targetPort {
t.Errorf("target port is %q instead of %q", target.Port, targetPort)
}
_, port, err := net.SplitHostPort(target.Address())
if err != nil {
t.Fatalf("split target.Address(): %v", err)
}
if port != targetPort {
t.Errorf("target.Address() contains port %q instead of %q", port, targetPort)
}
})
t.Run("ip_and_port", func(t *testing.T) {
target := adauth.NewTarget("", net.JoinHostPort(targetIP.String(), targetPort))
target.Resolver = resolver
if target.Port != targetPort {
t.Errorf("target port is %q instead of %q", target.Port, targetPort)
}
_, port, err := net.SplitHostPort(target.Address())
if err != nil {
t.Fatalf("split target.Address(): %v", err)
}
if port != targetPort {
t.Errorf("target.Address() contains port %q instead of %q", port, targetPort)
}
})
}
func TestTargetAddressWithAndWithoutPort(t *testing.T) {
targetHostname := "computer.tld"
t.Run("with_port", func(t *testing.T) {
target := adauth.NewTarget("ldap", targetHostname+":389")
if target.Address() != targetHostname+":389" {
t.Errorf("target.Address() is %q instead of %q", target.Address(), targetHostname)
}
if target.AddressWithoutPort() != targetHostname {
t.Errorf("target.AddressWithoutPort() is %q instead of %q", target.AddressWithoutPort(), targetHostname)
}
})
t.Run("without_port", func(t *testing.T) {
target := adauth.NewTarget("ldap", targetHostname)
if target.Address() != targetHostname {
t.Errorf("target.Address() is %q instead of %q", target.Address(), targetHostname)
}
if target.AddressWithoutPort() != targetHostname {
t.Errorf("target.AddressWithoutPort() is %q instead of %q", target.AddressWithoutPort(), targetHostname)
}
})
}
func TestNewTargetSPN(t *testing.T) {
hostname := "computer.tld"
testCases := []struct {
InputProtocol string
SPNProtocol string
}{
{
InputProtocol: "",
SPNProtocol: "host",
},
{
InputProtocol: "ldaps",
SPNProtocol: "ldap",
},
{
InputProtocol: "smb",
SPNProtocol: "cifs",
},
{
InputProtocol: "foo",
SPNProtocol: "foo",
},
{
InputProtocol: "http",
SPNProtocol: "http",
},
{
InputProtocol: "https",
SPNProtocol: "http",
},
}
for i, testCase := range testCases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
spn, err := adauth.NewTarget(testCase.InputProtocol, hostname).SPN(context.Background())
if err != nil {
t.Fatalf("SPN: %v", err)
}
expectedSPN := testCase.SPNProtocol + `/` + hostname
if spn != expectedSPN {
t.Fatalf("SPN for %q is %q instead of %q", testCase.InputProtocol, spn, expectedSPN)
}
})
}
}