如何防止用户直接访问网站根目录下的文件

How to prevent users from accessing files directly in the website root Directory

我正在使用具有以下设置的 经典 asp 网站:

我正在处理两个安全问题。

1) 用户当前可以通过输入“http://MyWebsite.com/test.txt”来访问文件(txt、pdf)。如何防止用户以这种方式访问​​非 asp 文件?

2) 有几个文件夹(例如上传)需要应用程序具有完全访问权限。但同样,用户不应该能够键入物理路径来访问这些文件夹下的此类文件。我该如何设置?

在某种程度上,我想创建一个 IIS URL 规则重写,它只显示其中包含 .asp 页面的文件。所以我可以 http://LocalHost/DisplayPDF.asp?ThePDF and be able to view the PDF but I want to prevent the user to go and enter http://LocalHost/ThePdf.pdf

我的猜测是我需要正确配置 IIS。任何建议表示赞赏。

要阻止用户访问非 asp 文件,您需要将这些文件从根目录移动到可访问的 Web 文件夹之外的文件夹。

然后您可以将具有不同安全策略的虚拟目录映射到该新文件夹。或者,如果您想间接访问这些文件,您需要一个页面,然后允许他们在需要时下载或显示内容。

对于上传文件夹,您应该能够在不属于网站的代码中映射文件夹,并将代码上传到它们。

我会通过为根目录文件创建 url 重写规则(以消除与继承相关的问题)并删除子目录的静态文件处理程序来使其工作。

将以下 web.config 放在应用程序的根目录中或相应地修改现有规则,如果还有其他规则,请不要忘记将规则移动到适当的位置。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="ROOT_FILE_CHECK">
                        <match url="^[^/]*$" />
                        <conditions>
                            <add input="{DOCUMENT_ROOT}\{R:0}" matchType="IsFile" />

                            <!-- allowed extensions -->
                            <add input="{REQUEST_FILENAME}" pattern="\.asp$" negate="true" />
                            <add input="{REQUEST_FILENAME}" pattern="\.allowed1$" negate="true" />
                            <add input="{REQUEST_FILENAME}" pattern="\.allowed2$" negate="true" />
                            <!-- allowed extensions -->
                        </conditions>
                        <action type="CustomResponse" statusCode="404" statusReason="Not Found" statusDescription="." />
                    </rule>
                </rules>
            </rewrite>
        </system.webServer>
</configuration>

并在您要阻止访问文件的每个子目录中放置以下 web.config。这个删除了静态文件处理程序,因此该目录中的静态文件变得不可访问。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <remove name="StaticFile" />
        </handlers>
    </system.webServer>
</configuration>

您可以从 IIS 中删除 MIME 类型,这将禁止 IIS 提供您不需要的任何文件类型。

https://technet.microsoft.com/en-us/library/cc770504(v=ws.10).aspx