kapieciiのブログ

日々学んだことを残しておくためのブログです。このブログはGoogle Analyticsを利用しています。

jarファイル(Java)をdecompileする方法する方法まとめ

f:id:kapiecii:20191030182703p:plain

jarファイルを解析する場面に出くわしました。
これまでは「jd-gui」を使っていたのですが、ソースコードの取得中に処理が止まる現象が発生したので、「jd-gui」以外の方法について調べました。
発生したエラーコードを含め、今後のためにメモを残しておきます。

目次

ツール

jd-gui

java-decompiler.github.io

GUI環境があり、特に説明を見なくても操作ができるくらいシンプルに使えます。
しかし、GUIメニューから「save all resources」jarから復元したjavaコードを保存しようとすると、途中で処理が止まることがありました。
2019/10月時点での最新バージョンは、1.6.3で、1.6.2でも同じ問題が発生することを確認しています。
調べてみると、この問題は数年前からstack overflowで質問されているようです。

stackoverflow.com

jad

varaneckas.com

有名なdecompilerですが、2001年から更新が止まっているようです。
「The class file version is 50.0 (only 45.3, 46.0 and 47.0 are supported)」というエラーが出力されることがあり、使えない場面が多い印象です。

エラー例

$ jad -o -r -sjava -dsrc **/*.class
{中略}
The class file version is 50.0 (only 45.3, 46.0 and 47.0 are supported)

jadx

github.com

CLIGUIの2種類があります。
jadx-guiは、jd-guiと同じく、説明が不要なくらいわかりやすくなっています。
CLIには各種オプションが用意されています。例えば、

$ ./jadx -ds {デコンパイルしたソースを出力するディレクトリ} {デコンパイルするファイル}

とすることで、デコンパイルしてソースを取得することができます。
input fileとして「.apk, .dex, .jar, .class, .smali, .zip, .aar, .arsc」という複数の形式が使えるのは便利ですね。

$ ./jadx -h

jadx - dex to java decompiler, version: 1.0.0

usage: jadx [options] <input file> (.apk, .dex, .jar, .class, .smali, .zip, .aar, .arsc)
options:
  -d, --output-dir                    - output directory
  -ds, --output-dir-src               - output directory for sources
  -dr, --output-dir-res               - output directory for resources
  -r, --no-res                        - do not decode resources
  -s, --no-src                        - do not decompile source code
  --single-class                      - decompile a single class
  --output-format                     - can be 'java' or 'json' (default: java)
  -e, --export-gradle                 - save as android gradle project
  -j, --threads-count                 - processing threads count (default: 1)
  --show-bad-code                     - show inconsistent code (incorrectly decompiled)
  --no-imports                        - disable use of imports, always write entire package name
  --no-debug-info                     - disable debug info
  --no-inline-anonymous               - disable anonymous classes inline
  --no-replace-consts                 - don't replace constant value with matching constant field
  --escape-unicode                    - escape non latin characters in strings (with \u)
  --respect-bytecode-access-modifiers - don't change original access modifiers
  --deobf                             - activate deobfuscation
  --deobf-min                         - min length of name, renamed if shorter (default: 3)
  --deobf-max                         - max length of name, renamed if longer (default: 64)
  --deobf-rewrite-cfg                 - force to save deobfuscation map
  --deobf-use-sourcename              - use source file name as class name alias
  --rename-flags                      - what to rename, comma-separated, 'case' for system case sensitivity, 'valid' for java identifiers, 'printable' characters, 'none' or 'all' (default)
  --fs-case-sensitive                 - treat filesystem as case sensitive, false by default
  --cfg                               - save methods control flow graph to dot file
  --raw-cfg                           - save methods control flow graph (use raw instructions)
  -f, --fallback                      - make simple dump (using goto instead of 'if', 'for', etc)
  -v, --verbose                       - verbose output
  --version                           - print jadx version
  -h, --help                          - print this help
Example:
  jadx -d out classes.dex

参考URL

下記の記事に、いくつかのjava decompilerがまとめられています。

darizotas.blogspot.com

「androguard」も気になるので、androidアプリの解析が必要になった際に試してみようと思います。

code.google.com

関連本

Java以外の解析本で面白かったものです。

たのしいバイナリの歩き方

たのしいバイナリの歩き方

  • 作者:愛甲 健二
  • 発売日: 2013/08/22
  • メディア: 単行本(ソフトカバー)

リバースエンジニアリングバイブル ~コード再創造の美学~

リバースエンジニアリングバイブル ~コード再創造の美学~

  • 作者:姜 秉卓
  • 発売日: 2013/09/20
  • メディア: 単行本(ソフトカバー)

jarのiconはこちらを使わせていただきました。

jar file icon:

Icons made by Freepik from www.flaticon.com

kapieciiのブログについてお問い合わせがある場合、下記のフォームからご連絡をお願い致します。
お問い合わせはこちら