Nosql 注入的简介
NoSQL 注入由于 NoSQL 本身的特性和传统的 SQL 注入有所区别。使用传统的SQL注入,攻击者利用不安全的用户输入来修改或替换应用程序发送到数据库引擎的 SQL 查询语句(或其他SQL语句)。
换句话说,SQL 注入使攻击者可以在数据库中 SQL 执行命令。
与关系数据库不同,NoSQL 数据库不使用通用查询语言。NoSQL 查询语法是特定于产品的,查询是使用应用程序的编程语言编写的:PHP,JavaScript,Python,Java 等。这意味着成功的注入使攻击者不仅可以在数据库中执行命令,而且可以在应用程序本身中执行命令,这可能更加危险。
以下是 OWASP 对于 Nosql 注入的介绍:
NoSQL databases provide looser consistency restrictions than traditional SQL databases. By requiring fewer relational constraints and consistency checks, NoSQL databases often offer performance and scaling benefits. Yet these databases are still potentially vulnerable to injection attacks, even if they aren’t using the traditional SQL syntax. Because these NoSQL injection attacks may execute within a procedural language, rather than in the declarative SQL language, the potential impacts are greater than traditional SQL injection.
NoSQL database calls are written in the application’s programming language, a custom API call, or formatted according to a common convention (such as
XML
,JSON
,LINQ
, etc). Malicious input targeting those specifications may not trigger the primarily application sanitization checks. For example, filtering out common HTML special characters such as< > & ;
will not prevent attacks against a JSON API, where special characters include/ { } :
.
NoSQL 注入的分类
有两种 NoSQL 注入分类的方式:
第一种是按照语言的分类,可以分为:PHP 数组注入,JavaScript 注入和 Mongo Shell 拼接注入等等。
第二种是按照攻击机制分类,可以分为:重言式注入,联合查询注入,JavaScript 注入、盲注等,这种分类方式很像传统 SQL 注入的分类方式。
- 重言式注入
又称为永真式,此类攻击是在条件语句中注入代码,使生成的表达式判定结果永远为真,从而绕过认证或访问机制。
- 联合查询注入
联合查询是一种众所周知的 SQL 注入技术,攻击者利用一个脆弱的参数去改变给定查询返回的数据集。联合查询最常用的用法是绕过认证页面获取数据。
- JavaScript 注入
MongoDB Server 支持 JavaScript,这使得在数据引擎进行复杂事务和查询成为可能,但是传递不干净的用户输入到这些查询中可以注入任意的 JavaScript 代码,导致非法的数据获取或篡改。
- 盲注
当页面没有回显时,那么我们可以通过 $regex
正则表达式来达到和传统 SQL 注入中 substr()
函数相同的功能,而且 NoSQL 用到的基本上都是布尔盲注。
下面我们便通过 PHP 和 Nodejs 来讲解 MongoDB 注入的利用方式。
PHP 中的 MongoDB 注入
测试环境如下:
-
Ubuntu
-
PHP 7.4.21
-
MongoDB Server 4.4.7
在 PHP 中使用 MongoDB 你必须使用 MongoDB 的 PHP 驱动:https://pecl.php.net/package/mongodb 。这里我们使用新版的 PHP 驱动来操作 MongoDB。一下实例均以 POST 请求方式为例。
重言式注入
首先在 MongoDB 中选中 test 数据库,创建一个 users 集合并插入文档数据:
> use testswitched to db test>> db.createCollection('users'){ "ok" : 1 }>> db.users.insert({username: 'admin', password: '123456'})WriteResult({ "nInserted" : 1 })> db.users.insert({username: 'whoami', password: '657260'})WriteResult({ "nInserted" : 1 })> db.users.insert({username: 'bunny', password: '964795'})WriteResult({ "nInserted" : 1 })> db.users.insert({username: 'bob', password: '965379'})WriteResult({ "nInserted" : 1 })>
然后编写 index.php:
<?php$manager = new MongoDB\\Driver\\Manager("mongodb://127.0.0.1:27017");$username = $\_POST\['username'\];$password = $\_POST\['password'\];$query = new MongoDB\\Driver\\Query(array( 'username' => $username, 'password' => $password));$result = $manager->executeQuery('test.users', $query)->toArray();$count = count($result);if ($count > 0) { foreach ($result as $user) { $user = ((array)$user); echo '====Login Success====<br>'; echo 'username:' . $user\['username'\] . '<br>'; echo 'password:' . $user\['password'\] . '<br>'; }}else{ echo 'Login Failed';}?>
如下,当正常用户想要登陆 whoami 用户时&#x