This repository has been archived by the owner on Jul 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
03-challange.php
80 lines (70 loc) · 2.28 KB
/
03-challange.php
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
<?php
function argmin(array $values)
{
$argmin = $min = null;
foreach ($values as $key => $value) {
if ($min === null || $min > $value) {
$argmin = $key;
$min = $value;
}
}
return $argmin;
}
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'http://q34.ctf.katsudon.org/',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_COOKIEFILE => true,
CURLOPT_FOLLOWLOCATION => true,
]);
$html = curl_exec($ch);
while (true) {
echo "\n\n---------------------\n\n";
echo $html;
$dom = new DOMDocument;
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$url = $xpath->evaluate('concat("http://q34.ctf.katsudon.org", //img/@src)');
$id = $xpath->evaluate('string(//input[@name="id"]/@value)');
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPGET => true,
]);
$binary = curl_exec($ch);
if ($id === '' || $binary === false) {
break;
}
$unknown = imagecreatefromstring($binary);
$key = '';
for ($d = 0; $d < 16; ++$d) {
$width = 15;
$height = 28;
$margin = 4;
$distances = [];
for ($r = 0; $r < 16; ++$r) {
$digitname = base_convert($r, 10, 16);
$filename = __DIR__ . '/images/manually-recognized/' . $digitname . '.png';
$digit = imagecreatefrompng($filename);
$distances[$digitname] = 0;
for ($h = 0; $h < $height; ++$h) {
for ($w = 0; $w < $width; ++$w) {
$x = 8 + $d * ($width + $margin) + $w;
$y = 8 + $h;
$ucolor = imagecolorat($unknown, $x, $y) === 0x000000 ? 0x000000 : 0xffffff;
$dcolor = imagecolorat($digit, $w, $h) === 0x000000 ? 0x000000 : 0xffffff;
$distances[$digitname] += (int)($ucolor !== $dcolor);
}
}
imagedestroy($digit);
}
$key .= argmin($distances);
}
curl_setopt_array($ch, [
CURLOPT_URL => 'http://q34.ctf.katsudon.org/challenge',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query(compact('id', 'key'), '', '&'),
]);
$html = curl_exec($ch);
}