Home php教程 php手册 总结了关于PHP xss 和 SQL 注入的问题

总结了关于PHP xss 和 SQL 注入的问题

Jun 21, 2016 am 08:49 AM
cookie filter php sql xss

 漏洞无非这么几类,XSS、sql注入、命令执行、上传漏洞、本地包含、远程包含、权限绕过、信息泄露、cookie伪造、CSRF(跨站请求)等。这些漏洞不仅仅是针对PHP语言的,本文只是简单介绍PHP如何有效防止这些漏洞。

  1.xss + sql注入(关于xss攻击详细介绍)

  其中占大头的自然是XSS与SQL注入,对于框架类型或者有公共文件的,建议在公共文件中统一做一次XSS和SQL注入的过滤。用PHP写个过滤函数,可由如下所示:

  $_REQUEST = filter_xss($_REQUEST);

  $_GET = filter_xss($_GET);

  $_POST = filter_xss($_POST);

  $_COOKIE = filter_xss($_COOKIE);

  $_POST = filter_sql($_POST);

  $_GET = filter_sql($_GET);

  $_COOKIE = filter_sql($_COOKIE);

  $_REQUEST = filter_sql($_REQUEST);

  最简单的filter_xss函数是htmlspecialchars()

  最简单的filter_sql函数是mysql_real_escape_string()

  当然,谁都知道这种过滤filter_sql(详细防止sql注入)只能过滤字符型和搜索型的注入,对于数字型是没有办法的,但也说明做了这层过滤后,只需在后面注意数字型的SQL语句就可以了,遇到了加intval过滤就可以了,这就变得容易多了。

  2. 命令执行

  对于命令执行,可以从关键字入手,总共可分为3类

  (1) php代码执行 :eval等

  (2)shell命令执行:exec、passthru、system、shell_exec等

  (3) 文件处理:fwrite、fopen、mkdir等

  对于这几类需要注意其参数是否用户可控。

  3.上传漏洞

  对于上传漏洞,也是重点关注的地方,要仔细分析它的处理流程,针对上传的绕过方式是很多的,最保险的方式:在保存文件是采用文件名随机命名和后缀白名单方式。其次要注意的一点是上传文件的地方可能不止一处,不要有遗漏,可能会碰到这样的情况,突然在某个目录里面包含了一个第三方的编辑器在里面。

  文件包含漏洞涉及的函数如include() 、include_once()、require()、require_once()、file_get_contents()等

  最常见的还是出在下载文件功能函数,例如download.php?file=../../../etc/passwd 这种类型中。

  4. 权限绕过

  权限绕过可分为两类吧

  (1)后台文件的未授权访问。后台的文件没有包含对session的验证,就容易出现这样的问题

  (2)未作用户隔离,例如mail.php?id=23显示了你的信件,那么换个ID, mail.php?id=24就查看到了别人的信件,编写代码是方便,把信件都存在一个数据表里,id统一编号,前端展现时只需按id取出即可,但未作用户隔离,判定归属,容易造成越权访问。

  这样的例子是很常见的,给某银行做评估是就经常发现这种漏洞。

  5. 信息泄露

  信息泄露算是比较低危的漏洞了,比如列目录这种就属于部署问题,而与代码审计无关了,而像暴路径、暴源码这种是需要防止的。曾经遇到这样的代码

  

  表面上似乎没问题,可是当请求变为 xx.php?a[]=1时,即参数变为数组的时候,就会发生错误以致路径泄露,而用isset判断则不会,当然一个个防太麻烦,建议在配置文件中关闭错误提示,或者在公共文件中加入如下代码以关闭错误显示功能:

  



Statement of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1280
29
C# Tutorial
1257
24
What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

Composer: Aiding PHP Development Through AI Composer: Aiding PHP Development Through AI Apr 29, 2025 am 12:27 AM

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

How to use MySQL functions for data processing and calculation How to use MySQL functions for data processing and calculation Apr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

Composer: The Package Manager for PHP Developers Composer: The Package Manager for PHP Developers May 02, 2025 am 12:23 AM

Composer is a dependency management tool for PHP, and manages project dependencies through composer.json file. 1) parse composer.json to obtain dependency information; 2) parse dependencies to form a dependency tree; 3) download and install dependencies from Packagist to the vendor directory; 4) generate composer.lock file to lock the dependency version to ensure team consistency and project maintainability.

How to configure the character set and collation rules of MySQL How to configure the character set and collation rules of MySQL Apr 29, 2025 pm 04:06 PM

Methods for configuring character sets and collations in MySQL include: 1. Setting the character sets and collations at the server level: SETNAMES'utf8'; SETCHARACTERSETutf8; SETCOLLATION_CONNECTION='utf8_general_ci'; 2. Create a database that uses specific character sets and collations: CREATEDATABASEexample_dbCHARACTERSETutf8COLLATEutf8_general_ci; 3. Specify character sets and collations when creating a table: CREATETABLEexample_table(idINT

How to rename a database in MySQL How to rename a database in MySQL Apr 29, 2025 pm 04:00 PM

Renaming a database in MySQL requires indirect methods. The steps are as follows: 1. Create a new database; 2. Use mysqldump to export the old database; 3. Import the data into the new database; 4. Delete the old database.

How to implement singleton pattern in C? How to implement singleton pattern in C? Apr 28, 2025 pm 10:03 PM

Implementing singleton pattern in C can ensure that there is only one instance of the class through static member variables and static member functions. The specific steps include: 1. Use a private constructor and delete the copy constructor and assignment operator to prevent external direct instantiation. 2. Provide a global access point through the static method getInstance to ensure that only one instance is created. 3. For thread safety, double check lock mode can be used. 4. Use smart pointers such as std::shared_ptr to avoid memory leakage. 5. For high-performance requirements, static local variables can be implemented. It should be noted that singleton pattern can lead to abuse of global state, and it is recommended to use it with caution and consider alternatives.

Composer's Purpose: Managing Project Dependencies in PHP Composer's Purpose: Managing Project Dependencies in PHP Apr 30, 2025 am 12:01 AM

We need Composer because it can effectively manage dependencies of PHP projects and avoid the hassle of version conflicts and manual library management. Composer declares dependencies through composer.json and uses composer.lock to ensure the version consistency, simplifying the dependency management process and improving project stability and development efficiency.

See all articles