Skip to content

Commit ab2da86

Browse files
committed
config: initial implementation
1 parent 340ee63 commit ab2da86

17 files changed

+3662
-0
lines changed

config/config.go

Lines changed: 847 additions & 0 deletions
Large diffs are not rendered by default.

config/flag/command_line.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package command_line
2+
3+
import (
4+
"flag"
5+
"os"
6+
7+
"github.com/oiweiwei/go-msrpc/config"
8+
)
9+
10+
func BindFlags(c *config.Config, flagSet *flag.FlagSet) {
11+
12+
flagSet.BoolVar(&c.Debug, "debug", c.Debug, "enable debug output")
13+
14+
flagSet.StringVar(&c.Server, "server", c.Server, "server to connect to")
15+
flagSet.StringVar(&c.Domain, "domain", c.Domain, "domain to authenticate to")
16+
flagSet.StringVar(&c.Username, "username", c.Username, "username to authenticate as")
17+
flagSet.StringVar(&c.Workstation, "workstation", c.Workstation, "workstation to authenticate from")
18+
19+
flagSet.DurationVar(&c.Timeout, "timeout", c.Timeout, "timeout")
20+
21+
flagSet.StringVar(&c.Credential.Password, "password", c.Credential.Password, "password to authenticate with")
22+
flagSet.StringVar(&c.Credential.NTHash, "nthash", c.Credential.NTHash, "NT hash to authenticate with")
23+
flagSet.StringVar(&c.Credential.MachineAccountPassword, "machine-account-password", c.Credential.MachineAccountPassword, "machine account password to authenticate with")
24+
flagSet.StringVar(&c.Credential.MachineAccountNTHash, "machine-account-nthash", c.Credential.MachineAccountNTHash, "machine account NT hash to authenticate with")
25+
26+
flagSet.StringVar(&c.Auth.Level, "auth-level", c.Auth.Level, "authentication level: none, connect, call, pkt, integrity, privacy")
27+
flagSet.StringVar(&c.Auth.Type, "auth-type", c.Auth.Type, "authentication type: ntlm, krb5")
28+
flagSet.StringVar(&c.Auth.TargetName, "target-name", c.Auth.TargetName, "target name")
29+
flagSet.BoolVar(&c.Auth.SPNEGO, "spnego", c.Auth.SPNEGO, "use spnego")
30+
flagSet.StringVar(&c.Auth.Impersonation, "impersonation", c.Auth.Impersonation, "impersonation level: anonymous, identify, impersonate, delegate")
31+
flagSet.StringVar(&c.Auth.KRB5.ConfigFile, "krb5-config-file", c.Auth.KRB5.ConfigFile, "path to krb5.conf")
32+
flagSet.StringVar(&c.Auth.KRB5.KDCServer, "krb5-kdc-server", c.Auth.KRB5.KDCServer, "KDC server to authenticate to")
33+
flagSet.StringVar(&c.Auth.KRB5.AdminServer, "krb5-admin-server", c.Auth.KRB5.AdminServer, "admin server to authenticate to")
34+
flagSet.StringVar(&c.Auth.KRB5.Keytab, "krb5-keytab-path", c.Auth.KRB5.Keytab, "path to keytab")
35+
flagSet.StringVar(&c.Auth.KRB5.CCache, "krb5-ccache-path", c.Auth.KRB5.CCache, "path to ccache")
36+
flagSet.Var(&c.Auth.KRB5.EncryptionTypes, "krb5-encryption-types", "encryption types to use: aes256-cts-hmac-sha1-96, aes128-cts-hmac-sha1-96, arcfour-hmac-md5")
37+
flagSet.BoolVar(&c.Auth.KRB5.DCEStyle, "krb5-dce-style", c.Auth.KRB5.DCEStyle, "use DCE style")
38+
flagSet.BoolVar(&c.Auth.KRB5.DisablePAFXFAST, "krb5-disable-pafx-fast", c.Auth.KRB5.DisablePAFXFAST, "disable PA-FX-FAST")
39+
flagSet.BoolVar(&c.Auth.KRB5.MutualAuthn, "krb5-mutual-authn", c.Auth.KRB5.MutualAuthn, "use mutual authentication")
40+
41+
flagSet.BoolVar(&c.Auth.NTLM.NTLMv1, "ntlm-v1", c.Auth.NTLM.NTLMv1, "use NTLMv1")
42+
flagSet.BoolVar(&c.Auth.NTLM.NoESS, "ntlm-no-ess", c.Auth.NTLM.NoESS, "use no extended session security")
43+
44+
flagSet.BoolVar(&c.Verify.Presentation, "verify-presentation", false, "verify presentation")
45+
flagSet.BoolVar(&c.Verify.Header2, "verify-header2", false, "verify header2")
46+
flagSet.BoolVar(&c.Verify.BitMask, "verify-bitmask", false, "verify bitmask")
47+
48+
flagSet.IntVar(&c.SMB.Port, "smb-port", 445, "SMB port")
49+
flagSet.BoolVar(&c.SMB.Sign, "smb-sign", false, "SMB signing")
50+
flagSet.BoolVar(&c.SMB.Seal, "smb-seal", false, "SMB sealing")
51+
flagSet.StringVar(&c.SMB.Dialect, "smb-dialect", c.SMB.Dialect, "SMB dialect: 2.0.2 (202), 2.1.0 (210), 3.0.0 (300), 3.0.2 (302), 3.1.1 (311)")
52+
53+
flagSet.BoolVar(&c.EPM.Enabled, "epm", c.EPM.Enabled, "use endpoint mapper")
54+
flagSet.StringVar(&c.EPM.AuthLevel, "epm-auth-level", c.EPM.AuthLevel, "endpoint mapper authentication level: none, connect, call, pkt, integrity, privacy")
55+
56+
flagSet.StringVar(&c.Protocol, "protocol", c.Protocol, "protocol to use, ncacn_np (smb), ncacn_ip_tcp (tcp)")
57+
}
58+
59+
func ParseAndValidate(cfg *config.Config, flagSet *flag.FlagSet) error {
60+
61+
flagSet.Parse(os.Args[1:])
62+
63+
if cfg.Server == "" && flagSet.NArg() > 0 {
64+
cfg.Server = flagSet.Arg(0)
65+
flagSet.Parse(flagSet.Args()[1:])
66+
}
67+
68+
return cfg.Validate()
69+
}

