てきとうなメモ

本の感想とか技術メモとか

Shellshockの攻撃をModSecurityで防ぐ

今回のShellshock問題の対策としては普通にbashのアップデートで良いのだけども、RedHatにModSecurityで防ぐ方法が載っていた。

The following mod_security rules can be used to reject HTTP requests containing data that may be interpreted by Bash as a function definition if set in its environment. They can be used to block attacks against web services, such as attacks against CGI applications.
Request Header values:

SecRule REQUEST_HEADERS ^\(\s*\)\s+{" "phase:1,deny,id:1000000,t:urlDecode,status:400,log,msg:'CVE-2014-6271 - Bash Attack'"

SERVER_PROTOCOL values:

SecRule REQUEST_LINE "^\(\s*\)\s+{" "phase:1,deny,id:1000001,status:400,log,msg:'CVE-2014-6271 - Bash Attack'"

GET/POST names:

SecRule ARGS_NAMES "^\(\s*\)\s+{" "phase:2,deny,id:1000002,t:urlDecode,t:urlDecodeUni,status:400,log,msg:'CVE-2014-6271 - Bash Attack'"

GET/POST values:

SecRule ARGS "^\(\s*\)\s+{" "phase:2,deny,id:1000003,t:urlDecode,t:urlDecodeUni,status:400,log,msg:'CVE-2014-6271 - Bash Attack'"

File names for uploads:

SecRule FILES_NAMES "^\(\s*\)\s+{"  "phase:2,deny,id:1000004,t:urlDecode,t:urlDecodeUni,status:400,log,msg:'CVE-2014-6271  - Bash Attack'"

It is possible, however unlikely, that these rules may result in false positives. These false positives, along with actual attempts, could result in log files of significant size.

Mitigating the shellshock vulnerability (CVE-2014-6271 and CVE-2014-7169) - Red Hat Customer Portal

と、結構ルールが多い。

Apache+bashCGIで調べた感じだと

  • QUERY_STRINGに入れるのは難しいのでARGS,ARGS_NAMESはいらないんじゃない?
    • リクエスト行に空白文字が入るとそこでURLが切れてしまう
    • 空白などをurl encodeしてもデコードしてくれない
  • ファイル名は環境変数にセットされない
  • 最初のルールは""がもれている
  • REQUEST_LINEはメソッド名で開始するので行頭からマッチさせても意味がない。

となるので、以下で良いんじゃないのかな。

SecRule REQUEST_HEADERS "^\(\s*\)\s+{" "phase:1,deny,id:1000000,status:400,log,logdata:%{MATCHED_VAR},msg:'CVE-2014-6271 - Bash Attack'"
SecRule REQUEST_LINE "\(\s*\)\s+{" "phase:1,deny,id:1000001,status:400,log,logdata:%{MATCHED_VAR},msg:'CVE-2014-6271 - Bash Attack'"

ログに何がマッチしたかを出した方が良いのでlogdataも追加している

あくまで自分が調べた範囲なので、他言語のCGIApache以外のWebサーバだと動作が異なってその他のルールも入れたほうが良いのかもしれないけど。