日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

LyScript實(shí)現(xiàn)對(duì)內(nèi)存堆棧掃描的方法詳解_python

作者:lyshark ? 更新時(shí)間: 2022-09-28 編程語(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

欄目分類
最近更新