因为有些脚本小子会注意到俺的网站,向网站文件夹里面搞一些乱七八糟的东西,或者传个木马上来,如果这样的话,俺会丢饭碗,会破产,所以俺用递归的方法写了个功能,来看看文件有没有给别人改动了,或者有不该存在的文件给传上来了,加密算法不写,因为怕人破解,只写遍历文件夹:
public static void SetAllSafeCode(string path)
{
path += "\\";
//if there is no Directories, only files exists
if (Directory.GetDirectories(path).Length == 0)
{
//check all files of this path
foreach (string name in Directory.GetFiles(path))
{
//initialize an safe string object
SafeString ss = new SafeString();
//set property of this object
ss.FileName = name.Substring(path.Length);
ss.FilePath = name;
ss.Code = GetSafeCode(GetFileContent(name));
//store this object into list
ssList.Add(ss);
}
return;
}
else//if this path contains Directory
{
//check all directory of this path
foreach (string name in Directory.GetDirectories(path))
{
SetAllSafeCode(name);//Recursion 递归
}
//check all files of this path
foreach (string name in Directory.GetFiles(path))
{
SafeString ss = new SafeString();
ss.FileName = name.Substring(path.Length);
ss.FilePath = name;
ss.Code = GetSafeCode(GetFileContent(name));
ssList.Add(ss);
}
}
}
public static void SetAllSafeCode(string path)
{
path += "\\";
//if there is no Directories, only files exists
if (Directory.GetDirectories(path).Length == 0)
{
//check all files of this path
foreach (string name in Directory.GetFiles(path))
{
//initialize an safe string object
SafeString ss = new SafeString();
//set property of this object
ss.FileName = name.Substring(path.Length);
ss.FilePath = name;
ss.Code = GetSafeCode(GetFileContent(name));
//store this object into list
ssList.Add(ss);
}
return;
}
else//if this path contains Directory
{
//check all directory of this path
foreach (string name in Directory.GetDirectories(path))
{
SetAllSafeCode(name);//Recursion 递归
}
//check all files of this path
foreach (string name in Directory.GetFiles(path))
{
SafeString ss = new SafeString();
ss.FileName = name.Substring(path.Length);
ss.FilePath = name;
ss.Code = GetSafeCode(GetFileContent(name));
ssList.Add(ss);
}
}
}
本文介绍了一种使用递归方法检查网站文件完整性的方案,通过遍历文件夹并验证每个文件的安全码,确保没有未经授权的更改或恶意文件上传。
1513

被折叠的 条评论
为什么被折叠?



