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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
;;; Copyright © 2019 - 2022 Jakob L. Kreuze <zerodaysfordays@sdf.org>
;;;
;;; This program is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License as
;;; published by the Free Software Foundation; either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program. If not, see
;;; <http://www.gnu.org/licenses/>.
(define-module (jakob dynamic captcha)
#:use-module (gcrypt base16)
#:use-module (gcrypt base64)
#:use-module (gcrypt hash)
#:use-module (gcrypt random)
#:use-module (ice-9 binary-ports)
#:use-module (ice-9 iconv)
#:use-module (ice-9 iconv)
#:use-module (ice-9 local-eval)
#:use-module (ice-9 match)
#:use-module (ice-9 popen)
#:use-module (ice-9 threads)
#:use-module (json)
#:use-module (rnrs bytevectors)
#:use-module ((rnrs base) #:select (assert))
#:use-module (rnrs exceptions)
#:use-module (srfi-197)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-9)
#:use-module (srfi srfi-11)
#:use-module (srfi srfi-19)
#:use-module (srfi srfi-35)
#:export (make-queue
id-queue-free
id-queue-allocated
release-id!
dequeue-id!
new-captcha!
validate-captcha!
make-captcha-challenge!
validate-proof-of-work!
make-pow-challenge!))
(define-record-type <id-queue>
(make-id-queue mutex min-free-threshold free-ids allocated-ids)
id-queue?
(mutex id-queue-mutex)
(min-free-threshold id-queue-min-free-threshold)
(free-ids id-queue-free set-id-queue-free!)
(allocated-ids id-queue-allocated set-id-queue-allocated!))
(define* (make-queue n #:key (min-free-threshold 32))
"Construct a stateful queue for tracking captcha IDs
The parameter N specifies how many free IDs should initially be allocated. The
optional keyword argument MIN-FREE-THRESHOLD specifies when `dequeue-id!' should
iterate through the allocated list and free anything exceeding an
internally-defined `time-to-live-seconds'."
(make-id-queue (make-mutex) min-free-threshold (iota n) (list)))
(define (append-to-free-queue! id queue)
"Add ID to the end of the free list of QUEUE"
(set-id-queue-free!
queue
(append! (id-queue-free queue) (list id))))
(define (remove-from-free-queue! id queue)
"Remove ID from the free list of QUEUE"
(set-id-queue-free! queue (delete! id (id-queue-free queue))))
(define (append-to-allocated-queue! id queue)
"Add ID to the end of the allocated list of QUEUE"
(set-id-queue-allocated!
queue
(append! (id-queue-allocated queue) (list (list id (current-time))))))
(define (remove-from-allocated-queue! id queue)
"Remove ID from the allocated list of QUEUE"
(set-id-queue-allocated!
queue
(filter! (lambda (x) (not (equal? id (car x))))
(id-queue-allocated queue))))
(define (release-id! id queue)
"Release ID to the free list of QUEUE"
(with-mutex (id-queue-mutex queue)
(assert (find (lambda (x) (equal? id (car x))) (id-queue-allocated queue)))
(assert (not (member id (id-queue-free queue))))
(append-to-free-queue! id queue)
(remove-from-allocated-queue! id queue)))
(define (dequeue-id! queue)
"Draw a random ID from QUEUE and mark it as allocated"
(define time-to-live-seconds (* 20 60))
(with-mutex (id-queue-mutex queue)
;; Initial pass to "unintrusively" free any stale IDs.
(when (< (length (id-queue-free queue))
(id-queue-min-free-threshold queue))
(for-each
(match-lambda
((id created-time)
(when (>= (- (time-second (current-time))
(time-second created-time))
time-to-live-seconds)
(remove-from-allocated-queue! id queue))))
(list-copy (id-queue-allocated queue))))
;; If we're still over the threshold, we'll need to be more intrusive.
;; Ideally, this is avoided by rate-limiting.
(when (< (length (id-queue-free queue))
(id-queue-min-free-threshold queue))
(let* ((to-take (- (id-queue-min-free-threshold queue)
(length (id-queue-free queue))))
(to-free (map car (take (id-queue-allocated queue) to-take))))
(set-id-queue-allocated! queue (drop (id-queue-allocated queue) to-take))
(set-id-queue-free! queue (append! (id-queue-free queue) to-free))))
(let* ((n (random (length (id-queue-free queue))))
(id (list-ref (id-queue-free queue) n)))
(remove-from-free-queue! id queue)
(append-to-allocated-queue! id queue)
id)))
(define proc-mutex (make-mutex))
(define tex-challenge-id-queue (make-queue 1024))
(define tex-challenges (make-hash-table 1024))
(define (random-term)
(match (random 5)
(0 `(* ,(+ 1 (random 10)) x))
(1 `(* ,(+ 1 (random 10)) (expt x ,(random 10))))
(2 `(* ,(+ 1 (random 10)) (exp x)))
(3 `(* ,(+ 1 (random 10)) (cos x)))
(4 `(* ,(+ 1 (random 10)) (sin x)))))
(define (sexp->latex sexp)
(match sexp
(('+ rest ...) (string-join (map sexp->latex rest) " + "))
(('* rest ...) (string-join (map sexp->latex rest) " \\cdot "))
(('sin term) (format #f "\\sin(~a)" (sexp->latex term)))
(('cos term) (format #f "\\cos(~a)" (sexp->latex term)))
(('expt term n) (format #f "~a^{~a}" (sexp->latex term) (sexp->latex n)))
(('exp term) (format #f "e^{~a}" (sexp->latex term)))
('x "x")
(n (cond ((and (number? n) (positive? n)) (format #f "~a" n))
((and (number? n) (negative? n)) (format #f "(~a)" n))
((number? n) "0")
(else (error "Do not know how to convert to latex." n))))))
(define (differentiate-sexp sexp)
(match sexp
(('+ rest ...) `(+ ,@(map differentiate-sexp rest)))
(('* coeff term) (if (number? coeff)
`(* ,coeff ,(differentiate-sexp term))
(error "Do not know how to differentiate.")))
(('sin term) `(* ,(differentiate-sexp term) (cos ,term)))
(('cos term) `(* -1 ,(differentiate-sexp term) (sin ,term)))
(('exp term) `(* ,(differentiate-sexp term) (exp ,term)))
(('expt term n) `(* ,n (expt ,term ,(- n 1))))
('x 1)
(n (if (number? n)
0
(error "Do not know how to differentiate.")))))
(define (simplify-sexp sexp)
(match sexp
(('+ rest ...) `(+ ,@(map simplify-sexp rest)))
(('* 1 term) (simplify-sexp term))
(('* 1 rest ...) (simplify-sexp `(* ,@rest)))
(('sin term) `(sin ,(simplify-sexp term)))
(('sin term) `(cos ,(simplify-sexp term)))
(('exp term) `(exp ,(simplify-sexp term)))
(('expt term 1) (simplify-sexp term))
(('expt term n) `(expt ,(simplify-sexp term) ,(simplify-sexp n)))
(term term)))
(define (random-expression)
(let ((n-terms (+ 2 (random 3))))
`(+ ,@(map (lambda (x) (random-term)) (iota n-terms)))))
(define (latex->image src)
(chdir "/tmp")
(with-mutex proc-mutex
(call-with-output-file "formula.tex"
(lambda (port)
(format port "\\def\\formula{~a}
\\documentclass[border=2pt]{standalone}
\\usepackage{amsmath}
\\usepackage{varwidth}
\\begin{document}
\\begin{varwidth}{\\linewidth}
\\[ \\formula \\]
\\end{varwidth}
\\end{document}
" src)))
(unless (eqv? 0 (status:exit-val (system "pdflatex formula.tex")))
(error "Cannot generate PDF"))
(let* ((port (open-input-pipe "convert -density 300 formula.pdf -quality 90 png:-"))
(data (get-bytevector-all port)))
(unless (eqv? 0 (status:exit-val (close-pipe port)))
(error "Cannot generate PNG"))
data)))
(define (new-captcha!)
(let* ((lower-bound (random 10))
(upper-bound (+ lower-bound 1 (random 9)))
(expression (random-expression))
(latex-src (sexp->latex (simplify-sexp (differentiate-sexp expression))))
(solution (- (local-eval expression (let ((x upper-bound)) (the-environment)))
(local-eval expression (let ((x lower-bound)) (the-environment)))))
(id (dequeue-id! tex-challenge-id-queue)))
(hash-set! tex-challenges id solution)
(values id
(latex->image (format #f "\\int_{~a}^{~a} ~a \\, dx"
lower-bound
upper-bound
latex-src)))))
(define (validate-captcha! user-answer id)
(define epsilon 0.1)
(let ((solution (hash-ref tex-challenges id))
(id-allocated (not (member id (id-queue-free tex-challenge-id-queue)))))
;; FIXME: The predictable IDs means that its' easy for someone to screw with
;; someone elses' captcha challenge (by invalidating it before they can
;; submit it). Given the combination of our reaping algorithm and
;; rate-limiting, does it make sense to only release the ID when the
;; response is correct?
(when (and solution id-allocated)
(release-id! id tex-challenge-id-queue))
(and solution
id-allocated
(<= (abs (- solution (string->number user-answer)))
epsilon))))
(define (make-captcha-challenge! request body)
"API endpoint handler for requesting a captcha challenge"
(let-values (((challenge-id image) (new-captcha!)))
(values '((content-type . (application/json)))
(scm->json-string
`((challenge-id . ,challenge-id)
(image . ,(format #f "data:image/jpeg;charset=utf-8;base64,~a"
(base64-encode image))))))))
(define pow-challenge-id-queue (make-queue 1024))
(define pow-challenges (make-hash-table 1024))
;; How many zeroes the SHA-256 hash has to be prefixed by to be a valid proof of work.
(define %hardness 4)
(define (new-proof-of-work-challenge!)
(let ((id (dequeue-id! pow-challenge-id-queue))
(challenge (base64-encode (gen-random-bv 32))))
(hash-set! pow-challenges id challenge)
(values id challenge)))
(define (validate-proof-of-work! prefix challenge-id)
(define zero-prefix (string-join (map (lambda (_) "0") (iota %hardness)) ""))
(when (member challenge-id (id-queue-free pow-challenge-id-queue))
(raise (condition (&message
(message "No such challenge ID")))))
(let* ((challenge (hash-ref pow-challenges challenge-id))
(hash-value (chain (list prefix challenge)
(string-concatenate _)
(string->bytevector _ "utf8")
(bytevector-hash _ (lookup-hash-algorithm 'sha256))
(bytevector->base16-string _))))
;; Invariant from `unless' form:
;; (not (member challenge-id (id-queue-free pow-challenge-id-queue)))
(when challenge
(release-id! challenge-id pow-challenge-id-queue))
(and (= 32 (string-length prefix))
(string-prefix? zero-prefix hash-value))))
(define (make-pow-challenge! request body)
"API endpoint handler for requesting a proof-of-work challenge"
(let-values (((challenge-id nonce) (new-proof-of-work-challenge!)))
(values '((content-type . (application/json)))
(scm->json-string
`((hardness . ,%hardness)
(challenge-id . ,challenge-id)
(nonce . ,nonce))))))
|