📄 spwdspy.html
字号:
<!-- saved from url=(0022)http://internet.e-mail -->
<!--#include virtual="header.shtml" -->
<!--#include virtual="/global/getCss.inc" -->
<center><h3><font color="#a0a099" face="Times New Roman">
Peeking into Password Edit & Internet Explorer - Super Password Spy++
</font></h3></center><hr>
<!-- Author and contact details -->
<p><font face="Times New Roman"><img src="SPwdSpyPortrait.jpg" width="80">
This article was contributed by <a href="mailto:codetiger@hotmail.com">Zhefu Zhang</a>.</font></p>
<p><font face="Times New Roman"><u>Environment:</u> VC6/VC7, MS Platform Core SDK, IE4.0+, <b>WinNT/2K/XP ONLY</b> (Test has passed on English/Chinese/Japanese Win2k/XP with IE6.0+SP1)</font></p>
<p><font face="Times New Roman"><u>Key Technology Used:</u> Windows Hook, IE COM Object, Win2k Security Context</font></p>
<p><font face="Times New Roman"><u>Applicable Article Category in CodeGuru:</u> IE Programming, Tool, System, Miscellaneous</font></p>
<h3><font face="Times New Roman">Summary</font></h3>
<p><font face="Times New Roman">If you need a tool to peek not only the password edit boxes on different programs, but also the **** password input field on a homepage residing inside Internet Explorer, here it is—<b><font color="#000080">SuperPasswordSpy++</font></b>.</font></p>
<p><font face="Times New Roman">With Windows Hook, it is not difficult to peek into a remote process's password edit, but how about the password input field inside a homepage, say, in an Internet Explorer window? The password input field, obviously, is NOT a window; you have to turn to the IHTMLDocument2 interface to enumerate the inside stuff and extract the password. This program will show you how to do with both the password edit common control and the password input field inside IE. Following is the screen shot of the program running, peeking the password in both cases.</font></p>
<p><a href="SPwdSpyInterface1.jpg" target="newframe">
<font face="Times New Roman"><br>
</font>
<P><font face="Times New Roman"><IMG src="SPwdSpyInterface1.jpg" width="500"><br>
<font size="3"><em>Click here for a larger image.</em></font> </font>
</a></p>
<p><font face="Times New Roman"><em>Figure 1: SuperPasswordSpy++ Peeking a Hotmail "Forget Your Password" Page</em></font></p>
<p><font face="Times New Roman">
<img src="SPwdSpyInterface2.jpg" width="350" height="341"></font></p>
<p><font face="Times New Roman"><em>Figure 2: SuperPasswordSpy++ Peeking a Password Edit inside IIS 5.0 on WinXP</em></font></p>
<h3><font face="Times New Roman">Architecture</font></h3>
<p><font face="Times New Roman">After the user begins to drag the magnifying glass around on the screen, the program captures the mouse and keeps track of the mouse move message. Whenever the mouse moves onto a new window, it checks the windows class name and window style to decide whether it is a password edit or an IE. (Note: actually, it is a Web browser control; I use IE for short.) In the latter case, a hook DLL has to be injected into the IE immediately to decide whether the IE contains a password input field.</font></p>
<p><font face="Times New Roman"><b>Please Note:</b> Here we have two options to implement the hook:</font></p>
<p><font face="Times New Roman"><u>The first</u> way is setting a WH_GETMESSAGE hook, which was done by Mr. Brian Friesen in his article <a href="http://www.codeguru.com/samples/pwdspy.html">PasswordSpy</a> in the Samples Section of <a href="http://www.codeguru.com">www.codeguru.com</a>. This hook DLL will intercept the posted message and a synchronization object (mutex, event, and so on) will be applied inside it. Please refer to Figure 3:</font></p>
<p><font face="Times New Roman">
<img src="SPwdSpyArchitecture.gif" width="418" height="182"></font></p>
<p><font face="Times New Roman"><em>Figure 3: General Spy Program's Hook Architecture</em></font></p>
<p><font face="Times New Roman">There are 5 steps involved:</font></p>
<ol>
<li><font face="Times New Roman">The Spy program injects a hook DLL into the Target program.</font></li>
<li><font face="Times New Roman">The Spy program posts a user message to the Target program that will be intercepted by the DLL.</font></li>
<li><font face="Times New Roman">The DLL intercepts the user message and reads the password edit content.</font></li>
<li><font face="Times New Roman">The DLL sends the data back to the Spy program.</font></li>
<li><font face="Times New Roman">The Spy program gets the data and un-hooks the DLL.</font></li>
</ol>
<p><font face="Times New Roman">Because the Spy program posts the user message, Step 2 will not "block" and the Spy program does NOT know when Step 4 will happen. Usually, this kind of Spy program uses WM_COPYDATA to transfer byte data; it is a "Send" message. Okay, here is the tough thing: The user may move the magnify glass here and there, and let's image the following case. The Spy program found a password edit, injected the DLL into the target program, posted the user message, and suddenly the user moved the magnify glass to another password edit, so the Spy program has to un-hook the DLL out of the old target program, hook into the new target program, and post the user message. Unfortunately, the WM_COPYDATA from the old target program has queued into the Spy program's message queue, and if you do not append the target window handle information to the WM_COPYDATA, you cannot tell the data of the current Target program from the old target program...</font></p>
<p><font face="Times New Roman"><u>Another option</u> is setting the WH_CALLWNDPROC hook. It's all the same as the first option, except in Step 2 we send the message instead of posting it to the target program. The hook DLL will intercept the user message and make inside password reading, and call SendMessage with WM_COPYDATA to pass the data to the Spy program. In one word, Step 2 is blocked until Steps 3 and 4 are finished; so, after Step 2 is finished, we can do Step 5 directly. In this way, the code will be much simpler than the first way. It is just like Block Socket and Non-Block Socket programming.</font></p>
<p><font face="Times New Roman">Sure, the second way has its disadvantage. Consider the following case: There are two password edits on the same window and the user must leave one to enter another. In the first way, we can check the two edits are on the same thread, so we can hook the DLL only once. In the second way, we hook twice which means (ignorable here in this program) a performance penalty.</font></p>
<h3><font face="Times New Roman">Implementation Description</font></h3>
<p><font face="Times New Roman"><b>1. How to get IHTMLDocument from Browser Control Window Handle:</b></font></p>
<p><font face="Times New Roman"><b>(with ref. from MSDN KB Q249232 HOWTO: Get IHTMLDocument2 from a HWND)</b></font></p>
<pre><font color="#800000" face="Times New Roman">BOOL HWnd2HtmlDocument()
{</font></pre>
<pre><font face="Times New Roman"><font color="#800000"> CoUninitialize();
HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );
if ( hInst == NULL ) return FALSE;
LRESULT lRes;
UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
::SendMessageTimeout( g_hTarget, nMsg,
0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );
LPFNOBJECTFROMLRESULT pfObjectFromLresult =
(LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst,
"ObjectFromLresult");
if ( pfObjectFromLresult == NULL )
{
::FreeLibrary( hInst );
CoUninitialize();
return FALSE;
}
WCHAR strDoc[] = L"{626fc520-a41e-11cf-a731-00a0c9082637}";
</font><font color="#008000"><span class="codeComment">//IID_IHTMLDocument2 CLSID</span></font><font color="#800000">
CLSID uuidDoc;
HRESULT hrDoc = CLSIDFromString((LPOLESTR)strDoc,
&uuidDoc <span class="codeComment">//IID_IHTMLDocument2</span>
);
if(!SUCCEEDED(hrDoc))
{
::FreeLibrary( hInst );
CoUninitialize();
return FALSE;
}
HRESULT hr = (*pfObjectFromLresult)( lRes, uuidDoc,
</font><font color="#008000"><span class="codeComment">//IID_IHTMLDocument,</span></font><font color="#800000">
0, (void**)&g_lpHTMLDocument2);
if ( SUCCEEDED(hr) )
{
</font><font color="#008000"><span class="codeComment">//OK, We Get Here Successfully</span></font><font color="#800000">
}
else
{
::FreeLibrary( hInst );
CoUninitialize();
return FALSE;
}
::FreeLibrary( hInst );
CoUninitialize();
return TRUE;
}</font>
</font></pre>
<p><font face="Times New Roman">There is something I need to explain here: g_hTarget is the handle of the browser, and its class name is "Internet Explorer_Server." Usually, it is not a problem with MS Internet Explorer, which always starts to navigate somewhere when it starts. But, in some applications hosting a Web Browser with ActiveX, before the browser navigates, the "Internet Explorer_Server" window does <b><font color="#000080">NOT</font></b> exist. Let me give you an example:</font></p>
<p><font face="Times New Roman">
<img src="SPwdSpyIE-class.gif" border="1" width="292" height="79"></font></p>
<p><font face="Times New Roman"><em>Figure 4: Spy++'s Screen Shot on a Dialog Hosting Two MS Web Browser ActiveXs</em></font></p>
<p><font face="Times New Roman">It is a screen shot from Spy++. There are two browsers on this dialog (00060294); only browser 000202D6 navigates to some URL. You can make an experiment by putting a WebBrowser ActiveX on a dialog, and check this by Spy++.</font></p>
<p><font face="Times New Roman"><b>2. Whether Current Homepage contains a Password Input Field:</b></font></p>
<pre><font face="Times New Roman"><font color="#800000">DWORD CheckHtmlDocument()
</font><font color="#008000"><span class="codeComment">//Ret: 0 -- No Password Input; Else -- Password Input Number</span></font><font color="#800000">
{
MSHTML::IHTMLElementCollection *pForm;
HRESULT hr = g_lpHTMLDocument2->get_all(&pForm);
<span class="codeComment"> </span></font><span class="codeComment"><font color="#008000">//g_lpHTMLDocument2 is a pointer of IHTMLDocument2</font></span><font color="#800000">
if(FAILED(hr)) return 0;
long len;
pForm->get_length(&len); </font><font color="#008000"><span class="codeComment">//How many elements on this form?</span></font><font color="#800000">
DWORD dwRet = 0;
for(int i = 0; i < len; i++)
{
LPDISPATCH lpItem = pForm->item(CComVariant(i), CComVariant(i));
MSHTML::IHTMLInputElementPtr lpInput;
HRESULT hr = lpItem->QueryInterface(&lpInput); <span class="codeComment">//Is it a input field?</span>
if(FAILED(hr)) continue;
_bstr_t type(_T("password"));
if(lpInput->Gettype() == type) </font><font color="#008000"><span class="codeComment">//Check Field Type</span></font><font color="#800000">
{
<span class="codeComment"> </span></font><span class="codeComment"><font color="#008000">//_bstr_t x = lpInput->Getvalue();</font><font color="#800000"> </font><font color="#008000">//If you want its string</font></span><font color="#800000">
dwRet++;
}
lpItem->Release(); </font><font color="#008000"><span class="codeComment">//Remember To Release this!</span></font><font color="#800000">
lpItem = NULL;
}
pForm->Release();
pForm = NULL;
return dwRet;
}</font>
</font></pre>
<p><font face="Times New Roman"><b>3. Extract password from Current Homepage's Password Input Field:</b></font></p>
<pre><font face="Times New Roman"> <font color="#800000">_bstr_t x = lpInput->Getvalue(); <span class="codeComment">//And you go!</span>
LPCTSTR lpWhatEver = (LPCTSTR)x;
<span class="codeComment"> </span></font><span class="codeComment"><font color="#008000">//Do something with the password here</font></span>
</font></pre>
<h3><font face="Times New Roman">Special Note with SuperPasswordSpy++</font></h3>
<p><font face="Times New Roman"><font color="#000080"><b>Once again, SuperPasswordSpy++ is for WinNT/2K/XP only and IE 4.0+ is needed; it is an Unicode-based Program</b></font>.</font></p>
<p><font face="Times New Roman">Interested readers can learn how to track window and hook remote processes by studying the source code of <b><font color="#000080">SuperPasswordSpy++</font></b>. It is highly recommended the readers study MS Platform SDK's Spy sample and <a href="http://www.codeguru.com/samples/pwdspy.html">PasswordSpy</a> of Mr. Brian Friesen. The author dislikes "recreating the wheel" things, and borrows mouse tracking code from Spy sample and a function "SmallestWindowFromPoint," plus the resource file from PasswordSpy. And, if the reader has questions about the hook technique, please read Mr. Brian Friesen's article; it is already detailed enough. In addition, readers can read Chapters 19 and 20 of <i>Programming Application for MS Windows</i>, 4th Edition written by Jeffrey Richter, published by Microsoft Press, 1999, for more detailed information about Shared Sections in DLLs.</font></p>
<p><font face="Times New Roman">Well, besides, something unclear with <font color="#000080"><b>SuperPasswordSpy++</b>'</font>s implementation does exist:</font></p>
<p><font face="Times New Roman"><b>1. Criteria of Judging Password Edit</b></font></p>
<pre><font color="#800000" face="Times New Roman">BOOL IsPasswordEdit(HWND hWnd)
{
TCHAR szClassName[64];
int nRet = GetClassName(hWnd, szClassName, 64);
if(nRet == 0) return FALSE;
szClassName[nRet] = 0;
if(::lstrcmp(szClassName, _T("Edit")) != 0 && ::lstrcmp(szClassName, _T("TEdit")) != 0</font></pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -