strictなシステムに対するSession Fixation対策 このエントリをはてなブックマークに>追加

Session Fixationについては話題になって久しいので今さら解説するまでもないと思う。……と思ったらそうでもなさそうなのでちょっと書いてみる。

Session Fixationとは何か

一般に、Session Fixationとして話題にのぼるのは、PHPなどにおける以下のような手法である。

http://www.example.com/index.php?PHPSESSID=abcde

こうすることで、(対策の取られていない)PHPは、セッションIDを’abcde’と解釈してしまう。セッションIDは本来サーバーが発行するはずのものであるが、URIに埋め込むことでクライアント側からセッションIDを指定することが出来てしまうものである。

この「セッションIDが任意のものに設定可能である」ことを利用した攻撃をSession Fixation攻撃と呼ぶ……と解釈している人は以下を引き続き読んで頂きたい。

permissiveとstrict

このPHPの脆弱性に対して、「URIからの指定でセッションIDが任意に指定できてしまう」点ばかりが大きくとりあげられる傾向にあるが(*1)、元凶は「送られてきたセッションIDがサーバーにない場合、そのセッションIDで新たにセッションを開始してしまう」という点だ。URI(GETメソッド)に限らず、POSTメソッドでもCookieを書き換えることでも任意の文字列をセッションIDに指定することは可能である。

このように、セッションIDを任意の文字列に設定可能であるシステムをpermissive(寛容)なシステム、これが通用しないものをstrict(厳格)なシステムと呼ぶ(*2)。最近「Session Fixation」というと、permissiveなシステムに対するものを指す場合が多いが、実はstrictなシステムにもSession Fixatonは生じうる。つまり、permissiveなシステムをstrictに変更しただけでは、本当のSession Fixation対策にはならないのだ。

Session Fixation対策として、

PHP Security Guide: Sessions

session_start();
if (!isset($_SESSION['initiated'])) {
    session_regenerate_id();
    $_SESSION['initiated'] = true;
}

のようなコードを散見する。これは一見、よくできた対策のように思える。URIに仕込んだ恣意的なセッションIDでは、$_SESSION[”initiated”]trueにすることは難しいため、seesion_regenerate_id()で生成された新しいセッションIDのみが取扱われるようになる。

しかしこれはpermissiveなシステムをstrictに近づける方策に過ぎない。この対策を取れば、システムがpermissiveであることにつけ込むSession Fixationは防げるが、strictなシステムに対するSession Fixationは依然として防げないのだ。

strictなシステムに対するSession Fixation

strictなシステムではセッションIDの偽造ができない。このためSession Fixation攻撃の可能性は極端に低くなるがゼロではない。それではstrictなシステムに対するSession Fixationとはどういったものだろうか。

例えば、認証を用いたシステムで、

  1. 認証以前のセッションIDを認証後も継続して利用する
  2. 認証状態が終了してもセッションIDを破棄しない

というWebアプリケーションが存在したとする。悪意のユーザAが自分のアカウントでログインする。当然セッションIDが発行されるが、この時のセッションIDをAは書き留めておく。そしてログアウトし、そのPCから去る。書き留めたセッションIDは、(2)により残ったままである。

その直後、善意のユーザBが同じPCに立ったとする。そしてログインする。この時、(1)によりAが書き留めたセッションIDが継続して利用されるため、Aは書き留めたセッションIDを使ってBに対してSession Hijackingを行うことができる。

こういったことは不可能に近いと思うかも知れない。しかし、ネットカフェのようなパブリックな場所にあるPCでは充分起こりうることだろう。

「これはSession Fixationではなく別の脆弱性だ」と思うかも知れない。しかし、Session Fixationは「ユーザーのセッションIDがログイン時以前に固定されている」ことが問題とされており、これもSession Fixationの一種と考えられる。(*3)

セッションに対する誤解がSession Fixationを生む

そもそもなぜSession Fixationが発生するのか。これはセッション管理の概念を誤解しているケースが多いのではないかと推測する。セッション管理とは、その名の通りある一連のセッションを維持するための仕組みである。そのため(一般的な)セッション管理では、セッション「開始時」にセッションIDというものを発行してUAに保持させる。そしてUA上のセッションIDを消失させた(あるいはUA側で破棄した)時点でセッションは事実上終了する(*4)。セッションIDはある一連のセッションを担保するキー(トークン)なのである。

