美文网首页
sqli注入-sprintf格式化字符串带来的注入隐患

sqli注入-sprintf格式化字符串带来的注入隐患

作者: gunginer | 来源:发表于2019-11-24 14:34 被阅读0次

    PHP中 sprintf()格式化输出详解

    sprintf()函数把格式化的字符串写入变量中。

    arg1、arg2、++参数将被插入到主字符串中的百分号(%)符号处。该函数是逐步执行的。在第一个%符号处,插入arg1,在第二个%符号处,插入arg2,以此类推。

    注释:如果%符号多于arg参数,则您必须使用占位符。占位符位于%符号之后,由数字和“\$”组成。

    实例:

    <?php

    $number = 2;

    $str = "Shanghai";

    $txt = sprintf("There are %u million cars in %s.",$number,$str);

    echo $txt;

    ?>

    运行结果为:There are 2 million cars in Shanghai.


    1.使用格式值%f:

    <?php

    $number = 123;

    $txt = sprintf("%f",$number);

    echo $txt;

    ?>

     运行结果为:123.000000


    2.使用占用符:

    <?php

    $number = 123;

    $txt = sprintf("带两位小数:%\$.2f
    不带小数:%1\$u",$number);

    echo $txt;

    ?>

    运行结果为:带有两位小数:123.00

    不带小数:123


    在php的格式化字符串中,%后的一个字符(除了'%')会被当作字符类型,而被吃掉,单引号',斜杠\也不例外。

    如果能提前将%' and 1=1#拼接入sql语句,若存在SQLi过滤,单引号会被转义成\'

    select * from user where username = '%\' and 1=1#';

    然后这句sql语句如果继续进入格式化字符串,\会被%吃掉,'成功逃逸

    sql = "select * from user where username = '%\' and 1=1#";

    $args = "admin";

    echo sprintf($sql,$args);

    result: select * from user where username = ' ' and 1=1#' and password = 'admin';

    参考:sprintf格式化字符串带来的注入隐患 - 奥利给胖虎 - 博客园

    相关文章

      网友评论

          本文标题:sqli注入-sprintf格式化字符串带来的注入隐患

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