CSRF(Cross-Site Request Forgery, 사이트 간 요청 위조) 공격은 웹 애플리케이션에서 사용자가 자신의 의지와는 무관하게, 공격자가 의도한 행동을 수행하도록 만드는 보안 취약점입니다. 이 공격은 사용자가 이미 어떤 웹 사이트에 로그인한 상태에서, 공격자가 준비한 악의적인 웹 페이지나 링크를 통해 그 사이트에 대한 요청을 송신하도록 유도함으로써 발생합니다. 이러한 요청은 사용자의 브라우저를 통해 이루어지므로, 해당 요청에는 사용자의 인증 토큰이나 쿠키가 포함되어 있어, 마치 진짜 사용자의 요청처럼 보일 수 있습니다.
CSRF 공격의 예
사용자가 은행 웹 사이트에 로그인한 상태에서, 공격자가 만든 악의적인 페이지에 접속하게 되었다고 가정해봅시다. 이 페이지에는 사용자가 모르는 사이에 은행 사이트로 송금 요청을 보내는 이미지 태그나 JavaScript 코드가 포함되어 있을 수 있습니다. 사용자의 브라우저는 이 코드를 실행하고, 이미 로그인된 세션을 사용하여 은행 사이트에 송금 요청을 보냅니다. 이때, 은행 사이트는 이 요청이 사용자로부터 온 것으로 인식하여 송금을 진행할 수 있습니다.
CSRF 공격 방어 방법
CSRF 공격을 방지하기 위한 주요 방법은 다음과 같습니다:
- CSRF 토큰 사용: 웹 애플리케이션은 사용자가 폼을 제출할 때마다 유일한 CSRF 토큰을 생성하여 폼에 포함시킵니다. 서버 측에서는 요청을 받을 때마다 이 토큰을 검증하여, 해당 요청이 정상적인 페이지에서 발생한 것인지 확인합니다.
- SameSite 쿠키 속성 설정**: 최신 웹 브라우저는 쿠키에
SameSite
속성을 설정할 수 있습니다. 이 속성을Strict
나Lax
로 설정하면, 브라우저가 다른 사이트에서 발생한 요청에 대해서는 쿠키를 전송하지 않도록 할 수 있습니다. - 사용자 인증 요구: 중요한 작업을 수행하기 전에 사용자의 비밀번호나 추가 인증 정보를 요구함으로써, 요청이 사용자의 의도에 따른 것임을 확인할 수 있습니다.
- Referer 검증: HTTP 요청 헤더의
Referer
값을 검증하여, 요청이 신뢰할 수 있는 페이지로부터 발생했는지 확인할 수 있습니다. 그러나 이 방법은Referer
헤더가 없거나 조작될 수 있기 때문에, 다른 방어 기법과 함께 사용하는 것이 좋습니다.
CSRF 공격은 사용자와 웹 애플리케이션 사이의 신뢰 관계를 악용하는 공격 유형입니다. 따라서, 웹 애플리케이션 개발자는 CSRF 취약점을 신중하게 고려하고, 위에서 언급한 방어 기법을 적절히 적용하여 애플리케이션의 보안을 강화해야 합니다.
XSS와 CSRF의 차이
XSS(Cross-Site Scripting)와 CSRF(Cross-Site Request Forgery)는 웹 보안에서 흔히 마주치는 두 가지 취약점입니다. 이 둘은 웹 애플리케이션의 취약점을 악용하지만, 그 방법과 목적에서 차이가 있습니다.
XSS(Cross-Site Scripting)
- 목적: XSS는 공격자가 사용자의 웹 브라우저에 악의적인 스크립트를 주입하여 실행시키는 것을 목표로 합니다. 이를 통해 사용자의 세션 쿠키를 탈취하거나, 웹 사이트를 변조하거나, 사용자 대신 악의적인 행동을 수행할 수 있습니다.
- 작동 방식: XSS 공격은 주로 웹 애플리케이션에서 사용자의 입력을 적절히 검증하거나 살균하지 않아 발생합니다. 공격자는 웹 페이지에 스크립트 태그를 삽입하고, 다른 사용자가 해당 페이지를 방문할 때 스크립트가 실행되도록 합니다.
- 방어 전략: 사용자 입력을 항상 검증하고, 살균하여 HTML로 출력하기 전에 안전하게 만들어야 합니다. 콘텐츠 보안 정책(CSP)을 설정하여 스크립트 실행을 제한하는 것도 효과적입니다.
CSRF(Cross-Site Request Forgery)
- 목적: CSRF는 사용자가 자신의 의지와는 무관하게 공격자가 의도한 웹 요청을 보내도록 만듭니다. 이를 통해 사용자가 로그인한 상태에서 공격자가 지정한 행동(예: 패스워드 변경, 송금 등)을 웹 애플리케이션에 요청하게 만듭니다.
- 작동 방식: CSRF 공격은 사용자가 이미 인증된 세션을 가지고 있을 때 발생합니다. 공격자는 사용자가 악의적인 웹 사이트나 이메일 링크를 클릭하게 함으로써, 인증된 사용자의 권한으로 서버에 요청을 보내게 합니다.
- 방어 전략: CSRF 토큰을 사용하여 각 폼 요청에 대한 유효성을 검증하고, SameSite 쿠키 속성을 적절히 설정하여 쿠키가 다른 사이트의 요청에 의해 전송되지 않도록 합니다.
주요 차이점
- 공격 대상: XSS는 사용자의 웹 브라우저를 대상으로 합니다. 반면, CSRF는 웹 애플리케이션의 프로세스를 대상으로 합니다.
- 사용자 입력의 역할: XSS는 악의적인 스크립트를 웹 페이지에 삽입하여 사용자의 입력을 악용합니다. CSRF는 사용자의 액션(예: 클릭, 웹 사이트 방문)을 악용하여 공격자가 원하는 요청을 서버에 보냅니다.
- 방어 메커니즘: XSS 방어는 주로 입력 살균(escaping)과 콘텐츠 보안 정책에 의존합니다. CSRF 방어는 CSRF 토큰과 SameSite 쿠키 설정에 초점을 맞춥니다.
이 두 취약점 모두 웹 애플리케이션의 보안을 위협할 수 있으므로, 개발자는 보안을 설계할 때 두 가지 모두를 고려하여 방어 전략을 구현해야 합니다.