当前位置:编程学习 > asp >>

Asp.net安全架构之4:Brute force(爆破)

原理
 
爆破是对系统的登录入口发起不间断的请求,达到暴力破解的目的。
 
实际案例
 
某系统存在爆破攻击点,只要模拟以下攻击,就能采用字典破解法,根据分析发现,只要返回状态为302的,为用户名密码正确,也就是被爆破了,状态为200的,为用户名密码错误。
 
在攻击的过程中,我们只要准备好字典,就能顺利实现爆破。像用户名为luminji,密码为123456这样的用户很容易就会被爆破掉。
 
请求:
 
POST /sso/ValidateUser.aspx HTTP/1.1
 
User-Agent: Fiddler
 
Accept-Language: zh-CN
 
Content-Type: application/x-www-form-urlencoded
 
Accept-Encoding: gzip, deflate
 
Host: 192.168.40.193
 
Content-Length: 37
 
 
 
loginId=luminji&password=123456 
 
以下是成功爆破的返回:
 
HTTP/1.1 302 Found
 
Cache-Control: private
 
Content-Length: 151
 
Content-Type: text/html; charset=utf-8
 
Location: http://192.168.40.193/portal/pages
 
Server: Microsoft-IIS/7.5
 
X-AspNet-Version: 2.0.50727
 
Set-Cookie: ASP.NET_SessionId=spycdd55b1cph0iohogufq55; path=/; HttpOnly
 
X-Powered-By: ASP.NET
 
Date: Mon, 07 May 2012 01:25:50 GMT
 
 
 
<html><head><title>Object moved</title></head><body>
 
<h2>Object moved to <a href="http://192.168.40.193/portal/pages">here</a>.</h2>
 
</body></html>
 
以下是失败的返回: www.zzzyk.com
 
HTTP/1.1 200 OK
 
Cache-Control: private
 
Transfer-Encoding: chunked
 
Content-Type: text/html; charset=utf-8
 
Content-Encoding: gzip
 
Vary: Accept-Encoding
 
Server: Microsoft-IIS/7.5
 
X-AspNet-Version: 2.0.50727
 
Set-Cookie: ASP.NET_SessionId=zxomk255e3115245tpqi3k45; path=/; HttpOnly
 
X-Powered-By: ASP.NET
 
Date: Mon, 07 May 2012 01:26:01 GMT
 
 
 
8a0
 
_�_
 
应对措施
 
一种思路是:定位客户端,限制客户端的请求频率。一般来说,通过两个途径可确定某个客户端,IP地址和Cookie。但是,这种方式一般来说也是被攻破的,比如使用AccessDriver这样的工具就可以更换IP地址,同时,再清空cookie就可以做到。
 
其次,使用验证码。这是一种非常有效的措施,但是一定程度上降低了用户体验。
 
Mads Kristensen提到了另一种方法是限制每个用户的登录次数,代码如下:
 
 
        protected void ButtonLogin_Click(object sender, EventArgs e)
        {
            string loginName = "luminji";
            if (AddAndGetLoginCount(loginName) > 5)
            {
                //block
            }
            else
            {
                if (CheckLogin(loginName) == true)
                {
                    ClearLoginCount(loginName);
                }
            }
        }
 
        //只适用于单机,如果是集群,需要分布式缓存
        int AddAndGetLoginCount(string userNanme)
        {
            if (Cache[userNanme] == null)
            {
                Cache.Insert("luminji", 1, null,
                    System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(10));
                return 1;
            }
            else
            {
                int count = (int)Cache[userNanme] + 1;
                Cache[userNanme] = count;
                return count;
            }
        }
 
        void ClearLoginCount(string userName)
        {
            if (Cache[userName] != null)
            {
                Cache.Remove(userName);
            }
        }
 
        private bool CheckLogin(string loginName)
        {
            throw new NotImplementedException();
        } 
 
这里还有一种方法,它看上去是正确的,但是有人一眼就看出来在其貌似能正确防范爆破下的本质错误,不知道你是否能察觉。这段代码的大致思路是:
 
将访问次数保存在cookie中,然后根据cookie保存的值来限制访问次序。保存在cookie中的值(该值所表达的意义是:谁在某个时间段内访问了几次),需要进行加密处理,只有这样,才能保证不让客户端进行模拟。全部代码实现,请参看代码:
 
 
        protected void ButtonLogin_Click(object sender, EventArgs e)
        {
            HttpCookie cookieGet = Request.Cookies.Get("btcookie");
            if (cookieGet == null)
            {
                SendBFCookieToClientAndRedirect();
            }
            else
            {
                ReceiveBFCookieAndCheckLogin(cookieGet);
            }
        }
 
        private void ReceiveBFCookieAndCheckLogin(HttpCookie cookieGet)
        {
            string loginName = "luminji"
补充:综合编程 , 安全编程 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,