Cookie Poisoning
OWASP ZAPで検出された「Cookie Poisoning」について解説します。
概要
Cookie Poisoningは、攻撃者がクッキーの値を改ざんまたは偽造することで、セッション情報を操作し、不正なアクセスを試みる攻撃手法です。これにより、ユーザーのセッション乗っ取りやアクセス制御のバイパスが可能になる場合があります。
背景
Cookieは、ウェブアプリケーションでユーザーの状態を保持するために使用される重要なメカニズムです。しかし、クライアント側で保存される性質上、以下のような脆弱性が存在する可能性があります:
- 暗号化されていないセッション情報の保存
- 改ざん検知機能の欠如
- セキュリティフラグの不適切な設定
- クッキーの有効期限設定の不備
セキュリティ上のリスク
- セッションハイジャック
- 権限昇格
- なりすまし攻撃
- アクセス制御のバイパス
- 機密情報の漏洩
対処方法の具体例
Apache2での対処
# .htaccessファイル
Header edit Set-Cookie ^(.*)$ $1;HttpOnly;Secure;SameSite=Strict
Nginxでの対処
# nginx.conf
proxy_cookie_path / "/; HttpOnly; Secure; SameSite=Strict";
PHPでの対処
// セキュアなクッキー設定
session_start([
'cookie_httponly' => true,
'cookie_secure' => true,
'cookie_samesite' => 'Strict',
'use_strict_mode' => true
]);
// クッキーの設定
setcookie('session_id', $token, [
'expires' => time() + 3600,
'path' => '/',
'domain' => $_SERVER['HTTP_HOST'],
'secure' => true,
'httponly' => true,
'samesite' => 'Strict'
]);
// クッキー値の検証
function validateCookie($cookieValue) {
if (!preg_match('/^[a-zA-Z0-9]{32}$/', $cookieValue)) {
return false;
}
return verifyHmac($cookieValue);
}
JavaScriptでの対処
// クッキーの操作をサーバーサイドに限定
document.cookie = 'key=value; Secure; HttpOnly; SameSite=Strict';
// クッキーの設定を確認
function checkCookieSettings() {
const cookies = document.cookie.split(';');
for (const cookie of cookies) {
if (!cookie.includes('Secure') || !cookie.includes('HttpOnly')) {
console.error('Insecure cookie detected:', cookie);
}
}
}
まとめ
Cookie Poisoning対策のために:
- HttpOnly フラグの設定
- Secure フラグの有効化
- SameSite 属性の設定
- セッションIDの暗号化
- クッキー値の検証メカニズム実装