HTTP攻击与防范PHP安全配置
1什么是安全性
所谓安全性就是保护web应用程序与网页不会受到黑客的攻击。有些黑客纯粹是为了好玩而入侵他人的电脑,但有更多的黑客费劲心思要窃取他人电脑中的机密文件,甚至使整台电脑瘫痪来达到他的目的。现象在网上有很多可以让黑客使用的软件,这些软件多半是免费的而且简单好用,所以一般人要攻击您的电脑,并不是一件非常困难的事情。关键是您对电脑进行了什么样的保护?如果只是安装了查毒软件或者防火墙以为平安无事了,那么您对安全性的真正意义可以说是完全不了解。
2 register global
从PHP4.2.0开始,php.ini的register_global选项的默认值预设为Off。当register_globals设定为On时,您的程序将可以接收来自服务器中的各种环境变量,包括表单提交的变量,而且由于PHP不必事先初始化变量的值,从而导致很大的安全隐患.例如HTML表单的请求变量。由于PHP不需要事先初始化变量的值,这就会更容易写出不安全的代码。这是个很艰难的抉择,但PHP社区还是决定默认关闭此选项。当打开时,人们使用变量时确实不知道变量是哪里来的,只能想当然。但是register_globals的关闭改变了这种代码内部变量和客户端发送的变量混杂在一起的糟糕情况。
3 安全模式
安全模式( safe_mode)是PHP用来限制文档的存取、限制环境变量的存取,以及控制外部程序的执行。
由于网站服务器是以单一系统使用者的模式在运行,因此这个系统的使用者账号必须能够读取每个使用者的文档。这表示在网站服务器上执行的任何代码文档都能够存取每个使用者的文档。PHP的安全模式在多用户的系统上设置一些限制选项来保障程序的安全运行。安全模式只能限制PHP的文档,但是不能限制PHP执行的外部应用程序。因此将可执行的应用程序放置在一个安全的文件夹内,不要让外部用户执行。 启动PHP的安全模式,将php.ini文件的safe_mode选项(directive)设置为On:
safe_mode = On
事例1:
test.php内容如下:
<?php if($authorized){ echo "变量赋值"; }else{ echo "变量没有赋值"; }
当php.ini中的register_globals=Off时
访问网址:http://localhost/test.php?authorized=1
输出结果为:
变量没有赋值。
当php.ini中的register_globals=On时
攻击:
变量未初始化,可以通过url对变量赋值
输出结果为
变量赋值
防护:
变量初始化,阻止通过url对变量赋值进行攻击。
需将代码改为:
<?php $authorized=false; if($authorized){ echo "变量赋值"; }else{ echo "变量没有赋值"; }
事例2:
例如:test.php内容如下:
<?phpif (isset($_SESSION['username'])){ echo "访问者:".$_SESSION['username'];}else{ echo "访问者尚未登陆";}
当访问http://localhost/test.php时,
输出:访问者尚未登陆
攻击:
在网址后面追加?_SESSION[username]=admin
即:http://localhost/test.php?_SESSION[username]=admin
输出:访问者:admin
防护:
session_start()开启session,获取session中的值,阻止通过url对session变量进行注入攻击。
代码改为<?phpsession_start ();if(isset($_SESSION['username'])){ echo "访问者:".$_SESSION['username'];}else{ echo "访问者尚未登陆";}
事例3:
当php.ini中的allow_url_fopen = On时
demo.php中的内容如下:
<?php @include "$path";if(!isset($path)){ echo "文件没有被调用";}
test.php中的内容为:
<?phpecho "this is test.php。文件被调用。";
当访问网址:
http://localhost/demo.php时
输出:文件没有被调用。
攻击:
在链接后面拼接?path=test.php
即:访问http://localhost/demo.php?path=test.php
输出:this is test.php。文件被调用。
保护:
同上对path变量初始化。
注:
可以调用ini_get_all函数来显示 PHP的设定值。
例如:
<?php echo "<pre class="brush:php;toolbar:false">"; print_r(ini_get_all()); echo "
运行结果部分如下:
可以通过
<?phpini_set ("allow_url_fopen",1);
在php文件中修改配置

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











There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.
