从服务结构上的自托管 owin webapi 中删除服务器标头

Ach*_* P. 5 header owin asp.net-web-api2 server service-fabric-stateless

我一直在尝试摆脱在我的自托管 webapi2 应用程序上返回“Microsoft-HTTPAPI/2.0”的“服务器”标头,该应用程序作为 azure 服务结构上的无状态服务托管。我尝试过但没有成功的事情:

我尝试实现 IFilter 以从 webapi 服务中删除标头,但调试应用程序表明此时标头不存在可删除的位置。

还尝试替换导致将新值附加到“Microsoft-HTTPAPI/2.0”值的值。

我尝试在 app.config 文件中设置网络服务器标志(有点像将其用作 web.config),但没有成功。

我尝试覆盖 OWIN 管道的 OnSerdingHeaders 事件,但“Server”标头无法删除,它显然是在稍后阶段添加的。

我尝试了在网上找到的有关使用应用程序生成器在代码中清除服务器的所有建议。

我尝试实现一个自定义委托处理程序来清除/覆盖标头,但也没有运气。

我什至尝试篡改调试计算机上的注册表以进行检查,但也没有成功。

我的渗透测试人员坚持要摆脱它。还有别的办法吗?

提前致谢!

MaM*_*zav 4

以下中间件解决了这个问题:

public class RemoveServerHeaderMiddleware
{
    private readonly RequestDelegate next;

    public RemoveServerHeaderMiddleware(RequestDelegate next)
    {
        this.next = Argument.NotNull(next, nameof(next));
    }

    public async Task Invoke(HttpContext context)
    {
        context.Response.OnStarting(() =>
        {
            context.Response.Headers.Add("Server", string.Empty);

            return Task.CompletedTask;
        });

        await this.next(context).ConfigureAwait(false);
    }
}
Run Code Online (Sandbox Code Playgroud)

当然,不要忘记注册中间件:

app.UseMiddleware<RemoveServerHeaderMiddleware>();
Run Code Online (Sandbox Code Playgroud)

该解决方案基于以下链接:

https://github.com/aspnet/HttpSysServer/issues/445

https://blogs.msdn.microsoft.com/timomta/2016/12/03/how-to-remove-the-server-header-from-weblistener/