对于我们想做web安全的人来说,最好就是拿来学习,可是万物抓根源,我们要的不是鱼而是渔。在国内,各种各样的php程序1.0版,2.0版像雨后春笋一样的冒出来,可是,大家关注的都是一些著名的cms,论坛,blog程序,很少的人在对那些不出名的程序做安全检测,对于越来越多的php程序员和站长来说,除了依靠服务器的堡垒设置外,php程序本身的安全多少你总得懂点吧。
有人说你们做php安全无非就是搞搞注入和跨站什么什么的,大错特错,如果这样的话,一个magic_quotes_gpc或者服务器里的一些安全设置就让我们全没活路了:(。我今天要说的不是注入,不是跨站,而是存在于php程序中的一些安全细节问题。OK!切入正题。
注意一些函数的过滤
有些函数在程序中是经常使用的,像include(),require(),fopen(),fwrite(),readfile(),unlink(),eval()以及它们的变体函数等等。这些函数都很实用,实用并不代表让你多省心,你还得为它们多费点心。 :)
1.include(),require()和fopen(),include_once(),require_once()这些都可以远程调用文件,对于它们的危害,google搜一下你就会很明了,对于所包含调用的变量没过滤好,就可以任意包含文件从而去执行。举个例子,看print.php
…
if (empty ($bn) ) { //检查是变量$bn是否为空
include (“$cfg_dir/site_${site}.php”); //把$cfg_dir这个路径里的site_${site}.php包含进来
…
不管存不存在$cfg_dir目录,$site这个变量你可以很自然的去使用,因为他根本没检查$site变量啊。可以把变量$site指定远程文件去调用,也可以是本地的一个文件,你所指定的文件里写上php的语句,然后它就去包含执行这个含有php语句的文件了.就像这样
列出文件目录甚至可以扩展到包含一些管理员文件,提升权限,典型的像以前phpwind,bo-blog的漏洞一样。除了依靠php.ini里的allow_url_fopen设为off禁止远程使用文件和open_base_dir禁止使用目录以外的文件外,你还得事先声明好只能包含哪些文件,这里就不多说废话了。
2.fopen(),file(),readfile(),openfile(),等也是该特别留意的地方。函数本身并没什么,它们的作用是去打开文件,可是如果对变量过滤不彻底的话,就会泄露源代码。这样的函数文本论坛里会有很多。
…
$articlearray=openfile(“$dbpath/$fid/$tid.php”); //打开$dbpath/$fid这个路径的$tid.php文件
$topic_detail=explode(“|”,$articlearray[0]); //用分割符|读出帖子的内容
…
很眼熟吧,这是ofstar以前版本的read.php,$fid和$tid没有任何过滤,$tid指定为某个文件提交,就发生了原代码泄露。就像这样。
$tid会被加上php的后缀,所以直接写index。这仅仅是个例子,接着看吧。
3.fwrite()和它的变体函数这种漏洞想想都想得出,对于用户提交的字符没过滤的话,写入一段php后门又不是不可以。
4.unlink()函数,前段时间,phpwind里任意删除文件就是利用这个函数,对于判断是否删除的变量没过滤,变量可以指定为任意文件,当然就可以删除任意文件的变量。
5.eval(),preg_replace()函数,它们的作用是执行php代码,如果字符串没被经过任何过滤的话,会发生什么呢,我就常看见一些cms里面使用,想想,一句话的php木马不就是根据eval()原理制作的吗?
6.对于system()这些系统函数,你会说在php.ini里禁止系统函数,对,这也是好办法,可是象一些程序里需要,那是不是就不用了呢?就像上次我看到的一套很漂亮的php相册一样。另外对于popen(),proc_open(),proc_close()函数你也得特别注意,尽管他们执行命令后并没有直接的输出,但你想这到底对黑客们有没有用呢。再这里php提供提供了两个函数,escapeshellarg(),escapeshellcmd(),这两个函数用来对抗系统函数的调用攻击,也就是过滤。
对于危害,来举个例子,我们来看某论坛prod.php
07 $doubleApp = isset($argv[1]); //初始化变量$doubleApp
…
14 if( $doubleApp ) //if语句
15 {
16 $appDir = $argv[1]; //初始化$appDir
17 system(“mkdir $prodDir/$appDir”); //使用系统函数system来创建目录$prodDir/$appDir
本来是拿来创建$prodDir/$appDir目录的,再接着看上去,程序仅仅检测是否存在$argv[1],缺少对$argv[1]的必要过滤,那么你就可以这样
/prod.php?argv[1]=|ls%20-la或者/prod.php?argv[1]=|cat%20/etc/passwd
(分割符| 在这里是UNIX的管道参数,可以执行多条命令。)
到这里,常见的漏洞类型应该知道点了吧。
所以一定不要只指望服务器端的设置,最好后台程序也需要注意下,一般情况下通过服务器针对某个站点设置应该能好点。但很多与数据库的操作等就不是这么容易控制了。

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

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 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.

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 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.

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

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.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools