CE搜索內存數據的原理


 
最近發現有朋友在玩游戲時, 使用一款工具來修改游戲的部分數據,作弊的效果, 也就是CE(Cheat Engine),這款工具是 delphi 編寫的, 於是好奇, 然后瞬間想到API OpenProcess,ReadProcessMemory,WriteProcessMemory,VirtualQueryEx 這幾個API,
OpenProcess是必須的(不講R0),MSDN上說,Read/Wirte一個進程虛擬內存時, 要先打開進程, 帶有讀寫虛擬內存權限才行,VirtualQueryEx 函數是用來檢查內存屬性,因為並不是所有的內存地址都是可讀可以寫, 所以很明白,
操作順序是 OpenProcess -> ReadProcessMemory(WriteProcessMemory) ->VirtualQueryEx , 等等, 很多問題,
VirtualQueryEx 是檢查內存屬性的, 已經讀寫了, 還檢查個啥子?? 
所以變成 OpenProcess  ->VirtualQueryEx-> ReadProcessMemory(WriteProcessMemory), 
問題又出現了, VirtualQueryEx 是檢查內存屬性的, 那么應該從那個地址檢查呢??
Windows 32位 系統, 內存尋址范圍是 2 的32次方, 4G左右, 也就是要尋址4G的地址??? 這個地址非常的大, 
再看MSDN, 發現 內存地址分成了兩部分, 用戶空間 和 內核空間(本文里不講內核空間), 各用了2G的空間, 而且用戶空間是不能直接訪問內核空間的, 所以確定了搜索范圍是 2G 內,即 0x7FFFFFF 個地址, 好,這個范圍不會錯誤了, 問題又來了, 2G的地址, 我怎么知道那個地址是可讀, 那個地址是可寫???, VirtualQueryEx函數MSDN上說了 
VirtualQueryEx provides information about a region of consecutive pages beginning at a specified address that share the following attributes:
The state of all pages is the same (MEM_COMMIT, MEM_RESERVE, MEM_FREE, MEM_PRIVATE, MEM_MAPPED, or MEM_IMAGE). 
If the initial page is not free, all pages in the region are part of the same initial allocation of pages. 
The access granted to all pages is the same (PAGE_READONLY, PAGE_READWRITE, PAGE_NOACCESS, PAGE_WRITECOPY, PAGE_EXECUTE, PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE, PAGE_EXECUTE_WRITECOPY, PAGE_GUARD, or PAGE_NOCACHE). 
The VirtualQueryEx function determines the attributes of the first page in the region and then scans subsequent pages until it scans the entire range of pages, or until it encounters a page with a nonmatching set of attributes. The function returns the attributes and the size of the region of pages with matching attributes, in bytes. For example, if there is a 40 megabyte (MB) region of free memory, and VirtualQueryEx is called on a page that is 10 MB into the region, the function will obtain a state of MEM_FREE and a size of 30 MB.

雖然我英語也是入門水平, MSDN說了, VirtualQueryEx  是可以檢查出一個內存頁面的屬性,也就是要讀寫內存,屬性就得是有讀寫屬性(PAGE_READWRITE,PAGE_WRITECOPY,PAGE_EXECUTE_READWRITE,PAGE_EXECUTE_WRITECOPY), 那么到底是那個呢???
不多說, 跟蹤下CE, 看看CE何如去搜索, 下面是跟蹤的情況
只跟蹤 VirtualQueryEx 函數即可

跟蹤可知, CE從 0x0400000這個地址開始搜索, 為什么從這個地址開始搜索呢?? 這里不多說, 請去了解下PE結構, 函數的第三個參數lpBuffer放了頁面內存的屬性,頁面大小,保護狀態等信息, 根據大小, 可計算出下一個要檢查的位置, 跟進發現, CE並沒有讀寫屬性的頁面, 所以, 我在程序里也將沒有讀寫屬性的過慮了, 這樣就可以降低搜索范圍,


實現后的效果, 基本於CE一樣

下面貼下核心代碼
 
 
 
 
jpg改rar


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM