php文件上传漏洞简单分析
文件上传漏洞这个问题不是php中所存在的只是我们用户在程序开发时可能做得不够好所导致的,以前的asp同样也存在文件上传漏洞,下面我来给大家介绍php文件上传漏洞简单分析与解决办法。
下面是一个简单的文件上传表单
代码如下 | 复制代码 |
php的配置文件php.ini,其中选项upload_max_filesize指定允许上传的文件大小,默认是2M
$_FILES数组变量
PHP使用变量$_FILES来上传文件,$_FILES是一个数组。如果上传test.txt,那么$_FILES数组的内容为:
代码如下 | 复制代码 |
$FILES Array { [file] => Array { [name] => test.txt //文件名称 [type] => text/plain //MIME类型 [tmp_name] => /tmp/php5D.tmp //临时文件 [error] => 0 //错误信息 [size] => 536 //文件大小,单位字节 } } |
如果上传文件按钮的name属性值为file
代码如下 | 复制代码 |
那么使用$_FILES['file']['name']来获得客户端上传文件名称,不包含路径。使用$_FILES['file']['tmp_name']来获得服务端保存上传文件的临时文件路径
存放上传文件的文件夹
PHP不会直接将上传文件放到网站根目录中,而是保存为一个临时文件,名称就是$_FILES['file']['tmp_name']的值,开发者必须把这个临时文件复制到存放的网站文件夹中。
$_FILES['file']['tmp_name']的值是由PHP设置的,与文件原始名称不一样,开发者必须使用$_FILES['file']['name']来取得上传文件的原始名称。
上传文件时的错误信息
$_FILES['file']['error']变量用来保存上传文件时的错误信息,它的值如下:
错误信息 数值 说 明
UPLOAD_ERR_OK 0 没有错误
UPLOAD_ERR_INI_SIZE 1 上传文件的大小超过php.ini的设置
UPLOAD_ERR_FROM_SIZE 2 上传文件的大小超过HTML表单中MAX_FILE_SIZE的值
UPLOAD_ERR_PARTIAL 3 只上传部分的文件
UPLOAD_ERR_NO_FILE 4 没有文件上传
文件上传漏洞
如果提供给网站访问者上传图片的功能,那必须小心访问者上传的实际可能不是图片,而是可以指定的PHP程序。如果存放图片的目录是一个开放的文件夹,则入侵者就可以远程执行上传的PHP文件来进行攻击。
下面是一个简单的文件上传例子:
代码如下 | 复制代码 |
// 设置上传文件的目录 // 检查file是否存在 |
这个例子没有检验文件后缀,可以上传任意文件,很明显的上传漏洞,要解决上面的问题很简单,我们从一段代码来看。
代码如下 | 复制代码 |
switch( $extension ) { case 'application/msword': $extension ='doc'; break; case 'application/vnd.ms-excel': $extension ='xls'; break; case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': $extension ='docx'; break; case 'application/vnd.ms-powerpoint': $extension ='ppt'; break; case 'application/pdf': $extension ='pdf'; break; case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': $extension ='xlsx'; break; default: die('只允许上传doc,docx,xls,pdf,ppt文件 重新上传'); } |
这个是用来限制用户上传的类型号或后缀名了,这样可以过滤简单不能直接上传php了,但是我们再看一个实例,你绘发现太可怕了。
用画图工具新建一个jpg或gif或png之类图片格式,开头一定是GIF或JPG或PNG之类。
将php网马的头部写入GIF 如图:
代码如下 | 复制代码 |
if($_FILES){ ";<br> print_r($_FILES);<br> echo " Copy after login echo "以下是错误的getimagesize() ";<br> print_r(getimagesize($_FILES['bug']['tmp_name']));<br> echo " Copy after login exit; $fp = fopen($_FILES['bug']['tmp_name'],"r"); $content = fread($fp,filesize ($_FILES['bug']['tmp_name'])); //echo $content 可以看到上传的源代码 } ?> |
就可以看到如图这样坑爹的效果了。
首先是print_r($_FILES) 直接显示的是扩展的jpg结果。
然后是php函数getimagesize()的结果是gif(它以文件开头那段为判断依据)。
在这种问题上php是无法解决的,但我们可以从服务器目录权限来操作,下面提供一些解决办。
其实,我们抓住几个地方即可,我们先来分析下,既然用户要上存文件,而且文件将是多种多样格式;可能有的文件内容与用户传入格式不一致,有的文件内容还夹杂木马代码。 那么,我们让用户上存文件,跟站点文件做一个分别授权,做隔离。
让保存上存目录独立开来,目录权限只读不能执行
这一步从系统设计加以授权,无论你上次什么文件,都不可能执行到。就算我不做任何检测,你的文件都上存到这里了,也不会对我系统构成安全。(如果有用户上存一些反动言语的图片,那另外需要处理的)
不直接使用服务器传入值,所有都要进行检测
这类跟我们做一切输入都是有害原则一样,对于客户端传入的:type, name ,都要进行判断,不直接使用。对于要生成到某个目录,某个文件名。
文件名最好方法是:自己写死目录(不要读取传入目录),文件名,最好自己随机生成,不读取用户文件名。文件扩展名,可以取最右边"."后面字符。
以上2个方法,刚好从2个方面对上存做了整体约束。
方法2 : 保存上存文件名,按照自己指定目录写入,并且文件名自己生成的。
方法1:只要保证文件写对了位置,然后从配置上,对写入目录进行权限控制,这个是治本。可以做到,你无论上存什么文件,都让你没有权限跳出去可以运行。
以上2个方法,一起使用,可以保证文件正确存到地方,然后,权限可以控制。 这里顺便说明下, 判断用户上存文件是否满足要求类型,就直接检查文件扩展名,只要满足扩展名就让上存。 反正,做了执行权限限制,你不按要求上存内容,也无妨。 反正,不能执行,也不会有多大危害性的。
正确步骤:
1.读取文件名,验证扩展名是不是在范围内
2.自己定义生成的文件名,目录,扩展名可以来自文件名扩展名。 其它值,都自己配置,不读取上存中内容
3.将文件 移到新目录(这个目录权限设置只读)

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

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

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.
