pythonpython-3.xno-op

Python is printing 0x90c2 instead of just 0x90 NOP


The following command is outputting 200 bytes of 'A' followed by one byte of 0x0a:

python3 -c "print('\x41'*200)" > out.txt

hexdump out.txt confirms this:

0000000 4141 4141 4141 4141 4141 4141 4141 4141
*
00000c0 4141 4141 4141 4141 000a
00000c9

However, whenever I try to output 200 bytes of NOP sled (0x90), for some reason, python decides to also add a series of 0xc2 after every 0x90. So I'm running this:

python3 -c "print('\x90'*200)" > out.txt

And according to hexdump out.txt:

0000000 90c2 90c2 90c2 90c2 90c2 90c2 90c2 90c2
*
0000190 000a
0000191

This is not an issue in perl as the following outputs 200 bytes of NOP sled:

perl -e 'print "\x90" x 200' > out.txt

Why is Python outputting 0x90 followed by 0xc2?


Solution

  • The following Python code resolved the issue:

    python3 -c "import sys; sys.stdout.buffer.write(b'\x90'*200)" > out.txt
    

    This is confirmed by hexdump -C out.txt:

    00000000  90 90 90 90 90 90 90 90  90 90 90 90 90 90 90 90  |................|
    *
    000000c0  90 90 90 90 90 90 90 90                           |........|
    000000c8