如何解密cookie?

时间:2017-08-29 07:08:26

标签: laravel laravel-5.3

我刚刚发现了一个关于哨兵报告的崩溃,我正在尝试调试并查看问题的根本原因。

幸运的是,在cookies面板中,我可以看到发生崩溃时使用的[2017-08-29 12:22:35] local.ERROR: BadMethodCallException: Method handle does not exist. in /home/vagrant/Code/myproject/vendor/laravel/framework/src/Illuminate/Support/Traits/Macroable.php:74 Stack trace: #0 /home/vagrant/Code/myproject/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php(294): Illuminate\Auth\SessionGuard->__call('handle', Array) #1 /home/vagrant/Code/myproject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Auth\AuthManager->__call('handle', Array) #2 /home/vagrant/Code/myproject/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) 值。

问题是,如何解密cookie?

2 个答案:

答案 0 :(得分:5)

您可以使用以下代码解密cookie:

    $cookie = 'eyJpdiI6ImFUQ0FvMWFSVlNvTmhlQjdLWGw1Z1E9PSIsInZhbHVlIjoicFh6Q09iTDl0K0huWU1Nc1NYVmxSY2hPRGU5Vk85dDJyYUpRbUVjRWg5R0JxYkVobkF3YkZVcVQrakFFUmxaVnZrTjFST3F3RTZ4akpDZEpvUFJiQXc9PSIsIm1hYyI6IjlhYmJhMTY3MWMxYWI3YjJmNmFjMmNkZWE0MWZmMmVhNTNiMjI5ZWY3NzUwNzQ0ZjAzMGQ1ZGU0YzVhNjJmZGYifQ==';
    $cookie_contents = json_decode( base64_decode( $cookie, true ));
    $value = base64_decode( $cookie_contents->value );
    $iv = base64_decode( $cookie_contents->iv );
    $clear = unserialize( \openssl_decrypt($value, \Config::get( 'app.cipher' ), \Config::get( 'app.key' ), OPENSSL_RAW_DATA, $iv));
    echo "Cookie contents (Session ID): $clear\n";

您应该得到一个类似于以下内容的会话ID:

  • Laravel 5.1:55782b00dbfcc3f848585ac2cefc66802d773cf5
  • Laravel 5.4:yPjeV74joY4MtMNNtTpeOYBP2CMixJBBChc9HRND

我没有使用Laravel 5.3测试,但我相信它会起作用。

使用此代码时,请务必将Cookie的全部内容粘贴到$cookie变量中,包括末尾的两个等号。

答案 1 :(得分:3)

对于laravel 6,我认为它几乎是相同的

$base64_key = "base64:ISAcSPwQ0HDqqLygaS9LyPzs5ZujMAKOjBou+gyz9sw=";
$payload = json_decode(base64_decode($_COOKIE["your_cookie_name"]), true);
$iv = base64_decode($payload['iv']);
$key = base64_decode(substr($base64_key, 7));
$sessionId = openssl_decrypt($payload['value'],  'AES-256-CBC', $key, 0, $iv);

echo "Session Id: $sessionId";

但是请检查几件事:

  • 密码编码,我的是'AES-256-CBC',如果密钥长度是16,则可以是'AES-128-CBC'
  • 密钥格式,我的格式以“ base64:”开头,因此我必须先删除此部分