Home Backend Development PHP Tutorial HTTP攻击与防范PHP安全配置

HTTP攻击与防范PHP安全配置

Jun 23, 2016 pm 01:04 PM

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 "变量没有赋值";    }
Copy after login

当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 "变量没有赋值";    }
Copy after login

事例2:

例如:test.php内容如下:

<?phpif (isset($_SESSION['username'])){        echo "访问者:".$_SESSION['username'];}else{        echo "访问者尚未登陆";}
Copy after login

当访问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 "访问者尚未登陆";}
Copy after login

事例3:

当php.ini中的allow_url_fopen = On时

demo.php中的内容如下:

<?php @include "$path";if(!isset($path)){        echo "文件没有被调用";}
Copy after login

test.php中的内容为:


<?phpecho "this is test.php。文件被调用。";
Copy after login

当访问网址:

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 "
Copy after login
";
运行结果部分如下:



可以通过

<?phpini_set ("allow_url_fopen",1);
Copy after login

在php文件中修改配置



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

See all articles