認証を使うWebアプリケーションにおいては、認証完了(ログイン)からログアウトまでがセッションといえるだろう。これにセッション管理を利用するならば、セッションIDはログイン時に「新たに」発行され、ログアウト時に破棄されるべきである。こうすれば大抵の(認証に対する)Session Fixation脆弱性は起こらない。

しかし、ログアウトしてから次回にログインするまでも何らかの情報を保持しておきたい場合もあるだろう。こういった場合、ログイン時もログアウト時もセッションIDを破棄しないような実装になるだろう。

この実装の場合、「このセッションIDは何を指しているか」を考えてみよう。ログイン時に発行されるのでもなければ、ログアウト時に破棄されるわけでもない。つまりこのセッションIDは認証セッションを指しているのではない。認証セッションをまたいだ漫然と長い期間をセッションとしているのである。この間、他のユーザがログインする可能性も充分ある。このセッションIDに紐付けられた「セッション」は「ログイン中かどうか」とは全く関係ないのである。

つまり、こういったシステムの場合、「ログイン中かどうか」に関してはいわゆるセッション管理は全く行われていないことになる。Session Fixation攻撃とはこの「正しいセッション管理が行われていない部分」に対して行われる攻撃であると言える。strictなシステムでも使い方を間違えれば脆弱性を生むのは当然と言えよう。もちろん上述の’initiated’をtrueにするような対策では間に合わない。

ではどうすればいいのか

答えは簡単で、「ログイン中かどうか」を保証するセッションIDの代わりになるものをもう1つ用意すれば良い。つまり、セッションの中にもう一つセッションを作ってやれば良いのだ。コードは以下のようになるだろう。

// ======== 認証成功時 ======== 
$token = md5(uniqid(rand(), true)); // この文字列がログイン中を示すセッションIDの代わり
$_SESSION['auth_key'] = $token;
$_COOKIE['auth_key']  = $token;

// ======== ログイン中 ======== 
if( ! isset($_SESSION['auth_key'])
 || $_SESSION['auth_key'] == ""
 || $_SESSION['auth_key'] !== $_COOKIE['auth_key']){
    // 認証エラー
    exit;
}

// ======== ログアウト時 ======== 
$_SESSION['auth_key'] = "";

‘auth_key’の内容は予測不可能な文字列であるとする。するとこれは、認証が成功したUAしか知らないことになる。上記の例でAがセッションIDを持ち帰ったとしても、Bが認証した時に受け取った’auth_key’の内容は知りようがないのでSession Fixationは成功しない。

注釈

(*1)セッションIDつきURIをWebサイトやメールで流せば、URIを開いたUAのセッションIDは全て’abcde’に固定されてしまい、無差別攻撃が可能となるため、「URIにセッションIDが埋め込み可能」という脆弱性が深刻な問題であったことは間違いない。

なお、session.use_only_cookies(セッションIDの受け渡しをCookieのみにする)を1にすればこの問題が解決するように解説しているサイトも多いが(もちろんかなり効果的な対策ではあるが)、Cookieのクロスドメイン脆弱性や、XSSによるjavascriptの埋め込みなどでCookieの内容そのものを書き換えてしまう攻撃手法が存在するらしい(検証したわけではない)ので、これだけでは充分な対策とはいえない。

(*2)permissiveとstrictの語については、Session Fixation Vulnerability in Web-based Applications @acrosより拝借した

Strict Session管理パッチ@yohgaki氏 で、PHPのpermissiveなセッション管理をstrictに変更するパッチが公開されている。

(*3) Session Fixation Vulnerability in Web-based Applications

We’ll call this class of attacks “session fixation” attacks, because the user’s session ID has been fixed in advance instead of having been generated randomly at login time.

拙訳「我々はこの種類の攻撃を”session fixation”攻撃と呼ぶことにする。ログイン時にランダムに生成されるはずのユーザーのセッションIDが、事前に固定されているからである」

(*4)本来ならサーバーがセッション情報を破棄した場合もセッションは終了されるべきであるが、これを行わないのがpermissiveなシステムである(厳密には、同じセッションIDで別のセッションを開始してしまう)

