在Unicode转义序列中混淆Python脚本 [英] Obfuscate a Python script in Unicode escape sequences
问题描述
我想通过使用Unicode转义序列来混淆Python脚本。
I want to obfuscate a Python script by using Unicode escape sequences.
例如,
print("Hello World")
在Unicode转义序列中为:
in Unicode escape sequences is:
\x70\x72\x69\x6e\x74\x28\x22\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x22\x29
在我的命令行中,我可以使用以下命令实现这一点:
From my command line, I can achieve this with:
$ python3 -c \x70\x72\x69\x6e\x74\x28\x22\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x22\x29
Hello World
我已经创建一个文件并将 Hello World Unicode转义序列放入其中作为源代码。
I've create a file and put the "Hello World" Unicode escape sequence in it as the source code.
但是当我运行它时,我得到:
But when I run it, I get:
$ python3 sample.py
SyntaxError: unexpected character after line continuation character
如何在源代码中使用Unicode转义序列。
How can I use Unicode escape sequences in my source code.
推荐答案
您可以使用PEP 263标头,该标头告诉Python编写了哪种编码的源代码。
You can use a PEP 263 header, which tells Python which encoding the source code is written in.
格式为:
# coding=<encoding name>
通过使用 unicode_escape
编解码器(从 https://docs.python.org/3/library/codecs.html ),Python将首先取消转义您的字符串。
By using the unicode_escape
codec (selected from https://docs.python.org/3/library/codecs.html), Python will unescape your strings first.
sample.py
# coding=unicode_escape
\x70\x72\x69\x6e\x74\x28\x22\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x22\x29
结果:
$ python3 sample.py
Hello World
这篇关于在Unicode转义序列中混淆Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!