examples/common/common.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package common
2+
3+
import "encoding/json"
4+
5+
var J = func(data any) string { b, _ := json.MarshalIndent(data, "", " "); return string(b) }

examples/samples_with_config/dnsp.go

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
//go:build exclude
2+
3+
// dnsp.go script dumps and print all zones (including cache) from the dns server.
4+
package main
5+
6+
import (
7+
"context"
8+
"flag"
9+
"fmt"
10+
"os"
11+
"strings"
12+
13+
"github.com/oiweiwei/go-msrpc/dcerpc"
14+
"github.com/oiweiwei/go-msrpc/ndr"
15+
16+
"github.com/oiweiwei/go-msrpc/ssp/gssapi"
17+
18+
config "github.com/oiweiwei/go-msrpc/config"
19+
config_flag "github.com/oiweiwei/go-msrpc/config/flag"
20+
21+
"github.com/oiweiwei/go-msrpc/msrpc/dnsp"
22+
"github.com/oiweiwei/go-msrpc/msrpc/dnsp/dnsserver/v5"
23+
"github.com/oiweiwei/go-msrpc/msrpc/dnsp/record"
24+
25+
_ "github.com/oiweiwei/go-msrpc/msrpc/erref/win32"
26+
)
27+
28+
var (
29+
cfg = config.New()
30+
zoneName string
31+
nodeName string
32+
serverName string
33+
)
34+
35+
var P = fmt.Println
36+
37+
func init() {
38+
39+
config_flag.BindFlags(cfg, flag.CommandLine)
40+
41+
flag.CommandLine.StringVar(&zoneName, "zone", "", "zone name")
42+
flag.CommandLine.StringVar(&nodeName, "node", "@", "node name")
43+
flag.CommandLine.StringVar(&serverName, "server-name", "", "server name")
44+
}
45+
46+
func main() {
47+
48+
if err := config_flag.ParseAndValidate(cfg, flag.CommandLine); err != nil {
49+
fmt.Fprintln(os.Stderr, err)
50+
return
51+
}
52+
53+
ctx := gssapi.NewSecurityContext(context.Background())
54+
55+
cc, err := dcerpc.Dial(ctx, cfg.ServerAddr(), cfg.DialOptions(ctx)...)
56+
if err != nil {
57+
fmt.Fprintln(os.Stderr, "dial", err)
58+
return
59+
}
60+
61+
defer cc.Close(ctx)
62+
63+
cli, err := dnsserver.NewDNSServerClient(ctx, cc, cfg.ClientOptions(ctx)...)
64+
if err != nil {
65+
fmt.Fprintln(os.Stderr, "new_dnsserver_client", err)
66+
return
67+
}
68+
69+
if zoneName != "" {
70+
P("-----------------------")
71+
P("ZONE:", zoneName)
72+
P("-----------------------")
73+
EnumRR(ctx, cli, zoneName, nodeName)
74+
return
75+
}
76+
77+
resp, err := cli.ComplexOperation2(ctx, &dnsserver.ComplexOperation2Request{
78+
ClientVersion: 0x0,
79+
ServerName: serverName,
80+
Operation: "EnumZones",
81+
TypeIn: uint32(dnsp.TypeIDDword),
82+
DataIn: &dnsp.Union{Value: &dnsp.Union_Dword{Dword: uint32(dnsp.ZoneRequestFilterAll)}},
83+
})
84+
85+
if err != nil {
86+
fmt.Fprintln(os.Stderr, "enum_zones", err)
87+
return
88+
}
89+
90+
for _, zone := range resp.DataOut.GetValue().(*dnsp.ZoneListW2K).ZoneArray {
91+
92+
P("-----------------------")
93+
P("ZONE:", zone.ZoneName)
94+
P("-----------------------")
95+
96+
EnumRR(ctx, cli, zone.ZoneName, "@")
97+
98+
}
99+
}
100+
101+
type ZoneNode struct {
102+
Zone string
103+
Node string
104+
}
105+
106+
func EnumRR(ctx context.Context, cli dnsserver.DNSServerClient, zone string, n string) {
107+
108+
next := []*ZoneNode{}
109+
110+
rr, err := cli.EnumRecords2(ctx, &dnsserver.EnumRecords2Request{
111+
ServerName: serverName,
112+
Zone: zone,
113+
NodeName: n,
114+
RecordType: 0x00FF,
115+
SelectFlag: 0x0000001F,
116+
})
117+
118+
if err != nil {
119+
fmt.Fprintln(os.Stderr, n, zone, err)
120+
return
121+
}
122+
123+
if n == "@" {
124+
n = ""
125+
} else {
126+
n = "." + n
127+
}
128+
129+
var nodes record.NodeList
130+
131+
if err := ndr.Unmarshal(rr.Buffer, &nodes, ndr.Opaque); err != nil {
132+
fmt.Fprintln(os.Stderr, "umarshal_nodes_list", err)
133+
return
134+
}
135+
136+
for _, node := range nodes.DNSNodes {
137+
138+
if len(node.DNSRecords) == 0 {
139+
if node.ChildCount > 0 {
140+
fmt.Printf("%s\t?\tNODE\t?\t(child: %d)\n", NodeName(node, n, zone), node.ChildCount)
141+
next = append(next, &ZoneNode{zone, string(node.DNSNodeName.DNSName) + n})
142+
}
143+
}
144+
145+
for _, rr := range node.RRs() {
146+
if rr.Header().Name == "" {
147+
rr.Header().Name = NodeName(node, n, zone)
148+
}
149+
P(rr)
150+
}
151+
}
152+
153+
for _, nxt := range next {
154+
P("-----------------------")
155+
P("ZONE CHILD:", nxt.Node+"."+strings.TrimPrefix(nxt.Zone, "."))
156+
P("-----------------------")
157+
EnumRR(ctx, cli, nxt.Zone, nxt.Node)
158+
}
159+
}
160+
161+
func NodeName(node *record.Node, p, zone string) string {
162+
n := string(node.DNSNodeName.DNSName)
163+
if n == "" {
164+
return NameJoin(p, zone)
165+
}
166+
return n
167+
}
168+
169+
func NameJoin(n ...string) string {
170+
171+
ret := ""
172+
173+
for _, nn := range n {
174+
ret += "." + strings.Trim(nn, ".")
175+
}
176+
177+
return strings.Trim(ret, ".") + "."
178+
}

0 commit comments

Comments
 (0)