用户上传a.jpg文件,文件内容实际为php代码,会不会有安全问题?如果有,如何防止?
用户上传a.jpg文件,文件内容实际为php代码,会不会有安全问题?如果有,如何防止?
回复内容:
用户上传a.jpg文件,文件内容实际为php代码,会不会有安全问题?如果有,如何防止?
设置 mimetype
有风险,判断文件头是不完全的。你可以参看
http://zone.wooyun.org/content/5429
你需要在服务端禁止执行这些代码。简单的说,就是不管他的扩展名是什么,反正这个目录是静态的,不要丢改cgi之类的东西。
如果你是虚拟主机环境,你可能面临对主机环境无法深度配置的问题。
如果你想求个放心,我建议把这些用户上传的静态文件,托管到又拍云、七牛云等外部的CDN服务器上去。
放到外边去就不可能(也不用关心)有php会被意外执行的安全问题了。对于虚拟主机跑得起来的小站,花费也不多甚至几近免费。
10-23补充:想到一个正面突破的好办法。
用户上传之后,存储成任意安全的扩展名(例:raw、bin等)。在需要调用的时候,通过一个php脚本作为中间层传递文件内容(读取请求的文件名,找到文件,把图片文件类型对应的MIME写入HTTP头,图片文件实际的内容写入HTTP正文)。
例如:http://example.com/attach.php?id=13776
。
也可以用一种更加优雅的语法:http://example.com/attach.php/13776.jpg
或http://example.com/attach.php/13776
;
甚至动用rewrite实现类似http://example.com/attach/13776
的URL。
这样会收到若干好处:
- 绝对安全。所有的恶意代码都绝对不经执行,通过HTTP原样返回浏览器,给攻击者个自讨没趣。
- URL显而易见,符合REST原则,外人无法从URL猜测程序的目录结构。
- 经过PHP,就方便实现鉴权、防盗链等若干实用功能。
- 保持网址恒定性,无论是程序目录结构修改,还是静态文件将来托管到CDN上,网址都保持不变。
据我所知 原来有个漏洞,如果目录名是 a.php
然后目录里的 a.jpg 会被当作 PHP 执行。
直接上图片处理,gd或者imagick等,重新存一下
用图形库验证一下。不是图片不让上传。
当 nginx 搭配 php-cgi 时候,在路径解析问题上可能要防下。当年争议比较大的就是 cgi.fix_pathinfo 的问题。另外当nginx 处于反代模式时。mimetype 设置的对,应该没什么问题。
如果是Nginx的话可以考虑区分jpg和php的文件夹,即使上传了jpg但是路径不对也不会被执行0.0
<code>chmod 0444 a.jpg </code>
禁止文件执行
gd库获取一下图片的长宽大小,为0阻止。
很简单,检测文件头,如果不是规定的类型就删除。
以下为摘抄自网络的代码示例。
<?php function file_type($filename) { $file = fopen($filename, "rb"); $bin = fread($file, 2); //只读2字节 fclose($file); $strInfo = @unpack("C2chars", $bin); $typeCode = intval($strInfo['chars1'].$strInfo['chars2']); $fileType = ''; switch ($typeCode) { case 7790: $fileType = 'exe'; break; case 7784: $fileType = 'midi'; break; case 8297: $fileType = 'rar'; break; case 8075: $fileType = 'zip'; break; case 255216: $fileType = 'jpg'; break; case 7173: $fileType = 'gif'; break; case 6677: $fileType = 'bmp'; break; case 13780: $fileType = 'png'; break; default: $fileType = 'unknown: '.$typeCode; } //Fix if ($strInfo['chars1']=='-1' AND $strInfo['chars2']=='-40' ) return 'jpg'; if ($strInfo['chars1']=='-119' AND $strInfo['chars2']=='80' ) return 'png'; return $fileType; } echo file_type('start.php'); // 6063 or 6033
摘自:http://justcoding.iteye.com/blog/891241

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

AI Hentai Generator
Generate AI Hentai for free.

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

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

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

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,

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.
