繁体   English   中英

如何摆脱asp.net中的路径遍历问题?

[英]How to get rid of path traversal issues in asp.net?

该应用程序允许用户输入来控制或影响文件系统操作中使用的路径或文件名。 此信息可进一步用于攻击可能导致敏感数据泄漏和利用的应用程序。

如何在处理之前对用户输入执行验证和清理。?

为了避免路径遍历,你可以,

  1. 为用户输入制定白名单(或限制),例如只允许用户输入特定的文件名,不包括“/”或“\\”等字符。

  2. 为系统文件夹配置适当的权限,例如,将您允许用户访问的资源文件放在特定文件夹中,并且只为程序帐户授予该文件夹的权限。 在这种情况下,系统会帮助您防止路径遍历攻击。

为了搜索者的利益,我已经制定了一个合理的起点来确定是否存在对文件的遍历尝试。 这假定文件名类似于“somefile.pdf”,而不是“c:/tmp/somefile.pdf”。

/// <summary>
/// This is to guage whether someone is trying a directory traversal attack.
/// I.e. they should be requesting 'somefilename.pdf'
/// but they request '../anotherlocation/someotherfilename.pdf
/// </summary>
/// <param name="fileName">The file name to check</param>
/// <returns></returns>
public bool IsDirectoryTraversing(string fileName)
{
    bool isTraversing = false;

    if(String.IsNullOrWhiteSpace(fileName))
    {
        return isTraversing;
    }

    // Url decode to reveal sneaky encoded attempts e.g. '%2f' (/) or '%2e%2e%2f' (../)
    var decodedFileName = HttpUtility.UrlDecode(fileName);

    if(decodedFileName.Contains("/") || 
        decodedFileName.Contains(@"\") ||
        decodedFileName.Contains("$") ||
        decodedFileName.Contains("..") ||
        decodedFileName.Contains("?"))
    {
        isTraversing = true;
    }

    return isTraversing;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM