2

I am trying to print shell code with python but it adds a \x0a terminator. Is there a way to print without the terminator?

# cat test.py 
print(b'\x41\x41\x41\x41')

# python test.py |xxd -g1
0000000: 41 41 41 41 0a                                   AAAA.

# python test.py | wc
  1       1       5
0

1 Answer 1

1

0x0A is a newline (LF, \n) which Python adds automatically to print statements.

For Python 3, use print(..., end="").

In Python 2, end the print statement with a comma like so:

print "abc",

However, Python will still try to print a newline when it exits. You may be better off using sys.stdout.write().

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

4 Comments

Thanks. For python 2 could you give an example of this please? I've tried ending with a comma in a couple different way but I can't seem to get it right lol. I guess you can import the new print function though from __future__ import print_function and print will behave like python3
Apparently Py2 likes to print a newline when it exits. Try: print "abc",, print "def" - there will be no newline (the space comes from the second print). I corrected my answer.
(In general, print in Python 2 is meant for human consumption - I would guess that it's trying to keep your terminal consistent (print the prompt at the start of the line) by ending with a newline)
Thanks! Yeah sys.stdout.write() is the way to go for printing the hex bytes without a newline byte.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.