屏幕取詞的實(shí)現(xiàn)方法(Windows 9x)
有關(guān)屏幕取詞
"鼠標(biāo)屏幕取詞"技術(shù)是在電子字典中得到廣泛地應(yīng)用的,如四通利方和金山詞霸等軟件,這個(gè)技術(shù)看似簡(jiǎn)單,其實(shí)在windows系統(tǒng)中實(shí)現(xiàn)卻是非常復(fù)雜的,總的來(lái)說(shuō)有兩種實(shí)現(xiàn)方式:
***種:采用截獲對(duì)部分gdi的api調(diào)用來(lái)實(shí)現(xiàn),如textout,textouta等。
第二種:對(duì)每個(gè)設(shè)備上下文(dc)做一分copy,并跟蹤所有修改上下文(dc)的操作。
第二種方法更強(qiáng)大,但兼容性不好,而***種方法使用的截獲windowsapi的調(diào)用,這項(xiàng)技術(shù)的強(qiáng)大可能遠(yuǎn)遠(yuǎn)超出了您的想象,毫不夸張的說(shuō),利用 windowsapi攔截技術(shù),你可以改造整個(gè)操作系統(tǒng),事實(shí)上很多外掛式windows中文平臺(tái)就是這么實(shí)現(xiàn)的!而這項(xiàng)技術(shù)也正是這篇文章的主題。
截windowsapi的調(diào)用,具體的說(shuō)來(lái)也可以分為兩種方法:
***種方法通過(guò)直接改寫(xiě)winapi 在內(nèi)存中的映像,嵌入?yún)R編代碼,使之被調(diào)用時(shí)跳轉(zhuǎn)到指定的地址運(yùn)行來(lái)截獲;第二種方法則改寫(xiě)iat(import address table輸入地址表),重定向winapi函數(shù)的調(diào)用來(lái)實(shí)現(xiàn)對(duì)winapi的截獲。
***種方法的實(shí)現(xiàn)較為繁瑣,而且在win95、98下面更有難度,這是因?yàn)殡m然微軟說(shuō)win16的api只是為了兼容性才保留下來(lái),程序員應(yīng)該盡可能地調(diào)用 32位的api,實(shí)際上根本就不是這樣!win 9x內(nèi)部的大部分32位api經(jīng)過(guò)變換調(diào)用了同名的16位api,也就是說(shuō)我們需要在攔截的函數(shù)中嵌入16位匯編代碼!
我們將要介紹的是第二種攔截方法,這種方法在win95、98和nt下面運(yùn)行都比較穩(wěn)定,兼容性較好。由于需要用到關(guān)于windows虛擬內(nèi)存的管理、打破進(jìn)程邊界墻、向應(yīng)用程序的進(jìn)程空間中注入代碼、pe(portable executable)文件格式和iat(輸入地址表)等較底層的知識(shí),所以我們先對(duì)涉及到的這些知識(shí)大概地做一個(gè)介紹,***會(huì)給出攔截部分的關(guān)鍵代碼。
先說(shuō)windows虛擬內(nèi)存的管理。windows9x給每一個(gè)進(jìn)程分配了4gb的地址空間,對(duì)于nt來(lái)說(shuō),這個(gè)數(shù)字是2gb,系統(tǒng)保留了2gb 到 4gb之間的地址空間禁止進(jìn)程訪問(wèn),而在win9x中,2gb到4gb這部分虛擬地址空間實(shí)際上是由所有的win32進(jìn)程所共享的,這部分地址空間加載了共享win32 dll、內(nèi)存映射文件和vxd、內(nèi)存管理器和文件系統(tǒng)碼,win9x中這部分對(duì)于每一個(gè)進(jìn)程都是可見(jiàn)的,這也是win9x操作系統(tǒng)不夠健壯的原因。 win9x中為16位操作系統(tǒng)保留了0到4mb的地址空間,而在4mb到2gb之間也就是win32進(jìn)程私有的地址空間,由于每個(gè)進(jìn)程的地址空間都是相對(duì)獨(dú)立的,也就是說(shuō),如果程序想截獲其它進(jìn)程中的api調(diào)用,就必須打破進(jìn)程邊界墻,向其它的進(jìn)程中注入截獲api調(diào)用的代碼,這項(xiàng)工作我們交給鉤子函數(shù)(setwindowshookex)來(lái)完成,關(guān)于如何創(chuàng)建一個(gè)包含系統(tǒng)鉤子的動(dòng)態(tài)鏈接庫(kù),《電腦高手雜志》在第?期已經(jīng)有過(guò)專(zhuān)題介紹了,這里就不贅述了。所有系統(tǒng)鉤子的函數(shù)必須要在動(dòng)態(tài)庫(kù)里,這樣的話,當(dāng)進(jìn)程隱式或顯式調(diào)用一個(gè)動(dòng)態(tài)庫(kù)里的函數(shù)時(shí),系統(tǒng)會(huì)把這個(gè)動(dòng)態(tài)庫(kù)映射到這個(gè)進(jìn)程的虛擬地址空間里,這使得dll成為進(jìn)程的一部分,以這個(gè)進(jìn)程的身份執(zhí)行,使用這個(gè)進(jìn)程的堆棧,也就是說(shuō)動(dòng)態(tài)鏈接庫(kù)中的代碼被鉤子函數(shù)注入了其它gui 進(jìn)程的地址空間(非gui進(jìn)程,鉤子函數(shù)就無(wú)能為力了),當(dāng)包含鉤子的dll注入其它進(jìn)程后,就可以取得映射到這個(gè)進(jìn)程虛擬內(nèi)存里的各個(gè)模塊(exe和 dll)的基地址,如:hmodule hmodule=getmodulehandle("mypro.exe");在mfc程序中,我們可以用afxgetinstancehandle() 函數(shù)來(lái)得到模塊的基地址。exe和dll被映射到虛擬內(nèi)存空間的什么地方是由它們的基地址決定的。它們的基地址是在鏈接時(shí)由鏈接器決定的。當(dāng)你新建一個(gè) win32工程時(shí),vc++鏈接器使用缺省的基地址0x00400000??梢酝ㄟ^(guò)鏈接器的base選項(xiàng)改變模塊的基地址。exe通常被映射到虛擬內(nèi)存的 0x00400000處,dll也隨之有不同的基地址,通常被映射到不同進(jìn)程的相同的虛擬地址空間處。
系統(tǒng)將exe和dll原封不動(dòng)映射到虛擬內(nèi)存空間中,它們?cè)趦?nèi)存中的結(jié)構(gòu)與磁盤(pán)上的靜態(tài)文件結(jié)構(gòu)是一樣的。即pe (portable executable) 文件格式。我們得到了進(jìn)程模塊的基地址以后,就可以根據(jù)pe文件的格式窮舉這個(gè)模塊的image_import_descriptor數(shù)組,看看進(jìn)程空間中是否引入了我們需要截獲的函數(shù)所在的動(dòng)態(tài)鏈接庫(kù),比如需要截獲"textouta",就必須檢查"gdi32.dll"是否被引入了。說(shuō)到這里,我們有必要介紹一下pe文件的格式,如右圖,這是pe文件格式的大致框圖,最前面是文件頭,我們不必理會(huì),從pe file optional header后面開(kāi)始,就是文件中各個(gè)段的說(shuō)明,說(shuō)明后面才是真正的段數(shù)據(jù),而實(shí)際上我們關(guān)心的只有一個(gè)段,那就是".idata"段,這個(gè)段中包含了所有的引入函數(shù)信息,還有iat(import address table)的rva(relative virtual address)地址。
說(shuō)到這里,截獲windowsapi的整個(gè)原理就要真相大白了。實(shí)際上所有進(jìn)程對(duì)給定的api函數(shù)的調(diào)用總是通過(guò)pe文件的一個(gè)地方來(lái)轉(zhuǎn)移的,這就是一個(gè)該模塊(可以是exe或dll)的".idata"段中的iat輸入地址表(import address table)。在那里有所有本模塊調(diào)用的其它dll的函數(shù)名及地址。對(duì)其它dll的函數(shù)調(diào)用實(shí)際上只是跳轉(zhuǎn)到輸入地址表,由輸入地址表再跳轉(zhuǎn)到dll真正的函數(shù)入口。
具體來(lái)說(shuō),我們將通過(guò)image_import_descriptor數(shù)組來(lái)訪問(wèn)".idata"段中引入的dll的信息,然后通過(guò)image_thunk_data數(shù)組來(lái)針對(duì)一個(gè)被引入的dll訪問(wèn)該dll中被引入的每個(gè)函數(shù)的信息,找到我們需要截獲的函數(shù)的跳轉(zhuǎn)地址,然后改成我們自己的函數(shù)的地址……
廢話不說(shuō)了,下面提供一個(gè)屏幕取詞具體的實(shí)現(xiàn)方法。
1. 安裝鼠標(biāo)鉤子,通過(guò)鉤子函數(shù)獲得鼠標(biāo)消息。
使用到的api函數(shù):setwindowshookex
2. 得到鼠標(biāo)的當(dāng)前位置,向鼠標(biāo)下的窗口發(fā)重畫(huà)消息,讓它調(diào)用系統(tǒng)函數(shù)重畫(huà)窗口。
使用到的api函數(shù):windowfrompoint,screentoclient,invalidaterect
3. 截獲對(duì)系統(tǒng)函數(shù)的調(diào)用,取得參數(shù),也就是我們要取的詞。
對(duì)于大多數(shù)的windows應(yīng)用程序來(lái)說(shuō),如果要取詞,我們需要截獲的是"gdi32.dll"中的"textouta"函數(shù)。
我們先仿照textouta函數(shù)寫(xiě)一個(gè)自己的mytextouta函數(shù),如:
- bool winapi mytextouta(hdc hdc, int nxstart, int nystart, lpcstr lpszstring,int cbstring)
- {
- // 這里進(jìn)行輸出lpszstring的處理
- // 然后調(diào)用正版的textouta函數(shù)
- }
把這個(gè)函數(shù)放在安裝了鉤子的動(dòng)態(tài)連接庫(kù)中,然后調(diào)用我們***給出的hookimportfunction函數(shù)來(lái)截獲進(jìn)程對(duì)textouta函數(shù)的調(diào)用,跳轉(zhuǎn)到我們的mytextouta函數(shù),完成對(duì)輸出字符串的捕捉。hookimportfunction的用法:
- hookfuncdesc hd;
- proc porigfuns;
- hd.szfunc="textouta";
- hd.pproc=(proc)mytextouta;
- hookimportfunction (afxgetinstancehandle(),"gdi32.dll",&hd,porigfuns);
下面給出了hookimportfunction的源代碼,相信詳盡的注釋一定不會(huì)讓您覺(jué)得理解截獲到底是怎么實(shí)現(xiàn)的很難,ok,let s go:
- ///////////////////////////////////////////// begin ///////////////////////////////////////////////////////////////
- #include <crtdbg.h>
- // 這里定義了一個(gè)產(chǎn)生指針的宏
- #define makeptr(cast, ptr, addvalue) (cast)((dword)(ptr)+(dword)(addvalue))
- // 定義了hookfuncdesc結(jié)構(gòu),我們用這個(gè)結(jié)構(gòu)作為參數(shù)傳給hookimportfunction函數(shù)
- typedef struct tag_hookfuncdesc
- {
- lpcstr szfunc; // the name of the function to hook.
- proc pproc; // the procedure to blast in.
- } hookfuncdesc , * lphookfuncdesc;
- // 這個(gè)函數(shù)監(jiān)測(cè)當(dāng)前系統(tǒng)是否是windownt
- bool isnt();
- // 這個(gè)函數(shù)得到hmodule -- 即我們需要截獲的函數(shù)所在的dll模塊的引入描述符(import descriptor)
- pimage_import_descriptor getnamedimportdescriptor(hmodule hmodule, lpcstr szimportmodule);
- // 我們的主函數(shù)
- bool hookimportfunction(hmodule hmodule, lpcstr szimportmodule,
- lphookfuncdesc pahookfunc, proc* paorigfuncs)
- {
- /////////////////////// 下面的代碼檢測(cè)參數(shù)的有效性 ////////////////////////////
- _assert(szimportmodule);
- _assert(!isbadreadptr(pahookfunc, sizeof(hookfuncdesc)));
- #ifdef _debug
- if (paorigfuncs) _assert(!isbadwriteptr(paorigfuncs, sizeof(proc)));
- _assert(pahookfunc.szfunc);
- _assert(*pahookfunc.szfunc != \0 );
- _assert(!isbadcodeptr(pahookfunc.pproc));
- #endif
- if ((szimportmodule == null) || (isbadreadptr(pahookfunc, sizeof(hookfuncdesc))))
- {
- _assert(false);
- setlasterrorex(error_invalid_parameter, sle_error);
- return false;
- }
- //////////////////////////////////////////////////////////////////////////////
- // 監(jiān)測(cè)當(dāng)前模塊是否是在2gb虛擬內(nèi)存空間之上
- // 這部分的地址內(nèi)存是屬于win32進(jìn)程共享的
- if (!isnt() && ((dword)hmodule >= 0x80000000))
- {
- _assert(false);
- setlasterrorex(error_invalid_handle, sle_error);
- return false;
- }
- // 清零
- if (paorigfuncs) memset(paorigfuncs, null, sizeof(proc));
- // 調(diào)用getnamedimportdescriptor()函數(shù),來(lái)得到hmodule -- 即我們需要
- // 截獲的函數(shù)所在的dll模塊的引入描述符(import descriptor)
- pimage_import_descriptor pimportdesc = getnamedimportdescriptor(hmodule, szimportmodule);
- if (pimportdesc == null)
- return false; // 若為空,則模塊未被當(dāng)前進(jìn)程所引入
- // 從dll模塊中得到原始的thunk信息,因?yàn)閜importdesc->firstthunk數(shù)組中的原始信息已經(jīng)
- // 在應(yīng)用程序引入該dll時(shí)覆蓋上了所有的引入信息,所以我們需要通過(guò)取得pimportdesc->originalfirstthunk
- // 指針來(lái)訪問(wèn)引入函數(shù)名等信息
- pimage_thunk_data porigthunk = makeptr(pimage_thunk_data, hmodule,
- pimportdesc->originalfirstthunk);
- // 從pimportdesc->firstthunk得到image_thunk_data數(shù)組的指針,由于這里在dll被引入時(shí)已經(jīng)填充了
- // 所有的引入信息,所以真正的截獲實(shí)際上正是在這里進(jìn)行的
- pimage_thunk_data prealthunk = makeptr(pimage_thunk_data, hmodule, pimportdesc->firstthunk);
- // 窮舉image_thunk_data數(shù)組,尋找我們需要截獲的函數(shù),這是最關(guān)鍵的部分!
- while (porigthunk->u1.function)
- {
- // 只尋找那些按函數(shù)名而不是序號(hào)引入的函數(shù)
- if (image_ordinal_flag != (porigthunk->u1.ordinal & image_ordinal_flag))
- {
- // 得到引入函數(shù)的函數(shù)名
- pimage_import_by_name pbyname = makeptr(pimage_import_by_name, hmodule,
- porigthunk->u1.addressofdata);
- // 如果函數(shù)名以null開(kāi)始,跳過(guò),繼續(xù)下一個(gè)函數(shù)
- if ( \0 == pbyname->name[0])
- continue;
- // bdohook用來(lái)檢查是否截獲成功
- bool bdohook = false;
- // 檢查是否當(dāng)前函數(shù)是我們需要截獲的函數(shù)
- if ((pahookfunc.szfunc[0] == pbyname->name[0]) &&
- (strcmpi(pahookfunc.szfunc, (char*)pbyname->name) == 0))
- {
- // 找到了!
- if (pahookfunc.pproc)
- bdohook = true;
- }
- if (bdohook)
- {
- // 我們已經(jīng)找到了所要截獲的函數(shù),那么就開(kāi)始動(dòng)手吧
- // 首先要做的是改變這一塊虛擬內(nèi)存的內(nèi)存保護(hù)狀態(tài),讓我們可以自由存取
- memory_basic_information mbi_thunk;
- virtualquery(prealthunk, &mbi_thunk, sizeof(memory_basic_information));
- _assert(virtualprotect(mbi_thunk.baseaddress, mbi_thunk.regionsize,
- page_readwrite, &mbi_thunk.protect));
- // 保存我們所要截獲的函數(shù)的正確跳轉(zhuǎn)地址
- if (paorigfuncs)
- paorigfuncs = (proc)prealthunk->u1.function;
- // 將image_thunk_data數(shù)組中的函數(shù)跳轉(zhuǎn)地址改寫(xiě)為我們自己的函數(shù)地址!
- // 以后所有進(jìn)程對(duì)這個(gè)系統(tǒng)函數(shù)的所有調(diào)用都將成為對(duì)我們自己編寫(xiě)的函數(shù)的調(diào)用
- prealthunk->u1.function = (pdword)pahookfunc.pproc;
- // 操作完畢!將這一塊虛擬內(nèi)存改回原來(lái)的保護(hù)狀態(tài)
- dword dwoldprotect;
- _assert(virtualprotect(mbi_thunk.baseaddress, mbi_thunk.regionsize,
- mbi_thunk.protect, &dwoldprotect));
- setlasterror(error_success);
- return true;
- }
- }
- // 訪問(wèn)image_thunk_data數(shù)組中的下一個(gè)元素
- porigthunk++;
- prealthunk++;
- }
- return true;
- }
- // getnamedimportdescriptor函數(shù)的實(shí)現(xiàn)
- pimage_import_descriptor getnamedimportdescriptor(hmodule hmodule, lpcstr szimportmodule)
- {
- // 檢測(cè)參數(shù)
- _assert(szimportmodule);
- _assert(hmodule);
- if ((szimportmodule == null) || (hmodule == null))
- {
- _assert(false);
- setlasterrorex(error_invalid_parameter, sle_error);
- return null;
- }
- // 得到dos文件頭
- pimage_dos_header pdosheader = (pimage_dos_header) hmodule;
- // 檢測(cè)是否mz文件頭
- if (isbadreadptr(pdosheader, sizeof(image_dos_header)) ||
- (pdosheader->e_magic != image_dos_signature))
- {
- _assert(false);
- setlasterrorex(error_invalid_parameter, sle_error);
- return null;
- }
- // 取得pe文件頭
- pimage_nt_headers pntheader = makeptr(pimage_nt_headers, pdosheader, pdosheader->e_lfanew);
- // 檢測(cè)是否pe映像文件
- if (isbadreadptr(pntheader, sizeof(image_nt_headers)) ||
- (pntheader->signature != image_nt_signature))
- {
- _assert(false);
- setlasterrorex(error_invalid_parameter, sle_error);
- return null;
- }
- // 檢查pe文件的引入段(即 .idata section)
- if (pntheader->optionalheader.datadirectory[image_directory_entry_import].virtualaddress == 0)
- return null;
- // 得到引入段(即 .idata section)的指針
- pimage_import_descriptor pimportdesc = makeptr(pimage_import_descriptor, pdosheader,
- pntheader->optionalheader.datadirectory[image_directory_entry_import].virtualaddress);
- // 窮舉pimage_import_descriptor數(shù)組尋找我們需要截獲的函數(shù)所在的模塊
- while (pimportdesc->name)
- {
- pstr szcurrmod = makeptr(pstr, pdosheader, pimportdesc->name);
- if (stricmp(szcurrmod, szimportmodule) == 0)
- break; // 找到!中斷循環(huán)
- // 下一個(gè)元素
- pimportdesc++;
- }
- // 如果沒(méi)有找到,說(shuō)明我們尋找的模塊沒(méi)有被當(dāng)前的進(jìn)程所引入!
- if (pimportdesc->name == null)
- return null;
- // 返回函數(shù)所找到的模塊描述符(import descriptor)
- return pimportdesc;
- }
- // isnt()函數(shù)的實(shí)現(xiàn)
- bool isnt()
- {
- osversioninfo stosvi;
- memset(&stosvi, null, sizeof(osversioninfo));
- stosvi.dwosversioninfosize = sizeof(osversioninfo);
- bool bret = getversionex(&stosvi);
- _assert(true == bret);
- if (false == bret) return false;
- return (ver_platform_win32_nt == stosvi.dwplatformid);
- }
【編輯推薦】