一个直接能落地的“防后门 拦截代码执行”方案。先说结论 最佳方式不是只改一个点而是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_include0、expose_php0。 - 好处简单、立刻生效。 第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.hstatic zend_execute_ex_hook_t old_execute_exNULL;static zend_op_array *(*old_compile_string)(zend_string *source_string, const char *filename, zend_compile_position position)NULL;static int guard_enabled1;/* 简单白名单示例只允许这些脚本路径执行敏感行为 */ 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 i0;allowed_prefixes[i]!NULL;i){size_t nstrlen(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 i0;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_enabledexecute_dataexecute_data-funcZEND_USER_CODE(execute_data-func-type|ZEND_INTERNAL_FUNCTION)){zend_function *funcexecute_data-func;const zend_op_array *opfunc-op_array;const char *fileop-filename ? ZSTR_VAL(op-filename):NULL;if(func-common.function_nameis_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_exzend_execute_ex;zend_execute_exguard_execute_ex;old_compile_stringzend_compile_string;zend_compile_stringguard_compile_string;returnSUCCESS;}PHP_MSHUTDOWN_FUNCTION(guard){zend_execute_exold_execute_ex;zend_compile_stringold_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 加extensionguard.soguard.enabled1--- 五、php.ini 强化模板直接可用;信息隐藏 expose_phpOff display_errorsOff log_errorsOn;禁高危函数运行时层 disable_functionssystem,exec,shell_exec,passthru,proc_open,popen,pcntl_exec,dl,putenv,ini_set;文件访问约束 open_basedir/var/www/app:/tmp allow_url_fopenOff allow_url_includeOff;反序列化相关风险面减少 phar.readonlyOn;会话安全 session.use_strict_mode1session.cookie_httponly1session.cookie_secure1--- 六、系统层必须配不配等于白做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. 持续监控告警拦截次数、来源文件、调用链 这样做不会一上来把业务打挂。