網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
LyScript插件中提供了三種基本的堆棧操作方法,其中push_stack
用于入棧,pop_stack
用于出棧,而最有用的是peek_stack
函數(shù),該函數(shù)可用于檢查指定堆棧位置處的內(nèi)存參數(shù),利用這個(gè)特性就可以實(shí)現(xiàn),對(duì)堆棧地址的檢測(cè),或?qū)Χ褩5膾呙璧取?/p>
LyScript項(xiàng)目地址:https://github.com/lyshark/LyScript
peek_stack命令傳入的是堆棧下標(biāo)位置默認(rèn)從0開始,并輸出一個(gè)十進(jìn)制有符號(hào)長(zhǎng)整數(shù),首先實(shí)現(xiàn)有符號(hào)與無(wú)符號(hào)數(shù)之間的轉(zhuǎn)換操作,為后續(xù)堆棧掃描做準(zhǔn)備。
from LyScript32 import MyDebug # 有符號(hào)整數(shù)轉(zhuǎn)無(wú)符號(hào)數(shù) def long_to_ulong(inter,is_64 = False): if is_64 == False: return inter & ((1 << 32) - 1) else: return inter & ((1 << 64) - 1) # 無(wú)符號(hào)整數(shù)轉(zhuǎn)有符號(hào)數(shù) def ulong_to_long(inter,is_64 = False): if is_64 == False: return (inter & ((1 << 31) - 1)) - (inter & (1 << 31)) else: return (inter & ((1 << 63) - 1)) - (inter & (1 << 63)) if __name__ == "__main__": dbg = MyDebug() connect_flag = dbg.connect() print("連接狀態(tài): {}".format(connect_flag)) for index in range(0,10): # 默認(rèn)返回有符號(hào)數(shù) stack_address = dbg.peek_stack(index) # 使用轉(zhuǎn)換 print("默認(rèn)有符號(hào)數(shù): {:15} --> 轉(zhuǎn)為無(wú)符號(hào)數(shù): {:15} --> 轉(zhuǎn)為有符號(hào)數(shù): {:15}". format(stack_address, long_to_ulong(stack_address),ulong_to_long(long_to_ulong(stack_address)))) dbg.close()
通過(guò)上述封裝函數(shù),即可實(shí)現(xiàn)對(duì)有符號(hào)和無(wú)符號(hào)數(shù)的轉(zhuǎn)換。
繼續(xù)完善該功能,我們使用get_disasm_one_code()
函數(shù),掃描堆棧地址并得到該地址處的反匯編代碼。
from LyScript32 import MyDebug # 有符號(hào)整數(shù)轉(zhuǎn)無(wú)符號(hào)數(shù) def long_to_ulong(inter,is_64 = False): if is_64 == False: return inter & ((1 << 32) - 1) else: return inter & ((1 << 64) - 1) # 無(wú)符號(hào)整數(shù)轉(zhuǎn)有符號(hào)數(shù) def ulong_to_long(inter,is_64 = False): if is_64 == False: return (inter & ((1 << 31) - 1)) - (inter & (1 << 31)) else: return (inter & ((1 << 63) - 1)) - (inter & (1 << 63)) if __name__ == "__main__": dbg = MyDebug() connect_flag = dbg.connect() print("連接狀態(tài): {}".format(connect_flag)) for index in range(0,10): # 默認(rèn)返回有符號(hào)數(shù) stack_address = dbg.peek_stack(index) # 反匯編一行 dasm = dbg.get_disasm_one_code(stack_address) # 根據(jù)地址得到模塊基址 if stack_address <= 0: mod_base = 0 else: mod_base = dbg.get_base_from_address(long_to_ulong(stack_address)) print("stack => [{}] addr = {:10} base = {:10} dasm = {}".format(index, hex(long_to_ulong(stack_address)),hex(mod_base), dasm)) dbg.close()
得到的堆棧參數(shù)如下:
由此我們可以得到堆棧處的反匯編參數(shù),但如果我們需要檢索堆棧特定區(qū)域內(nèi)是否存在返回到模塊的地址,該如何實(shí)現(xiàn)呢?
其實(shí)很簡(jiǎn)單,首先我們需要得到程序全局狀態(tài)下的所有加載模塊的基地址,然后得到當(dāng)前堆棧內(nèi)存地址內(nèi)的實(shí)際地址,并通過(guò)實(shí)際內(nèi)存地址得到模塊基地址,對(duì)比全局表即可拿到當(dāng)前模塊是返回到了哪里。
from LyScript32 import MyDebug # 有符號(hào)整數(shù)轉(zhuǎn)無(wú)符號(hào)數(shù) def long_to_ulong(inter,is_64 = False): if is_64 == False: return inter & ((1 << 32) - 1) else: return inter & ((1 << 64) - 1) # 無(wú)符號(hào)整數(shù)轉(zhuǎn)有符號(hào)數(shù) def ulong_to_long(inter,is_64 = False): if is_64 == False: return (inter & ((1 << 31) - 1)) - (inter & (1 << 31)) else: return (inter & ((1 << 63) - 1)) - (inter & (1 << 63)) if __name__ == "__main__": dbg = MyDebug() connect_flag = dbg.connect() print("連接狀態(tài): {}".format(connect_flag)) # 得到程序加載過(guò)的所有模塊信息 module_list = dbg.get_all_module() # 向下掃描堆棧 for index in range(0,10): # 默認(rèn)返回有符號(hào)數(shù) stack_address = dbg.peek_stack(index) # 反匯編一行 dasm = dbg.get_disasm_one_code(stack_address) # 根據(jù)地址得到模塊基址 if stack_address <= 0: mod_base = 0 else: mod_base = dbg.get_base_from_address(long_to_ulong(stack_address)) # print("stack => [{}] addr = {:10} base = {:10} dasm = {}".format(index, hex(long_to_ulong(stack_address)),hex(mod_base), dasm)) if mod_base > 0: for x in module_list: if mod_base == x.get("base"): print("stack => [{}] addr = {:10} base = {:10} dasm = {:15} return = {:10}" .format(index,hex(long_to_ulong(stack_address)),hex(mod_base), dasm, x.get("name"))) dbg.close()
運(yùn)行后,即可掃描到堆棧內(nèi)的所有返回模塊的位置。
原文鏈接:https://www.cnblogs.com/LyShark/p/16545888.html
相關(guān)推薦
- 2022-09-04 react?表單數(shù)據(jù)形式配置化設(shè)計(jì)_React
- 2022-07-01 Python查詢?nèi)笔е档?種方法總結(jié)_python
- 2022-12-27 python中g(shù)etopt()函數(shù)用法詳解_python
- 2022-07-16 mybatis的相同攔截器—切面執(zhí)行的順序
- 2022-06-04 c#中String類型的存儲(chǔ)原理詳解_C#教程
- 2023-02-26 Docker格式化輸出命令:"docker?inspect?--format"?學(xué)習(xí)記錄_docke
- 2022-12-21 Android開發(fā)ThreadPoolExecutor與自定義線程池詳解_Android
- 2023-06-21 C#高級(jí)靜態(tài)語(yǔ)言效率利器之泛型詳解_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支