再谈Cookies欺骗

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

在上一篇关于cookies欺骗的随笔中,提到的解决方案是把密码md5加密之后存入cookies中,确实这种方法实现了效果,不过把密码留在客户端等待着去被破解不是一个合适的方法,在此也感谢 @老牛吃肉 以及各位小伙伴们的热烈讨论。在这里改写了一下方案,记录一下。

废话不多说直接上代码:

先写一个DES加密类,可以直接拿走用,所以虽然是写DEMO也给单独拿出来了。

1usingSystem;2usingSystem.Data;3usingSystem.Web.Security;4usingSystem.Security.Cryptography;5usingSystem.Text;6usingSystem.IO;78///<summary>9///DES 的摘要说明10///</summary>11publicclassDES12{13publicDES()14{}1516///<summary>17///加密18///</summary>19///<param name="Text"></param>20///<param name="sKey"></param>21///<returns></returns>22publicstaticstringEncrypt(stringText,stringsKey)23{24DESCryptoServicePRovider provider =newDESCryptoServiceProvider();25byte[] bytes =Encoding.Default.GetBytes(Text);26provider.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPassWordForStoringInConfigFile(sKey,"md5").Substring(0,8));27provider.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey,"md5").Substring(0,8));28MemoryStream stream =newMemoryStream();29CryptoStream stream2 =newCryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);30stream2.Write(bytes,0, bytes.Length);31stream2.FlushFinalBlock();32StringBuilder builder =newStringBuilder();33foreach(bytenuminstream.ToArray())34{35builder.AppendFormat("{0:X2}", num);36}37returnbuilder.ToString();38}394041///<summary>42///以默认密钥加密43///</summary>44///<param name="Text"></param>45///<returns></returns>46publicstaticstringEncrypt(stringText)47{48returnEncrypt(Text,"http://www.cnblogs.com/webconfig");49}505152///<summary>53///解密54///</summary>55///<param name="Text"></param>56///<param name="sKey"></param>57///<returns></returns>58publicstaticstringDecrypt(stringText,stringsKey)59{60DESCryptoServiceProvider provider =newDESCryptoServiceProvider();61intnum = Text.Length /2;62byte[] buffer =newbyte[num];63for(inti =0; i < num; i++)64{65intnum3 = Convert.ToInt32(Text.Substring(i *2,2),0x10);66buffer[i] = (byte)num3;67}68provider.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey,"md5").Substring(0,8));69provider.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey,"md5").Substring(0,8));70MemoryStream stream =newMemoryStream();71CryptoStream stream2 =newCryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);72stream2.Write(buffer,0, buffer.Length);73stream2.FlushFinalBlock();74returnEncoding.Default.GetString(stream.ToArray());75}76777879///<summary>80///以默认密钥解密81///</summary>82///<param name="Text"></param>83///<returns></returns>84publicstaticstringDecrypt(stringText)85{86returnDecrypt(Text,"http://www.cnblogs.com/webconfig");87}88}

Login

1protectedvoidPage_Load(objectsender, EventArgs e)2{3//为了方便演示,每次加载登录页面“归零”4Response.Cookies["uid"].Value ="0";5}6protectedvoidButton1_Click(objectsender, EventArgs e)7{8//验证步骤略过,假设用户通过验证并且得到如下信息:910intuid =1;//用户唯一ID11stringusername ="admin";//用户名12stringuserpwd ="123456";//用户密码1314stringuidstr =DES.Encrypt(uid.ToString());1516//接下来要保存用户登录状态17Response.Cookies["uid"].Value =uidstr;1819//跳转到登录后的页面20Response.Redirect("Main.aspx");2122}23protectedvoidButton2_Click(objectsender, EventArgs e)24{25//未经验证直接进入Main.aspx26Response.Redirect("Main.aspx");27}

Main

1protectedvoidPage_Load(objectsender, EventArgs e)2{3CheckLogin();4}567privatevoidCheckLogin()8{910if(Request.Cookies["uid"] !=null&& Request.Cookies["uid"].Value !="")//判断cookies是否存在11{12intuid =0;13try14{15uid =int.Parse(DES.Decrypt(Request.Cookies["uid"].Value));16}17catch//捕获 cookies参数错误:非正确的 DES加密格式 或者解密后类型无法转换为int18{19Response.Write("您尚未登陆,<a href='login.aspx'>点此登录</a>");20return;21}2223if(GetUserInfo(uid,"") !="")//判断UID 是否存在24{25Response.Write(GetUserInfo(uid,"username") +"已登录");26}27else28{29Response.Write("您尚未登陆,<a href='login.aspx'>点此登录</a>");30}31}32else33{34Response.Write("您尚未登陆,<a href='login.aspx'>点此登录</a>");35}36}373839///<summary>40///模拟一个获取用户信息的方法41///然而实际操作中需要通过ID查询数据库来获取用户信息42///这里为了方便演示就直接return固定的值了43///</summary>44///<param name="uid"></param>45///<param name="key"></param>46///<returns></returns>47privatestringGetUserInfo(intuid,stringkey)48{49if(uid ==1)//这里只设置uid为1的用户,其他的UID的用户不存在50{51switch(key)52{53case"username":return"admin";54case"password":return"123456";55default:return"null";56}57}58else59{60return"";61}62}

最后来看测试效果:

UID是加密过的字符串,如果用户想用通过修改cookies的方法,那么他需要满足一下两个条件

1,猜中其他用户的UID

2,猜中网站DES加密的密钥

最后通过UID和密钥进行加密,把cookies修改为加密后的字符串

不过,第一条可能可以猜中,但是第二条,一般情况下密钥管理的好的话是不会有太大问题的。

本例DEMO:http://files.cnblogs.com/webconfig/Cookies%E6%AC%BA%E9%AA%972.rar

本文出自 低调码农的笔记簿http://www.cnblogs.com/webconfig/p/3624831.html 转载请注明出处,如有谬误不当之处,欢迎指正拍砖,不胜感谢!!

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航