目录遍历问题c#

时间:2013-07-01 20:09:23

标签: c# asp.net security testing veracode

我有一个C#应用程序,我上传图像并将其存储在目录中。我正在检查文件,以便在上传之前允许一种图像格式。当我使用Veracode测试我的模块时,它显示目录遍历问题CWE ID 73 ..我不确定它是否正确,因为我在上传前检查文件类型

我上传文件的代码在

下面
if (filebigimage.HasFile)
{
   if ((((((this.filebigimage.PostedFile.ContentType == "image/gif") | (this.filebigimage.PostedFile.ContentType == "image/pjpeg")) | (this.filebigimage.PostedFile.ContentType == "image/jpeg")) | (this.filebigimage.PostedFile.ContentType == "image/jpg")) | (this.filebigimage.PostedFile.ContentType == "image/png")) | (this.filebigimage.PostedFile.ContentType == "image/bmp"))
                        {
                           // string StrBigImageName = (ConfigurationManager.AppSettings["offerImagePath"] + "BigImages/Temp/");
                            if ((this.filebigimage.PostedFile.ContentType == "image/gif"))
                            {
                                strBigimgformat = ".gif";
                                objBigImgFormat = System.Drawing.Imaging.ImageFormat.Gif; 
                            }
                            else if ((this.filebigimage.PostedFile.ContentType == "image/pjpeg") | (this.filebigimage.PostedFile.ContentType == "image/jpeg") | (this.filebigimage.PostedFile.ContentType == "image/jpg"))
                            {
                                strBigimgformat = ".jpg";
                                objBigImgFormat = System.Drawing.Imaging.ImageFormat.Jpeg;

                            }
                            else if ((this.filebigimage.PostedFile.ContentType == "image/png"))
                            {
                                strBigimgformat = ".png";
                                objBigImgFormat = System.Drawing.Imaging.ImageFormat.Png; 
                            }
                            filebigimage.SaveAs(Server.MapPath(ConfigurationManager.AppSettings["offerImagePath"] + "BigImages/Temp/") + filebigimage.FileName);
                            strbigimagename = filebigimage.FileName;
                        } 
}

然后我将值存储到数据库中并获取行的id号并创建文件名并将此文件从temp移动到主文件夹

if (File.Exists(Server.MapPath(ConfigurationManager.AppSettings["offerImagePath"]
+ "BigImages/Temp/" + strbigimagename)))
{
   File.Move(Server.MapPath("../" + ConfigurationManager.AppSettings["offerImagePath"] + "BigImages/Temp/"
+ strbigimagename), Server.MapPath("../" + ConfigurationManager.AppSettings["offerImagePath"] + "BigImages/" + strorderid + "_bigimage" + strBigimgformat));
}

请让我知道它有什么问题......是否容易受到目录遍历问题的影响

1 个答案:

答案 0 :(得分:0)

看起来您正在使用用户输入(filebigimage.FileName)作为服务器上文件名的一部分。这是一种安全风险。您应该提出一些其他方法来命名文件。