php安全小记(一)
总结一下php.ini中常用安全配置和常见的可能引发安全问题的函数
php.ini
php.ini中有一些重要的参数,若配置不当则有可能引发安全问题,在5.4之前又许多参数在5.4之后被移除,因为都更新到php7,所以这里就不写那些被移除的参数了
指令 | 说明 | 可配置范围 |
---|---|---|
allow_url_include | (是否允许远程包含文件) | PHP_INI_ALL |
allow_url_fopen | (是否允许打开远程文件) | php.ini only |
open_basedir | (可访问目录) | PHP_INI_ALL |
disable_functions | (禁用函数) | php.ini only |
display_errors 和 error_reporting | (错误显示) | PHP_INI_ALL |
expose_php | (是否在服务器返回信息HTTP头显示PHP版本) | php.ini only |
max_execution_time | (每个脚本最多执行秒数) | PHP_INI_ALL |
memory_limit | (每个脚本能够使用的最大内存数量) | PHP_INI_ALL |
log_errors | (将错误输入到日志文件) | PHP_INI_ALL |
log_errors_max_len | (设定log_errors的最大长度) | PHP_INI_ALL |
variables_order | (此指令描述了php注册GET,POST,Cookie,环境和内置变量的顺序,注册使用从左往右的顺序,新的值会覆盖旧的值) | PHP_INI_PERDIR |
post_max_size | (PHP可以接受的最大的POST数据大小) | PHP_INI_PERDIR |
auto_prepend_file | (在任何PHP文件之前自动包含的文件) | PHP_INI_PERDIR |
auto_append_file | (在任何PHP文档之后自动包含的文件) | PHP_INI_PERDIR |
extension_dir | (可加载的扩展(模块)的目录位置) | PHP_INI_SYSTEM |
file_uploads | (是否允许HTTP文件上传) | PHP_INI_SYSTEM |
upload_tmp_dir | (对于HTTP上传文件的临时文件目录) | PHP_INI_SYSTEM |
upload_max_filesize | (允许上传的最大文件大小) | PHP_INI_SYSTEM |
session.cookie_secure | (仅允许在 HTTPS 协议下访问会话 ID cookie) | PHP_INI_ALL |
session.cookie_httponly | (禁止 JavaScript 访问会话 cookie) | PHP_INI_ALL |
session.gc_maxlifetime | (设置会话过期时间) | PHP_INI_ALL |
session.save_path | (存储会话数据的路径) | PHP_INI_ALL |
注:
PHP_INI_ALL 可在任何地方设定
PHP_INI_PERDIR 可在 php.ini*,.htaccess* 或 httpd.conf 中设定
PHP_INI_SYSTEM 可在 php.ini 或 httpd.conf 中设定
命令执行
代码执行
- eval()
- assert()
- preg_replace()+’/e’ (PHP5.5废弃)
- call_user_func()
- call_user_func_array()
- create_function()
- array_map()
命令执行
- system()
- passthru()
- exec()
- pcntl_exec()
- shell_exec()
- popen()
- proc_open()
- ``(反引号)
- ob_start()
文件操作
文件上传
- move_uploaded_file()
- getimagesize() (验证文件头只要为Gif89a就返回真)
文件删除
- unlink()
文件包含
- require()
- include()
- require_once()
- include_once()
文件读取
- highlight_file($filename)
- show_source($filename)
- hightlight_file($filename)
- show_source($filename)
- print_r(php_strip_whitespace($filename))
- print_r(file_get_contents($filename))
- readfile($filename)
- print_r(file($filename))
- fread(fopen(filename,”r”),size)
- include($filename); // 非php代码
- include_once($filename); // 非php代码
- require($filename); // 非php代码
- require_once($filename); // 非php代码
- print_r(fread(popen(“cat flag”, “r”), $size))
- print_r(fgets(fopen($filename, “r”))); // 读取一行
- fpassthru(fopen($filename, “r”)); // 从当前位置一直读取到 EOF
- print_r(fgetcsv(fopen(filename,”r”),size));
- print_r(fgetss(fopen($filename, “r”))); // 从文件指针中读取一行并过滤掉 HTML 标记
- print_r(fscanf(fopen(“flag”, “r”),”%s”));
- print_r(parse_ini_file($filename)); // 失败时返回 false , 成功返回配置数组
读取目录
- print_r(glob(“*”)); // 列当前目录
- print_r(glob(“/*”)); // 列根目录
- print_r(scandir(“.”));
- $d=opendir(“.”);while(false!==($f=readdir($d))){echo”$f\n”;}
- $d=dir(“.”);while(false!==($f=$d->read())){echo $f.”\n”;}
变量覆盖
- extract()
- $$
- import_request_variables()
- parse_str()
- mb_parse_str()
- 全局变量覆盖:register_globals为ON,$GLOBALS
- Post Title: php安全小记(一)
- Post Author: Katharsis
- Post Link: http://yoursite.com/2020/10/08/php-security/
- Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.