Python will not print NOP

Viewed 596

I am trying to develop shellcode to do a binary exploitation exercise. One of the instructions that I need to print out to a file is a NOP (0x90). For whatever reason, my Python environment (running on Ubuntu 20.04) hangs whenever I try to print out this character. In other words, I cannot run any other line of Python code unless I Ctrl-D out of the environment and use the python command again. This does not happen with other unprintable characters. To show you want I mean, here is an example:

$ python3
Python 3.8.2 (default, Apr 27 2020, 15:53:34) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print('\x55')
U
>>> print('\x80') # Python doesn't hang with other weird characters

>>> print('\x91')

>>> print('\x89')

>>> print('\x90') # Python does not respond after this line

Also, if I try using this command in my shell, I get a different result. It works without hanging, but 0xc2 is printed out alongside with 0x90.

$ python3 -c "print('\x90')" > test
$ xxd test 
00000000: c290 0a                                  ...

Is there any way to print out the 0x90 character to a file without Python hanging or adding the 0xc2 character?

2 Answers

Open the file in binary mode and use write rather than print.

Put a b in front of the '\x90' to make it a byte string.

So I did eventually figure out a way to print stuff without needing to open a file.

$ python -c "import sys; sys.stdout.buffer.write(b'\x90')" > test
$ xxd test
00000000: 90
Related