반응형

안녕하세요.

오늘 말해볼 사항은, 새로고침을 죽어라 하는 등의 공격이 비정상적일 때 제한을 주는 로직입니다.

여러명이 동시에 이러한 공격을 하면 제대로 막지 못합니다. 밴리스트 자체가 많아지고, 예전에 중국발

수백명짜리 DDOS 같은 것은 꿈도 못꾸죠.

하지만 소규모 서버에서 최소한으로 탑재 시켜놓은면 적은양의 공격은 막을 수 있다더군요.

가장 좋은 방법은 웹 방화벽 등에서 네트워크타고 들어오기전에 차단하는 것이지만,

원리는 아주 간단합니다.

웹캐시를 이용해 지정된 시간동안 몇 회 이상 접근시 막는 것이죠~, 기존에 제가 사용하는 제 라이브러리에 있던 것을 복사해봤습니다.



public static class ActionValidation
{
private const int DURATION = 10; //10 min period
public const int CountAction = 100;
/*
* Type of actions and their maximum value per period
*
*/

private class HitInfo
{
public int Hits;
private DateTime _ExpiresAt = DateTime.Now.AddMinutes(DURATION);
public DateTime ExpiresAt { get { return _ExpiresAt; } set { _ExpiresAt = value; } }
}

public static bool IsValid()
{
HttpContext context = HttpContext.Current;
if (context.Request.Browser.Crawler) return false;

string key = context.Request.UserHostAddress;

HitInfo hit = (HitInfo)(context.Cache[key] ?? new HitInfo());

if (hit.Hits > (int)CountAction)
{
hit.Hits++;
hit.ExpiresAt = DateTime.Now.AddMinutes(DURATION);

context.Cache.Remove(key);
context.Cache.Add(key, hit, null, DateTime.Now.AddMinutes(DURATION),
System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);

return false;
}
else
hit.Hits++;

if (hit.Hits == 1)
context.Cache.Add(key, hit, null, DateTime.Now.AddMinutes(DURATION),
System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);

return true;
}
}

이것을 ControllerBase 와 같이 설정해서 컨트롤러 밑에 깔아줬었습니다.

로그남기거나, 화면을 Redirect 해주는 등의 구현이 추가로 들어가면 되겠습니다.

.NET 원폼인 경우 글로벌에 그냥 넣어주면 되겠습니다.


public class ControllerBase : Controller
{
private JHFrk.Com.Security.JHSession jhs;
public JHFrk.Com.Security.JHSession JHS
{
get
{
if (jhs == null)
{
jhs = new JHFrk.Com.Security.JHSession();
}
return jhs;
}
}

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!JHFrk.Com.Util.ActionValidator.IsValid())
{
Response.Write("DDOS");
Response.End();
Response.Close();
}
base.OnActionExecuting(filterContext);
}
}
반응형

+ Recent posts