xss.haozi.me靶机XSS练习心得


xss.haozi.me靶机XSS练习心得

  1.  //server code
     function render (input) {
       return '
    ' + input + '
    ' }

    第一题测试js的语法,js都是利用

  2.  function render (input) {
       return ''
     }
    

    第二题测试的是textarea标签,这个标签内,所有的脚本语言都是字符串,无法被调用执行,所以使用他的闭合标签进行闭合,然后再闭合后输入js代码:

  3.  function render (input) {
       return ''
     }
    

    考察es6语法中 符号进行字符串拼接,以及html标签事件属性 poc" onmouseover="alert`1``

  4.  function render (input) {
       const stripBracketsRe = /[()]/g
       input = input.replace(stripBracketsRe, '')
       return input
     }
    

    考察正则语法,替换了会把圆括号替换为空

    
    
    
    
    
  5.  function render (input) {
       const stripBracketsRe = /[()`]/g
       input = input.replace(stripBracketsRe, '')
       return input
     }
    

    正则替换规则多了一个`,考虑使用实体符号进行绕过,也可以考虑svg标签,body标签

  6.  function render (input) {
       input = input.replace(/-->/g, '??')
       return ''
     }
    

    正则替换了-->,这个是html的注释符,注释符有2中写法,分别是

    一种是 另一种是 所以我们用第二种注释去提前闭合

    poc: --!>

  7.  function render (input) {
       input = input.replace(/auto|on.*=|>/ig, '_')
       return ``
     }
    

    此处正则替换的是auto,on.*=和>字符,忽略大小写 auto替换无法使用auto开头的事件属性,on开头的事件属性也无法使用

    另外利用html语言只要关键字和语法正确,不受空格和换行符影响的特性

    poc:

    type="image" src="xxx" onerror
    =alert(1)
    
  8.  function render (input) {
       const stripTagsRe = /<\/?[^>]+>/gi
       input = input.replace(stripTagsRe, '')
       return `
    ${input}
    ` }

    此处正则替换的是<开头 以>结尾的中间是非">"得多个字符串

    这里还是利用html语言特性最后一行的标签的闭合标签可以不写">"

    poc:

  9.  function render (src) {
       src = src.replace(/<\/style>/ig, '/* \u574F\u4EBA */')
       return `
         
       `
     }
    

    这题中的html中已经有了一个

  10. function render (input) {
      let domainRe = /^https?:\/\/www\.segmentfault\.com/
      if (domainRe.test(input)) {
        return ``
      }
      return 'Invalid URL'
    }
    

    正则中字符串中必须要有https?://www.segmentfault.com/ 意味着payload必须要写在后头,所以用/script>进行闭合

    poc:https://www.segmentfault.com/script>

    因为语法正确,回车 空格都可以,所以poc里也可以任意添加空格和回车

  11. function render (input) {
      function escapeHtml(s) {
        return s.replace(/&/g, '&')
                .replace(/'/g, ''')
                .replace(/"/g, '"')
                .replace(//g, '>')
                .replace(/\//g, '/')
      }
      const domainRe = /^https?:\/\/www\.segmentfault\.com/
      if (domainRe.test(input)) {
        return ``
      }
      return 'Invalid URL'
    }
    
    • 这题正则中会替换& ` " < > /为实体符号,意味着我们写入实体符号,其中的&字符就会被替换,导致无法正常解析,并且和上一题一样必须开头字符串必须是 https?://www.segmentfault.com/

    • 先介绍一个url语法中@字符作用,可以用来隔断域名,前后2个域名会忽略前面那个.注意一个细节2个域名的协议必须是一致的,必须都是http或者https

    • 我们可以自己部署一个域名带上js, js中写上弹窗进行访问即可

      poc: https?://www.segmentfault.com/@www.xxx.com/test.js

  12. function render (input) {
      input = input.toUpperCase()
      return `

    ${input}

    ` }
  • 题中进行了转换成大写操作,html中标签和属性名不区分大小写,但是path部分区分大小写,比如

    等号后面的就是path内容

  • 这题可以尝试第10题用script标签的src添加外部js链接

  • 也可以用poc: 这个poc原理是js进行解析前会先对unicode进行解码,html中的Unicode格式为 &#编码的十进制数值,一般的格式还有直接\u开头的16进制四位编码,一定是四位

  1. function render (input) {
      input = input.replace(/script/ig, '')
      input = input.toUpperCase()
      return '

    ' + input + '

    ' }
  • 和12题相比多了个script的过滤,我们可以双写+unicode编码绕过

    poc: