How to encrypt session id in cookie?

2020-05-28 00:11发布

While I was reading about session hijacking articles, i learned that it would be nice to encrypt session id value that is stored in a cookie.

As far as I know, when I start a session by calling session_start(), PHP does not encrypt session id value in a cookie.

How do I encrypt session id value and then initialize session with it?

8条回答
别忘想泡老子
2楼-- · 2020-05-28 00:32

It's more important that your session IDs are random (that is, someone can't use their session ID to guess another person's), as the real danger is somebody getting their hands on another user's session ID. As long as you keep them truly random, there's no reason to or utility in encrypting it

查看更多
够拽才男人
3楼-- · 2020-05-28 00:37

The session ID is relatively unguessable, so that's not really the issue.

There are a things you can do related to this to counteract attacks:

  • create a new session when a user signs in
  • limit the length of a session

There are quite a few other things as well. I always recommend studying the Rails Guide on these issues-- it offers a very accessible explanation of known problems and countermeasures-- all equally applicable to PHP code.

查看更多
疯言疯语
4楼-- · 2020-05-28 00:40

Encrypting won't help. The session cookie is just a magic number anyway. Encrypting it just means there's a different magic number to hijack. Depending on what hijacking scenarios you have in mind, there are other possible mitigations. For example, you can limit sessions to a single IP. That poses some issues though, e.g. people switching between wireless points.

查看更多
\"骚年 ilove
5楼-- · 2020-05-28 00:41

It makes sense to encrypt cookie data when you use cookies to store sensitive info. that only the server should read (decrypt).

There's no reason to encrypt session id, since the hacker can use that encrypted session id to impersonate his victim.

查看更多
ら.Afraid
6楼-- · 2020-05-28 00:42

Unfortunately encrypting the session ID is not going to increase security much, as the attacker can just use the encrypted form (which is the only thing visible to them anyways).

The only thing this might prevent is the trick where you send someone a link with ?PHPSESSID=foo in it, which will cause PHP to create that session. You can prevent that by using encryption and validation, but you should rather turn off session ID transfer in the URL completely.

查看更多
唯我独甜
7楼-- · 2020-05-28 00:44

Assuming your session cookie is a GUID, there is no point encrypting it. It would just replace one pseudo-random string with another.

查看更多
登录 后发表回答