search
HomeBackend DevelopmentPHP TutorialMongoDB安全,php中的注入攻击

2016-06-02 15:06:12 来源:360安全播报 作者:默白 阅读:35次

分享到:

在讨论MongoDB注入之前,我们必须要了解它到底是什么,以及我们之所以相较于其他数据库更偏爱它的原因。因为MongoDB不使用SQL,所以人们就假想它不容易受到任何形式的注入攻击。但是相信我,没有什么东西生来就内置安全防护。我们还是要设置一些逻辑代码来防止攻击。

MongoDB是什么?

简单来说,MongoDB是MongoDB公司开发的一个开源数据库,可以以不同结构的类似于JSCON文档的形式存储文件。相关的信息会被存储在一起,这样有利于使用MongoDB查询语言进行快速查找。

为什么要使用MongoDB?

因为大家都想要快速查询,所以MongoDB非常受欢迎。它的性能非常好(1000millionsquries/秒)。它更受欢迎的另一个原因就是它擅长在很多相关数据库不能很好适应的案例中发挥作用。例如,非结构化的应用程序、半结构化和多态数据或是可伸缩性要求高并且拥有多个数据中心的应用程序。

到此为止吧!如果在运行任何开源式应用程序的话,为了防止糟糕的情况发生,还是到此为止吧。我们为开源项目提供一种免费的渗透测试。在这里提交应用程序吧,我们会进行评价。

让我们来看一下注入攻击

第一种情况下,我们有一个PHP脚本,该脚本可以显示一个特定ID的用户名和对应密码。

在上面的脚本中可以看到数据库名称是安全,集合名称是用户。U-id参数会由GET算法获得,然后将其传递到数组,之后就会给出相关结果。听上去很好?我们试着放入一些比较运算符和数组。

糟糕!!它得出的结果是整个数据库。问题到底出在哪呢?这是因为输入了http://localhost/mongo/show.php?u_id[$ne]=2,创建了下面这种MongoDB查询,$qry= array(“id” => array(“$ne” => 2))。所以它显示了除id=2之外的所有结果,可以从截屏1中看到。

让我们来考虑一下另一种情况,在前期脚本的工作内容是一样的,但是我们会用findOne方法来创建MongoDB查询。

首先我们来看一下findOne的工作原理。此方法有下列语法:

db.collection.findOne(查询、投影)

这将返回到满足指定查询条件的文档。例如,我们需要找到与id=2相关的结果,就会出现下列命令:

现在让我们看一下源代码:

这里的关键点就是在某种程度上打破查询,然后再修复它。那如果我们键入以下查询,又会发生什么呢?

http://localhost/mongo/inject.php?u_name=dummy’});return{something:1,something:2}}//&u_pass=dummy

这会打破查询并返回所需参数。让我们来检查一下输出:

这带来了两个错误,但是这只是因为我们想要访问两个不存在的参数吗?这种错误间接地表明,用户名和密码在数据库中是一种参数,而这就是我们想要的结果。

只要我们键入正确的参数,错误就会消除。

现在我们想要找出数据库名称。在MongoDB中,用于查找数据库名称的方法是db.getName() 。因此查询就变成了:

为了将此数据库转储,我们首先要找出集合的名称。在MongoDb中,用于查找集合名称的方法是db.getCollectionNames()。

所以到现在为止,我们已经得到了数据库和集合的名称。剩下的就是要找到用户名集合,做法如下:

同样的,我们可以通过改变内部函数db.users.find()[2]来获得其他的用户名和密码,比如说:

既然大家都对MongoDb很熟悉,那么大家可能会想要了解相关的预防措施。

让我们考虑一下第一种情况,参数在数组中传递。要想防止这种注入,我们可能需要停止执行数组中的比较运算符。因此,其解决方案之一就是用下列方式使用implode()函数:

implode()函数返回值是一个字符串数组。因此我们得到的只会是一个对应特定ID的结果,而不是所有结果。

在第二种情况中,我们可以使用 addslashes() 方法来保证查询不被攻击者打破。使用正则表达式来替换特殊符号是一个好主意。可以使用下列的正则表达式:

$u_name =preg_replace(‘/[^a-z0-9]/i’, ‘\’, $_GET[‘u_name’]);

这样之后,如果我们试图打破查询,它就不会再重蹈覆辙。

本文由 360安全播报 翻译,转载请注明“转自360安全播报”,并附上链接。

原文链接:http://blog.securelayer7.net/mongodb-security-injection-attacks-with-php/
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)