总结了关于PHP xss 和 SQL 注入的问题
漏洞无非这么几类,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判断则不会,当然一个个防太麻烦,建议在配置文件中关闭错误提示,或者在公共文件中加入如下代码以关闭错误显示功能:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.
