ASP.NET Web应用程序可防止拒绝服务攻击

时间:2011-06-14 08:17:39

标签: asp.net security denial-of-service

我可以使用哪些工具或技术来保护我的ASP.NET Web应用程序免受拒绝服务攻击

3 个答案:

答案 0 :(得分:3)

尝试动态IP限制扩展http://www.iis.net/download/dynamiciprestrictions

不是一个完美的解决方案,但有助于提高标准=)

答案 1 :(得分:3)

这是一个广泛的领域,所以如果您可以更具体地了解您的应用程序,或者您想要保护的威胁级别,我相信更多人可以帮助您。

然而,无论如何,你可以选择一种缓存解决方案,例如Squid:http://www.blyon.com/using-squid-proxy-to-fight-ddos/,动态IP限制(如Jim所解释的),如果你有基础设施,那么主动 - 被动故障转移设置,您的被动计算机提供的占位符内容不会影响您的数据库/任何其他计算机。这是最后防御,因此您可以最大限度地缩短DDOS可能使整个站点脱机的时间。

答案 2 :(得分:1)

确定硬件解决方案是防止DOS攻击的最佳选择,但考虑到您无法访问硬件配置或IIS设置的情况,这绝对是开发人员必须拥有一些方便阻止或至少减少的原因dos攻击效果。

逻辑的核心概念依赖于FIFO(先入先出)集合,例如Queue,但由于它有一些限制,我决定创建自己的集合。

不讨论更多细节,这是我使用的完整代码:

public class AntiDosAttack
{
  readonly static List<IpObject> items = new List<IpObject>();

  public static void Monitor(int Capacity, int Seconds2Keep, int AllowedCount)
  {
    string ip = HttpContext.Current.Request.UserHostAddress;

    if (ip == "")
    return;

    // This part to exclude some useful requesters
    if(HttpContext.Current.Request.UserAgent != null && HttpContext.Current.Request.UserAgent == "Some good bots")
      return;

    // to remove old requests from collection
    int index = -1;
    for (int i = 0; i < items.Count; i++)
    {

      if ((DateTime.Now - items[i].Date).TotalSeconds > Seconds2Keep)
      {
        index = i;
        break;
      }
    }

    if (index > -1)
    {
      items.RemoveRange(index, items.Count - index);
    }

    // Add new IP
    items.Insert(0, new IpObject(ip));

    // Trim collection capacity to original size, I could not find a better reliable way
    if (items.Count > Capacity)
    {
      items.RemoveAt(items.Count - 1);
    }

    // Count of currect IP in collection
    int count = items.Count(t => t.IP == ip);

    // Decide on block or bypass
    if (count > AllowedCount)
    {
      // alert webmaster by email (optional)
      ErrorReport.Report.ToWebmaster(new Exception("Blocked probable ongoing ddos attack"), "EvrinHost 24 / 7 Support - DDOS Block", "");

      // create a response code 429 or whatever needed and end response
      HttpContext.Current.Response.StatusCode = 429;
      HttpContext.Current.Response.StatusDescription = "Too Many Requests, Slow down Cowboy!";
      HttpContext.Current.Response.Write("Too Many Requests");
      HttpContext.Current.Response.Flush(); // Sends all currently buffered output to the client.
      HttpContext.Current.Response.SuppressContent = true;  // Gets or sets a value indicating whether to send HTTP content to the client.
      HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event.
    }
  }

  internal class IpObject
  {
    public IpObject(string ip)
    {
      IP = ip;
      Date = DateTime.Now;
    }

    public string IP { get; set; }
    public DateTime Date { get; set; }
  }
}

内部类旨在保留请求日期。

当然,DOS攻击请求会在每个请求上创建新会话,而网站上的人工请求包含在一个会话中打包的多个请求,因此可以在Session_Start中调用该方法。

用法:

protected void Session_Start(object sender, EventArgs e)
{
   // numbers can be tuned for different purposes, this one is for a website with low requests
   // this means: prevent a request if exceeds 10 out of total 30 in 2 seconds
   AntiDosAttack.Monitor(30, 2, 10);
}

对于重要的请求网站,您可以将秒数更改为毫秒,但请考虑此代码导致的额外负载。

我不知道是否有更好的解决方案来阻止对网站的故意攻击,所以我感谢任何评论和建议来改进代码。到那时,我认为这是以编程方式防止对ASP.NET网站进行DOS攻击的最佳做法。

相关问题