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
Nitro
  • 1,263
  • 4
  • 16
  • 37

1 Answers1

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().

DimeCadmium
  • 168
  • 7
  • 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 – Nitro Mar 30 '17 at 01:13
  • 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. – DimeCadmium Mar 30 '17 at 01:27
  • (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) – DimeCadmium Mar 30 '17 at 01:28
  • Thanks! Yeah `sys.stdout.write()` is the way to go for printing the hex bytes without a newline byte. – Nitro Mar 30 '17 at 04:12