forked from tazjin/watchblob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
179 lines (153 loc) · 4.08 KB
/
main.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
package main
import (
"bytes"
"crypto/tls"
"encoding/xml"
"io"
"log"
"net/http"
"os"
"strings"
"github.com/urfave/cli/v2"
)
var debug bool
var inSecure bool
type AuthDomain struct {
Name string `xml:"name"`
}
type AuthDomains struct {
AuthDomain []AuthDomain `xml:"auth-domain"`
}
type WatchguardResponse struct {
Action string `xml:"action"`
LogonStatus int `xml:"logon_status"`
LogonId int `xml:"logon_id"`
AuthDomains AuthDomains `xml:"auth-domain-list"`
Error string `xml:"errStr"`
Challenge string `xml:"chaStr"`
}
func main() {
app := &cli.App{
Name: "watchblob",
Usage: "2-factor WatchGuard VPNs with OpenVPN",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "username",
Usage: "Username",
},
&cli.StringFlag{
Name: "password",
Usage: "Password",
},
&cli.BoolFlag{
Name: "password-stdin",
Usage: "take the password from stdin",
},
&cli.StringFlag{
Name: "token",
Usage: "token that is used to answer the challenge",
},
&cli.StringFlag{
Name: "host",
Required: true,
Usage: "Watchguard fqdn",
},
&cli.BoolFlag{
Name: "debug",
Value: false,
Usage: "enable debug output",
},
&cli.BoolFlag{
Name: "insecure",
Value: false,
Usage: "allow insecure ssl connection to watchguard",
},
},
Action: func(cCtx *cli.Context) error {
username := cCtx.String("username")
password := cCtx.String("password")
token := cCtx.String("token")
var err error
if username == "" {
username, err = readUsername()
if err != nil {
log.Fatalf("unable to read username: %s", err)
}
}
if cCtx.Bool("password-stdin") {
contents, err := io.ReadAll(os.Stdin)
if err != nil {
return err
}
password = strings.TrimSuffix(string(contents), "\n")
password = strings.TrimSuffix(password, "\r")
}
if password == "" {
password, err = readPassword()
if err != nil {
log.Fatalf("unable to read password: %s", err)
}
}
debug = cCtx.Bool("debug")
inSecure = cCtx.Bool("insecure")
run(cCtx.String("host"), username, password, token)
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func run(host, username, password, token string) {
log.Printf("Requesting challenge from %s as user %s\n", host, username)
challenge, err := triggerChallengeResponse(&host, &username, &password)
if err != nil {
log.Fatalf("unable to perform challenge request: %s", err)
}
if challenge.Error != "" {
log.Fatalf("Challenge authentication failed, with login status: %d(%s)", challenge.LogonStatus, challenge.Error)
}
token, err = readToken(&challenge, token)
if err != nil {
log.Fatalf("unable to read token: %s", err)
}
var response WatchguardResponse
switch challenge.LogonStatus {
case 4:
response, err = request(templateUrl(&host, templateResponseUri(challenge.LogonId, &token)))
case 8:
response, err = request(templateUrl(&host, templateMfaResponseUri(challenge.LogonId, &token)))
default:
log.Fatalf("unsupported Login Status: %d", challenge.LogonStatus)
}
if err != nil {
log.Fatalf("unable to perform response request: %s", err)
}
if response.LogonStatus != 1 {
log.Fatalf("response authentication failed: %v", response)
}
log.Printf("Login succeeded, you may now (quickly) authenticate OpenVPN with `%s` and `%s` as your password\n", username, token)
}
func triggerChallengeResponse(host *string, username *string, password *string) (r WatchguardResponse, err error) {
return request(templateUrl(host, templateChallengeTriggerUri(username, password)))
}
func request(url string) (r WatchguardResponse, err error) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: inSecure},
}
client := &http.Client{Transport: tr}
resp, err := client.Get(url)
if err != nil {
return
}
defer resp.Body.Close()
var buf bytes.Buffer
tee := io.TeeReader(resp.Body, &buf)
decoder := xml.NewDecoder(tee)
err = decoder.Decode(&r)
data, _ := io.ReadAll(&buf)
if debug {
log.Printf("Response: %s", data)
}
return
}