IE 頂層 body 節點通過IHTMLElement->get_all 方法無法獲取iframe 里面的節點列表
CComPtr<IHTMLElement> body;

CComPtr<IDispatch> spDispCollection;
body->get_all(&spDispCollection);
所以要獲取iframe/frame(frameset) 里面的節點列表的話, 則需要根據body/doc 找到frames, 然后從frames -> IHTMLWindow2 -> IHTMLDocument2 . 主要有2個方法, 下面是代碼片段
方法一:
IHTMLDocument2 *pDoc = 瀏覽器的Document(IWebBrowser2->IDispatch->IHTMLDocument2);
IHTMLWindow2 *pHTMLWnd = NULL;
IHTMLDocument2 *pFrameDoc=NULL;
IHTMLFramesCollection2 *pFramesCollection=NULL;
LPDISPATCH lpDispatch;
long p;
VARIANT varindex,varresult;
varresult.vt=VT_DISPATCH;
varindex.vt = VT_I4;
if(pDoc!=NULL)
{
HRESULT hr=pDoc->get_frames(&pFramesCollection);
if(SUCCEEDED(hr)&&pFramesCollection!=NULL)
{
hr=pFramesCollection->get_length(&p);
if(SUCCEEDED(hr))
for(int i=0; i<p; i++)
{
varindex.lVal = i;
if(pFramesCollection->item(&varindex, &varresult) ==S_OK)
{
lpDispatch=(LPDISPATCH)varresult.ppdispVal;
if (SUCCEEDED(lpDispatch->QueryInterface(IID_IHTMLWindow2, (LPVOID *)&pHTMLWnd)))
{
if(SUCCEEDED(pHTMLWnd->get_document( &pFrameDoc)))
{
//work with the pFrameDoc
}
pHTMLWnd->Release();
pHTMLWnd=NULL;
}
}
}
pFramesCollection->Release();
}
pDoc->Release();
}
方法二:
CComQIPtr<IHTMLElement> pElem = ; // 可以遞歸上面的 CComPtr<IDispatch> spDispCollection 來得到
CComBSTR bstrTagName;
pElem->get_tagName(&bstrTagName);
if ( lstrcmpiW(L"IFRAME", bstrTagName)==0 ||
lstrcmpiW(L"FRAME", bstrTagName)==0 )
{
CComQIPtr<IHTMLFrameBase2> _framebase2;
CComPtr<IHTMLWindow2> _framewindow;
CComPtr<IHTMLDocument2> _framedoc;
if( (_framebase2 = spItem)
&& SUCCEEDED( _framebase2->get_contentWindow(&_framewindow) ) && _framewindow!=NULL
&& SUCCEEDED( _framewindow->get_document(&_framedoc) ) && _framedoc!=NULL )
{
// 對 _framedoc 節點進行處理
}
}
iframe 跨域訪問(cross frame) zz from : http://codecentrix.blogspot.com/2007/10/when-ihtmlwindow2getdocument-returns.html
由于安全性限制, 為防止跨域腳本攻擊, 當frames 跨域的時候, IHTMLWindow2::get_document 調用將返回 E_ACCESSDENIED .
下面函數 HtmlWindowToHtmlDocument 對于跨域的frame 通過 IHTMLWindow2 -> IID_IWebBrowserApp -> IHTMLWindow2 繞過了限制.
// Converts a IHTMLWindow2 object to a IHTMLDocument2. Returns NULL in case of failure.
// It takes into account accessing the DOM across frames loaded from different domains.
CComQIPtr<IHTMLDocument2> HtmlWindowToHtmlDocument(CComQIPtr<IHTMLWindow2> spWindow)
{
ATLASSERT(spWindow != NULL);
CComQIPtr<IHTMLDocument2> spDocument;
HRESULT hRes = spWindow->get_document(&spDocument);
if ((S_OK == hRes) && (spDocument != NULL))
{
// The html document was properly retrieved.
return spDocument;
}
// hRes could be E_ACCESSDENIED that means a security restriction that
// prevents scripting across frames that loads documents from different internet domains.
CComQIPtr<IWebBrowser2> spBrws = HtmlWindowToHtmlWebBrowser(spWindow);
if (spBrws == NULL)
{
return CComQIPtr<IHTMLDocument2>();
}
// Get the document object from the IWebBrowser2 object.
CComQIPtr<IDispatch> spDisp;
hRes = spBrws->get_Document(&spDisp);
spDocument = spDisp;
return spDocument;
}
// Converts a IHTMLWindow2 object to a IWebBrowser2. Returns NULL in case of failure.
CComQIPtr<IWebBrowser2> HtmlWindowToHtmlWebBrowser(CComQIPtr<IHTMLWindow2> spWindow)
{
ATLASSERT(spWindow != NULL);
CComQIPtr<IServiceProvider> spServiceProvider = spWindow;
if (spServiceProvider == NULL)
{
return CComQIPtr<IWebBrowser2>();
}
CComQIPtr<IWebBrowser2> spWebBrws;
HRESULT hRes = spServiceProvider->QueryService(IID_IWebBrowserApp, IID_IWebBrowser2, (void**)&spWebBrws);
if (hRes != S_OK)
{
return CComQIPtr<IWebBrowser2>();
}
return spWebBrws;
}
附:
IE(控件/接口)中主要有4個部分, Browser, Document, Frame/IFrame, Element , 其對應接口分別是
Browser - IWebBrowser2
Document - IHTMLDocument2
Frame/IFrame- IHTMLWindow2
Element - IHTMLElement
可以通過下面方法互相獲取
browser -> document IWebBrowser2::get_Document
document -> frame IHTMLDocument2::get_parentWindow
frame -> document IHTMLWindow2::get_document
frame -> parent frame IHTMLWindow2::get_parent
frame -> children frames IHTMLWindow2::get_frames
element -> Frame IHTMLElement->QI(IHTMLFrameBase2) -> IHTMLFrameBase2->get_contentWindow -> IHTMLWindow2
ref:
在多Frame的網頁中怎么取出各個Frame的IHTMLDocument2的接口!急用.(高分)在文章
When IHTMLWindow2::get_document returns E_ACCESSDENIED 解決了iframe 跨域訪問的問題
同自動化瀏覽器(http://blog.joycode.com/jiangsheng/archive/2005/10/20/65489.aspx)相比,自動化瀏覽器控件(WebBrowser Control) 在應用程序中更加常用。從Outlook的預覽窗格到Maxthon這樣的基于IE引擎的瀏覽器,從無界面的HTML分析器到Norton Antivirusd的主界面,瀏覽器控件在眾多領域被用作各種各樣的用途。這也使得有必要根據具體的用戶需求自定義瀏覽器控件的行為。
在應用程序中加入瀏覽器控件
集成瀏覽器控件的最簡單的方法是找一個支持ActiveX的集成開發環境,在工具箱中加入Microsoft Web Browser這個控件,往表單上拖一個這個控件就可以完成工作。你甚至可以用集成開發環境添加ActiveX的事件處理函數。如果要直接導入ActiveX的話,建議使用mehrcpp的vbMHWB控件(http://www.codeproject.com/atl/vbmhwb.asp)。這個控件在瀏覽器控件的基礎上進行了擴展,暴露了很多底層接口。
通常導入ActiveX就可以滿足大部分需求 ,但是有些類庫中也集成了瀏覽器控件,并且提供了更多的功能,例如MFC的CHTMLView和CDHtmlDialog,ATL的HTML Control,以及.Net 2.0中的Windows.Forms.WebBrowser。如果使用Visual C++來進行非托管編程,那么建議使用MFC或者ATL的封裝類,或者使用vbMHWB控件。托管編程中當然首選Windows.Forms.WebBrowser。除非這些類的BUG影響到了應用程序的開發,否則建議使用這些功能更加強大的封裝類。
在使用瀏覽器控件及其封裝類的時候要注意一些已知問題
常見任務
在集成瀏覽器控件之后,可以完成基本的網頁瀏覽,但是對于不同的任務,也需要進一步的處理,例如設置控件的屬性、為控件添加事件處理、操作HTML文檔等等。
修改瀏覽器控件的屬性
這在集成開發環境中可以很容易地設置,也可以自己實現容器來設置,但是CHTMLView這樣的封裝類沒有這個選項(http://support.microsoft.com/kb/197921)。
- 鏈接目標解析。對于用瀏覽器控件來做瀏覽器的場合來說,需要將瀏覽器的RegisterAsBrowser屬性設置為true。這使得Internet Explorer在解析HTML鏈接的target屬性指定的目標窗口時可以找到這個窗口。
- 禁用拖放。對于使用瀏覽器控件來做預覽窗格的場合來說,需要將瀏覽器的RegisterAsDropTarget屬性設置為false。這使得窗口不接受拖進來的文件和鏈接。
- 禁用消息框。對于用瀏覽器控件來做HTML分析器的場合來說,有時需要屏蔽腳本產生的消息框以避免阻塞程序運行。這可以通過設置瀏覽器的Silent屬性來實現,或者實現IDocHostShowUI::ShowMessage。
捕獲瀏覽器控件的事件
集成開發環境中可以也很容易地添加瀏覽器的事件處理函數。比較常用的事件包括
操作MSHTML文檔
通常HTML分析和瀏覽器自動化程序都需要分析網頁的結構,找到需要操作的元素。這需要對網頁的結構進行分析,找到目標元素的標識方法。 一些常用的操作包括:
在頁面包含框架的時候,可能需要跨框架訪問HTML文檔。可以通過查詢框架元素所支持的IWebBrowser2接口或者IHTMLWindow2接口來訪問框架中的文檔(http://support.microsoft.com/kb/196340),但是也有可能因為安全設置而無法訪問(http://support.microsoft.com/kb/167796)。
在瀏覽器控件中顯示其它類型的文檔時,可以用IWebBrowser2的document屬性來訪問ActiveX文檔,例如在顯示Microsoft Word時,IWebBrowser2的document屬性就是Word的文檔對象,在顯示文件夾的時候,IWebBrowser2的document屬性就是文件夾對象等等。
擴展瀏覽器的宿主
瀏覽器控件在創建時會查詢ActiveX容器的IOleClientSite的實現的如下接口:IDocHostUIHandler, IDocHostUIHandler2 and IDocHostShowUI。
雖然在無法自定義ActiveX容器的情況下可以用ICustomDoc::SetUIHandler來掛接IDocHostUIHandler到瀏覽器控件,但是這樣也會造成內存泄漏(http://support.microsoft.com/kb/893629)。一些類庫,例如MFC、ATL和.Net類庫都實現了IDocHostUIHandler接口。
除了專門用于瀏覽器用途的程序之外,通常都需要自定義瀏覽器控件的上下文菜單。這需要實現IDocHostUIHandler::ShowContextMenu。通常的實現包括完全禁用上下文菜單、完全替換上下文菜單、以及修改部分上下文菜單。經常被從上下文菜單中移除的菜單項包含查看源代碼、刷新和屬性。一種替代的方案是在容器中過濾右鍵消息(http://support.microsoft.com/kb/231578)。
與瀏覽器相比,一些Internet Explorer的宿主功能在瀏覽器控件中并不是默認啟用。在某些場合,默認啟用的宿主功能可能并非預期。這時需要實現IDocHostUIHandler::GetHostInfo。可以通過實現IDocHostUIHandler::GetHostInfo來自定義的功能包括:
- 自動完成功能。對于用瀏覽器控件來做瀏覽器的場合來說,這個功能是有必要啟用的。啟用的方法是設置DOCHOSTUIFLAG_ENABLE_FORMS_AUTOCOMPLETE位
- 如果瀏覽器中的鏈接網址包含非ASCII的字符,那么需要實現IDocHostUIHandler::GetHostInfo,并且在返回的DOCHOSTUIINFO結構中設置dwFlags成員的DOCHOSTUIFLAG_URL_ENCODING_ENABLE_UTF8位。這使得網址會在發送之前用UTF-8編碼。
- 3D邊框、滾動條,禁用文字選擇功能和禁用頁面上的腳本。
- 對于使用瀏覽器控件來做HTML編輯器的場合來說,有時需要修改默認的頁面樣式。這都需要實現IDocHostUIHandler::GetHostInfo(http://support.microsoft.com/kb/328803)。注意在有些版本的IE中IDocHostUIHandler::GetHostInfo只在MSHTML被初始化的時候被調用,所以如果你需要在MSHTML被初始化之后使你的修改生效,你需要瀏覽到一個Word之類的非HTML Active document文檔,之后再瀏覽回來。
在使用瀏覽器控件來做數據錄入界面的場合,需要更改瀏覽器控件默認的Tab鍵處理使得用戶可以使用Tab鍵切換到容器中的其他控件。這需要實現IDocHostUIHandler::TranslateAccelerator來自定義瀏覽器控件的快捷鍵處理。對于MFC這樣用消息鉤子來做消息預處理的可自定義容器來說,也可以用PreTranslateMessage來過濾F5鍵盤消息,而不是實現IDocHostUIHandler::TranslateAccelerator。
在腳本中調用應用程序對瀏覽器控件的擴展,這需要實現IDocHostUIHandler::GetExternal。使用.Net的WebBrowser控件的話設置ObjectForScripting屬性就可以了。
對于用瀏覽器控件來做HTML分析器的場合來說,有時需要屏蔽腳本產生的消息框。這需要實現IDocHostShowUI::ShowMessage,或者設置瀏覽器的Silent屬性。
另外,瀏覽器也會查詢IOleClientSite來獲得其它的服務信息,例如
其他控制
對于用瀏覽器控件來做HTML分析器的場合來說,有時需要禁用瀏覽器的腳本、ActiveX或者圖片下載。這可以通過在容器中實現IDispatch,處理DISPID_AMBIENT_DLCONTROL來做到(http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/overview/Overview.asp)。
看來離線瀏覽的控制并不能用這種方法來控制(http://support.microsoft.com/kb/247336)。不過你可以自己編寫一個HTTP層傳遞 BINDF_OFFLINEOPERATION標志 (http://groups-beta.google.com/group/microsoft.public.inetsdk.programming.mshtml_hosting/msg/76bf4910a289d4b3)
在瀏覽器控件中java小程序可能不能正常運行,如果使用Sun JVM1.4之后的版本,可以用SetEnvironmentVariable 來設置JAVA_PLUGIN_WEBCONTROL_ENABLE為1來啟用Sun JVM。
默認情況下在頁面載入時會有點擊聲。屏蔽點擊聲的一個方法是在程序運行時修改注冊表鍵(http://support.microsoft.com/kb/201901),另一個方法是將瀏覽器控件隱藏,在調用Navigate2之后再顯示,但是這也需要鎖定控件的更新區域(LockWindowUpdate)以避免閃爍。在IE7中,也可以調用 CoInternetSetFeatureEnabled函數,傳遞FEATURE_DISABLE_NAVIGATION_SOUNDS來禁用瀏覽時的聲音。
在需要使用代理服務器時,有可能需要在應用程序中使用非默認的代理服務器設置。這可以通過調用UrlMkSetSessionOption來實現。
Overriding IInternetSecurityManager in a CComControl class
CAxWindow implements IObjectWithSite interface (get it with QueryHost
method). Call SetSite passing your implementation of IServiceProvider.
At this point, AxWin will forward all QueryService calls from hosted
WebBrowser to your implementation.
對ie 的攻擊分好幾類, 有修改主頁, 彈出窗口, 惡意插件, 網頁木馬等. 其中一些是利用了ie 的腳本的自帶功能, 而另外一些要對ie 實施攻擊后才能做到, 主要手段有利用第三方軟件漏洞, ie 堆噴射等, 文章 [ [JavaScript中的堆風水]|[ http://www.team509.com/download/Heap%20Feng%20Shui%20in%20JavaScript_en_cn.htm ] ] 對堆噴射進行了介紹, 主要是利用覆蓋函數返回地址或者對象的虛函數表來執行shellcode, 主要涉及到ie & 系統的內存管理.
一般shellcode 基本只完成攻擊動作, 具體的對系統的后續攻擊肯定離不了下載執行 exe 文件等動作, 并且在攻擊ie 也會有各種癥狀, 可以根據這些基本實現惡意代碼.
1. 根據ie 癥狀
當ie 訪問惡意頁面的遭受攻擊時, 其重要表現是
a. 內存使用
b. cpu 使用率
所以實時監測這些參數可以基本判斷是否有攻擊
2. 根據行為
shellcode 如果被執行, 那么肯定會進行木馬下載執行等步驟. 一般純shellcode 里面的內容不會很多, 所以不可能完成很多復雜的攻擊.
win32 創建進程的API調用串是:
WinExec/ShellExecuteA/CreateProcessA->CreateProcessInternalA->CreateProcessInternalW->ZwCreateProcessEx
或
CreateProcessW->CreateProcessInternalW->ZwCreateProcessEx
win32 要執行下載的API 主要是wsock32.dll 的
recv , recvfrom
所以對上述API 進行攔截, 一般可以檢測到是否有ie 是否被攻擊, 但是這個只能在攻擊成功后, shellcode 執行后才能被檢測到
3.
攔截一些操作注冊表, 創建窗口等API , 可以做到防止被修改主頁, 彈出窗口等
利用ms 的Detours 可以很容易的實現對系統 API 的hook
http://blog.csdn.net/hu0406/archive/2008/03/05/2150358.aspx
http://blog.csdn.net/hu0406/archive/2008/03/05/2150351.aspx
http://www.moon-soft.com/doc/2288.htm
http://blog.csdn.net/dedodong/archive/2006/10/07/1323925.aspx
[ [JavaScript中的堆風水]|[ http://www.team509.com/download/Heap%20Feng%20Shui%20in%20JavaScript_en_cn.htm ] ] ie 堆噴射
[ [也聊inline-hook]|[ http://blog.tom.com/tigerkings941220/article/9211.html ] ] 介紹了 進程自身保護(通過攔截LoadLibraryW)和IE漏洞防護(通過攔截CreateProcessInternalW)
[ [maxthon2(遨游2) mxsafe.dll對網頁木馬的防護以及繞過]|[ http://hi.baidu.com/54nop/blog/item/b52cff6e713964d980cb4a9e.html ] ] 討論了maxthon2 防止網頁木馬的策略( 攔截 ZwCreateProcessEx/ZwCreateProcess, ZwWriteVirtualMemory, LoadLibraryExW, CreateProcessInternalW )以及對抗策略, 其實這個只是hook & unhook 的游戲了..
[ [小議PE病毒技術]|[ http://blog.vckbase.com/windowssky/archive/2007/04/17.html ] ] 介紹了 pe 病毒 & win32 進程加載內部
[ [360安全衛士程序員志愿者]|[ http://blog.csdn.net/dedodong/archive/2006/10/07/1323925.aspx ] ] 通過攔截 NtCreateProcessEx/NtCreateProcess 實現了"""編寫一個程序,在此程序中運行a.exe,并使得a.exe認為是由explorer.exe運行它的"""
[ [阻擊惡意軟件--清除和保護你的網站的小技巧]|[ http://www.googlechinawebmaster.com/labels/badware.html ] ] google 上的對惡意軟件(badware) 的介紹
[ [StopBadware Blog]|[ http://blogs.stopbadware.org/articles/2007/11 ] ]
You have been using MSN for quite some time wondering how it works. Well You need not look any further. This article will not just tell you how MSN works but will also tell you how to make your own version of MSN messenger. You can download a sample application from here MSN Clone .Let's get ready to rumble!!!!
We can split up the working of MSN messenger into 2 phases
-
Authentication Phase
-
Instant Messaging Phase
The Authentication Phase involves logging into the MSN messenger server and also (friends) list retrieval in this case.
The Instant Messaging Phase involves sending/accepting requests for an Instant Messaging session and also sending/receiving messages.
The MSN messenger protocol is an ASCII based protocol. In other words the commands are in pure English !!!.The first phase involves connecting to an MSN messenger server .In this case we shall connect to the server 64.4.13.58 on port 1863(MSN messenger works through port 1863).
Once the connection is done we need to start the log in process. The first stage in this phase is the versioning stage. In this stage the client (in this case your app) lists/sends the versions that it can support to the server and waits for the server to respond.
VER 0 MSNP7 MSNP6 MSNP5 MSNP4 CVRO
In the MSN messenger protocol a "trial id" is sent along with every command. The trial id starts from 0 and is incremented every time the server responds successfully to the client's commands.
The server responds like this
VER 0 MSNP7 MSNP6 MSNP5 MSNP4
The Client and the server have agreed on a version in which they will communicate.
Next the client sends a request to the server asking it for the name of the security package it supports for authentication.
INF 1
Unlike Yahoo, Rediff and a few other Messengers MSN does not actually send the password as it is.It encrypts the password while sending it ensuring that your password will not be leaked out easily if somebody monitors your port.
The server responds with this
INF 1 MD5
Here MD5 is the name of the security package which the server currently supports.
Next the client sends the userid to the server
USR 2 MD5 I venky_dude@hotmail.com
Here the server does a check whether it contains all the relevant details about the user for authentication .If it does not then it sends the following reply
XFR 2 NS 64.4.13.55:1863 0
What the server says is that the client should connect to the Notification Server(NS) on 64.4.13.55 on port 1863. We close the current connection and repeat the steps while being connected to the new server i.e 64.4.13.55
-
(client) VER 3 MSNP7 MSNP6 MSNP5 MSNP4 CVRO
-
(server) VER 3 MSNP7 MSNP6 MSNP5 MSNP4
-
(client) INF 4
-
(server) INF 4 MD5
-
(client) USR 5 MD5 I venky_dude@hotmail.com
Now the server to which we are connected to has the relevant information about the user trying to log in. The server replies this way
USR 5 MD5 S 989048851.1851137130
The string which is sent by the server is the " MD5 Hash". It is a hash generated by the server and is used in the authentication process. The client then has to send the password which is encrypted using the MD5 algorithm.In effect the client has to send the unique MD5 equivalent of the MD5 hash i.e 989048851.1851137130 in this case and the password combined .i.e. MD5 equivalent of (hash+pass). In this case it turns out to be 3b7926d277068ec49576a0c40598ff21.
USR 6 MD5 S 3b7926d277068ec49576a0c40598ff21
If the password is right then the server replies with this
USR 6 OK venky_dude@hotmail.com venkat
Here the last word is the nickname/name by which the user is known.
In the new version of the protocol (MSNP7) the server sends additional data like some general information about the user and a authentication code something similar to a cookie which can be used for various other functions.
MSG Hotmai Hotmail 362
MIME-Version: 1.0
Content-Type: text/x-msmsgspro file; charset=UT
LoginTime: 1011252477
EmailEnabled: 1
MemberIdHigh: 84736
MemberIdLow: - 1434729391
lang _preference: 103
preferredEmai l: venky_dude@hotmail.com
country: IN
PostalCode:
Gender: M
Kid:0
Age: 22
sid: 517
kv: 2
MSPAuth: 2AAAAAAAADU0p4uxxxJtDJozJSlUTS0i7YpwnC9PUHRv56YKxxxCTWmg$$
Now we are logged into the server but our status is still offline. We need to change our status to online in order to send and receive messages. The client does this in the following way
CHG 7 NLN
The server replies with friends who are online and in various states.
CHG 7 NLN
ILN 7 NLN btxxxe@hotmail.com nick
ILN 7 AWY wmpyxxx@msn.com mike
ILN 7 BSY tehpxxpxx@hotmail.com yeaxxx
MSG Hotmail Hotmail 223
MIME-Version: 1.0
Content-Type: text/x-msmsgsinitialemailnotification; charset=UTF-8
Inbox-Unread: 293
Folders-Unread: 0
Inbox-URL: /cgi-bin/HoTMaiL
Folders-URL: /cgi-bin/folders
Post-URL: http://www.hotmail.com
The next command to be sent to the server pertains to the version of the client currently being used.The client send to the server it's version number and also information about the machine like the OS and the build.
CVR 8 0x0409 win 4.10 i386 MSMSGS 4.5.0127 MSMSGS
Here 0x409 win 4.10 i386 specifies that the client is running win98 on a intel microprocessor, and MSMSGS 4.5.0127 MSMSGS here specifies the version and build no of msmsgs.exe (basically the version no of MSN messenger).
The server responds with the url to download the latest version and some other info
CVR 8 4.5.0127 4.5.0127 1.0.0863 http://download.microsoft.com/download/msnmessenger/install/4.5/win98me/en-us/mmssetup.exe http://messenger.microsoft.com
It is not necesarry to send the CVR command, the messenger protocol will function properly regardless of this command being sent
To get a list of people who are in our friends list we may send this command
LST 9 RL
On sending this command the server will reply by sending the reverse list .The reverse list is basically a list of users who can see you when you are online and send you a message.You could alternatively also request for the forward list by sending LST 9 FL .The forward list contains a list of all users whom the user has added to his/her list.
The server responds this way
LST 9 RL 69 1 19 venky_dude@hotmail.com venkat
LST 9 RL 69 2 19 puxxxxx@hotmail.com PUJA
LST 9 RL 69 3 19 vancxxxxx@hotmail.com ramachandran
LST 9 RL 69 4 19 moxxxxx@hotmail.com chandramouli
LST 9 RL 69 5 19 v_n_xxxxx@hotmail.com Narayanaswamy
LST 9 RL 69 6 19 dexxxxx@hotmail.com Venkatesh
LST 9 RL 69 7 19 lousydxxxxx@hotmail.com deepika%20kalyani%20Vairam LST 9 RL 69 8 19 hexxxxxr@hotmail.com Hetchar%20Ramachandran
LST 9 RL 69 9 19 ambxxxxx@hotmail.com Aiyer
LST 9 RL 69 10 19 suxxx@hotmail.com Ganesh
LST 9 RL 69 11 19 deexxxxx@hotmail.com Deepak
LST 9 RL 69 12 19 anilxxxxx@hotmail.com anil
LST 9 RL 69 13 19 dixxxxx@hotmail.com <Diamond>
LST 9 RL 69 14 19 nvxxxx@hotmail.com giri
LST 9 RL 69 15 19 shxxx@hotmail.com Hari
LST 9 RL 69 16 19 radhikashuxxxxx@hotmail.com radhika
LST 9 RL 69 17 19 eskaxxxxx@hotmail.com kannan
LST 9 RL 69 18 19 shaxxxxx@hotmail.com Shankar
LST 9 RL 69 19 19 puneetagarxxxxx@hotmail.com puneet
*Every time a friend comes online the server(NS) sends us the following command
NLN 10NLN deaxxxx@hotmail.com Venkatesh
and when the friend goes offline the server sends us this
FLN 10 FLN deaxxxx@hotmail.com
With the MSNP7 protocol msn has introduced a new challenege authentication mechanism. The MSN server sends t a challenge key which the user has to authenticate succesfully in order for the session to continue.
CHL 0 20881396011366812350
The client has to send the md5 equivalent of this string which is formed by appending this hash with the string "Q1P7W2E4J9R8U3S5".So the final string which will be sent to the server will be the md5 equivalent of 20881396011366812350Q1P7W2E4J9R8U3S5
i.e MD5string(20881396011366812350Q1P7W2E4J9R8U3S5 )
So the client response would be something like this
QRY 18 msmsgs@msnmsgr.com 32
0212eaad0876afb8505859ca75d21a78
Here 18 is the trial id .Replace it by the appropriate trial id in your program .
The server will respond in the following way if the authentication is right
QRY 18
We have successfully logged into the MSN Messenger server. The Instant Messaging phase is next.
Instant Messaging in MSN Messenger is session based . The people in between whom the conversation is going to take place have to be in a session mode. We cannot send/receive messages unless we start a chat session with a user.
There are basically two methods in which a user can be in a chat session
- User sends a chat session request to another user
- User receives a chat session request from another user
User sends a chat session request
The client(user) sends a command to the server asking it for the address of the SwitchBoard(SB) server. All instant messaging conversation take place via the switchboard server.
XFR 9 SB
The server(SB) replies back with the ip address of the switchboard server(SB),the port on which to connect and a CKI hash. CKI is a security package and the client has to use the hash to connect to the switchboard server.
XFR 9 SB 64.4.13.88:1863 CKI 989487642.2070896604
Now we have to make another new connection this time to the switchboard server. Our previous connection to the MSN messenger server must be kept as it is. If we lose connection with that server we would log out.
After we have connected to the switchboard server(SB) we send the following command to the switchboard server.
USR 1 venky_dude@hotmail.com 989487642.2070896604
If the CKI hash sent by us is right the server(SB) responds back with this
USR 1 OK venky_dude@hotmail.com venkat
After this has been done the user has to "Call" the other user to the chat session. This is done by sending the following command.
CAL 2 deadxxx@hotmail.com
The server replies back with the a session id which it will pass on to the other user
CAL 2 RINGING 11717653
When the other user replies and is ready for a chat the server(SB) sends us this command
JOI deadlee@hotmail.com Venkatesh
This indicates that the other user has joined in the conversation and we are now ready to send and receive messages.
User receives a chat session request
When we are being invited to a chat session by a user the server(NS) send us the following message.
RNG 11742066 64.4.13.74:1863 CKI 989495494.750408580 deaxxxx@hotmail.com Venkatesh
Here the server(NS) sends us the session id ,the ip address of the SwitchBoard server to connect to,the port on which to connect to ,the CKI hash and the user trying to start a conversation with us.
Now we have to make another new connection this time to the switchboard server. Our previous connection to the MSN messenger server must be kept as it is. If we loose connection with that server we would log out.
We connect to the switchboard server and send the following command
ANS 1 venky_dude@hotmail.com 989495494.750408580 11742066
Here we send our login name ,the CKI hash that was sent to us and the session Id that was sent to us
The server responds back with
IRO 1 1 1 deaxxxx@hotmail.com Venkatesh
and
ANS 1 OK
We are now ready to send and receive messages.
Before sending/receiving messages let us see how the message is constructed.
When we are sending a message we build the header information in the following way
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
X-MMS-IM-Format: FN=Microsoft%20Sans%20Serif; EF=; CO=0; CS=0; PF=22
While sending a message we send it this way
MSG 2 N 137 MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
X-MMS-IM-Format: FN=Microsoft%20Sans%20Serif; EF=; CO=0; CS=0; PF=22
hello
Here 2 is the trial id which has to incremented each time we send a message. 137 is the total length of the message i.e length of the header and length of the actual message that we are sending in this case it is 'hello'.
While receiving the message it is more or less similar
Here is an example of a message received
MSG deaxxxx@hotmail.com Venkatesh 137
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
X-MMS-IM-Format: FN=Microsoft%20Sans%20Serif; EF=; CO=0; CS=0; PF=22
hello
When the other user is typing a message we receive the foll message
MSG deaxxxx@hotmail.com Venkatesh 100
MIME-Version: 1.0
Content-Type: text/x-msmsgscontrol
TypingUser: deaxxxx@hotmail.com
I guess now u guys are well on your way to make your own version of MSN messenger.Post your doubts/comments/message in the Forumn .Do visit my projects page for some cool vb & c++ codes
I'll keep adding to the protocol ,will try to put in addition functions like add/remove users ,rename user id,file transfer and voice chat, so keep checking back
References:
You could take a look at these sites for more information
This is the original protocol published by microsoft.
http://www.tlsecurity.net/Textware/Misc/draft-movva-msn-messenger-protocol-00.txt
This is the MD5 homepage where u can find programs/codes for doing the MD5 encryption
http://userpages.umbc.edu/~mabzug1/cs/md5/md5.html
MSNP10協議分析 01.登錄 [by progsoft]
http://blog.csdn.net/progsoft/archive/2004/08/24/82938.aspxMSN協議中文釋義(Zz)
http://blog.csdn.net/fanccYang/archive/2005/03/16/321198.aspxMSN Protocol Version 8
http://msnpiki.msnfanatic.com/index.php/Main_Page介紹了MSNP 的整個協議 & 服務器 Notification Server (NS) & Switchboard (SB) 的功能
MSN Messenger Protocol Version 9
http://zoronax.bot2k3.net/很詳細的介紹, 里面還有原始包例子
MSN Messenger Protocol
http://www.hypothetic.org/docs/msn/client/invitation_types.phpMSNP 的詳細命令介紹
Reverse-engineering MSN Messenger's Video Conversation Formats[Ramiro Polla]
http://www.libing.net.cn/read.php/1031.htm