I am trying to translate the code into binary data, then execute it.

Source:

exec(print(123)) 

Translated code:

 \x65\x78\x65\x63\x28\x22\x70\x72\x69\x6e\x74\x28\x31\x32\x33\x29\x22\x29 

When writing this string to a variable, code execution does not occur.

Translated as follows:

 a = 'exec("print(123)")' print('\\x'.join([codecs.encode(bytes(x, 'utf8'), 'hex').decode('utf8') for x in a])) 

What's wrong? Do I translate correctly?

Python 3

  • one
    All wrong. The source code is incorrect. It is equivalent to: exec(None) which leads to TypeError. What is "binary data" in the context of the question is not clear. The mere presence of "\\ x" says that the code is broken. How are you going to perform them? Are you trying to create a pyc file? Or just a code object? What prevents the source code from being directly transmitted? - jfs

1 answer 1

The fact is that the phrase "binary code" does not always mean what can be done as a program.

In this case, the "binary string" is simply an indication that you need to work with the data simply as with a sequence of bytes.

That is, it is just such a specific kind of string. It has nothing to do with binary software code, and it cannot be done.

  • And even this is not true. The result displayed by print () in the last example is the author's Unicode string, not bytes. - jfs
  • @jfs, I did not write about the final result. There it is not at all clear what he hoped to get as a final result. I just saw that he used bytes there, and suggested that he simply does not understand the difference between "binary data" and "binary executable code." On this misunderstanding, as I understand it, he has all the confusion going on. - Xander