简体   繁体   English

如何在python中用\\x00填充地址

[英]How to pad an address with \x00 in python

I am trying to print the address 0x004007ad to the terminal for the purposes of a buffer overflow assignment.我正在尝试将地址 0x004007ad 打印到终端以进行缓冲区溢出分配。 I have to do it in little endian because of my processor.由于我的处理器,我必须以小端方式进行。 However when I try print the address the \\x00 is not being included.但是,当我尝试打印地址时, \\x00 不包括在内。 Below is the code for my python program:下面是我的python程序的代码:

hex_string = "\xad\x07\x40\x00"
print "\x01" * 28 + hex_string

How can I print it so that the \\x00 is included?如何打印它以便包含 \\x00 ?

escape all of your backslashes by putting another backslash in front of them...通过在它们前面放置另一个反斜杠来逃避所有反斜杠......

hex_string = "\\xad\\x07\\x40\\x00"
print "\x01" * 28 + hex_string

that should do the trick!这应该够了吧!

在此处输入图片说明

EDIT: regarding your question...编辑:关于你的问题......

import re
hex_string = "\\xad\\x07\\x40\\x00"
other_string = 28*"\\x01"
hex_string = re.sub("\\\\", "", hex_string)
other_string = re.sub("\\\\", "", other_string)
print(other_string + hex_string)

gives...给... 在此处输入图片说明

Not sure if I'm answering too late.不知道是不是我回答太晚了。

My understanding is you got a string buffer of raw address and you want to dump it in human convention to stdout.我的理解是你有一个原始地址的字符串缓冲区,你想按照人类约定将它转储到标准输出。

Unfortunately python doesn't have very good handling of hex formatting.不幸的是,python 没有很好地处理十六进制格式。 One way of doing it is to convert the raw value into proper Python int and formatting it using %x and manually prepend 0x at the front.一种方法是将原始值转换为正确的 Python int 并使用 %x 对其进行格式化,并在前面手动添加 0x。

Sample code follows示例代码如下

import struct
hex_string = "\xad\x07\x40\x00"
v, = struct.unpack('<I', hex_string)
print ('0x' + '%08x' % v)

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

相关问题 NET-SNMP + Python Mac地址显示为\\ x00 \\ - NET-SNMP + Python Mac Address shows as \x00\ how to store bytes like b'PK\x03\x04\x14\x00\x08\x08\x08\x009bwR\x00\x00\x00\x00\x00\x00\x00 to dataframe or csv in python - how to store bytes like b'PK\x03\x04\x14\x00\x08\x08\x08\x009bwR\x00\x00\x00\x00\x00\x00\x00 to dataframe or csv in python 如何删除那些“\x00\x00” - How to remove those "\x00\x00" 为什么Python的string.format pad不能用“\\ x00”? - Why can't Python's string.format pad with “\x00”? 我如何将像 b&#39;\\x08\\x00\\x00\\x00&#39; 这样的 Python/socket 读取的雷达数据解码为数字 - How do I decode radar data read by Python/socket like b'\x08\x00\x00\x00' to numbers 我如何在 python 中将 &quot;\\xff\\xfet\\x00e\\x00s\\x00t\\x00&quot; 转换为 b&#39;\\xff\\xfet\\x00e\\x00s\\x00t\\x00&quot;&#39; - How can i convert "\xff\xfet\x00e\x00s\x00t\x00" into b'\xff\xfet\x00e\x00s\x00t\x00"' in python python 视频 b'\x1aE\xdf\xa3\x01\x00\x00\x00\x00\x00\x00\ - python video b'\x1aE\xdf\xa3\x01\x00\x00\x00\x00\x00\x00\ python将\\ x00连接为字符串 - python concatenating \x00 as string Python PyGSM \\ x00收到短信 - Python PyGSM \x00 received SMS 如何修复此字节对象的编码以仅保留实际文本并删除 Python3 中的 &#39;\\x00\\x05*\\x00\\x00\\x0e\\x00bjbj&#39;? - How can I fix the encoding of this bytes object to only keep the actual text and remove the '\x00\x05*\x00\x00\x0e\x00bjbj' in Python3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM