PHP开发中保险防范知识

WBOY
Release: 2016-06-13 10:56:35
Original
789 people have browsed it

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

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!