[轉]WebQQ登錄過程分析

近來遇到vista和QQ的兼容性問題,丫的,無奈用起來WebQQ,面對這垃圾網絡,又一看是http不禁對其安全性產生了質疑,近來抄起了.net 於是用fiddler2來capture,得到密鑰分析其加密過程,一百度,發現有人早有分析,且看相關分析:
http://htsoft.net.cn/html/ytag/qq%E5%AF%86%E7%A0%81%E5%8A%A0%E5%AF%86%E6%96%B9%E5%BC%8F
關於在網頁登陸QQ時密碼的加密方式
類歸於: C#相關 — HtSoft @ 2009-10-18 13:25:56 還沒有評論

在網頁上登錄QQ時,密碼是通過加密後POST給服務器的,如何加密的可以找到JS文件加以分析,JS文件中貌似整的比較複雜,全是位運算,本想把JS轉成C#,一個函數一個函數轉換,嘗試了一下放棄了,太麻煩。

最後非常簡單的解決了這個問題,其實看JS文件被誤導了,加密方式很簡單,如下:

MD5(MD5_3(QQ密碼)+驗證碼大寫),順便貼上C#代碼:

public static class qqPwdEncrypt
{
/// <summary>
/// 計算網頁上QQ登錄時密碼加密後的結果
/// </summary>
/// <param name="pwd" />QQ密碼</param>
/// <param name="verifyCode" />驗證碼</param>
/// <returns></returns>
public static String Encrypt(string pwd, string verifyCode)
{
return (md5(md5_3(pwd).ToUpper() + verifyCode.ToUpper())).ToUpper();
}
/// <summary>
/// 計算字符串的三次MD5
/// </summary>
/// <param name="s" /></param>
/// <returns></returns>
private static String md5_3(String s)
{
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s);

bytes = md5.ComputeHash(bytes);
bytes = md5.ComputeHash(bytes);
bytes = md5.ComputeHash(bytes);

md5.Clear();

string ret = "";
for (int i = 0; i < bytes.Length; i++)
{
ret += Convert.ToString(bytes[i], 16).PadLeft(2, '0');
}

return ret.PadLeft(32, '0');
}
/// <summary>
/// 計算字符串的一次MD5
/// </summary>
/// <param name="s" /></param>
/// <returns></returns>
private static String md5(String s)
{
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s);

bytes = md5.ComputeHash(bytes);

md5.Clear();

string ret = "";
for (int i = 0; i < bytes.Length; i++)
{
ret += Convert.ToString(bytes[i], 16).PadLeft(2, '0');
}

return ret.PadLeft(32, '0');
}
}


=====================================================================

先介紹一下有關加密的信息:

1、打開http://xiaoyou.qq.com/

2、開啓fiddler監聽HTTP包

3、登錄QQ,我這裏就隨手寫了個QQ:123456密碼123456驗證碼efta

U代表QQ號

P代表加密後的密碼

verifycode代表驗證碼

其餘都是固定的,不敘述了

要登錄農場,必須要模擬發送以上數據來登錄校友,而密碼必須加密成如上才能通過驗證。

現在講解一下如何把明文的1234加密成09967317CCFC266ADA83C9B1BEA30825(這麼噁心的密碼)

這個是騰訊加密的JavaScript腳本 密碼加密JavaScript

初步看了一下覺得相當之噁心,所以在網上搜索了半天找到的一點線索就是 3次MD5加密後轉換大些 + 驗證碼轉換大寫,再次MD5加密

可是我按照這方法試了得到的結果卻不是正確的。百思不得其解,求助了園子裏認識的朋友,他給我發了段C#的代碼,確實可以得到正確的。代碼如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using System.Collections;

namespace QQ校友助手
{
class QQPassword
{
public static string binl2hex(byte[] buffer)
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < buffer.Length; i++)
{
builder.Append(buffer[i].ToString("x2"));
}
return builder.ToString();
}
public static string md5_3(string input)
{
MD5 md = MD5.Create();
byte[] buffer = md.ComputeHash(Encoding.Default.GetBytes(input));
buffer = md.ComputeHash(buffer);
buffer = md.ComputeHash(buffer);
return binl2hex(buffer);
}

public static string md5(string input)
{
byte[] buffer = MD5.Create().ComputeHash(Encoding.Default.GetBytes(input));
return binl2hex(buffer);
}

public static string getPassword(string password, string verifycode)
{
return md5(md5_3(password).ToUpper() + verifycode.ToUpper()).ToUpper();
}


}
}


Encoding.Default.GetBytes(input)獲取的是字符串的ASCII碼

用VS跟蹤調試了好久,發現問題出在 C#的computehash是初級hash,得到的並不是16進制的MD5字符串,需要經過binl2hex才能轉換成MD5字符串。也就是說初級hash了3次,才合併大寫驗證碼進行最後一次MD5加密。


知道了原理後,我改成Delphi源碼:

procedure TForm1.btn4Click(Sender: TObject);
var
hash: T4x4LongWordRecord;
hashstr: string;
i: integer;
temp3: string;
begin
SetLength(hashstr, 16);
with TIdHashMessageDigest5.Create do begin
hash := HashValue('123456');    //此處的hash也是初級的
Move(hash, hashstr[1], 16);
for i := 1 to Length(hashstr) do
temp3 := temp3 + Char(hashstr[i]);
hash := HashValue(temp3);
Move(hash, hashstr[1], 16);
temp3 := '';
for i := 1 to Length(hashstr) do
temp3 := temp3 + Char(hashstr[i]);
temp3 := AsHex(HashValue(temp3));  //此處的AsHex就是轉換最終的MD5字符串(32位)
temp3 := temp3 + 'EFTA';
temp3 := AsHex(HashValue(temp3));
ShowMessage(temp3);
Free;
end;
end;


以上代碼可以得到09967317CCFC266ADA83C9B1BEA30825這加密後的字符串

我現在寫的QQ農夫只是用瀏覽器直接登錄,沒有采用模擬post數據的方式登錄。所以之前沒有對這個加密算法做研究。

希望對QQ密碼加密的分析對大家有啓發!


文章來自學IT網:http://www.xueit.com/js/show-4765-2.aspx
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章