장고(Django)의 CSRF 웹 취약점 대응

웹(WWW)이 대중화되고 많은 프로그램들이 웹으로 넘어오면서 웹 보안의 중요성은 날이 갈수록 강조되고 있습니다. 웹은 타 인터넷 네트워크 프로토콜에 비해 상대적으로 안전하다고 인식되기 때문에 일반적으로 서비스 포트(80)가 열려있으며 다양한 웹 어플리케이션이 동작하고 서비스됩니다. 그러다보니 웹을 통한 네트워크 취약점 또한 많이 연구되고 있으며, 악의적인 목적을 가진 임의의 공격자 역시 예전의 다양한 네트워크 프로토콜에 관련된 것보다는 직접 웹 어플리케이션의 취약점을 노리는 경우가 많습니다.

따라서, 웹어플리케이션을 구현할 때는 보안에 보다 더 주의해야 합니다. 국제 웹 보안 표준기구인 OWASP에서는 주기적으로 보안 10대 취약점을 발표하는데, 이 내용을 기준으로 자신의 웹 어플리케이션에 취약점이 없는가 점검하는 일은 매우 중요합니다.

OWASP 2007 Top 10 에 의하면 다양한 취약점들이 보고되고 있는데(조만간 2010년 Top 10이 발표될 듯 합니다), 그 가운데 매우 중요한 보안요소임에도 불구하고 많은 부분 신경쓰고 있지 못하는 취약점이 바로 CSRF 취약점입니다.

CSRF(Cross-site Request Forgery, 크로스사이트 요청 위조) 공격은 원클릭 공격, 사이드 재킹, 세션 라이딩 등으로도 알려져 있고, 약어로는 XSRF로도 알려져 있습니다. 이 공격은 사이트가 신뢰하는 사용자를 통해 공격자가 원하는 명령을 사이트로 전송하는 기법을 사용합니다. 공격이 사용자를 통해 이루어지기 때문에 공격자의 IP는 추적 불가능한 특성이 있습니다.

은행사이트를 예를 들자면, 공격자 A는 피해자 B가 접속하는 은행 사이트에 조작된 이미지 태그를 게시판에 남깁니다.
<img src="http://bank.example.com/withdraw?account=B&amount=100000&for=A" />
피해자 B 가 은행사이트에 접속하고 로그인하면 세션 정보가 남아있는 상태이고 이때 공격자 A가 게시판에 남겨놓은 글을 B 가 읽게 되면 해당 링크가 요청되면서 공격이 실행됩니다. 원래는 이미지를 불러오기 위해 지정된 이미지 링크롤 GET 메쏘드로 요청하게 되는데, 피해자 B가 인증되어 있는 상태인점을 이용하여 이렇게 우회공격을 할 수 있게 된 것입니다. 특히 대부분의 게시판들이 자바스크립트는 막아놓지만, 이미지 포스팅은 막지 않는 것도 공격에 유리한 상황을 만들어줍니다.

최근에 발생했던 옥션의 1800만명 개인 정보 유출 사고는 CSRF 공격을 당한 것으로 밝혀졌다고 합니다. 중국 해커는 직접 서버를 공격하는 대신, 옥션 운영진을 대상으로 악성 코드를 첨부한 메일을 대량으로 유포했습니다. 운영자가 메일을 확인한 순간 ID를 얻을 수 있었고, 해커는 이 ID를 이용하여 옥션 서버에 로그인할 수 있었다고 합니다. (용어사전 CSRF의 내용을 일부 인용했습니다.)

이러한 취약점을 막는 가장 기본적인 방법은 서버의 상태를 변경하는 요청에 대해 GET 을 쓰지 않는 것입니다. 하지만 만일의 경우 공격자가 스크립트를 이용하여 POST로 보낼 수 도 있으므로 POST 메쏘드인 경우에도 대비를 해야합니다. 따라서 가장 일반적인 해결 방법은,
 1. 서버의 상태를 변경하는 요청은 GET 을 쓰지 않고,
 2. POST 의 경우에도 hidden 필드에 임의의 키값을 전달하고 그 키값이 맞는가를 매번 확인하는 것입니다.

하지만, 실제 보통의 웹 어플리케이션은 2번의 방비가 되어있지 않습니다. 이는 불특정 사용자가 서버의 상태를 임의로 변경할 수 있는 약점을 가지고 있게 합니다.

장고(Django)에서는 1.2 버전부터 이러한 CSRF 취약점을 막는 기능을 기본으로 제공합니다. 모든 POST 방식의 폼 전송에는 hidden 필드로 세션에 따른 임의 키값을 전송하며, 해당 키 값이 유효한지를 매번 확인합니다.

이를 위해서는
 1. 설정파일(settings.py)에 미들웨어에 django.middleware.csrf.CsrfViewMiddleware를 추가하고,
 2. POST 가 사용된 폼 템플릿에 {% csrf_token %} 을 직접 삽입해야 합니다.
<form action="" method="post">{% csrf_token %}
만일 미들웨어를 쓸 수 없는 경우라면, django.views.decorators.csrf 의 csrf_protect 장식자(decorator)를 쓸 수 도 있습니다.
from django.views.decorators.csrf import csrf_protect
from django.template import RequestContext

@csrf_protect
def my_view(request):
    c = {}
    # ...
    return render_to_response("a_template.html", c,
                               context_instance=RequestContext(request))
특정 뷰에 대해 csrf를 적용하고 싶지 않다면 csrf_exampt 장식자를 사용합니다.
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def my_view(request):
    return HttpResponse('Hello world')
