PHP代码审计之入门实战

来源:岁月联盟 编辑:猪蛋儿 时间:2020-02-10
存储型XSS过滤
思路
网站前台有留言功能,留言就会想到存储型XSS,2333 嗝:

结果
定位到留言函数的代码:
index/module/info_main.php
function add_message()
{
    safe('message');
    global $global,$smarty,$lang;
    $mes_email = post('email');
    $mes_type = post('type');
    $mes_title = post('title');
    $mes_text = post('text');
    $mes_show = post('show');
    if($mes_email == '' || $mes_type == '' || $mes_title == '' || $mes_text == '')
    {
        $info_text = $lang['submit_error_info'];
    }else{
        $mes_add_time = time();
        if($mes_show != '2')
        {
            $mes_show = '0';
        }
        $obj = new message();
        $obj->set_value('mes_user_id',$global['user_id']);
        $obj->set_value('mes_type',$mes_type);
        $obj->set_value('mes_email',$mes_email);
        $obj->set_value('mes_title',$mes_title);
        $obj->set_value('mes_text',$mes_text);
        $obj->set_value('mes_add_time',$mes_add_time);
        $obj->set_value('mes_show',$mes_show);
        $obj->set_value('mes_lang',S_LANG);
        $obj->add();
        if(intval(get_varia('sentmail')))
        {
            $email_title = '您的网站有了新的留言';
            $email_text = "[$mes_type] $mes_title  $mes_text";
            call_send_email($email_title,$email_text,$global['user_id'],$mes_email);
        }
        $info_text = $lang['submit_message'];
    }
    $smarty->assign('info_text',$info_text);
    $smarty->assign('link_text',$lang['go_back']);
    $smarty->assign('link_href',url(array('channel'=>'message')));   
}
可以看到前台用户传入的数据经过了post()函数,追踪到post()函数的定义处:
include/function.php
function post($val,$filter = 'strict')
{
    return $filter(isset($_POST[$val])?$_POST[$val]:'');
}
??? 继续找到strict的定义处:
include/function.php
//严格过滤字符串中的危险符号
function strict($str)
{
    if(S_MAGIC_QUOTES_GPC)
    {
        $str = stripslashes($str);
    }
    $str = str_replace(','<',$str);
    $str = str_replace('>','>',$str);
    $str = str_replace('?','?',$str);
    $str = str_replace('%','%',$str);
    $str = str_replace(chr(39),''',$str);
    $str = str_replace(chr(34),'"',$str);
    $str = str_replace(chr(13).chr(10),'',$str);
    return $str;
}
可以发现 我们的存储XSS所用到的尖括号完全被过滤掉了:
$str = str_replace(','<',$str);
$str = str_replace('>','>',$str);
这也导致了 管理员后台可以直接看到XSS Payload ,场面一度非常尴尬:

用户评论的核心代码也被过滤了:
index/module/info_main.php
function add_comment()
{
    safe('comment');

上一页  [1] [2] [3] [4] [5] [6] [7]  下一页