0

I have a hex file in this format: \xda\xd8\xb8\x7d

When I load the file with Python, it loads with two back slashes instead of one.

with open('shellcode.txt', 'r') as file: shellcode = file.read().replace('\n', '') 

Like this: \\xda\\xd8\\xb8\\x7d

I've tried using hex.replace("\\", "\"), but I'm getting an error

EOL while scanning string literal

What is the proper way to replace \\ with \?

6
  • Those are backslashes, not forward slashes. The first one escapes the second one. There isn't a problem. Try printing the string Commented Sep 17, 2016 at 19:17
  • Use raw strings hex.replace(r"\\", r"\") You escaped the final double-quote, hence the error. However I'm dubious if the characters really look like this as a string, or this is just a string representation of the data. Commented Sep 17, 2016 at 19:17
  • How did you display the data? Please show your code. If repr() is used (which interactive python uses) the the extra \ will be seen, but if str() is used (which print uses) then it will not be added. Commented Sep 17, 2016 at 19:23
  • @cdarke this hex is not used in the Python program. I'm doing security work, and this hex is sent to another program to be executed as raw hex. Commented Sep 17, 2016 at 19:26
  • So how do you know there are two backslashes? How is it being displayed? Did you try raw strings? Commented Sep 17, 2016 at 19:27

3 Answers 3

1

Here is an example

>>> h = "\\x123" >>> h '\\x123' >>> print h \x123 >>> 

The two backslashes are needed because \ is an escape character, and so it needs to be escaped. When you print h, it shows what you want

Sign up to request clarification or add additional context in comments.

Comments

0

Backshlash (\) is an escape character. It is used for changing the meaning of the character(s) following it.

For example, if you want to create a string which contains a quote, you have to escape it:

s = "abc\"def" print s # prints: abc"def 

If there was no backslash, the first quote would be interpreted as the end of the string.

Now, if you really wanted that backslash in the string, you would have to escape the bacsklash using another backslash:

s = "abc\\def" print s # prints: abc\def 

However, if you look at the representation of the string, it will be shown with the escape characters:

print repr(s) # prints: 'abc\\def' 

Therefore, this line should include escapes for each backslash:

hex.replace("\\", "\") # wrong hex.replace("\\\\", "\\") # correct 

But that is not the solution to the problem!

There is no way that file.read().replace('\n', '') introduced additional backslashes. What probably happened is that OP printed the representation of the string with backslashes (\) which ended up printing escaped backslashes (\\).

Comments

0

You can make a bytes object with a utf-8 encoding, and then decode as unicode-escape.

>>> x = "\\x61\\x62\\x63" >>> y = bytes(x, "utf-8").decode("unicode-escape") >>> print(x) \x61\x62\x63 >>> print(y) abc 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.