在IIS中运行PHP时,默认配置会暴露版本号并启用大量危险函数。直接通过修改php.ini中的expose_php为Off隐藏版本信息,并使用disable_functions禁用exec、system等高危函数即可实现基础安全加固。

php.ini核心安全参数调整
打开PHP安装目录下的php.ini文件(或IIS PHP Manager生成的配置文件),定位到expose_php和disable_functions两项配置。

将expose_php = On改为expose_php = Off,防止HTTP响应头泄露PHP版本。接着找到disable_functions,追加exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source。注意各函数间用逗号分隔,无空格。保存后重启IIS服务使配置生效。
验证配置与安全测试脚本
创建一个名为test_security.php的文件放在网站根目录,用于验证配置是否生效。

<?php
// test_security.php
// 验证PHP安全配置是否生效
echo "<h2>安全配置检查结果</h2>";
// 1. 检查版本号是否隐藏
$headers = @get_headers('http://localhost/' . basename(__FILE__));
$version_exposed = false;
if ($headers) {
foreach ($headers as $header) {
if (stripos($header, 'X-Powered-By') !== false && strpos($header, 'PHP') !== false) {
$version_exposed = true;
break;
}
}
}
echo "<p>版本号隐藏状态: " . ($version_exposed ? '<strong style="color:red">未隐藏 (失败)</strong>' : '<strong style="color:green">已隐藏 (成功)</strong>') . "</p>";
// 2. 检查危险函数是否禁用
$dangerous_funcs = ['exec', 'system', 'passthru', 'shell_exec', 'proc_open'];
$disabled_count = 0;
foreach ($dangerous_funcs as $func) {
if (function_exists($func)) {
echo "<p>函数 <code>$func</code>: <strong style="color:red">未被禁用 (风险)</strong></p>";
} else {
$disabled_count++;
echo "<p>函数 <code>$func</code>: <strong style="color:green">已禁用 (安全)</strong></p>";
}
}
echo "<p>总计检查 " . count($dangerous_funcs) . " 个危险函数,已禁用 <code>$disabled_count</code> 个。</p>";
运行脚本后,若X-Powered-By头不再出现且所有列出的危险函数显示“已禁用”,则说明配置正确。若发现某个函数未被禁用,请检查php.ini中disable_functions列表是否拼写错误或未重启IIS。对于curl类函数,若业务需要,可单独从列表中移除并加强输入过滤。
该脚本检查关键函数是否被禁用,并尝试获取PHP版本以确认隐藏效果。确保IIS应用池身份具有读取权限,通过浏览器访问该文件查看输出结果。
disable_functions对自定义扩展的影响排查
若业务依赖某些被禁用的函数(如curl相关功能),需评估替代方案或调整策略。
当disable_functions包含curl_exec时,基于libcurl的PHP扩展可能失效。此时可考虑使用file_get_contents配合stream_context实现HTTP请求,或在php.ini中移除特定函数但加强输入校验。务必测试所有涉及网络IO的代码路径,避免因函数缺失导致运行时错误。