-
Notifications
You must be signed in to change notification settings - Fork 0
/
captchasrv.go
243 lines (203 loc) · 6.4 KB
/
captchasrv.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
237
238
239
240
241
242
243
package main
import (
"crypto/hmac"
"crypto/sha256"
"embed"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"flag"
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"time"
)
//go:embed font.css form.html success.html
var templateFS embed.FS
//go:embed font/*
var staticFS embed.FS
var (
// Generate using e.g. “openssl rand -hex 32”.
hmacSecretStr = flag.String("hmac_secret_key",
"",
"Shared secret key between RobustIRC and captchasrv for message HMACs")
hmacSecret []byte
recaptchaSiteKey = flag.String("recaptcha_site_key",
"",
"Site key, from the Adding reCAPTCHA to your site section")
recaptchaSecretKey = flag.String("recaptcha_secret_key",
"",
"Site key, from the Adding reCAPTCHA to your site section")
listenAddr = flag.String("listen",
":8080",
"host:port to listen on for HTTP requests")
)
var templates = template.Must(template.New("").ParseFS(templateFS, "*.html", "*.css"))
func verifyCaptcha(response string) error {
resp, err := http.PostForm("https://www.google.com/recaptcha/api/siteverify",
url.Values{
// remoteip is deliberately not set to make it easier to deploy
// captchasrv behind a proxy and/or docker.
"secret": {*recaptchaSecretKey},
"response": {response}})
if err != nil {
return err
}
if got, want := resp.StatusCode, http.StatusOK; got != want {
return fmt.Errorf("Unexpected HTTP status code verifying captcha: got %d, want %d", got, want)
}
defer func() {
ioutil.ReadAll(resp.Body)
resp.Body.Close()
}()
var reply struct {
Success bool `json:"success"`
// TODO: challenge_ts
Hostname string `json:"hostname"`
ErrorCodes []string `json:"error-codes"`
}
if err := json.NewDecoder(resp.Body).Decode(&reply); err != nil {
return fmt.Errorf("Error decoding JSON: %v", err)
}
// TODO: should we verify hostname?
if !reply.Success {
return errors.New("captcha unsuccessful")
}
return nil
}
type request struct {
// response holds the captcha response, from the g-recaptcha-response HTTP
// form value.
response string
// purpose, challenge and mac hold the randomly generated per-RobustSession
// challenge purpose, value and corresponding Message Authentication Code
// (unverified), decoded from the challenge HTTP form value.
purpose []byte
challenge []byte
mac []byte
}
// fromHTTP constructs a request from an HTTP request. Any error will be
// displayed verbatim to the client (attacker).
func fromHTTP(r *http.Request) (request, error) {
var result request
if got, want := r.Method, "POST"; got != want {
return result, fmt.Errorf("Invalid HTTP method: got %q, want %q", got, want)
}
if result.response = r.FormValue("g-recaptcha-response"); result.response == "" {
return result, fmt.Errorf("g-recaptcha-response parameter not set")
}
challenge := r.FormValue("challenge")
if challenge == "" {
return result, fmt.Errorf("challenge parameter not set")
}
parts := strings.Split(challenge, ".")
if got, want := len(parts), 3; got != want {
return result, fmt.Errorf("Unexpected number of challenge parts: got %d, want %d", got, want)
}
decoded := make([][]byte, 3)
var err error
for idx, part := range parts {
decoded[idx], err = base64.StdEncoding.DecodeString(part)
if err != nil {
return result, err
}
}
result.purpose = decoded[0]
result.challenge = decoded[1]
result.mac = decoded[2]
return result, nil
}
func writeForm(w http.ResponseWriter, msg string) {
if err := templates.ExecuteTemplate(w, "form.html", struct {
SiteKey string
Msg string
}{
SiteKey: *recaptchaSiteKey,
Msg: msg,
}); err != nil {
log.Printf("rendering form: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func main() {
flag.Parse()
if *recaptchaSiteKey == "" {
log.Fatalf("-recaptcha_site_key is required")
}
if *recaptchaSecretKey == "" {
log.Fatalf("-recaptcha_secret_key is required")
}
var err error
hmacSecret, err = hex.DecodeString(*hmacSecretStr)
if err != nil {
log.Fatalf("Could not decode -hmac_secret=%q as hex string: %v", *hmacSecretStr, err)
}
http.Handle("/font/", http.FileServer(http.FS(staticFS)))
http.HandleFunc("/submit", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Frame-Options", "DENY")
w.Header().Set("X-XSS-Protection", "1; mode=block")
req, err := fromHTTP(r)
if err != nil {
log.Printf("bad request: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// First verify the captcha was solved correctly in order to prevent
// attackers from driving up processing cost.
if err := verifyCaptcha(req.response); err != nil {
log.Printf("verifyCaptcha: %v", err)
writeForm(w, "Captcha could not be verified")
return
}
// Verify the challenge actually came from RobustIRC before issuing a
// token, so that attackers cannot generate tokens in advance by
// solving captchas.
mac := hmac.New(sha256.New, hmacSecret)
mac.Write(req.purpose)
mac.Write(req.challenge)
if !hmac.Equal(req.mac, mac.Sum(nil)) {
writeForm(w, "Challenge could not be verified")
return
}
// Craft the authenticated captcha confirmation message to RobustIRC.
mac = hmac.New(sha256.New, hmacSecret)
purpose := []byte("okay:" + string(req.purpose))
mac.Write(purpose)
mac.Write(req.challenge)
token := strings.Join([]string{
base64.StdEncoding.EncodeToString(purpose),
base64.StdEncoding.EncodeToString(req.challenge),
base64.StdEncoding.EncodeToString(mac.Sum(nil)),
}, ".")
purposeparts := strings.Split(string(req.purpose), ":")
if err := templates.ExecuteTemplate(w, "success.html", struct {
Purposeparts []string
Token string
}{
Purposeparts: purposeparts,
Token: token,
}); err != nil {
log.Printf("rendering form: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Set cache time to one hour, so that frontends like CloudFlare can
// serve this request for us.
utc := time.Now().UTC()
cacheSince := utc.Format(http.TimeFormat)
cacheUntil := utc.Add(1 * time.Hour).Format(http.TimeFormat)
w.Header().Set("Cache-Control", "max-age=3600, public")
w.Header().Set("Last-Modified", cacheSince)
w.Header().Set("Expires", cacheUntil)
w.Header().Set("X-Frame-Options", "DENY")
w.Header().Set("X-XSS-Protection", "1; mode=block")
writeForm(w, "")
})
http.ListenAndServe(*listenAddr, nil)
}