防范要点
- 账号
- 禁止使用类似admin superadmin之类的
- 密码
- 操作系统、数据库、用户
- 定时过期,强制修改
- 强度要求
- 数据库连接字符串加密
- 明文传输
- 任意文件上传
- 上传文件重命名
- 判断文件类型
- 文件后缀名
- 文件头
- 常用文件类型的文件头
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21/// <summary>
/// 常用文件类型的文件头
/// </summary>
public static Dictionary<string, byte[]> FileHeaders = new Dictionary<string, byte[]>
{
{"gif", new byte[] { 71, 73, 70, 56, 57, 97 } },
{"bmp", new byte[] { 66, 77 } },
{"jpg", new byte[] { 255, 216, 255 } },
{"png", new byte[] { 137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82 } },
{"pdf", new byte[] { 37, 80, 68, 70, 45, 49, 46 } },
{"docx", new byte[] { 80, 75, 3, 4, 20, 0, 6, 0, 8, 0, 0, 0, 33 } },
{"xlsx", new byte[] { 80, 75, 3, 4, 20, 0, 6, 0, 8, 0, 0, 0, 33 } } ,
{"pptx", new byte[] { 80, 75, 3, 4, 20, 0, 6, 0, 8, 0, 0, 0, 33 } },
{"doc", new byte[] { 208, 207, 17, 224, 161, 177, 26, 225 } },
{"xls", new byte[] { 208, 207, 17, 224, 161, 177, 26, 225 } },
{"ppt", new byte[] { 208, 207, 17, 224, 161, 177, 26, 225 } },
{"rar", new byte[] { 82, 97, 114, 33, 26, 7 } },
{"zip", new byte[] { 80, 75, 3, 4 } },
{"7z", new byte[] { 55, 122, 188, 175, 39, 28, 0, 4 } },
//{"csv", new byte[] { 49, 44 } },
};- 判断文件头
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61using (var fs = new FileStream(finalPath, FileMode.Create))
{
var index = 0;
foreach (var part in files.OrderBy(x => x.Length).ThenBy(x => x))//排一下序,保证从0-N Write
{
var bytes = System.IO.File.ReadAllBytes(part);
<<<<<<< HEAD
=======
>>>>>>> a8fd0b0b16fb234960e5e99868aebc4812989c23
// 文件头判断
if (index == 0)
{
// 查找是否有匹配的文件头
var isMatch = false;
foreach (var fileHeader in FileHeaders)
{
if (bytes.Length > fileHeader.Value.Length)
{
var currIsMatch = true;
for (int i = 0; i < fileHeader.Value.Length; i++)
{
if (fileHeader.Value[i] != bytes[i])
{
currIsMatch = false;
break;
}
}
<<<<<<< HEAD
=======
>>>>>>> a8fd0b0b16fb234960e5e99868aebc4812989c23
isMatch = currIsMatch;
if (isMatch)
{
break;
}
}
}
<<<<<<< HEAD
=======
>>>>>>> a8fd0b0b16fb234960e5e99868aebc4812989c23
if (!isMatch)
{
result.Message = "抱歉,安全验证文件类型有误,无法上传。";
return Json(result);
}
}
fs.Write(bytes, 0, bytes.Length);
bytes = null;
System.IO.File.Delete(part);//删除分块
<<<<<<< HEAD
=======
>>>>>>> a8fd0b0b16fb234960e5e99868aebc4812989c23
index++;
}
fs.Flush();
fs.Close();
}
- 设置目录不可执行程序
- 信息漏洞泄漏
- 低版本固有漏洞
- 鉴权(垂直)
- Sql注入
- 测试、其他非正式系统关停
- 其他
客户的服务器里面不要存一些密码啥的txt记录,以及里面的远程都不能记住密码。配置文件里的数据库密码链接要加密、服务器的远程密码不能是弱口令、服务器里面的远程软件不能是弱口令(能关就关了)、服务器防火墙必须开