小林大模型|大模型面试高频知识点合集2
2026/4/29 1:41:22
一个直接能落地的“防后门 + 拦截代码执行”方案。先说结论: 最佳方式不是只改一个点,而是4层一起上:编译裁剪层 + 内核拦截层 + 运行时策略层 + 系统隔离层。 单点防护一定会漏。 --- 一、先讲大白话:后门最常走哪几条路 PHP 后门常见入口就这些:1. eval/assert/create_function 这类“动态执行字符串”2. system/exec/shell_exec/passthru/proc_open/popen 这类命令执行3. include/require 远程或可控路径加载4. 反序列化 + 魔术方法链触发执行5. 扩展函数(FFI、dl)绕过普通限制 所以你要做的是:把“能执行代码/命令”的路,默认全封,再按白名单开小口。 --- 二、最佳加固架构(生产推荐) 第1层:编译期裁剪(最硬) - 直接在构建时关掉高风险能力(比如 --with-ffi 不开,--enable-dl 不开)。 - 好处:运行时连“可利用面”都没有。 第2层:内核/扩展级拦截(最关键) - 用 zend_execute_ex + zend_compile_string hook。 - 在“函数调用点”和“动态编译点”拦截敏感行为。 - 好处:比 disable_functions 更难绕。 第3层:php.ini 策略(必须) - disable_functions、open_basedir、allow_url_include=0、expose_php=0。 - 好处:简单、立刻生效。 第4层:OS 沙箱隔离(兜底) - PHP-FPM 用独立用户、最小权限、容器/AppArmor/SELinux/seccomp。 - 好处:就算 Web 层漏了,也难拿到系统权限。 --- 三、完整代码(内核级拦截扩展示例) 下面给你一份可编译的 PHP 扩展模板,做两件事:1. 拦截危险函数调用(system/exec/eval/...)2. 拦截 eval()/动态字符串编译(zend_compile_string) ▎ 文件:php_guard.c#ifdef HAVE_CONFIG_H#include "config.h"#endif#include "php.h"#include "zend_compile.h"#include "zend_extensions.h"#include "ext/standard/info.h"static zend_execute_ex_hook_t old_execute_ex=NULL;static zend_op_array *(*old_compile_string)(zend_string *source_string, const char *filename, zend_compile_position position)=NULL;static int guard_enabled=1;/* 简单白名单示例:只允许这些脚本路径执行敏感行为 */ static const char *allowed_prefixes[]={"/var/www/app/safe_jobs/","/var/www/app/cli_tools/", NULL};static int path_allowed(const char *path){if(!path)return0;for(int i=0;allowed_prefixes[i]!=NULL;i++){size_t n=strlen(allowed_prefixes[i]);if(strncmp(path, allowed_prefixes[i], n)==0){return1;}}return0;}static int is_dangerous_function(const zend_string *name){if(!name)return0;static const char *deny[]={"system","exec","shell_exec","passthru","proc_open","popen","pcntl_exec","assert","create_function","putenv","dl", NULL};for(int i=0;deny[i]!=NULL;i++){if(zend_string_equals_literal_ci(name, deny[i])){return1;}}return0;}static void guard_execute_ex(zend_execute_data *execute_data){if(guard_enabled&&execute_data&&execute_data->func&&ZEND_USER_CODE(execute_data->func->type|ZEND_INTERNAL_FUNCTION)){zend_function *func=execute_data->func;const zend_op_array *op=&func->op_array;const char *file=op->filename ? ZSTR_VAL(op->filename):NULL;if(func->common.function_name&&is_dangerous_function(func->common.function_name)){if(!path_allowed(file)){php_error_docref(NULL, E_WARNING,"[guard] blocked dangerous function: %s in %s", ZSTR_VAL(func->common.function_name),file?file:"(unknown)");zend_throw_error(NULL,"Security policy violation");return;}}}old_execute_ex(execute_data);}static zend_op_array *guard_compile_string(zend_string *source_string, const char *filename, zend_compile_position position){if(guard_enabled){php_error_docref(NULL, E_WARNING,"[guard] blocked dynamic code compile (eval)");zend_throw_error(NULL,"Dynamic code execution is forbidden by security policy");returnNULL;}returnold_compile_string(source_string, filename, position);}PHP_INI_BEGIN()STD_PHP_INI_BOOLEAN("guard.enabled","1", PHP_INI_SYSTEM, OnUpdateBool, guard_enabled, zend_guard_globals, guard_globals)PHP_INI_END()PHP_MINIT_FUNCTION(guard){REGISTER_INI_ENTRIES();old_execute_ex=zend_execute_ex;zend_execute_ex=guard_execute_ex;old_compile_string=zend_compile_string;zend_compile_string=guard_compile_string;returnSUCCESS;}PHP_MSHUTDOWN_FUNCTION(guard){zend_execute_ex=old_execute_ex;zend_compile_string=old_compile_string;UNREGISTER_INI_ENTRIES();returnSUCCESS;}PHP_MINFO_FUNCTION(guard){php_info_print_table_start();php_info_print_table_header(2,"guard support","enabled");php_info_print_table_row(2,"policy","block dangerous functions + eval");php_info_print_table_end();}zend_module_entry guard_module_entry={STANDARD_MODULE_HEADER,"guard", NULL, PHP_MINIT(guard), PHP_MSHUTDOWN(guard), NULL, NULL, PHP_MINFO(guard),"0.1.0", STANDARD_MODULE_PROPERTIES};#ifdef COMPILE_DL_GUARD# ifdef ZTSZEND_TSRMLS_CACHE_DEFINE()# endifZEND_GET_MODULE(guard)#endif--- 四、扩展构建文件(完整) ▎ 文件:config.m4 PHP_ARG_ENABLE(guard, whether toenableguard extension,[--enable-guard Enable guard extension], no)iftest"$PHP_GUARD"!="no";thenPHP_NEW_EXTENSION(guard, php_guard.c,$ext_shared)fi构建安装: phpize ./configure --enable-guardmake-j$(nproc)makeinstallphp.ini 加:extension=guard.soguard.enabled=1--- 五、php.ini 强化模板(直接可用);信息隐藏 expose_php=Off display_errors=Off log_errors=On;禁高危函数(运行时层) disable_functions=system,exec,shell_exec,passthru,proc_open,popen,pcntl_exec,dl,putenv,ini_set;文件访问约束 open_basedir=/var/www/app:/tmp allow_url_fopen=Off allow_url_include=Off;反序列化相关风险面减少 phar.readonly=On;会话安全 session.use_strict_mode=1session.cookie_httponly=1session.cookie_secure=1--- 六、系统层必须配(不配等于白做)1. www-data 这类低权限用户运行 FPM2. 网站目录只读,上传目录单独挂载且不可执行3. noexec,nodev,nosuid 挂载策略4. 容器/主机上配 AppArmor 或 SELinux 策略5. 出网最小化(很多后门要回连 C2) --- 七、最佳方式一句话版 生产最优解:自定义 guard 扩展做执行拦截 + php.ini 收口 + 构建裁剪 + OS 沙箱隔离。 只靠 disable_functions 不够,只改内核不做系统隔离也不够。 --- 八、上线顺序(避免误杀)1. 先“审计模式”只记录不拦截3-7 天2. 统计真实业务调用3. 调整白名单4. 再切“阻断模式”5. 持续监控告警(拦截次数、来源文件、调用链) 这样做不会一上来把业务打挂。