XSS
Cross-site Scripting (XSS) 跨站脚本
恶意代码注入
XSS using Script in Attributes
XSS attacks may be conducted without using tags. Other tags will do exactly the same thing, for example:
or other attributes like: onmouseover, onerror.
onmouseover
onerror
XSS using Script Via Encoded URI Schemes
使用编码绕过过滤 如:a=A (UTF-8)
XSS using code encoding
We may encode our script in base64 and place it in META tag.
|
|
伪协议
反射型XSS
获取用户cookies
|
|
存储型
隐蔽性高
DOM(Document Object Model) XSS
动态修改html页面
|
|
The malicious script can be embedded in the URL as follows in two ways:
|
|
浏览器防护(自动URL编码)
%3Cscript%3Ealert(document.cookie)%3C/script%3E
XSS Ajax提交表单getshell
XSS (Cross Site Scripting) Prevention Cheat Sheet
https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet
- 仔细验证不可信数据
- HTML实体编码 You MUST use the escape syntax for the part of the HTML document you’re putting untrusted data into.
XSS Prevention Rules
RULE #0 don’t put untrusted data into your HTML document
RULE #1 HTML Escape
RULE #2 - Attribute Escape
RULE #3 - JavaScript Escape
- Ensure JavaScript variables are quoted
- JavaScript Hex Encoding
- JavaScript Unicode Encoding
- Avoid backslash encoding (\” or \’ or \)
RULE #4 - CSS Escape And Strictly Validate
- CSS Hex encoding
Except for alphanumeric characters, escape all characters with ASCII values less than 256 with the \HH escaping format.
RULE #5 - URL Escape
RULE #6 - Sanitize HTML Markup
PHP Html Purifier - http://htmlpurifier.org/
RULE #7 - Prevent DOM-based XSS
Others
Use HTTPOnly cookie flag
XSS Filter Evasion
https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet
- XSS Platform
- XSS 编码
- OWASP XSSER
- 编码
|
|
HTML实体编码
显示结果 | 描述 | 实体名称 | 实体编号 |
---|---|---|---|
空格 | |   | |
< | 小于号 | < | < |
> | 大于号 | > | > |
& | 和号 | & | & |
“ | 引号 | " | " |
‘ | 撇号 | ' (IE不支持) | ' |
Unicode编码
- 单引号
<IMG SRC=javascript:alert(String.fromCharCode(88,83,83))>
- 其他
使用tab键空开<IMG SRC="jav ascript:alert('XSS');">
编码tab<IMG SRC="jav	ascript:alert('XSS');">
使用制表符, 换行符和回车符
加入新行; Only 09 (horizontal tab), 10 (newline) and 13 (carriage return) work.
<IMG SRC="jav
ascript:alert('XSS');">
Type | Horizontal Tab | New line | Carriage Return |
URL | %09 | %10 | %13 |
Minimal Sized Hex | 	 | 
 | 
 |
Maximum Sized Hex | 	 | 
 | 
 |
Minimum Sized Decimal | 	 | 
 | 
 |
Maximum Sized Decimal | 	 | 	 | 	 |
Cross-Site Request Forgery (CSRF) 跨站脚本请求伪造
向服务器提交数据
在a.com中访问b.com(或者受害者通过邮件点击访问example.com/delete?rule=*),某些浏览器将自动发送其cookie。
- 自动删除文章
- 自动添加管理员账号
GET scenario
POST scenario
结合XSS漏洞攻击将悄无声息。
Prevent CSRF Vulnerabilities
Check standard headers to verify the request is same origin
- Origin Header
- Referer Header (绕过referer)
AND Check CSRF token
same-origin policy
同源策略:同协议、域名、端口,不能跨域访问。
如何实现跨域访问?
1. Access-Control-Allow-Origin 头
a.com 允许其他域访问本域资源
b.com
2. getJSON
3. iframe
跨域策略文件
crossdomain.xml