Skip to content

Commit 1fdd48f

Browse files
Refactoring
1 parent 129849f commit 1fdd48f

File tree

7 files changed

+195
-259
lines changed

7 files changed

+195
-259
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ Kerberos authentication is possible even when specifying the target via IP
5353
address if reverse lookups are possible. Similarly, the domain can be omitted
5454
when the target hostname contains the domain.
5555

56-
The library also contains helper packages for LDAP and DCERPC, a Kerebros PKINIT
57-
implementation as well as helpers for creating and writing CCache files (see
58-
examples).
56+
The library also contains helper packages for LDAP, SMB and DCERPC, a Kerebros
57+
PKINIT implementation as well as helpers for creating and writing CCache files
58+
(see examples).
5959

6060
## Features
6161

@@ -70,12 +70,14 @@ examples).
7070
* Kerberos, NTLM, Simple Bind
7171
* mTLS Authentication / Pass-the-Certificate (LDAPS or LDAP+StartTLS)
7272
* Channel Binding (Kerberos and NTLM)
73+
* SMB
74+
* Kerberos, NTLM
75+
* Signing and Sealing
7376
* DCERPC:
7477
* Kerberos, NTLM
7578
* Raw endpoits (with port mapping)
7679
* Named pipes (SMB)
77-
* Signing
78-
* Sealing
80+
* Signing and Sealing
7981

8082
## Caveats
8183

