【问题标题】:Unable to open export excel file on hiding response headers using URL Rewrite无法在使用 URL 重写隐藏响应标头时打开导出 excel 文件
【发布时间】:2021-06-18 23:13:53
【问题描述】:

我有一个 export to excel 代码如下:

<%
    Server.ScriptTimeout = 600
    
    'Security check passed, proceed:
    Dim conn, rs, sql, x, outlen
    
    Set conn = Server.CreateObject("ADODB.Connection")
    Set rs = Server.CreateObject("ADODB.Recordset")
    
    sql = "exec " & Request.QueryString("sp") & " "
    For t = 1 To Request.QueryString("p").Count
        'Escape single quotes for SQL:
        If t > 1 Then sql = sql & ", "
        sql = sql & "'" & Replace(Request.QueryString("p").Item(t), "'", "''") & "'"
    Next
    conn.CommandTimeout = 600
    conn.Open strConnString_CyberAgent_WithProvider
    rs.Open sql, conn

    'Prepare the response:
    Response.Buffer = True
    Response.CacheControl = "Private"  
    Response.Expires = 0 
    Response.Clear
    outlen = 0
    
    'Prepare the message headers (Any subsequent response.writes will fall inside the file!)
    Response.AddHeader "Content-Disposition", "attachment; filename=Export.xls"
    Response.ContentType = "application/vnd.ms-excel"
    
    'Output headers:
    For x = 0 To rs.Fields.Count - 1
        If x > 0 Then
            Response.Write(chr(9))
            outlen = outlen + 1
        End If
        Response.Write(rs.Fields.Item(x).Name)
        outlen = outlen + len(rs.Fields.Item(x).Name)
    Next
    Response.Write(chr(13) & chr(10))
    outlen = outlen + 2
    'Loop through records:
    Do While Not rs.EOF
        For x = 0 To rs.Fields.Count - 1
            If x > 0 Then
                Response.Write(chr(9))
                outlen = outlen + 1
            End If
            Response.Write(rs.Fields.Item(x).Value)
            if not(isnull(rs.Fields.Item(x).Value)) then outlen = outlen + len(rs.Fields.Item(x).Value)
        Next
        Response.Write(chr(13) & chr(10))
        outlen = outlen + 2

        rs.MoveNext
    Loop
    rs.Close
    conn.Close 
    
    Set rs = Nothing
    Set conn = Nothing

    Response.AddHeader "Content-Length", outlen

    'Flush the buffer, sending the user the file
    Response.Flush
%>

我能够正常下载报告,但为了缓解安全漏洞,我在 IIS 中使用 URL Rewrite 隐藏响应标头,例如 server nameasp.net version,方法是在 IIS 中添加以下内容我的web.config

<rewrite>
    <outboundRules>
        <rule name="Remove Server">
            <match serverVariable="RESPONSE_SERVER" pattern=".+" />
            <action type="Rewrite" />
        </rule>
        <rule name="Remove Asp.Net Version">
            <match serverVariable="RESPONSE_X-ASPNET-VERSION" pattern=".+" />
            <action type="Rewrite" />
        </rule>
            <rule name="Remove Umbraco Version">
            <match serverVariable="RESPONSE_X-UMBRACO-VERSION" pattern=".+" negate="false" />
            <action type="Rewrite" />
        </rule>
    </outboundRules>
</rewrite>

<customHeaders>
<remove name="Server" />
<remove name="X-AspNet-Version" />
<remove name="X-Powered-By" />
</customHeaders>

问题是添加规则以阻止响应标头导致下载时无法打开 excel 文件。它会抛出一个错误,这让我想知道是否有任何其他方法可以在不干扰导出过程的情况下阻止响应标头。

【问题讨论】:

  • 你能告诉我错误信息吗?
  • @samwu 它说,无法打开文件。不支持的 Excel 格式。但是当我恢复我的web.config 更改时,它会正常打开

标签: asp.net iis url-rewriting httpresponse


【解决方案1】:

禁用 url 重写后能否打开 Excel 文件?如果你想在不使用url重写的情况下屏蔽响应头,你可以尝试以下两种方法。

  1. 使用注册表项。

    在以下注册表项中创建一个名为 DisableServerHeader 的双字条目并将值设置为 1。

    HKLM\SYSTEM\CurrentControlSet\Services\HTTP\Parameters
    

    添加注册表项后,使用 net stop http 命令和 net start http 命令重新启动 HTTP 服务。如果 HTTP 服务没有启动,则使用 iisreset 命令。如果这也不起作用,那么您可以重新启动服务器。

  2. 使用 URLScan 工具。

    在您的机器上安装 URLScan。请点击以下链接:

    https://www.microsoft.com/security/blog/2013/01/22/microsofts-free-security-tools-urlscan-security-tool/

    安装 URLScan 后,打开通常位于 %WINDIR%\System32\Inetsrv\URLscan 文件夹中的 URLScan.ini 文件。打开后,搜索键 RemoveServerHeader 。默认设置为 0,但要删除 Server 标头,请将值更改为 1。

【讨论】:

  • 不,如果我在 web.config 中实现 URL 重写模块规则,excel 文件会抛出错误。
  • 另外,我尝试了第二种方法。但是,似乎 Microsoft 网站上不再提供 URL 扫描工具。该博客引用了此页面上的链接:docs.microsoft.com/en-us/iis/extensions/working-with-urlscan/…,并且他们提到的链接中似乎缺少下载文件。由于安全问题,我无法从未知来源下载。如果您能够找到下载此工具的合法链接,请在您的答案中更新相同的链接。感谢您的帮助
  • 抱歉,微软好像已经从网站上删除了 URLScan 工具,或者你可以尝试第一种方法。
  • 谢谢,但我会避免修改注册表项,因为版本控制系统无法跟踪这种形式的更改,如果将来发生服务器迁移,我们肯定会忘记此更改.
【解决方案2】:

好的,所以我能够阻止除 Server 之外的所有响应标头,我将分享到目前为止对我有用的方法:

X-AspNet-版本:

只需将属性enableVersionHeader="false" 添加到&lt;httpRuntime/&gt;。它应该如下所示:

<system.web>
  <httpRuntime enableVersionHeader="false" />
</system.web>

X-Powered-By:

  1. 打开 IIS
  2. 选择Sites文件夹下的站点名称
  3. IIS 下选择 HTTP 响应标头
  4. 选择X-Powered-By 并删除现有值并添加一个空字符串。

注意:一旦我能够找到一种方法来阻止服务器标头以及此答案,我将进行更新。

【讨论】:

    猜你喜欢
    • 2021-11-14
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多