<?php
error_reporting(0);
error_log(0);
require_once("flag.php");
function is_trying_to_hak_me($str)
{
$blacklist = ["' ", " '", '"', "`", " `", "` ", ">", "<"];
if (strpos($str, "'") !== false) { //判断$tr是否有引号,有则进入下面
if (!preg_match("/[0-9a-zA-Z]'[0-9a-zA-Z]/", $str)) { //判断写反了,导致存在漏洞
/*
返回 pattern 的匹配次数。 它的值将是 0 次(不匹配)或 1 次,因为 preg_match() 在第一次匹配后 将会停止搜索。preg_match_all() 不同于此,它会一直搜索subject 直到到达结尾。
如果发生错误preg_match()返回 FALSE。
如果存在单有引号两边有数字或字符的字符串,构造admin'ddd,这样的字符串,模式匹配,得到1,然后加!得到假,这样返回false
*/
return true;
}
}
foreach ($blacklist as $token) {
if (strpos($str, $token) !== false) return true; //只要单引号两边没有空格字符就能返回false
}
return false;
}
if (isset($_GET["pls_help"])) {
highlight_file(__FILE__);
exit;
}
if (isset($_POST["user"]) && isset($_POST["pass"]) && (!empty($_POST["user"])) && (!empty($_POST["pass"]))) {
$user = $_POST["user"];
$pass = $_POST["pass"];
if (is_trying_to_hak_me($user)) {
die("why u bully me");
}
$db = new SQLite3("/var/db.sqlite");
$result = $db->query("SELECT * FROM users WHERE username='$user'");
/*
构造如下语句
a'union select 1,'8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92$456'--
'union左边可以不用空格,sqllite3为什么可以这样没明白
其中8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92为hash("sha256",'123'.'456')生成
密码填123
*/
if ($result === false) die("pls dont break me");
else $result = $result->fetchArray();
if ($result) {
$split = explode('$', $result["password"]);
$password_hash = $split[0];
$salt = $split[1];
if ($password_hash === hash("sha256", $pass.$salt)) $logged_in = true;
/*
$result["password"]为注入的8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92$456
$password_hash为8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92
$salt为456
hash("sha256", "123"."456")
*/
else $err = "Wrong password";
}
else $err = "No such user";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Hack.INI 9th - SQLi</title>
</head>
<body>
<?php if (isset($logged_in) && $logged_in): ?>
<p>Welcome back admin! Have a flag: <?=htmlspecialchars($flag);?><p>
<?php else: ?>
<form method="post">
<input type="text" placeholder="Username" name="user" required>
<input type="password" placeholder="Password" name="pass" required>
<button type="submit">Login</button>
<br><br>
<?php if (isset($err)) echo $err; ?>
</form>
<?php endif; ?>
<!-- <a href="/?pls_help">get some help</a> -->
</body>
</html>