akkietech’s diary

セキュリティ関連メインの自分用メモ書き。twitter: @akkietech

2/7 snortでSQLインジェクションの検知シグネチャ作成

前回SQLmapでSQLインジェクションやってみたの続き

Snortで検知シグネチャ作成
SQLmapまでで約2時間ほど経ってしまったが
ようやくシグネチャ作成に移る

とりあえずcontentにSQLiを狙ったっぽい文字列が含まれていれば
検知することにする
下記に示す例は上から順番に
「'」、「"」、「and」、「or」、「union」、「sleep」
の文字列が含まれている場合のルール

alert tcp any any -> any 80 (msg: "['] SQL Injection Detected"; content: "%27"; sid:1000011; rev:1;)
alert tcp any any -> any 80 (msg: "[\"]SQL Injection Detected"; content: "%22"; sid:1000012; rev:1;)
alert tcp any any -> any 80 (msg: "AND SQL Injection Detected"; content: "%20and%20"; nocase; sid:1000013; rev:1;)
alert tcp any any -> any 80 (msg: "OR SQL Injection Detected"; content: "%20or%20"; nocase; sid:1000014; rev:1;)
alert tcp any any -> any 80 (msg: "UNION SQL Injection Detected"; content: "union"; nocase; sid:1000015; rev:1;)
alert tcp any any -> any 80 (msg: "SLEEP SQL Injection Detected"; content: "sleep"; nocase; sid:1000016; rev:1;)

個人的な工夫をした点として
・'と"はURLエンコードで文字列を定義している
・andとorの両端に%20(半角スペース)を入れる

そしてsnort起動して、同じようにsqlmapを実行すると
snortのコンソール側でこんな感じで大量に出力される

02/07-23:35:08.087927  [**] [1:1000015:1] UNION SQL Injection Detected [**] [Priority: 0] {TCP} 192.168.179.44:37038 -> 192.16
8.179.10:80
02/07-23:35:08.087927  [**] [1:1000011:1] ['] SQL Injection Detected [**] [Priority: 0] {TCP} 192.168.179.44:37038 -> 192.168.
179.10:80
02/07-23:35:08.087927  [**] [1:1000014:1] OR SQL Injection Detected [**] [Priority: 0] {TCP} 192.168.179.44:37038 -> 192.168.1
79.10:80

そして上で定義したシグネチャのactionを全てrejectにして
再度両方実行すると
sqlmapでは下記のように出力され、サーバ情報が取得できなくなったことがわかる

[23:51:26] [INFO] testing connection to the target URL
[23:51:26] [INFO] checking if the target is protected by some kind of WAF/IPS
[23:51:26] [WARNING] turning off pre-connect mechanism because of connection reset(s)
[23:51:26] [CRITICAL] heuristics detected that the target is protected by some kind of WAF/IPS
do you want sqlmap to try to detect backend WAF/IPS? [y/N] y
[23:51:40] [WARNING] dropping timeout to 10 seconds (i.e. '--timeout=10')
[23:51:40] [INFO] using WAF scripts to detect backend WAF/IPS protection
[23:51:40] [WARNING] there is a possibility that the target (or WAF/IPS) is resetting 'suspicious' requests
[23:51:40] [CRITICAL] WAF/IPS identified as 'Generic (Unknown)'
are you sure that you want to continue with further target testing? [y/N] y
[23:52:14] [WARNING] please consider usage of tamper scripts (option '--tamper')
[23:52:14] [INFO] testing if the target URL content is stable
[23:52:14] [INFO] target URL content is stable
[23:52:14] [INFO] testing if POST parameter 'name' is dynamic
[23:52:14] [INFO] POST parameter 'name' appears to be dynamic
[23:52:14] [CRITICAL] connection reset to the target URL. sqlmap is going to retry the request(s)
[23:52:14] [CRITICAL] connection reset to the target URL
[23:52:14] [WARNING] HTTP error codes detected during run:
404 (Not Found) - 1 times

ちゃんと続行するのかどうか聞いてくれるあたり親切だな


■ちなみに
flaskで作ったログインページをsqlmapでやってみたらどうなるのか
試してみた

[00:12:21] [CRITICAL] all tested parameters do not appear to be injectable. Try to increase values for '--level'/'--risk' options if you wish to perform more tests. If you suspect that there is some kind of protection mechanism involved (e.g. WAF) maybe you could try to use option '--tamper' (e.g. '--tamper=space2comment') and/or switch '--random-agent'

どうやらデフォルトの実行レベルでは探りが足りないようなので最大限までリスクレベルをあげてみる

# sqlmap -u "http://localhost/login" --data "username=abcde&password=abcde" --risk=3 --level=5

sqlmapの出力とかwiresharkの中身を見てみると
POSTリクエスト送りまくって徹底的にリサーチしていることがわかる

やっぱりflaskってちゃんとセキュリティ的にも考慮して
作られるようになってるのかなと感じた

■まとめ
SQLi用のWebページで苦労したけど
シグネチャ作成自体は思ってた通りで、サクサク進められた

ただクオテーションとかorとかandだけを検知するロジックだと
かなりfalse positiveが増える気がするから
もっとうまくできないものかなとは思う
気が向いたらそこ詰めてみるか

sqlmapは本気出したらかなり徹底的にやってくれることも知れた