今天遇到了个很好玩的话题,怎么模(魔)糊(幻)打印Hello World。和平常第一次接触编程语言的Hello World不一样,平常一行就能完成的功能这里实现的能够异常复杂,并且生成了一堆看似乱码的字符,结果用python解析运行却能得到和Hello World一样的结果,原理我到现在还没完全搞明白,先做个记录后面慢慢理解吧。
先看要求:
Task:
Create an obfuscated program that prints Hello World! (exactly like that). Your program may not have any strings in it.
Rules:
You can use any programming language you like.
Make it as obfuscated as possible
This is a popularity-contest, so the answer with the most upvotes wins.
如下答案高票得选:
1 | (lambda _, __, ___, ____, _____, ______, _______, ________: getattr(__import__(True.__class__.__name__[_] + [].__class__.__name__[__]), ().__class__.__eq__.__class__.__name__[:__] + ().__iter__().__class__.__name__[_____:________])(_, (lambda _, __, ___: _(_, __, ___))(lambda _, __, ___: chr(___ % __) + _(_, __, ___ // __) if ___ else (lambda: _).func_code.co_lnotab, _ << ________, (((_____ << ____) + _) << ((___ << _____) - ___)) + (((((___ << __) - _) << ___) + _) << ((_____ << ____) + (_ << _))) + (((_______ << __) - _) << (((((_ << ___) + _)) << ___) + (_ << _))) + (((_______ << ___) + _) << ((_ << ______) + _)) + (((_______ << ____) - _) << ((_______ << ___))) + (((_ << ____) - _) << ((((___ << __) + _) << __) - _)) - (_______ << ((((___ << __) - _) << __) + _)) + (_______ << (((((_ << ___) + _)) << __))) - ((((((_ << ___) + _)) << __) + _) << ((((___ << __) + _) << _))) + (((_______ << __) - _) << (((((_ << ___) + _)) << _))) + (((___ << ___) + _) << ((_____ << _))) + (_____ << ______) + (_ << ___))))(*(lambda _, __, ___: _(_, __, ___))((lambda _, __, ___: [__(___[(lambda: _).func_code.co_nlocals])] + _(_, __, ___[(lambda _: _).func_code.co_nlocals:]) if ___ else []), lambda _: _.func_code.co_argcount, (lambda _: _, lambda _, __: _, lambda _, __, ___: _, lambda _, __, ___, ____: _, lambda _, __, ___, ____, _____: _, lambda _, __, ___, ____, _____, ______: _, lambda _, __, ___, ____, _____, ______, _______: _, lambda _, __, ___, ____, _____, ______, _______, ________: _))) |
第一印象,什么鬼??第二眼还是看不懂。
来看看作者怎么解释的。
1 | Here is a more readable version: http://codepad.org/UzSmoxF2 |
编写generate.py
脚本通过对输入字符串加密:
1 | # How to use? |
Run tese case:
1 | (base) huang@mlp:~/taichi/intel$ python generate.py huang zhiyuan |
利用上面生成的加密字符填充运行脚本run.py
:
1 | huang@mlp:~/taichi/intel$ cat run.py |
验(zhuang)证(bi)结(shi)果(ke):
1 | (base) huang@mlp:~/taichi/intel$ python2 run.py |
版本目前来看只支持python2,否则会报error,从python2到python3有些built-in函数attribute命名规则发生了改变。
1 | The function attributes named func_X have been renamed to use the __X__ form, freeing up these names in the function attribute namespace for user-defined attributes. To wit, func_closure, func_code, func_defaults, func_dict, func_doc, func_globals, func_name were renamed to __closure__, __code__, __defaults__, __dict__, __doc__, __globals__, __name__, respectively. |
1 | (base) huang@mlp:~/taichi/intel$ python run.py |
参考链接:
https://codegolf.stackexchange.com/questions/22533/weirdest-obfuscated-hello-world
https://benkurtovic.com/2014/06/01/obfuscating-hello-world.html
http://codepad.org/UzSmoxF2
http://codepad.org/oVuFVcB5