Created
February 13, 2018 12:53
-
-
Save pekeq/164f812794f593b0a45426b186f0684d to your computer and use it in GitHub Desktop.
バックドア選考で利用していたPHPスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('DB_HOST', '172.17.0.2'); | |
define('DB_USER', 'user'); | |
define('DB_PASS', 'pass'); | |
define('DB_NAME', 'backdoor'); | |
function h($str) { | |
return htmlspecialchars($str, ENT_QUOTES, 'UTF-8'); | |
} | |
function error($msg) { | |
$json = array( | |
'error' => h($msg) | |
); | |
header('Content-Type: application/octet-stream'); | |
echo json_encode($json); | |
exit(); | |
} | |
function no_results() { | |
$json = array( | |
'error' => FALSE, | |
'results' => array('not found'), | |
); | |
header('Content-Type: application/octet-stream'); | |
echo json_encode($json); | |
exit(); | |
} | |
// いじわるタイムスタンプ | |
function check_stamp($src) { | |
$magic = 5111; | |
// 2018年補足: ここは現在時刻を使ってもっと意地の悪いチェックを考えていたのですが、 | |
// そうするとJS側の解析を必要とするので、固定値のビットマスクによる確認だけにしました。 | |
if ((intval($src) & $magic) != $magic) { | |
// いじわる: timestamp errorとno resultsの区別がない | |
no_results(); | |
} | |
return TRUE; | |
} | |
function query($query) { | |
$json = array('error' => 'error'); | |
// いじわる1: スペースとタブを削除する | |
$query = str_replace(array(' ', "\t", "\n"), '', $query); | |
try { | |
$db = mysql_connect(DB_HOST, DB_USER, DB_PASS); | |
if ($db === FALSE) { | |
throw new Exception(mysql_error()); | |
} | |
$r = mysql_set_charset('utf8', $db); | |
if ($r === FALSE) { | |
throw new Exception(mysql_error()); | |
} | |
$r = mysql_select_db(DB_NAME, $db); | |
if ($r === FALSE) { | |
throw new Exception(mysql_error()); | |
} | |
// 2018年補足: ここがセキュリティホールです | |
$sql = "SELECT text FROM words WHERE keyword LIKE '%{$query}%' LIMIT 1000"; | |
$result = mysql_query($sql, $db); | |
if ($result === FALSE) { | |
throw new Exception(mysql_error()); | |
} | |
$rows = mysql_num_rows($result); | |
if ($rows === FALSE) { | |
throw new Exception(mysql_error()); | |
} | |
if ($rows > 0) { | |
$a = array(); | |
for ($i = 0; $i < 1000; $i++) { | |
$row = mysql_fetch_assoc($result); | |
if ($row === FALSE) { | |
break; | |
} | |
$json['results'][] = h($row['text']); | |
} | |
} else { | |
no_results(); | |
} | |
$json['error'] = FALSE; | |
mysql_close($db); | |
} catch (Exception $e) { | |
error($e->getMessage()); | |
} | |
return $json; | |
} | |
if (isset($_GET['q']) && isset($_GET['ts'])) { | |
$query = trim($_GET['q']); | |
$stamp = trim($_GET['ts']); | |
if (!empty($query) && !empty($stamp)) { | |
check_stamp($stamp); | |
$json = query($query); | |
header('Content-Type: application/octet-stream'); | |
echo json_encode($json); | |
exit(); | |
} | |
} | |
no_results(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment