FLATzブログ

[Ruby on Rails]の記事一覧

Ruby On Rails Security Guideの訳 : 3.1 CSRF Countermeasures

2010年02月19日(金)18:49|kimura|FLATzブログ, Ruby on Rails, 技術情報このエントリをdel.icio.usに追加このエントリをはてなブックマークに追加

こんにちは。木村です。


今回は3.1 CSRF Countermeasuresです。


原文の単語と全く違う言葉に置きかえている場合が多々あります。原文ページと併せて、ご覧下さい。気になる箇所や間違っている箇所があれば、どうかご指摘下さい。


では、以下訳です。


——————————————————


3.1 CSRF Countermeasures


3.1 CSRFの対策方法


- First, as is required by the W3C, use GET and POST appropriately. Secondly, a security token in non-GET requests will protect your application from CSRF.


- まず最初にW3Cが求めるようにGETとPOSTを適切に使用する。2番目に、GETではないリクエストにセキュリティトークンを付加することでCSRFからあなたのアプリケーションを守ります。


The HTTP protocol basically provides two main types of requests – GET and POST (and more, but they are not supported by most browsers). The World Wide Web Consortium (W3C) provides a checklist for choosing HTTP GET or POST:


HTTPプロトコルは基本的に主に2つのタイプのリクエストを提供します。GETとPOSTです(他にもありますが、ほとんどのブラウザでサポートされていません)。 The World Wide Web Consortium(W3C)はHTTP GETまたはPOSTの仕様を選択する時のチェックリストを提供しています。


Use GET if:


GETを使う場合:


  • The interaction is more like a question (i.e., it is a safe operation such as a query, read operation, or lookup).


  • インタラクションな操作が問い合わせである(すなわち、検索や読み込みや参照のような安全な操作のことです)


Use POST if:


POSTを使う場合:


  • The interaction is more like an order, or
  • The interaction changes the state of the resource in a way that the user would perceive (e.g., a subscription to a service), or
  • The user is held accountable for the results of the interaction.


  • インタラクションな操作が命令である、または
  • 操作の影響でリソースの状態を変更することがユーザにわかりやすい(例えば、サービスへの登録です)、または
  • ユーザが操作結果の責任を負わせられる。


If your web application is RESTful, you might be used to additional HTTP verbs, such as PUT or DELETE. Most of today‘s web browsers, however do not support them – only GET and POST. Rails uses a hidden _method field to handle this barrier.


もしあなたのWebアプリケーションがRESTfulならば、あなたはPUTやDELETEのような追加のHTTPメソッドに慣れていることでしょう。
しかしながら、今日のWebブラウザのほとんどは、GETとPOSTだけをサポートしてPUTやDELETEをサポートしていません。
Railsはこの障壁に対処するために、hiddenの_methodフィールドを使用します。


The verify method in a controller can make sure that specific actions may not be used over GET. Here is an example to verify the use of the transfer action over POST. If the action comes in using any other verb, it redirects to the list action.


コントローラのverifyメソッドはGETで特定のアクションが使用できないように確認することができます。POST上でtransferアクションの使用を検証する例は次の通りです。もし何か別のメソッドを使用したアクションが来た場合、listアクションにリダイレクトしています。



verify :method => :post, :only => [:transfer], :redirect_to => {:action => :list}


With this precaution, the attack from above will not work, because the browser sends a GET request for images, which will not be accepted by the web application.


この予防策によって、ブラウザが送るイメージへのGETリクエストがWebアプリケーションに受理されないため、上記の攻撃(3. Cross-Site Request Forgery (CSRF)で例に挙げられている攻撃)は動作しません。


But this was only the first step, because POST requests can be sent automatically, too. Here is an example for a link which displays www.harmless.com as destination in the browser’s status bar. In fact it dynamically creates a new form that sends a POST request.


しかし、これは最初のステップに過ぎません。なぜなら、POSTリクエストも自動的に送ることができるからです。ブラウザのステータスバーに目的地としてwww.harmless.comと表示しているリンクの例が次の通りです。実際にはそのリンクはPOSTリクエストを送る新しいフォームを動的に生成します。



<a href="http://www.harmless.com/" onclick="
var f = document.createElement('form');
f.style.display = 'none';
this.parentNode.appendChild(f);
f.method = 'POST';
f.action = 'http://www.example.com/account/destroy';
f.submit();
return false;"
>To the harmless survey</a>


Or the attacker places the code into the onmouseover event handler of an image:


つまり、攻撃者はイメージのonmouseoverイベントハンドラにコードを設置しています。



<img src="http://www.harmless.com/img" width="400" height="400" onmouseover="..." />


There are many other possibilities, including Ajax to attack the victim in the background. The solution to this is including a security token in non-GET requests which check on the server-side. In Rails 2 or higher, this is a one-liner in the application controller:


バックグラウンドで攻撃するためにAjaxを含んでいると、他に多くの可能性があります。この問題の解決方法はGETではないリクエストにサーバ側でチェックするセキュリティトークンを付加することです。Rails2またはそれ以上のバージョンでは、application controllerでワンライナーで書かれています。



protect_from_forgery :secret => "123456789012345678901234567890..."


This will automatically include a security token, calculated from the current session and the server-side secret, in all forms and Ajax requests generated by Rails. You won’t need the secret, if you use CookieStorage as session storage. It will raise an ActionController::InvalidAuthenticityToken error, if the security token doesn’t match what was expected.


これはRailsによって生成される全てのフォームとAjaxリクエストに、現在のセッションとサーバ側の秘密の文字列から作られるセキュリティトークンを自動的に付加します。セッションストレージとしてCookieStorageを使用しているならば、秘密の文字列は必要ありません。セキュリティトークンが期待されるものとマッチしなければ、ActionController::InvalidAuthenticityTokenエラーが発生するでしょう。


Note that cross-site scripting (XSS) vulnerabilities bypass all CSRF protections. XSS gives the attacker access to all elements on a page, so he can read the CSRF security token from a form or directly submit the form. Read more about XSS later.


cross-site scripting(XSS)は全てのCSRF対策を回避することに注意してください。
XSSは攻撃者にページ上の全ての要素へのアクセスを許します。そのため、攻撃者はフォームからCSRFセキュリティトークンを読み込むか、直接フォームを投稿することができます。
XSSについての詳細は後述します。


——————————————————


次回は4 Redirection and Filesです。

続きを読む


Ruby On Rails Security Guideの訳 : 3. Cross-Site Request Forgery (CSRF)

2010年02月05日(金)14:07|kimura|FLATzブログ, Ruby on Rails, 技術情報このエントリをdel.icio.usに追加このエントリをはてなブックマークに追加

こんにちは。木村です。


今回は3 Cross-Site Request Forgery (CSRF)です。


原文の単語と全く違う言葉に置きかえている場合が多々あります。原文ページと併せて、ご覧下さい。気になる箇所や間違っている箇所があれば、どうかご指摘下さい。


では、以下訳です。


3 Cross-Site Request Forgery (CSRF)


3 クロスサイトリクエストフォージェリ (CSRF)


- This attack method works by including malicious code or a link in a page that accesses a web application that the user is believed to have authenticated. If the session for that web application has not timed out, an attacker may execute unauthorized commands.


- この攻撃方法は、悪意のあるコードやリンクを含んだページが、ユーザが認証済みのWebアプリケーションにアクセスするによって動作します。もし、Webアプリケーションのセッションがタイムアウトになることがなければ、攻撃者は不正なコマンドを実行する可能性があります。


In the session chapter you have learned that most Rails applications use cookie-based sessions. Either they store the session id in the cookie and have a server-side session hash, or the entire session hash is on the client-side. In either case the browser will automatically send along the cookie on every request to a domain, if it can find a cookie for that domain. The controversial point is, that it will also send the cookie, if the request comes from a site of a different domain.
Let’s start with an example:


sessionの章で、ほとんどのRailsアプリケーションはクッキーベースのセッションを使用していることを学びました。クッキーにセッションIDを保存して、サーバサイドにセッション情報をもつか、または全てのセッション情報をクライアントサイドに置く方法です。どちらにせよ、ブラウザはあるドメインのクッキーを見つければ、リクエスト毎にそのドメインにクッキーを送ります。問題となる点は、もしリクエストが違うドメインのサイトから来たとしても、ブラウザがクッキーを送ってしまうことです。ある例から始めましょう。


  • Bob browses a message board and views a post from a hacker where there is a crafted HTML image element.
    The element references a command in Bob’s project management application, rather than an image file.
  • Bob’s session at www.webapp.com is still alive, because he didn’t log out a few minutes ago.
  • By viewing the post, the browser finds an image tag.
    It tries to load the suspected image from www.webapp.com. As explained before, it will also send along the cookie with the valid session id.
  • The web application at www.webapp.com verifies the user information in the corresponding session hash and destroys the project with the ID 1.

    It then returns a result page which is an unexpected result for the browser, so it will not display the image.
  • Bob doesn’t notice the attack – but a few days later he finds out that project number one is gone.


  • ボブは掲示板を開いて、巧妙に作られたHTMLイメージ要素が置かれたハッカーの投稿を見ます。

    その要素はイメージファイルではなく、ボブのプロジェクトマネジメントアプリケーションのコマンドを参照しています。
  • ボブは数分前にログアウトしなかったため、www.webapp.comのセッションはまだ有効です。
  • ハッカーのポストを見ることによって、ブラウザはイメージタグを見つけます。

    ブラウザはwww.webapp.comから疑わしいイメージを読み込もうとします。

    前述の通り、その前にブラウザは有効なセッションIDを持ったクッキーを送ります。
  • www.webapp.comのWebアプリケーションはセッションハッシュと一致するユーザ情報を確認し、ID 1のプロジェクトを削除します。

    それからWebアプリケーションはブラウザにとって予期しない結果のページを返します。なぜなら、それは画像が表示されないからです。
  • ボブは攻撃に気づきません。しかし数日後に彼はプロジェクトナンバー1がなくなっていることに気づくでしょう。


It is important to notice that the actual crafted image or link doesn’t necessarily have to be situated in the web application’s domain, it can be anywhere – in a forum, blog post or email.


実際の巧妙に作られたイメージやリンクに配慮することは重要です。それらは必ずしもWebアプリケーションのドメインに置かれているとは限りません。フォーラムやブログ記事、またはメールなどのどこにでも存在するのです。


CSRF appears very rarely in CVE (Common Vulnerabilities and Exposures) – less than 0.1% in 2006 – but it really is a ‘sleeping giant’ [Grossman]. This is in stark contrast to the results in my (and others) security contract work – CSRF is an important security issue.


CSRFは脆弱性情報データベース(Common Vulnerabilities and Exposures)に現れることはめったにありません。2006年では0.1%未満です。しかし実際には’眠れる巨人’なのです[Grossman]。この結果は、私が(また他の人が)請け負っているセキュリティの仕事での結果とは全く対照的です。CSRFは重要なセキュリティ問題なのです。


次回は3.1 CSRF Countermeasuresです。

続きを読む


Page 3 of 9«12345»...Last »

このページの先頭へ