-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsearcher.pl-dabe04e9b2c8f7818f4e11a6dc1d79a6
More file actions
210 lines (183 loc) · 5.9 KB
/
searcher.pl-dabe04e9b2c8f7818f4e11a6dc1d79a6
File metadata and controls
210 lines (183 loc) · 5.9 KB
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
use strict;
use warnings;
use utf8;
use DBI;
use Data::Section::Simple qw/get_data_section/;
use File::Basename;
use File::Spec;
use JSON;
use Plack::Builder;
use Plack::Request;
use SQL::Maker;
my $sql_builder = SQL::Maker->new(driver => 'SQLite');
my $dbh = DBI->connect(
"dbi:SQLite:dbname=q20.db",
'',
'',
+{
RaiseError => 1,
sqlite_unicode => 1,
}
);
my $app = sub {
my $env = shift;
my $req = Plack::Request->new($env);
if ($req->path_info eq '/') {
return render_html('index.html');
} elsif ($req->path_info eq '/search' && $req->method eq 'POST') {
my $q = $req->param('q') // die 'missing q';
warn "q: $q\n";
my $where = eval {
decode_json($q);
};
if ($@) {
die 'illegal JSON';
}
die 'missing name parameter' unless $where->{name};
die 'invalid name parameter' unless $where->{name} =~ /^[\x20-\x7f]+$/;
for my $key (keys %$where) {
$where->{$key} =~ s/[%_]//g;
$where->{$key} = $where->{$key} eq '' ? '' : "%$where->{$key}%";
$where->{$key} = {like => $where->{$key}};
}
my ($sql, @bind) = $sql_builder->select('user', ['*'], $where, {});
my $users = $dbh->selectall_arrayref($sql, {Slice => {}}, @bind);
return render_json($users);
} elsif ($req->path_info eq '/admin') {
if ($req->method eq 'POST') {
my $name = $req->param('name') // die 'missing name';
my $password = $req->param('password') // die 'missing password';
warn "password: $password\n";
my ($sql, @bind) = $sql_builder->select(__CENSORED__, ['*'], {
__CENSORED__ => $name,
__CENSORED__ => $password,
}, {});
my ($admin) = $dbh->selectrow_array($sql, {Slice => {}}, @bind);
if ($admin) {
return render_html('flag.html');
} else {
return [403, [], ['login failure']];
}
} else {
return render_html('admin.html');
}
} elsif ($req->path_info eq '/source') {
return render_html('source.html');
} else {
return [404, [], ['not found']];
}
};
builder {
enable 'Plack::Middleware::ReverseProxy';
enable 'Plack::Middleware::Static',
path => qr{^/js/},
root => File::Spec->catdir(dirname(__FILE__));
$app;
};
sub render_html {
[
200,
['Content-Type' => 'text/html; charset=utf-8'],
[get_data_section($_[0])]
]
}
sub render_json {
[
200,
['Content-Type' => 'application/json; charset=utf-8'],
[encode_json($_[0])]
]
}
__DATA__
@@ index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>searcher</title>
<script src="/js/jquery-1.9.1.min.js"></script>
<script>
$(function () {
$("#search input[name='name']").focus().keyup(function() {
var submit = $("#search input[type='submit']");
if ($(this).val() === "") {
submit.attr("disabled", "disabled");
} else {
submit.removeAttr("disabled");
}
});
$("#search").submit(function() {
var name = $("#search input[name='name']").val();
if (name === "") return false;
$.ajax("/search", {
type: "POST",
dataType: "json",
data: {
q: JSON.stringify({"name": name})
},
success: function(data, textStatus, jqXHR) {
$("#result .row").remove();
for (var i = 0, length = data.length; i < length; i++) {
var tr = $("<tr />").addClass("row");
var password = data[i].password.replace(/./g, '*');
tr.append($("<td />").text(data[i].name));
tr.append($("<td />").text(password));
$("#result").append(tr);
}
}
});
return false;
});
});
</script>
</head>
<body>
<p>Q. How do I login admin page?</p>
<form id="search">
<input type="text" name="name" />
<input type="submit" value="Search" disabled="disabled" />
</form>
<h2>Search result: user</h2>
<table id="result" border="1">
<tr>
<th>name</th>
<th>password</th>
</tr>
</table>
<hr />
<p>Go to <a href="/admin">admin page</a><p>
</body>
</html>
@@ admin.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>searcher</title>
</head>
<body>
<form method="POST" action="/admin">
Password: <input type="password" name="password" />
<input type="hidden" name="name" value="admin" />
<input type="submit" value="Login" />
</form>
</body>
</html>
@@ flag.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>searcher</title>
</head>
<body>
<pre> ____ U ___ u _ _ ____ ____ _ _____ _ _ _ _ _____ U ___ u _ _ ____ _
U /"___| \/"_ \/ | \ |"| U /"___|uU | _"\ u U /"\ u |_ " _|U |"|u| | |"| U /"\ u |_ " _| ___ \/"_ \/ | \ |"| / __"| uU|"|u
\| | u | | | |<| \| |>\| | _ / \| |_) |/ \/ _ \/ | | \| |\| |U | | u \/ _ \/ | | |_"_| | | | |<| \| |><\___ \/ \| |/
| |/__.-,_| |_| |U| |\ |u | |_| | | _ < / ___ \ /| |\ | |_| | \| |/__ / ___ \ /| |\ | | .-,_| |_| |U| |\ |u u___) | |_|
\____|\_)-\___/ |_| \_| \____| |_| \_\ /_/ \_\ u |_|U <<\___/ |_____|/_/ \_\ u |_|U U/| |\u\_)-\___/ |_| \_| |____/>> (_)
_// \\ \\ || \\,-._)(|_ // \\_ \\ >> _// \\_(__) )( // \\ \\ >> _// \\_.-,_|___|_,-. \\ || \\,-.)( (__)|||_
(__)(__) (__) (_") (_/(__)__) (__) (__)(__) (__)(__) (__) (__) (_")("_)(__) (__)(__) (__)\_)-' '-(_/ (__) (_") (_/(__) (__)_)</pre>
<p>The flag is <code>admin's password</code>.</p>
</body>
</html>