
オープンソースツール + 少量の自作コードを使用して、便利で軽量な静的解析を作成する
このリポジトリには、ShellCon 2019 の講演「Rolling Your Own: How to Write Custom, Lightweight Static Analysis Tools」(スライド)の PoC コードが含まれています。
一言で言えば、このリポジトリは、オープンソースツールと少量のカスタムコードを使って、いくつかの興味深い静的解析を構築する方法の具体的な例を示しています。
このリポジトリのコード例:
before_action を理解する。これらの例の仕組み
大まかに言うと、この例の実装は次のように動作します。
このプロジェクトは Docker を使って実行することを想定しています。
そのためには、まずいくつかの設定を行う必要があります。
Dockerfile は GitHub のパッケージレジストリでホストされている semantic Docker イメージをベースにしているため、GitHub Package Registry で Docker を使用するための設定 を行う必要があります。
$ docker login docker.pkg.github.com -u USERNAME -p TOKENdocker build -t lightweight_static_analysis .bash シェルを実行し、スクリプトを実行します。# Run this
# (Make sure to run this from a terminal in this repo's project root)
$ docker run -it --rm --entrypoint /bin/bash -v $PWD:/lightweight_static_analysis lightweight_static_analysis
# cd into this project's source code within the
# running container
$ cd /lightweight_static_analysis
# Run main.py with different config options, described further below
Docker コンテナ内で bash シェルを起動したら(docker run コマンドは上記参照)、main.py をいくつかのモードのいずれかで実行できます。
/lightweight_static_analysis> $ python3 src/main.py <options>
利用可能なオプションは、src/main.py をオプションなしで実行するか、main.py の parser.add_argument のセクションを参照することで確認できます。
Rails コードベースを対話的に調査するのに役立ついくつかのコマンドを紹介します。
まず、1 つ以上の Rails リポジトリをクローンして examples/ に配置します。サンプルリポジトリが必要な場合は、rubygems.org のソースコードか、Open Source Rails に掲載されているリポジトリを使用できます。
# Print out the class, super class, defined methods, and before actions
# for all controllers
$ python3 src/main.py --rails-summarize-controllers examples/<repo_name>
# Print out every controller name, grouped by super class
#
# This can find examples where security protections defined in a parent class
# (e.g. ApplicationController or Api::BaseController) aren't applied because
# the vulnerable controller didn't subclass the appropriate class.
$ python3 src/main.py --rails-controllers-by-superclass examples/<repo_name>
# For every before_action used by any controller, list the controllers that
# use that before_action and the routes that it is and isn't applied to
# (e.g. handle the 'except' and 'only" keywords)
#
# This can:
# * Give you quick insight the various before_actions the application defines,
# yielding some intuition as to the code's flow and organization.
# * `verify_with_otp` - Hm, that sounds interesting, I probably want to
# review how that filter is implemented.
# * Show you where a given before_action is and isn't applied across an entire
# code base, potentially leading to bugs where it is inconsistently used
#
# For example
# * Is there a before_action that's used to protect all state0-changing API
# routes except for 1 model? That's strange.
# * Is there an authentication or authorization before_action applied to every
# action in a controller except one? Why?
$ python3 src/main.py --rails-controllers-by-before_action examples/<repo_name>
パース完了後に ipdb REPL に落として、パースされた Ruby コードを対話的に調べたい場合は、上記のコマンドに --repl フラグも渡すことで実現できます。
これらの例は ast_node.py で定義されているさまざまな AstNode クラスに依存しており、Rails 固有のコードはすべて ruby.py にあります。
exec() を介したコマンドインジェクションの発見この実装はまだ整理・文書化されていませんが、main.py の batch_parse_json() と、visitor.py の visit() およびそれが呼び出す他のメソッドを参照してください。
この作業について詳しくお話しできることを嬉しく思います。お気軽に issue を開くか、Twitter でご連絡ください: @clintgibler、@defreez。
このプロジェクトや私たちが取り組んでいる他のプロジェクトの最新情報を知りたい場合は、tl;dr sec ニュースレター をチェックしてください。そこでは、主要なセキュリティ講演の詳細な要約や、優れたセキュリティツールとリソースへのリンクを送信しています。
このニュースレターは、低頻度で高品質な情報を提供し、最新のセキュリティ動向を把握して、より効率的かつ効果的に仕事ができるようにし、情報セキュリティの同僚と共有できる有益な情報を提供することを目的としています。