IIS在服务器级异常时终止连接的STIG合规配置方法问询
嘿,先把你提到的STIG相关规则内容整理出来:
The web server defines a set of exceptions for every HTTP status code. Each exception class has a status code according to RFC 2068: Codes with 100-300 are not really errors; 400s are client errors, and 500s are server errors. If not directly specified, headers will be added to the default response headers.
In the event of an anomaly or exception during the processing of requests, it is safer to terminate the connection to prevent malformed requests from exploiting potential protocol vulnerabilities.
Verify the web server terminates the connection if server-level exceptions are triggered when handling requests.
If the web server does not terminate the connection if server-level exceptions are triggered when handling requests, this is a finding.
针对这个要求,在IIS里可以通过以下几种方式配置:
一、通过IIS图形界面可视化配置
- 打开IIS管理器,定位到你需要配置的目标网站,双击右侧功能栏里的【错误页】
- 在错误页界面,点击右上角的【编辑功能设置】,选择【自定义错误页】(确保我们能自定义错误响应行为)
- 找到所有5xx开头的服务器错误(比如500、502、503等),逐个选中后点击【编辑】
- 在弹出的编辑对话框中,响应操作选择“终止连接”,如果需要自定义错误页面,可以同时指定本地路径或URL,关键是要确保连接在响应后被终止
二、通过web.config文件精准配置(推荐用于批量部署或版本控制)
直接修改网站根目录下的web.config文件,添加或修改以下配置节点,确保服务器错误响应时发送Connection: close头并终止连接:
<system.webServer> <!-- 自定义5xx错误页并设置响应模式 --> <httpErrors errorMode="Custom"> <remove statusCode="500" subStatusCode="-1" /> <error statusCode="500" subStatusCode="-1" path="/custom-500.html" responseMode="File" /> <!-- 可以根据需要添加其他5xx错误的配置 --> <remove statusCode="503" subStatusCode="-1" /> <error statusCode="503" subStatusCode="-1" path="/custom-503.html" responseMode="File" /> </httpErrors> <!-- 添加Connection: close头,确保响应后终止连接 --> <httpProtocol> <customHeaders> <add name="Connection" value="close" /> </customHeaders> </httpProtocol> </system.webServer>
注意:如果只需要针对5xx错误添加这个头,可以结合URL重写规则来实现更精准的控制,避免影响正常的成功响应。
三、通过命令行(appcmd)自动化配置
如果你需要批量配置或者用脚本自动化处理,可以打开管理员命令提示符,执行以下命令:
- 为目标网站添加
Connection: close自定义头:
appcmd set config "你的网站名称" /section:httpProtocol /customHeaders."Connection":close
- 配置特定5xx错误的响应模式为文件(并关联自定义错误页):
appcmd set config "你的网站名称" /section:httpErrors /+"[statusCode='500',subStatusCode='-1',path='/custom-500.html',responseMode='File']"
验证配置是否符合STIG要求
配置完成后,你可以通过以下步骤验证:
- 手动触发一个服务器级异常(比如在ASP.NET页面中抛出未处理的异常,或者配置一个不存在的FastCGI处理器)
- 使用curl命令行工具测试:
curl -v http://你的网站域名/触发异常的路径,查看输出中是否包含Connection: close响应头,并且连接在响应后被断开 - 或者用网络抓包工具(如Wireshark)查看,确认服务器在返回5xx错误后主动关闭TCP连接
备注:内容来源于stack exchange,提问作者DerHaifisch




