有恶意请求,增加恶意请求的拦截

如题
This commit is contained in:
tianshuanbao
2025-12-19 16:58:44 +08:00
parent fe9a71d896
commit 7963156155
13 changed files with 637 additions and 446 deletions

View File

@@ -94,6 +94,7 @@ namespace WebSite
}
protected override void Application_Start(object sender, EventArgs e)
{
logger.Error("Web重启了");
// 在应用程序启动时调用
PreHot();
log4net.Config.XmlConfigurator.Configure();//初始化log4net
@@ -279,16 +280,17 @@ namespace WebSite
{
try
{
hostServer.Close();
System.Threading.Thread.Sleep(5000);
System.Net.WebRequest request = System.Net.WebRequest.Create(currentUrl);
System.Net.WebResponse response = request.GetResponse();
logger.Error("系统重启Web服务");
response.Close();
//hostServer.Close();
//System.Threading.Thread.Sleep(5000);
//System.Net.WebRequest request = System.Net.WebRequest.Create(currentUrl);
//System.Net.WebResponse response = request.GetResponse();
//logger.Error("系统重启Web服务");
//response.Close();
}
catch (Exception)
catch (Exception ex)
{
logger.Error("重启原因为:" + ex.Message);
logger.Error(ex.StackTrace);
}
}
@@ -696,10 +698,10 @@ namespace WebSite
_client.UpdateCheckInOrOutRecord(dr["Code"].ToString(), Convert.ToInt64(dr["ID"]), flag);//更新已同步状态
string RoomNUM = dr["RoomNumber"].ToString();
var T = new Tuple<int, string,RoomStatus>(hotelID,RoomNUM,roomStatus);
var T = new Tuple<int, string, RoomStatus>(hotelID, RoomNUM, roomStatus);
Task.Factory.StartNew((state) =>
{
var T1 = state as Tuple<int, string,RoomStatus>;
var T1 = state as Tuple<int, string, RoomStatus>;
Host host = HostManager.GetByRoomNumber(T1.Item2, T1.Item1);
if (host != null)
{
@@ -711,7 +713,7 @@ namespace WebSite
ddd.Status = T1.Item3;
CSRedisCacheHelper.Set_PartitionWithForever<RoomStatusRequest>(Key, ddd);
}
},T);
}, T);
}
/// <summary>
/// 定时上报主机异常记录
@@ -737,13 +739,119 @@ namespace WebSite
resp.status = 2;
resp.faultType = 1;
resp.faultData = 1;
XuanZhuOperation.ReportService(hotel.FaultPushURL, resp);
//XuanZhuOperation.ReportService(hotel.FaultPushURL, resp);
MyHttp.SendHttpData(hotel.FaultPushURL, resp);
}
}
logger.Error(string.Format("定时上报酒店({0})主机异常记录", hotel.Code));
}
}, System.Threading.CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
}
protected void Application_Error(object sender, EventArgs e)
{
try
{
Exception ex = Server.GetLastError();
logger.Error("IIS:" + ex.Message);
logger.Error(ex.StackTrace);
}
catch (Exception ex)
{
logger.Error("IIS Exception:" + ex.Message);
}
}
// 存储恶意IP和请求频率
private static ConcurrentDictionary<string, RequestInfo> _ipRequests = new ConcurrentDictionary<string, RequestInfo>();
protected void Application_BeginRequest(object sender, EventArgs e)
{
try
{
var ip = Request.UserHostAddress;
var url = Request.Url.AbsolutePath.ToLower();
// 定义要拦截的路径模式
var maliciousPatterns = new[]
{
@".*\.git/*",
@"^/sdk",
@"^/HNAP1",
@"^/evox",
@"^/nmaplowercheck",
@"^/phpmyadmin",
@"^/admin",
@"^/wp-login",
@"^/shell",
@"^/cmd",
@".*\.php$",
@".*\.asp$",
@".*\.git/credentials"
//@".*\.aspx$"
};
// 检查是否为恶意路径
if (maliciousPatterns.Any(pattern => System.Text.RegularExpressions.Regex.IsMatch(url, pattern)))
{
LogMaliciousRequest(ip, url);
Response.StatusCode = 403;
//Response.End();
Context.ApplicationInstance.CompleteRequest();
return;
}
// 频率限制(可选)
if (IsMaliciousRequestRate(ip))
{
Response.StatusCode = 429; // Too Many Requests
Context.ApplicationInstance.CompleteRequest();
//Response.End();
return;
}
}
catch (Exception ex)
{
logger.Error("恶意请求出错了:" + ex.Message);
}
}
private void LogMaliciousRequest(string ip, string url)
{
logger.Error("恶意IP:" + ip + " URL:" + url);
}
private bool IsMaliciousRequestRate(string ip)
{
// 简单的频率检查逻辑
var now = DateTime.UtcNow;
var info = _ipRequests.GetOrAdd(ip, _ => new RequestInfo());
lock (info)
{
info.RequestCount++;
info.LastRequest = now;
// 如果1分钟内超过100次请求认为是恶意请求
if (info.RequestCount > 100 && (now - info.FirstRequest).TotalMinutes < 1)
{
return true;
}
}
return false;
}
private class RequestInfo
{
public RequestInfo()
{
this.FirstRequest = DateTime.UtcNow;
}
public int RequestCount { get; set; }
public DateTime FirstRequest { get; set; }
public DateTime LastRequest { get; set; }
}
/// <summary>
/// 获取当前登录帐号
/// </summary>