PHP安全防范知识
PHP代码安全和XSS,SQL注入等对于各类网站的安全非常中用,尤其是UGC(User Generated Content)网站,论坛和电子商务网站,常常是XSS和SQL注入的重灾区。这里简单介绍一些基本编程要点, 相对系统安全来说,php安全防范更多要求编程人员对用户输入的各种参数能更细心.
php编译过程中的安全
建议安装Suhosin补丁,必装安全补丁
php.ini安全设置
register_global = off
magic_quotes_gpc = off
display_error = off
log_error = on
# allow_url_fopen = off
expose_php = off
open_basedir =
safe_mode = on
disable_function = exec,system,passthru,shell_exec,escapeshellarg,escapeshellcmd,proc_close,proc_open,dl,popen,show_source,get_cfg_var
safe_mode_include_dir =
DB SQL预处理
mysql_real_escape_string (很多PHPer仍在依靠addslashes防止SQL注入,但是这种方式对中文编码仍然是有问题的。addslashes的问题在于黑客可以用 0xbf27来代替单引号,GBK编码中0xbf27不是一个合法字符,因此addslashes只是将0xbf5c27,成为一个有效的多字节字符,其 中的0xbf5c仍会被看作是单引号,具体见这篇文章)。用mysql_real_escape_string函数也需要指定正确的字符集,否则依然可能 有问题。
prepare + execute(PDO)
ZendFramework可以用DB类的quote或者quoteInto, 这两个方法是根据各种数据库实施不用方法的,不会像mysql_real_escape_string只能用于mysql
用户输入的处理
无需保留HTML标签的可以用以下方法
strip_tags, 删除string中所有html标签
htmlspecialchars,只对””,”;”,”’”字符进行转义
htmlentities,对所有html进行转义
必须保留HTML标签情况下可以考虑以下工具:
HTML Purifier: HTML Purifier is a standards-compliant HTML filter library written in PHP.
PHP HTML Sanitizer: Remove unsafe tags and attributes from HTML code
htmLawed: PHP code to purify & filter HTML
上传文件
用is_uploaded_file和move_uploaded_file函数,使用HTTP_POST_FILES[]数组。并通过去掉上传目录的PHP解释功能来防止用户上传php脚本。
ZF框架下可以考虑使用File_upload模块
Session,Cookie和Form的安全处理
不要依赖Cookie进行核心验证,重要信息需要加密, Form Post之前对传输数据进行哈希, 例如你发出去的form元素如下:
POST回来之后对参数进行验证
$str = "";
foreach($_POST['H'] as $key=>$value) {
$str .= $key.$value;
}
if($_POST['hash'] != md5($str.$secret)) {
echo "Hidden form data modified"; exit;
}
PHP安全检测工具(XSS和SQL Insertion)
Wapiti - Web application security auditor(Wapiti - 小巧的站点漏洞检测工具) (SQL injection/XSS攻击检查工具)
安裝/使用方法:
apt-get install libtidy-0.99-0 python-ctypes python-utidylib
python wapiti.py http://Your Website URL/ -m GET_XSS
Pixy: XSS and SQLI Scanner for PHP( Pixy - PHP 源码缺陷分析工具)
安裝: apt-get install default-jdk

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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Validator can be created by adding the following two lines in the controller.
