php编程的安全防范知识

WBOY
Freigeben: 2016-07-25 08:59:06
Original
995 Leute haben es durchsucht
  1. register_global = off
  2. magic_quotes_gpc = off
  3. display_error = off
  4. log_error = on
  5. # allow_url_fopen = off
  6. expose_php = off
  7. open_basedir =
  8. safe_mode = on
  9. disable_function = exec,system,passthru,shell_exec,escapeshellarg,escapeshellcmd,proc_close,proc_open,dl,popen,show_source,get_cfg_var
  10. safe_mode_include_dir =
复制代码

2,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。

3,用户输入的处理 无需保留html标签的可以用以下方法 strip_tags, 删除string中所有html标签 htmlspecialchars,只对””,”;”,”'”字符进行转义 htmlentities,对所有html进行转义 必须保留HTML标签情况下可以考虑以下工具:

  1. HTML Purifier: HTML Purifier is a standards-compliant HTML filter library written in PHP.
  2. PHP HTML Sanitizer: Remove unsafe tags and attributes from HTML code
  3. htmLawed: PHP code to purify & filter HTML
复制代码

4,上传文件 用is_uploaded_file和move_uploaded_file函数,使用HTTP_POST_FILES[]数组。并通过去掉上传目录的PHP解释功能来防止用户上传php脚本。 ZF框架下可以考虑使用File_upload模块 Session,Cookie和Form的安全处理 不要依赖Cookie进行核心验证,重要信息需要加密, Form Post之前对传输数据进行哈希。 例如发出去的form元素如下:

  1. POST回来之后对参数进行验证
  2. $str = "";
  3. foreach($_POST['H'] as $key=>$value) {
  4. $str .= $key.$value;
  5. }
  6. if($_POST['hash'] != md5($str.$secret)) {
  7. echo "Hidden form data modified"; exit;
  8. }
复制代码

5,PHP安全检测工具(XSS和SQL Insertion) Wapiti - Web application security auditor(Wapiti - 小巧的站点漏洞检测工具) (SQL injection/XSS攻击检查工具)

6,安裝/使用方法:

  1. apt-get install libtidy-0.99-0 python-ctypes python-utidylib
  2. python wapiti.py http://Your Website URL/ -m GET_XSS
复制代码

Pixy: XSS and SQLI Scanner for PHP( Pixy - PHP 源码缺陷分析工具) 安裝: apt-get install default-jdk

就介绍这些吧,如果上面介绍的这些安全措施,你都能完全做到的话,你的php代码已经是非常安全了。



Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!