山东大学软件学院项目实训-创新实训-山大软院网络攻防靶场实验平台(十九)-XSS(2)

本文档详述了使用vue和springboot构建XSS漏洞靶场的过程,包括反射型和存储型XSS的后端及前端实现,并展示了修复XSS的安全编码方法。通过设置代理和编写相应代码,分别测试并验证了两种XSS漏洞的效果。
摘要由CSDN通过智能技术生成


前言:

继上一篇博客,本篇博客记录下实际搭建XSS漏洞靶场环境。


2、项目配置

前端使用vue框架,后端使用springboot框架搭建

pom.xml

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.20</version>
        </dependency>
        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>3.10.3</version>
        </dependency>

vue:vue.config.js

// 跨域配置
module.exports = {
    devServer: {                //记住,别写错了devServer//设置本地默认端口  选填
        port: 8080,
        proxy: {                 //设置代理,必须填
            '/api': {              //设置拦截器  拦截器格式   斜杠+拦截器名字,名字可以自己定
                target: 'http://localhost:9090',     //代理的目标地址
                changeOrigin: true,              //是否设置同源,输入是的
                pathRewrite: {                   //路径重写
                    '^/api': ''                     //选择忽略拦截器里面的内容
                }
            }
        }
    }
}

3、编写XSS漏洞后端代码

经过小组讨论,我们决定实现反射型XSS漏洞和存储型XSS漏洞两个基本漏洞的靶场,对于反射型XSS漏洞,其实现较为简单,仅需将用户从前端输入到后端的payload返回到前端渲染,不进行过滤,即可构造XSS漏洞靶场。对于存储型XSS,可以通过将用户输入的payload存储到数据库中,待其他用户访问再次取出,即可触发XSS漏洞,类似于评论的功能点易出现此类型漏洞。我们采用存储到cookie的方式来实现存储型XSS漏洞,其基本原理与存储到数据库类型相似。

3.1、反射型XSS

package com.sducsrp.csrp.controller.BUGcontroller;

import com.sducsrp.csrp.common.Constants;
import com.sducsrp.csrp.common.Result;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;


/**
 * @author lhw 2022.04.07
 */
@RestController
@RequestMapping("/xss")
public class XssController {
    /**
     * Vuln Code.
     * ReflectXSS
     * http://localhost:8080/xss/reflect?xss=<script>alert(1)</script>
     *
     * @param xss unescape string
     */
    @PostMapping("/reflect")
    public @ResponseBody  Result reflect(@RequestBody String xss) {
        System.out.println(xss+":ok1");

        Result res=new Result(Constants.CODE_200,null,xss);
        return res;
    }

3.2、存储型XSS

漏洞代码:

/**
     * Vul Code.
     * StoredXSS Step1
     * http://localhost:8080/xss/stored/store?xss=<script>alert(1)</script>
     *
     * @param xss unescape string
     */
@RequestMapping("/stored/store")
public @ResponseBody Result store(@RequestBody String xss, HttpServletResponse response) {
    Cookie cookie = new Cookie("xss", xss);
    response.addCookie(cookie);

    Result res=new Result(Constants.CODE_200,null,"Set param into cookie,访问/stored/show验证一下");
    return res;
}

验证代码:

/**
     * Vul Code.
     * StoredXSS Step2
     * http://localhost:8080/xss/stored/show
     *
     * @param xss unescape string
     */
@RequestMapping("/stored/show")
public @ResponseBody Result show(@CookieValue("xss") String xss) {
    System.out.println(xss);
    Result res=new Result(Constants.CODE_200,null,xss);
    return res;
}

3.3、修复后的安全代码

/**
     * safe Code.
     * http://localhost:8080/xss/safe
     */
@RequestMapping("/safe")
@ResponseBody
public static String safe(String xss) {
    return encode(xss);
}

private static String encode(String origin) {
    origin = StringUtils.replace(origin, "&", "&amp;");
    origin = StringUtils.replace(origin, "<", "&lt;");
    origin = StringUtils.replace(origin, ">", "&gt;");
    origin = StringUtils.replace(origin, "\"", "&quot;");
    origin = StringUtils.replace(origin, "'", "&#x27;");
    origin = StringUtils.replace(origin, "/", "&#x2F;");
    return origin;
}

4、编写XSS漏洞前端代码

前端采用vue框架

4.1、反射型XSS

<!--题目名字:XSS-Reflect-->
<!--题目类型:XSS-->
<!--题目描述:反射型XSS漏洞-->
<!--题目难度:2星-->
<template>
  <title>反射型XSS</title>

