Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to replace \x00 in python lists?

I have a list of values from a parsed PE file that include /x00 null bytes at the end of each section. I want to be able to remove the /x00 bytes from the string without removing all "x"s from the file. I have tried doing .replace and re.sub, but not which much success.

Using Python 2.6.6

Example.

import re

List = [['.text\x00\x00\x00'], ['.data\x00\x00\x00'], ['.rsrc\x00\x00\x00']]

while count < len(List):
    test = re.sub('\\\\x00', '', str(list[count])
    print test
    count += 1

>>>test  (removes x, but I want to keep it) #changed from tet to test
>>>data
>>>rsrc

I want to get the following output

text data rsrc

Any ideas on the best way of going about this?

like image 205
user2292661 Avatar asked Apr 17 '13 22:04

user2292661


People also ask

What is B in front of string python?

The b" notation is used to specify a bytes string in Python. Compared to the regular strings, which have ASCII characters, the bytes string is an array of byte variables where each hexadecimal element has a value between 0 and 255.

What is x00 in Ascii?

\x00 is thus a byte with all its bits at 0. (As Ryne pointed out, a null character translates to this.) Other examples: \xff is 11111111, \x7f is 01111111, \x80 is 10000000, \x2c is 00101010, etc.


1 Answers

>>> L = [['.text\x00\x00\x00'], ['.data\x00\x00\x00'], ['.rsrc\x00\x00\x00']]
>>> [[x[0]] for x in L]
[['.text\x00\x00\x00'], ['.data\x00\x00\x00'], ['.rsrc\x00\x00\x00']]
>>> [[x[0].replace('\x00', '')] for x in L]
[['.text'], ['.data'], ['.rsrc']]

Or to modify the list in place instead of creating a new one:

for x in L:
    x[0] = x[0].replace('\x00', '')
like image 199
jamylak Avatar answered Nov 03 '22 00:11

jamylak