IIS:如何禁用 HTTP 方法 TRACE?

IIS: How to disable HTTP method TRACE?

我按照 this, which led to this 尝试禁用我的网站接受 TRACE 方法(动词)。基本上我将下面的部分添加到 Web.config 内的 <system.webServer>(默认网站和其他网站):

<security>
   <requestFiltering>
       <verbs applyToWebDAV="false">
          <add verb="TRACE" allowed="false" />
       </verbs>
   </requestFiltering>
</security>

没用。然后我转到 C:\Windows\System32\inetsrv\config\applicationHost.config 并替换了 <handlers> 中的所有处理程序动词。简而言之,所有行都是这样的:

<add name="StaticFile" path="*" verb="*" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" resourceType="Either" requireAccess="Read" />

变成了这个:

<add name="StaticFile" path="*" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" resourceType="Either" requireAccess="Read" />

我什至重新启动了服务器,但是当我检查可用方法时,TRACE 仍然存在:

$ curl -I -X OPTIONS https://example.com/mysite
HTTP/1.1 200 OK
Allow: OPTIONS, TRACE, GET, HEAD, POST
Public: OPTIONS, TRACE, GET, HEAD, POST
X-XSS-Protection: 1; mode=block
Date: Thu, 07 Feb 2019 21:03:49 GMT
Content-Length: 0

那么,我该如何摆脱它呢?

要真正阻止 TRACE 请求,您仍应保留带有 TRACE 动词已阻止的请求过滤规则。

curl命令向IIS发送OPTIONS请求,ProtocolSupportModule生成响应信息,

https://docs.microsoft.com/en-us/iis/get-started/introduction-to-iis/iis-modules-overview

但是,Microsoft 并未在 system.webServer/httpProtocol 部分设计任何设置来让您更改 headers 的值以从那里隐藏 TRACE

但解决方法是使用 URL 重写模块,

https://blogs.msdn.microsoft.com/benjaminperkins/2012/11/02/change-or-modify-a-response-header-value-using-url-rewrite/

在其中为 AllowPublic 创建两个出站规则 headers,

<rewrite>
    <outboundRules>
        <rule name="ChangeHeaders" stopProcessing="false">
            <match serverVariable="RESPONSE_Allow" pattern="OPTIONS, TRACE, GET, HEAD, POST" />
            <action type="Rewrite" value="OPTIONS, GET, HEAD, POST" />
        </rule>
        <rule name="Public">
            <match serverVariable="RESPONSE_Public" pattern="OPTIONS, TRACE, GET, HEAD, POST" />
            <action type="Rewrite" value="OPTIONS, GET, HEAD, POST" />
        </rule>
    </outboundRules>
</rewrite>

在我的案例中有效:

 <requestFiltering>
      <verbs allowUnlisted="false">
        <clear/>
        <add verb="GET" allowed="true" />
        <add verb="HEAD" allowed="true" />
        <add verb="POST" allowed="true" />
      </verbs>
    </requestFiltering>

请注意我不允许选项。 当我不允许 OPTIONS 时,TRACE 也不被允许。