  <p>该页面存在反射型XSS漏洞,能否构造payload,使得页面出现弹窗?</p>
  <div style="margin-top: 100px">
    <el-input v-model="xss_reflect" style="width: 250px;margin-right: 50px" placeholder="payload"></el-input>
    <el-button @click="xss_reflect_fun" type="primary">submit</el-button>
  </div

</template>

<script>
import request from "@/utils/request";
import router from "@/router";

export default {
  data(){
    return{
      xss_reflect:'',
      myresult:''
    }
  },
  methods:{
    xss_reflect_fun(){
      request.post("/xss/reflect",this.xss_reflect).then(res =>{
        this.myresult=res.data;
        // alert(this.myresult)
        // this.myresult
        document.write(this.myresult);
        //window.location.href="pro011";
        router.push("/pro011");
        //router.push("/pro011");
        //self.location.href="/pro011"
      })
    }
  }
}
</script>

4.2、存储型XSS

<!--题目名字:XSS-Store-->
<!--题目类型:XSS-->
<!--题目描述:存储型XSS漏洞-->
<!--题目难度:2星-->
<template>
  <title>存储型XSS</title>

  <p>该页面存在存储型XSS漏洞,能否构造payload,使得页面出现弹窗?</p>
  <div style="margin-top: 100px">
    <el-input v-model="xss_store" style="width: 250px;margin-right: 50px" placeholder="payload"></el-input>
    <el-button @click="xss_store_fun" type="primary">submit</el-button>
  </div>
  <br>
  <p> 点击按钮访问/stored/show </p>
  <el-button @click="show"> 点击访问 </el-button>



</template>

<script>
import request from "@/utils/request";
import router from "@/router";

export default {
  data(){
    return{
      xss_store:'',
      myresult:'',
      myresult_cookie:''
    }
  },
  methods:{
    xss_store_fun(){
      request.post("/xss/stored/store",this.xss_store).then(res =>{
        this.myresult=res.data;
        alert(this.myresult)
      })
    },
    show(){
      request.post("/xss/stored/show").then(res =>{
        this.myresult=res.data;
        if(this.myresult==null){
          alert("请先输入payload,再点击该按钮进行验证。")
        }
        else {
          // alert(this.myresult)
          // this.myresult
          document.write(this.myresult);
          //window.location.href="pro011";
          router.push("/pro012");
          //router.push("/pro011");
          //self.location.href="/pro011"
        }

      })
    }
  }
}
</script>

5、运行测试

5.1、反射型XSS漏洞测试

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-J7GKeEae-1652659606599)(项目记录.assets/wps1.jpg)]

进入反射型XSS

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7IxV9FxC-1652659606600)(项目记录.assets/wps2.jpg)]

输入payload:

<script>alert(1)</script>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4U6BD1Me-1652659606601)(项目记录.assets/wps3.jpg)]

发现弹窗,XSS漏洞验证成功。


5.2、存储型XSS漏洞测试

进入存储型XSS

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NlzpSHNn-1652659606601)(项目记录.assets/wps4.jpg)]

输入payload:

<script>alert(1)</script>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-taoG9GA0-1652659606602)(项目记录.assets/wps5.jpg)]

访问/stored/show验证

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FAIwTEP8-1652659606602)(项目记录.assets/wps6.jpg)]

出现弹窗,存储型XSS验证成功。


5.3、修复后测试

访问修复的代码:http://localhost:8080/xss/safe

get请求:

xss=<script>alert(1)</script>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZWZx2azS-1652659606603)(项目记录.assets/wps7.jpg)]

发现并没有将输入的内容解析,成功修复XSS漏洞代码。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌兮_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值