美文网首页
【Code-Audit-Challenges】Challenge

【Code-Audit-Challenges】Challenge

作者: Pino_HD | 来源:发表于2017-12-08 13:03 被阅读0次

    0x01 题目

    #GOAL: get password from admin;
    <?php 
        
    error_reporting(0);
    
    $link = mysqli_connect('localhost', 'xxx', 'xxx', 'test');
    if (!$link) { 
        die('Could not connect to MySQL: ' . mysqli_connect_error()); 
    } 
    
    $link->set_charset("utf8");
    
    
    function clean($str){
        if(get_magic_quotes_gpc()){
            $str=stripslashes($str);
        }
        return htmlentities($str, ENT_QUOTES);
    }
    $username = @clean((string)$_GET['username']);
    $password = @clean((string)$_GET['password']);
    $query='select * from user where username=\''.$username.'\' AND password=\''.$password.'\';';
    var_dump($query); //提示:htmlentities函数的效果可以通过右键查看网页源代码来看效果
    echo "<br />";
    $result=mysqli_query($link,$query);
    if(!$result || mysqli_num_rows($result) < 1){
        die('Invalid password!');
    }
    
    $row = mysqli_fetch_assoc($result);
    
    echo "Hello ".$row['username']."</br>";
    echo "Your password is:".$row['password']."</br>";
    ?>
    

    0x02 解题

    原题不是这样的,里面加了我自己的注释和数据库信息,有一个比较坑的是htmlentities()函数需要右键源代码才能看到效果。。

    然后分析一下代码,首先已知用户名admin,目标是获取密码。其中username和password都使用了一个自定义的clean函数进行过滤,看一下clean函数,首先使用stripslashes函数去掉反斜杠,然后返回通过htmlentities()函数编码的字符串,并且在函数中使用了ENT_QUOTES,将单引号和双引号都进行实体编码,因此不能使用单引号进行注入,但是htmlentities函数是不编码反斜杠的,因此我们可以通过输入一个反斜杠,转义掉一个单引号,从而完成注入,payload如下:

    ?admin\&password=%20or%201=1%23
    

    相关文章

      网友评论

          本文标题:【Code-Audit-Challenges】Challenge

          本文链接:https://www.haomeiwen.com/subject/cqatixtx.html