怎么判断上传的文件是挂马文件呢? mm.asp.jpg是吗 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用UltraEdit打开,看看它是不是一个jpeg图片数据。 如何mm.asp.jpg 是木马,我不知道 他是怎么执行的 要看了文件才知道。有可能这是一个脚本,或者shellcode片段,或者一个执行文件。 这样取名,木马的概率大的很。你的文件名有分号吗?mm.asp;.jpg 这样?如果是的话,就是利用了iis漏洞,这是个上传的大问题。 你要做的不是防止或者说是过滤木马文件,因为过滤不完的,而是不管上传什么文件,都不能让他有破坏作用,比如帐号降低权限,上传的文件不放在iis目录下等等。 这个不好判断。这是杀毒软件要解决的问题。即使你是根据文件内容判断也是靠不住的。比如说前段时间不是有图片文件利用系统的jpeg溢出漏洞来执行代码吗。 检测上传文件的真实类型,不能靠扩展名,例子<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> void Alert(string s) { Page.ClientScript.RegisterStartupScript(Page.GetType(), "js", "alert('" + s + "')", true); } protected void Button1_Click(object sender, EventArgs e) { saveFile(); } protected String saveFile() { String MaxSize = "1024"; //最大文件大小 int imgMaxSize = Convert.ToInt32(MaxSize) * 1024 * 1024; HttpPostedFile imgFile = FuImg.PostedFile; if (imgFile == null || FuImg.FileName == "") { Alert("请选择文件。"); return ""; } String dirPath = Server.MapPath("~/"); string saveUrl = Page.ResolveUrl("~/"); if (!System.IO.Directory.Exists(dirPath)) { Alert("上传目录不存在。"); return ""; } String fileName = imgFile.FileName; String fileExt = System.IO.Path.GetExtension(fileName).ToLower(); if (imgFile.InputStream == null || imgFile.InputStream.Length > imgMaxSize) { Alert("上传文件大小超过限制。"); return ""; } //验证文件格式 String fpath = IsAllowedExtension(imgFile); if ("" == fpath) { Alert("图片格式不正确。"); return ""; } String ymd = DateTime.Now.ToString("yyyyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo); dirPath += ymd + "/"; saveUrl = saveUrl + ymd + "/"; //判断目录是否存在 if (!System.IO.Directory.Exists(dirPath)) { //创建目录 System.IO.Directory.CreateDirectory(dirPath); } String newFileName = Guid.NewGuid().ToString() + fileExt;//图片名字 String filePath = dirPath + newFileName; System.IO.File.Move(fpath, filePath); String fileUrl = saveUrl + newFileName; Img.ImageUrl = fileUrl; //ImageUrl = saveUrl + newFileName; return fileUrl; } public String IsAllowedExtension(HttpPostedFile f) { String newFile = Server.MapPath("~/" + System.Guid.NewGuid().ToString("D") + ".tmp"); f.SaveAs(newFile); System.IO.FileStream fs = new System.IO.FileStream(newFile, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.IO.BinaryReader r = new System.IO.BinaryReader(fs); string fileclass = ""; byte buffer; buffer = r.ReadByte(); fileclass = buffer.ToString(); buffer = r.ReadByte(); fileclass += buffer.ToString(); r.Close(); fs.Close(); /* 文件扩展名说明 *7173 gif *255216 jpg *13780 png *6677 bmp */ Dictionary<String, String> ftype = new Dictionary<string, string>(); //添加允许的文件类型 ftype.Add("7173", "gif"); ftype.Add("255216", "jpg"); ftype.Add("13780", "png"); ftype.Add("6677", "bmp"); if (ftype.ContainsKey(fileclass)) { return newFile; } else { System.IO.File.Delete(newFile); return ""; } }</script><html xmlns="http://www.w3.org/1999/xhtml"><head id="Head1" runat="server"></head><body> <form id="form1" runat="server"> <asp:FileUpload ID="FuImg" runat="server" /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传测试" /> <asp:Image ID="Img" runat="server" /> </form></body></html> c# 检查word文档中是否存在页码、分栏、页眉、页脚 请教读取数据问题。 “找不到请求的 .net Framework 数据提供程序。可能没有安装。” -- 如何解决 C#的两个问题,序列化和COM的 excel 导出 后再导入 问一个sql转换成orcal问题 再开一帖请教,简单的C#调用ORACLE的存储过程,好象应答的人很少啊 ?????200分咨询:C#程序生成的文件在Win98下汉字显示有问题(顶者有分) 请问各位高手 乱码转换问题 不支持关键字database 转化为xml的问题
你的文件名有分号吗?mm.asp;.jpg 这样?如果是的话,就是利用了iis漏洞,这是个上传的大问题。
例子
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Alert(string s)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "js", "alert('" + s + "')", true);
}
protected void Button1_Click(object sender, EventArgs e)
{
saveFile();
}
protected String saveFile()
{
String MaxSize = "1024";
//最大文件大小
int imgMaxSize = Convert.ToInt32(MaxSize) * 1024 * 1024;
HttpPostedFile imgFile = FuImg.PostedFile;
if (imgFile == null || FuImg.FileName == "")
{
Alert("请选择文件。");
return "";
}
String dirPath = Server.MapPath("~/");
string saveUrl = Page.ResolveUrl("~/");
if (!System.IO.Directory.Exists(dirPath))
{
Alert("上传目录不存在。");
return "";
}
String fileName = imgFile.FileName;
String fileExt = System.IO.Path.GetExtension(fileName).ToLower();
if (imgFile.InputStream == null || imgFile.InputStream.Length > imgMaxSize)
{
Alert("上传文件大小超过限制。");
return "";
}
//验证文件格式
String fpath = IsAllowedExtension(imgFile);
if ("" == fpath)
{
Alert("图片格式不正确。");
return "";
}
String ymd = DateTime.Now.ToString("yyyyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo);
dirPath += ymd + "/";
saveUrl = saveUrl + ymd + "/";
//判断目录是否存在
if (!System.IO.Directory.Exists(dirPath))
{
//创建目录
System.IO.Directory.CreateDirectory(dirPath);
}
String newFileName = Guid.NewGuid().ToString() + fileExt;//图片名字
String filePath = dirPath + newFileName;
System.IO.File.Move(fpath, filePath);
String fileUrl = saveUrl + newFileName;
Img.ImageUrl = fileUrl;
//ImageUrl = saveUrl + newFileName;
return fileUrl;
}
public String IsAllowedExtension(HttpPostedFile f)
{
String newFile = Server.MapPath("~/" + System.Guid.NewGuid().ToString("D") + ".tmp");
f.SaveAs(newFile);
System.IO.FileStream fs = new System.IO.FileStream(newFile, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
string fileclass = "";
byte buffer;
buffer = r.ReadByte();
fileclass = buffer.ToString();
buffer = r.ReadByte();
fileclass += buffer.ToString();
r.Close();
fs.Close();
/* 文件扩展名说明
*7173 gif
*255216 jpg
*13780 png
*6677 bmp
*/
Dictionary<String, String> ftype = new Dictionary<string, string>();
//添加允许的文件类型
ftype.Add("7173", "gif");
ftype.Add("255216", "jpg");
ftype.Add("13780", "png");
ftype.Add("6677", "bmp");
if (ftype.ContainsKey(fileclass))
{
return newFile;
}
else
{
System.IO.File.Delete(newFile);
return "";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
</head>
<body>
<form id="form1" runat="server">
<asp:FileUpload ID="FuImg" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传测试" />
<asp:Image ID="Img" runat="server" />
</form>
</body>
</html>