laravel 如何解密cookie?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45932817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to decrypt cookie?
提问by Mohammad AL-Raoosh
I've just caught a crash reported on sentry, I am trying to debug and see the root cause for the problem.
我刚刚发现了哨兵报告的崩溃,我正在尝试调试并查看问题的根本原因。
Luckily, in the cookies panel, I can see the laravel_session
value that was used while crash happened.
幸运的是,在 cookie 面板中,我可以看到laravel_session
发生崩溃时使用的值。
The question, is, how can decrypt the cookie?
问题是,如何解密cookie?
回答by JamesG
You can decrypt the cookie with the following code:
您可以使用以下代码解密 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";
You should end up with a session ID that looks something like this:
你应该得到一个看起来像这样的会话 ID:
- Laravel 5.1: 55782b00dbfcc3f848585ac2cefc66802d773cf5
- Laravel 5.4: yPjeV74joY4MtMNNtTpeOYBP2CMixJBBChc9HRND
- Laravel 5.1:55782b00dbfcc3f848585ac2cefc66802d773cf5
- Laravel 5.4:yPjeV74joY4MtMNNtTpeOYBP2CMixJBBChc9HRND
I didn't test with Laravel 5.3, but I'm confident it will work.
我没有使用 Laravel 5.3 进行测试,但我相信它会起作用。
When using this code, make sure you paste the entire contents of the cookie into the $cookie
variable, including the two equals signs at the end.
使用此代码时,请确保将 cookie 的全部内容粘贴到$cookie
变量中,包括末尾的两个等号。
回答by Jean-Roch B.
For laravel 6 I think it's pretty much the same
对于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";
But check few things:
但检查几件事:
- Cipher encoding, mine is 'AES-256-CBC', it can be 'AES-128-CBC' if your key length is 16
- Key format, mine start with "base64:" so I have to remove this part first
- 密码编码,我的是“AES-256-CBC”,如果你的密钥长度是 16,它可以是“AES-128-CBC”
- 密钥格式,我的以“base64:”开头,所以我必须先删除这部分