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
http://www.bkjia.com/phpjc/327405.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/327405.htmltecharticlephp代码安全和xss,sql注入等对于各类网站的安全非常中用,尤其是ugc(user generated content)网站,论坛和电子商务网站,常常是xss和sql注入的重...