tag:crieit.net,2005:https://crieit.net/tags/obfuscation/feed
「obfuscation」の記事 - Crieit
Crieitでタグ「obfuscation」に投稿された最近の記事
2019-04-19T10:09:42+09:00
https://crieit.net/tags/obfuscation/feed
tag:crieit.net,2005:PublicArticle/14925
2019-04-15T19:50:32+09:00
2019-04-19T10:09:42+09:00
https://crieit.net/posts/Nuitka-python-code
Nuitka はあなたの python code の変数名、関数名、クラス名その他を見えなくしてくれるのか?
<h1 id="背景"><a href="#%E8%83%8C%E6%99%AF">背景</a></h1>
<p><a target="_blank" rel="nofollow noopener" href="http://bitboost.com/python-obfuscator/articles_and_resources/does_nuitka_remove_names_of_variables_functions_classes_etc/">Does Nuitka Remove Or Obscure Names of Variables, Functions, Classes, Etc. From Your Python Code?</a> という記事を<strong>たまたま</strong>見つけて(というのをブログで見かけるとたいがい「ウソつけ!」ですよね ^^; かくいう私も嘘ついてて、本当は nuitka と decompile で検索してたらトップがこれだった)記事は「全部残ってるよ」という内容だったので、気になったので試してみました</p>
<p>Python の obfuscator って free のが見つからなくて(obfuscator-llvm みたいな OSS がある分だけ C++ っていいですね、ってC++ で Web アプリ書く気にはならないのですが ^^;;;)、で、別に obfuscate しなくても binary で strip してあれば実質、ほとんどのマリシャスな人は手ださないんじゃないかなと思い、ところで nuitka でコンパイルした binary ってどれぐらい元コードの情報残ってるもんなんだろ?と思ってググってたら冒頭のショッキングな記事が...</p>
<p>よくみるとこの記事、やっぱり高い obfuscator の<a target="_blank" rel="nofollow noopener" href="http://bitboost.com">ベンダーさん</a>が書いてるみたいですね</p>
<h1 id="結論"><a href="#%E7%B5%90%E8%AB%96">結論</a></h1>
<p>記事に反して全然残ってないみたいに思えるのですが、自信ないのでこちらに晒しておこうと思いました次第です<br />
マサカリ大歓迎です、というかそれが欲しくて晒してます</p>
<h1 id="実験"><a href="#%E5%AE%9F%E9%A8%93">実験</a></h1>
<h2 id="version of Nuitka"><a href="#version+of+Nuitka">version of Nuitka</a></h2>
<pre><code class="python:">pip list
Nuitka (0.6.3)
</code></pre>
<h2 id="素のコード"><a href="#%E7%B4%A0%E3%81%AE%E3%82%B3%E3%83%BC%E3%83%89">素のコード</a></h2>
<p>たまたま書いてた(嘘、本当は obfuscate したいと思ってる)Tornado のアプリケーションです<br />
<strong>protocol</strong> とか <strong>port</strong> とか文字列たくさん含んでます<br />
<strong>Screen_WebHander</strong> とかクラス名もありますね</p>
<pre><code class="python:">#-*- coding:utf-8 -*-
# Copy Right Takeyuki UEDA © 2019 - All rights reserved.
import tornado.ioloop
import tornado.web
from tornado.options import define, options
import ssl
import os
import sys
import pprint
import importlib
import screen
if __name__ == "__main__":
# options
define("protocol", default="wss:", help="ws: or wss:(default)")
define("port", default=8888, help="listening port", type=int)
define("data_dir", default="", help="cert file path for running with ssl")
define("cert_file", default="cert.pem", help="cert file name for running with ssl")
define("privkey_file", default="privkey.pem", help="privkey file name for running with ssl")
define("config_file", default="", help="config file path")
define("static_path", default="./", help="[mandatory] handler class name of rhizome")
options.parse_command_line()
'''
The priority of Option file
1. options.config_file
2. ./config.py
'''
if os.path.exists('./config.py'):
options.parse_config_file('./config.py', final=False)
if options.config_file:
options.parse_config_file(options.config_file, final=False)
'''
command line is the first priority
'''
options.parse_command_line()
# app
BASE_DIR = os.path.dirname(__file__)
app_params = {}
app_params["handler"] = [("/", screen.Screen_WebHander),
("/screen_connection/(.*)",screen.Screen_WSHandler),
("/remote_connection/(.*)",screen.Remote_WSHandler),
("/remote/(.*)", screen.Remote_WebHander)
]
app_params["static_path"] = os.path.join(BASE_DIR, options.static_path)
app = tornado.web.Application(
app_params["handler"],
template_path = ".",
static_path = app_params["static_path"],
)
if options.protocol == "ws:":
http_server = tornado.httpserver.HTTPServer(app)
else:
ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
data_dir = options.data_dir
ssl_ctx.load_cert_chain(os.path.join(data_dir, options.cert_file),
os.path.join(data_dir, options.privkey_file))
http_server = tornado.httpserver.HTTPServer(app, ssl_options=ssl_ctx)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
</code></pre>
<h2 id="コンパイル"><a href="#%E3%82%B3%E3%83%B3%E3%83%91%E3%82%A4%E3%83%AB">コンパイル</a></h2>
<p>こんな感じでコンパイルしました</p>
<pre><code class="bash:">python -m nuitka --follow-imports ソースファイル名
</code></pre>
<p>.bin ファイルができます</p>
<h2 id="なかを見る"><a href="#%E3%81%AA%E3%81%8B%E3%82%92%E8%A6%8B%E3%82%8B">なかを見る</a></h2>
<p>nm してみると</p>
<pre><code class="bash">nm homeserver.bin
nm: homeserver.bin: no symbols
</code></pre>
<p>ちゃんと strip してあるみたいですので、-D をつけてみると</p>
<pre><code class="bash:">nm -D homeserver.bin
U abort
005b5be0 B __bss_end__
005b5be0 B _bss_end__
005a2218 B __bss_start
005a2218 B __bss_start__
U __ctype_b_loc
U dirname
005a2218 D _edata
005b5be0 B _end
005b5be0 B __end__
005369f4 T _fini
U getenv
w __gmon_start__
00013b90 T _init
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
w _Jv_RegisterClasses
U __libc_start_main
U mbstowcs
U memcpy
U memset
U printf
U _PyArg_NoKeywords
U PyArg_ParseTuple
U PyArg_ParseTupleAndKeywords
U PyArg_UnpackTuple
0054be88 D _Py_ascii_whitespace
005a3690 B PyBaseObject_Type
005a3d98 B PyBaseString_Type
...
</code></pre>
<p>Python の構文用の文字列は残ってるみたいですけど前述の <strong>protocol</strong> や <strong>port</strong> 、<strong>Screen_WebHander</strong> とかいった文字列はみつかりません</p>
<p>他に objdump -xT も試してみるのですがやっぱりみつかりません<br />
念のために od -A -tx1z でダンプしてみたのですが、やっぱりなにもみつかりません</p>
<h1 id="考察"><a href="#%E8%80%83%E5%AF%9F">考察</a></h1>
<p>試したかんじだと元記事の内容に反して「残ってなくて削除してくれてる」ようなのですが...<br />
使ってる nuitka のバージョンが違うのかな?</p>
<h1 id="Threats to validity"><a href="#Threats+to+validity">Threats to validity</a></h1>
<p>ぜんぜんわかりません ヽ(^o^)丿 ← お手上げ<br />
なんか「そこ、おかしいよ」みたいなご指摘でも「それでいいんだよ」っていうご指摘でもどちらでもかまわないのでご意見いただけたら大歓迎です<br />
つうか、全然 threat to validity になってないな...</p>
<h1 id="References"><a href="#References">References</a></h1>
<ul>
<li><a target="_blank" rel="nofollow noopener" href="http://bitboost.com/python-obfuscator/articles_and_resources/does_nuitka_remove_names_of_variables_functions_classes_etc/">Does Nuitka Remove Or Obscure Names of Variables, Functions, Classes, Etc. From Your Python Code?</a> 元記事です</li>
<li><a target="_blank" rel="nofollow noopener" href="http://nuitka.net">Nuitka</a> Nuitka のサイトです</li>
<li><a target="_blank" rel="nofollow noopener" href="https://github.com/obfuscator-llvm/obfuscator/wiki">obfuscator-llvm</a>のサイトです</li>
<li>obfuscator-llvm の<a target="_blank" rel="nofollow noopener" href="https://crypto.junod.info/spro15.pdf">作りました論文</a> の<a target="_blank" rel="nofollow noopener" href="https://qiita.com/UedaTakeyuki/items/4dd32bee474433af4b6e">解説記事</a>、そもそも obfuscation って何?って人に興味もってもらえるように丁寧に書いたつもりだったんですけど...</li>
</ul>
Dr. Takeyuki Ueda