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 #include "service.h"
30
31 #include "../../util/pblock.h"
32 #include "../../util/util.h"
33 #include "cx/string.h"
34
35 #define SIMPLE_COOKIE_PAGE_TEMPLATE \
36 "<!DOCTYPE html>\n" \
37 "<html>\n" \
38 " <head>\n" \
39 " <title>Anti Bot Challenge</title>\n" \
40 " <script>\n" \
41 " fuckcounter=0;\n" \
42 " addEventListener(\"mousemove\", (event) => {\n" \
43 " fuckcounter++;\n" \
44 " })\n" \
45 " setTimeout(function() {\n" \
46 " const b = document.getElementById(''fuckai'');\n" \
47 " b.style.display = ''initial'';\n" \
48 " b.addEventListener(''click'', function() {\n" \
49 " if (fuckcounter < 50) return;\n" \
50 " document.cookie = ''%s=%s; path=/'';\n" \
51 " window.location.reload();\n" \
52 " })\n" \
53 " }, 1000);\n" \
54 " </script>\n" \
55 " </head>\n" \
56 " <body>\n" \
57 " <h1>Simple Anti Bot Check</h1>\n" \
58 " <p>Please click the button below to continue.</p>\n" \
59 " <button style=\"display: none\" id=\"fuckai\" type=\"button\">I am a Human</button>\n" \
60 " </body>\n" \
61 "</html>"
62
63 static cxstring gen_simple_cookie_page(
pool_handle_t *pool,
const char *cookie_name,
const char *cookie_value) {
64 size_t cname_len = strlen(cookie_name);
65 size_t cvalue_len = strlen(cookie_value);
66 size_t template_len =
sizeof(
SIMPLE_COOKIE_PAGE_TEMPLATE) -
1;
67
68 size_t len = template_len + cname_len + cvalue_len;
69 char *buffer = pool_malloc(pool, len +
1);
70 if(!buffer) {
71 return (cxstring){
NULL,
0 };
72 }
73
74 len = snprintf(buffer, len +
1,
SIMPLE_COOKIE_PAGE_TEMPLATE, cookie_name, cookie_value);
75 return (cxstring){ buffer, len };
76 }
77
78
79
80
81
82
83
84
85
86 int sab_simple_service(pblock *pb, Session *sn, Request *rq) {
87 char *cookie_name = pblock_findval(
"cookie", pb);
88 char *cookie_value = pblock_findval(
"value", pb);
89 if(!cookie_name) {
90 log_ereport(
LOG_MISCONFIG,
"sab-simple-service: no cookie specified");
91 return REQ_ABORTED;
92 }
93
94 char *is_bot = pblock_findval(
"is-bot", rq->vars);
95 if(!is_bot) {
96 return REQ_NOACTION;
97 }
98
99
100
101
102 if(!cookie_value) {
103 cookie_value =
"sab-1";
104 }
105
106 cxstring response = gen_simple_cookie_page(sn->pool, cookie_name, cookie_value);
107
108 pblock_removekey(pb_key_content_type, rq->srvhdrs);
109 pblock_kvinsert(pb_key_content_type,
"text/html; charset=utf-8",
24, rq->srvhdrs);
110 pblock_nninsert(
"content-length", (
int)response.length, rq->srvhdrs);
111 protocol_status(sn, rq,
200,
NULL);
112 http_start_response(sn, rq);
113
114 net_write(sn->csd, response.ptr, response.length);
115
116 return REQ_PROCEED;
117 }
118