장고 뿐 아니라 최신의 웹 프레임워크 (Ruby on rails, Spring 등)은 모두 CSRF를 위한 별도의 방어 방법들을 제공합니다. 하지만 장고는 별도의 설정없이도 CSRF 대응이 가능하도록 구현되어 웹 초보개발자도 취약점이 존재하는 사이트를 만들 수 있는 여지를 사전에 차단한다는 점이 특징이라고 할 수 있습니다.

인실리코젠 KM팀에서 구현하는 대부분의 웹 어플리케이션은 최신 버전의 장고를 사용하고 있으며, 중요한 보안위험요소들을 주기적으로 검토하고 있습니다.

Posted by Quipu

2010/03/10 17:52 2010/03/10 17:52
, , ,
Response
No Trackback , 4 Comments
RSS :
http://insilicogen.com/blog/rss/response/55

Trackback URL : http://insilicogen.com/blog/trackback/55

Comments List

  1. UGG Boots 5469 Classic Cardy 2011/08/13 14:41 # M/D Reply Permalink

    <a href="http://www.top10ugg.net">buy UGG Boots 5469 Classic Cardy</a> on sale!The Best ugg boots online. from here you can find the UGG Boots 5498 Adirondack Tall,UGG Boots.5819 Classic Cardy,UGG Boots 5854 Classic Mini.

  2. loan 2011/11/19 02:58 # M/D Reply Permalink

    Do not enough cash to buy a building? Do not worry, because that's available to take the business loans to resolve such problems. Thence take a sba loan to buy everything you need.

  3. moncler uk 2011/11/26 12:20 # M/D Reply Permalink

    The tone was set http://www.monclersole.com/moncler-jackets-womens-c-3.html at the outset after it appeared that the congressional “super http://www.monclersnorge.com/committee” on deficit reduction would fail to reach an agreement by Congress’ Wednesday deadline.Given Europe’s financial crisis, the super http://www.monclerfactoryoutlets.com/ committee’s stalemate was viewed as another sign of http://www.monclerstyle2011.com/moncler-jackets-kids-c-7.html governments’ inability or http://www.monclerstyle2011.com/moncler-jackets-men-c-4.html unwillingness to deal with their spiraling debt loads.“There is just no good news,” said Dave Rovelli, head of http://www.monclerstyle2011.com/moncler-vest-men-c-6.html
    http://www.monclerstyle2011.com/moncler-jackets-men-c-4.html equity trading at brokerage Canaccord Adams in New York.He said http://www.monclerstyle2011.com/moncler-jackets-women-c-3.html some of the selling in stocks http://www.doudounemoncleralpin.com/ stemmed from fear that the U.S. could http://www.canadagooseexpedition.ca/ soon face another downgrade of its bond rating. Shares worldwide plunged in early August after Standard & Poor’s cut the U.S. rating to AA+ from http://www.imonclerdoudoune.com/ AAA.Stocks fell sharply in Europe overnight as Spanish government http://www.monclerfronline.com/ bond yields jumped to new euro-era http://www.canadiangoosejacket.ca/highs. Voters' decision http://www.monclerdoudounepascher2011.com/ to throw out the http://www.canadagooseolinestore.com/ Socialist government failed to make investors feel more confident about holding Spanish debt. The 10-year Spanish bond yield rose to 6.55% from 6.38% on
    http://www.moncler2u.co.uk/ Friday.

  4. gucci 2011/12/07 15:53 # M/D Reply Permalink

    The processing of leather belt can be very many, the main materials for, cowhide (including the head of <a href="http://www.fakeguccibeltswholesale.com">cheap gucci belts</a>
    <a href="http://www.fakeguccibeltswholesale.com">cheap gucci belts for men</a>
    <a href="http://www.fakeguccibeltswholesale.com">cheap gucci belts for women</a>
    <a href="http://www.fakeguccibeltswholesale.com">fake gucci belts</a>
    <a href="http://www.fakeguccibeltswholesale.com">fake gucci belts for men</a>
    <a href="http://www.fakeguccibeltswholesale.com">fake gucci belts for women</a>the leather, leather, second floor move film, second floor coating), PU leather and PVC leather, renewable leather (regeneration move membrane leather, renewable spraying), as currently in the market of high-grade leather-for the first layer cowhide (head layer is divided into LanShiPi and tree anointed <a href="http://www.fakeguccibeltswholesale.com">cheap louis vuitton belts</a>
    <a href="http://www.fakeguccibeltswholesale.com">fake louis vuitton belts</a>
    <a href="http://www.fakeguccibeltswholesale.com">hermes belts</a>
    <a href="http://www.fakeguccibeltswholesale.com">cheap louis vuitton belts for men</a>
    <a href="http://www.fakeguccibeltswholesale.com">fake louis vuitton belts for men</a>
    <a href="http://www.fakeguccibeltswholesale.com">cheap louis vuitton belts for women</a>
    <a href="http://www.fakeguccibeltswholesale.com">fake louis vuitton belts for women</a>leather skin). LanShiPi--that is obviously after processing leather waterworks light blue effect (specific process to deal with ShuiChang reference leather).

Leave a comment
[로그인][오픈아이디란?]

블로그 이미지

Archives

Authors

  1. Quipu

Calendar

«   2012/02   »
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29      

Site Stats

Total hits:
84927
Today:
22
Yesterday:
34

« Previous : 1 : ... 42 : 43 : 44 : 45 : 46 : 47 : 48 : 49 : 50 : ... 97 : Next »