dcerpcauth/dcerpcauth.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import (
2121
// function.
2222
type Options struct {
2323
// SMBOptions holds options for the SMB dialer. This dialer is only used
24-
// with the named pipe transport. If SMBOptions is nil, sealing will be
25-
// enabled for the smb dialer, specify an empty slice to disable this
26-
// default.
24+
// with the named pipe transport. If SMBOptions is nil, encryption/sealing
25+
// will be enabled for the SMB dialer, specify an empty slice to disable
26+
// this default.
2727
SMBOptions []smb2.DialerOption
2828
// PKINITOptions can be used to modify the Kerberos PKINIT behavior.
2929
PKINITOptions []pkinit.Option

examples/dcerpc/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/oiweiwei/go-msrpc/msrpc/dtyp"
1414
"github.com/oiweiwei/go-msrpc/msrpc/epm/epm/v3"
1515
"github.com/oiweiwei/go-msrpc/msrpc/samr/samr/v1"
16-
"github.com/oiweiwei/go-msrpc/ssp/gssapi"
1716
"github.com/spf13/pflag"
1817
)
1918

@@ -43,7 +42,7 @@ func run() error {
4342
return err
4443
}
4544

46-
ctx := gssapi.NewSecurityContext(context.Background())
45+
ctx := context.Background()
4746

4847
dcerpcOpts, err := dcerpcauth.AuthenticationOptions(ctx, creds, target, dcerpcauthOpts)
4948
if err != nil {

examples/smb/main.go

Lines changed: 88 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,105 @@
11
package main
22

33
import (
4-
"context"
5-
"fmt"
6-
"github.com/RedTeamPentesting/adauth/smbauth"
7-
"github.com/oiweiwei/go-msrpc/smb2"
8-
"github.com/oiweiwei/go-msrpc/ssp"
9-
"net"
10-
"os"
11-
"path/filepath"
12-
13-
"github.com/RedTeamPentesting/adauth"
14-
"github.com/oiweiwei/go-msrpc/ssp/gssapi"
15-
"github.com/spf13/pflag"
16-
)
4+
"context"
5+
"fmt"
6+
"net"
7+
"os"
8+
"path/filepath"
179

18-
var (
19-
debug bool
20-
authOpts = &adauth.Options{
21-
Debug: adauth.NewDebugFunc(&debug, os.Stderr, true),
22-
}
23-
)
10+
"github.com/RedTeamPentesting/adauth/smbauth"
2411

25-
func init() {
26-
pflag.CommandLine.BoolVar(&debug, "debug", false, "Enable debug output")
27-
authOpts.RegisterFlags(pflag.CommandLine)
28-
gssapi.AddMechanism(ssp.SPNEGO)
29-
gssapi.AddMechanism(ssp.NTLM)
30-
}
12+
"github.com/RedTeamPentesting/adauth"
13+
"github.com/spf13/pflag"
14+
)
3115

3216
func run() error {
33-
pflag.Parse()
34-
35-
if len(pflag.Args()) != 1 {
36-
return fmt.Errorf("usage: %s <target> [--debug]", binaryName())
37-
}
38-
39-
creds, target, err := authOpts.WithTarget(context.Background(), "host", pflag.Arg(0))
40-
if err != nil {
41-
return err
42-
}
43-
44-
ctx := gssapi.NewSecurityContext(context.Background())
45-
46-
smbOpts, secOpts, err := smbauth.AuthenticationOptions(ctx, creds, target, &smbauth.Options{})
47-
if err != nil {
48-
return err
49-
}
50-
51-
// Create go-smb2 Dialer
52-
dialer := smb2.NewDialer(append(smbOpts, smb2.WithSecurity(secOpts...))...)
53-
54-
conn, err := net.Dial("tcp", net.JoinHostPort(target.AddressWithoutPort(), "445"))
55-
if err != nil {
56-
return err
57-
}
58-
defer conn.Close()
59-
60-
sess, err := dialer.Dial(conn)
61-
if err != nil {
62-
return err
63-
}
64-
defer sess.Logoff()
65-
66-
names, err := sess.ListSharenames()
67-
if err != nil {
68-
return err
69-
}
70-
71-
for _, name := range names {
72-
fmt.Println(name)
73-
}
74-
return nil
17+
var (
18+
debug bool
19+
authOpts = &adauth.Options{
20+
Debug: adauth.NewDebugFunc(&debug, os.Stderr, true),
21+
}
22+
smbauthOpts = &smbauth.Options{
23+
Debug: authOpts.Debug,
24+
}
25+
)
26+
27+
pflag.CommandLine.BoolVar(&debug, "debug", false, "Enable debug output")
28+
authOpts.RegisterFlags(pflag.CommandLine)
29+
pflag.Parse()
30+
31+
if len(pflag.Args()) != 1 {
32+
return fmt.Errorf("usage: %s [options] <target>", binaryName())
33+
}
34+
35+
creds, target, err := authOpts.WithTarget(context.Background(), "host", pflag.Arg(0))
36+
if err != nil {
37+
return err
38+
}
39+
40+
if target.Port == "" {
41+
target.Port = "445"
42+
}
43+
44+
ctx := context.Background()
45+
46+
smbDialer, err := smbauth.Dialer(ctx, creds, target, smbauthOpts)
47+
if err != nil {
48+
return fmt.Errorf("setup SMB authentication: %w", err)
49+
}
50+
51+
conn, err := net.Dial("tcp", target.Address())
52+
if err != nil {
53+
return fmt.Errorf("dial: %w", err)
54+
}
55+
56+
defer conn.Close()
57+
58+
sess, err := smbDialer.DialContext(ctx, conn)
59+
if err != nil {
60+
return fmt.Errorf("create session: %w", err)
61+
}
62+
63+
defer sess.Logoff()
64+
65+
shares, err := sess.ListSharenames()
66+
if err != nil {
67+
return fmt.Errorf("list share names: %w", err)
68+
}
69+
70+
if len(shares) == 0 {
71+
fmt.Println("No shares available")
72+
73+
return nil
74+
}
75+
76+
fmt.Println("Shares:")
77+
78+
for _, share := range shares {
79+
fmt.Printf(" - %s\n", share)
80+
}
81+
82+
return nil
7583
}
7684

7785
func binaryName() string {
78-
executable, err := os.Executable()
79-
if err == nil {
80-
return filepath.Base(executable)
81-
}
86+
executable, err := os.Executable()
87+
if err == nil {
88+
return filepath.Base(executable)
89+
}
8290

83-
if len(os.Args) > 0 {
84-
return filepath.Base(os.Args[0])
85-
}
91+
if len(os.Args) > 0 {
92+
return filepath.Base(os.Args[0])
93+
}
8694

87-
return "list-shares"
95+
return "smb"
8896
}
8997

9098
func main() {
91-
err := run()
92-
if err != nil {
93-
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
99+
err := run()
100+
if err != nil {
101+
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
94102

95-
os.Exit(1)
96-
}
103+
os.Exit(1)
104+
}
97105
}

go.mod

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/go-ldap/ldap/v3 v3.4.11-0.20250110131057-5d1b644709df
88
github.com/jcmturner/gokrb5/v8 v8.4.4
99
github.com/oiweiwei/go-msrpc v1.2.5
10+
github.com/oiweiwei/go-smb2.fork v1.0.0
1011
github.com/oiweiwei/gokrb5.fork/v9 v9.0.2
1112
github.com/spf13/pflag v1.0.6
1213
github.com/vadimi/go-ntlm v1.2.1
@@ -26,10 +27,9 @@ require (
2627
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
2728
github.com/mattn/go-colorable v0.1.14 // indirect
2829
github.com/mattn/go-isatty v0.0.20 // indirect
29-
github.com/oiweiwei/go-smb2.fork v1.0.0 // indirect
30-
github.com/rs/zerolog v1.33.0 // indirect
31-
golang.org/x/crypto v0.35.0 // indirect
32-
golang.org/x/net v0.36.0 // indirect
33-
golang.org/x/sys v0.30.0 // indirect
34-
golang.org/x/text v0.22.0 // indirect
30+
github.com/rs/zerolog v1.34.0 // indirect
31+
golang.org/x/crypto v0.37.0 // indirect
32+
golang.org/x/net v0.39.0 // indirect
33+
golang.org/x/sys v0.32.0 // indirect
34+
golang.org/x/text v0.24.0 // indirect
3535
)

go.sum

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ github.com/oiweiwei/gokrb5.fork/v9 v9.0.2/go.mod h1:KEnkAYUYqZ5VwzxLFbv3JHlRhCvd
5555
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
5656
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5757
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
58-
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
5958
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
60-
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
61-
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
6259
github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY=
6360
github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ=
6461
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
@@ -82,8 +79,6 @@ golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliY
8279
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
8380
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
8481
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
85-
golang.org/x/crypto v0.35.0 h1:b15kiHdrGCHrP6LvwaQ3c03kgNhhiMgvlhxHQhmg2Xs=
86-
golang.org/x/crypto v0.35.0/go.mod h1:dy7dXNW32cAb/6/PRuTNsix8T+vJAqvuIy5Bli/x0YQ=
8782
golang.org/x/crypto v0.37.0 h1:kJNSjF/Xp7kU0iB2Z+9viTPMW4EqqsrywMXLJOOsXSE=
8883
golang.org/x/crypto v0.37.0/go.mod h1:vg+k43peMZ0pUMhYmVAWysMK35e6ioLh3wB8ZCAfbVc=
8984
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
@@ -102,8 +97,8 @@ golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
10297
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
10398
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
10499
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
105-
golang.org/x/net v0.36.0 h1:vWF2fRbw4qslQsQzgFqZff+BItCvGFQqKzKIzx1rmoA=
106-
golang.org/x/net v0.36.0/go.mod h1:bFmbeoIPfrw4sMHNhb4J9f6+tPziuGjq7Jk/38fxi1I=
100+
golang.org/x/net v0.39.0 h1:ZCu7HMWDxpXpaiKdhzIfaltL9Lp31x/3fCP11bc6/fY=
101+
golang.org/x/net v0.39.0/go.mod h1:X7NRbYVEA+ewNkCNyJ513WmMdQ3BineSwVtN2zD/d+E=
107102
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
108103
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
109104
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -124,8 +119,6 @@ golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
124119
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
125120
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
126121
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
127-
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
128-
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
129122
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
130123
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
131124
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
@@ -146,8 +139,6 @@ golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
146139
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
147140
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
148141
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
149-
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
150-
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
151142
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
152143
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
153144
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

0 commit comments

Comments
 (0)