防止cookie 欺骗

按照浏览器的约定,只有来自同一域名的cookie才可以读写,而cookie只是浏览器的,对通讯协议无影响,所以要进行cookie欺骗可以有多种途径,最简单的方法自己建立一个网站,在C:\WINDOWS\system32\drivers\etc\hosts 中把这个自己建立的网站制定成想要欺骗的域名,cookie写入以后再把hosts的值改回来,这样这个本地的网站的cookie就可以抛到你想要入侵的域名下。

     这个漏洞可能大家都知道,关键是怎么预防,依照我个人的理解解决方法如下:

     请支持我的网站:http://www.haoy7.com 好游戏网

登陆代码:

      public void Logined(ModelUser model)
        {
            int outTime = GetLoginOutTime();
            HttpContext.Current.Response.Cookies["USERNAME"].Value=model.User_Name;
            HttpContext.Current.Response.Cookies["USERNAME_CHECK"].Value =DesSecurity.DesEncrypt(model.User_Name);
            HttpContext.Current.Response.Cookies["USERNAME"].Expires = DateTime.Now.AddMinutes(outTime);
            HttpContext.Current.Response.Cookies["USERNAME_CHECK"].Expires = DateTime.Now.AddMinutes(outTime);
            SetUserModel(model);
        }

可以看到保存了两个cookie值,都是用户名,一个是加密的一个是未加密的

验证是否登陆的代码如下:

        public bool IsLogin()
        {
            bool isLogin = false;
            if (HttpContext.Current.Request.Cookies["USERNAME"] != null)
            {
                if (HttpContext.Current.Request.Cookies["USERNAME_CHECK"] != null)
                {
                    string userName = HttpContext.Current.Request.Cookies["USERNAME"].Value;
                    string userNameCheck = HttpContext.Current.Request.Cookies["USERNAME_CHECK"].Value;
                    if (userName == DesSecurity.DesDecrypt(userNameCheck))
                        isLogin = true;
                }
            }
            return isLogin;
        }

OK了。

注意:代码是给需要的人看的,本人水平有限也不是作家请不要妄加评论,谢谢!

posted @ 2011-02-17 14:12  野百合也有春天324  阅读(939)  评论(2编辑  收藏  举报