日期:2014-05-18  浏览次数:20397 次

帮忙检查下这段代码有没有漏洞 谢谢
这段上传代码有没有漏洞 上传文件的大小 在WEBCONFIG的<httpRuntime>节点限制了 那下面这段上传代码还有什么漏洞吗?
 private void upl()
  {
  try
  {
  if (FileUpload1.PostedFile.FileName == "")
  {
  this.Label1.Text = "请选择文件!";
  }
  else
  {
  string filepath = FileUpload1.PostedFile.FileName;
  if (IsAllowedExtension(FileUpload1) == true)
  {
  string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
  string serverpath = Server.MapPath("aaa/author/") + filename;
  FileUpload1.PostedFile.SaveAs(serverpath);
  this.Label1.Text = "上传成功!";
  }
  else
  {
  this.Label1.Text = "请上传图片";
  }
  }
  }
  catch (Exception error)
  {
  this.Label1.Text = "上传发生错误!原因:" + error.ToString();
  }
  }


  public static bool IsAllowedExtension(FileUpload hifile)
  {
  System.IO.FileStream fs = new System.IO.FileStream(hifile.PostedFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
  System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
  string fileclass = "";
  byte buffer;
  try
  {
  buffer = r.ReadByte();
  fileclass = buffer.ToString();
  buffer = r.ReadByte();
  fileclass += buffer.ToString();

  }
  catch
  {

  }
  r.Close();
  fs.Close();
  if (fileclass == "255216" || fileclass == "7173")//说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar 
  {
  return true;
  }
  else
  {
  return false;
  }

  }

------解决方案--------------------
取filename的时候可以用Path.GetFileName(filepath);
不用去substring截取了.麻烦
------解决方案--------------------
catch (Exception error) 

this.Label1.Text = "上传发生错误!原因:" + error.ToString(); 



这种把error直接暴露出来好不好哟?有安全问题么?
讨论下

在web.config中进行的设置是针对未捕获的异常,这种已捕获的异常会不会泄露程序信息?
我先试试
------解决方案--------------------
看不出来
------解决方案--------------------
控件命名不规范。