28 件のコメント - “strictなシステムに対するSession Fixation対策”

  1. Thai Chicken Recipe のコメント

    Links…

    […]Sites of interest we have a link to[…]……

  2. hawke scopes のコメント

    Related…

    […]just beneath, are numerous totally not related sites to ours, however, they are surely worth going over[…]…

  3. sale ads online のコメント

    How To Find A Mentor For Learning

    […]This is really cool. Thank you for writting this[…]

  4. how to get a boyfriend のコメント

    abating

    replica frank muller casablanca watchesIn 2007,ugg bailey button triplet nuss Art and Sole was named the Best Charity Event of the Year by Accessories Magazine,billig ugg classic mini

  5. Chicken Recipes For Dinner のコメント

    Great website…

    […]here are some hyper-links to sites that we link to as we feel they are really worth browsing[…]…

  6. boot camp Glasgow のコメント

    News info

    I was reading the news and I saw this really cool info

  7. moncler jackets のコメント

    Online Article…

    […]The information mentioned in the article are some of the best available […]…

  8. security dvr のコメント

    Website Trackback Link

    […]the time to read or visit the content or sites we have linked to below the[…]

  9. Sophia Allison のコメント

    Cool sites

    […]we came across a cool site that you might enjoy. Take a look if you want[…]…

  10. Fix Slow Computer のコメント

    Interesting….

    A very neat post.

  11. dating site axes 5000 fatties のコメント

    online date

    I get pleasure from, lead to I found exactly what I was taking a look for. You’ve ended my four day lengthy hunt! God Bless you man. Have a nice day. Bye

  12. AMG Services のコメント

    Awesome website

    […]the time to read or visit the content or sites we have linked to below the[…]…

  13. Acupunture A Quick Overview のコメント

    Amazing web-site

    […]below are some url links to web sites we link to seeing that we believe there’re truly worth browsing[…]

  14. AMG Services のコメント

    Read was interesting, stay in touch…

    […]please visit the sites we follow, including this one, as it represents our picks from the web[…]…

  15. Sunset Cove Phangan のコメント

    Best Info On Sunset Cove Koh Phangan

    […]I see you know a good deal about what you happen to be writing about. Interesting study. Thank you for writting this[…]

  16. Click Plane Tickets のコメント

    Interesting….

    A very neat post.

  17. Foal Color Calculator のコメント

    The best website……

    […]here are some links to sites that we link to because we think they are worth visiting[…]……

  18. Premium Web Directory のコメント

    You should check this out

    […] Wonderful story, reckoned we could combine a few unrelated data, nevertheless really worth taking a look, whoa did one learn about Mid East has got more problerms as well […]…

  19. mueller scopes のコメント

    Websites we think you should visit

    […]although websites we backlink to below are considerably not related to ours, we feel they are actually worth a go through, so have a look[…]…

  20. panama sailing のコメント

    Superb website

    […]always a big fan of linking to bloggers that I love but don’t get a lot of link love from[…]…

  21. http://zukabooka.ru/user/alesyvizlv/ のコメント

    Really interesting post

    This phenomenal genuinely answered my challenge , thank you!.

  22. christmas wreath のコメント

    Gems form the internet

    […]very few websites that happen to be detailed below, from our point of view are undoubtedly well worth checking out[…]…

  23. simmons scopes のコメント

    Recommeneded websites

    […]Here are some of the sites we recommend for our visitors[…]…

  24. solar panels のコメント

    Wikia

    Wika linked to this website

  25. billiga solglasögon のコメント

    New Website About Solglasögon

    […]It’s wonderful that individuals nonetheless know quite a bit about thing like that. I will verify your web-site within the long term.[…]

  26. Music Station のコメント

    Links

    […]Sites of interest we have a link to[…]…

  27. law firms Glasgow のコメント

    Great website

    […]we like to honor many other internet sites on the web, even if they aren’t linked to us, by linking to them. Under are some webpages worth checking out[…]…

  28. how to show a guy you like him のコメント

    Fantastic items from you, man. I’ve take note your stuff prior to and you are just extremely excellent. I really like what you have bought right here, certainly like what you are saying and the way wherein you assert it. You make it entertaining and you still care for to keep it wise. I can not wait to read much more from you. This is really a terrific web site.

コメントをどうぞ。

使用できる XHTML タグ : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>