简体   繁体   English

PHP会话劫持

[英]PHP Session Hijacking

I have a question regarding session hijacking in PHP. 我有一个关于PHP中会话劫持的问题。 I have been reading about it this morning and I have a few questions that just weren't answered clearly in the documentation I read. 我今天早上一直在阅读这篇文章,我有一些问题在我阅读的文档中没有得到明确解答。

Can a user change their session on my website? 用户可以在我的网站上更改会话吗? ie if they have a session of X when the login, can they change that session to Y, or Z, if they so choose? 即如果他们在登录时有X会话,他们是否可以将该会话更改为Y或Z,如果他们选择的话?

I thought that sessions were set by the browser and they couldn't be changed, but all of this session hijacking stuff I've been reading has put some doubt in my mind. 我认为这些会话是由浏览器设置的,并且它们无法更改,但所有这些会话劫持我一直在阅读的内容都让我产生了一些疑问。

The term "session" is overloaded to mean different things on the server and in the browser. 术语“会话”被重载以表示服务器和浏览器中的不同内容。 Browser sessions are at best tenuously connected to server sessions. 浏览器会话最多只是连接到服务器会话。 "Session hijacking" refers to server sessions . “会话劫持”是指服务器会话

Server-side, a session has an ID (which is passed between the client and server), content (stored on the server) and potentially other properties, such as last access time. 在服务器端,会话具有ID(在客户端和服务器之间传递),内容(存储在服务器上)以及可能的其他属性,例如上次访问时间。 The session ID is usually passed as a cookie. 会话ID通常作为cookie传递。 In PHP the default name for the cookie is "PHPSESSID". 在PHP中,cookie的默认名称是“PHPSESSID”。 If cookies aren't available, PHP will (optionally) use a query string parameter of the same name ("PHPSESSID"). 如果cookie不可用,PHP将(可选)使用同名的查询字符串参数(“PHPSESSID”)。 This cookie (or query param) can easily be changed and therefore the session identifier can be changed too. 可以轻松更改此cookie(或查询参数),因此也可以更改会话标识符。

The contents of a session (ie containing the login state of a user) cannot be changed by the client, the data is stored on the server and can only be changed by a PHP script on that server. 客户端无法更改会话的内容 (即包含用户的登录状态),数据存储在服务器上,只能通过该服务器上的PHP脚本进行更改。 Note that in a shared-hosting environment (shared by other services or users), the sessions can be overwritten if using the default session storage directory ( /tmp ). 请注意,在共享托管环境(由其他服务或用户共享)中,如果使用默认会话存储目录( /tmp ),则可以覆盖会话。 To protect against that, either use a database through session_set_save_handler() or set a custom session directory using session.save_path with the proper directory permissions set (preferably 700 which means that only the owner (the PHP user) can read and write to it). 为了防止这种情况,要么通过session_set_save_handler()使用数据库,要么使用session.save_path设置自定义会话目录,并设置正确的目录权限(最好是700,这意味着只有所有者(PHP用户)可以读取和写入) 。

To protect against session hijacking, you must have other ways to identify the user against a session. 要防止会话劫持,您必须有其他方法来识别会话中的用户。 This can be a user agent, IP address or another cookie. 这可以是用户代理,IP地址或其他cookie。 The previously mentioned methods are just workarounds, best way to protect against stealing of the session cookie is by using HTTPS if a session is involved. 前面提到的方法只是解决方法,如果涉及会话,则通过使用HTTPS来防止窃取会话cookie的最佳方法。 Do not forget to set the httponly flag to true using session_set_cookie_params() 不要忘记使用session_set_cookie_params()httponly标志设置为true

Client-side, "session" is again overloaded and used in various contexts (eg session managers, which restore open pages when a browser is opened, session cookies and sessionStorage ). 客户端,“会话”再次被重载并在各种上下文中使用(例如会话管理器,其在打开浏览器时恢复打开的页面,会话cookie和sessionStorage )。 We can try to combine these meanings (into what is by no means a standard one) by saying a browser session consists of a collection of views and their associated data. 我们可以尝试通过说浏览器会话由一组视图及其相关数据组成来组合这些含义(并不是标准的含义)。 (By "view" I mean roughly tabs in tabbed browsers and windows in non-tabbed browsers; the DOM window object exposes a view to JS.) Each view has a history, a current page and page data. (“视图”我指的是选项卡浏览器和非选项卡式浏览器中的window大致标签; DOM window对象向JS公开视图。)每个视图都有历史记录,当前页面和页面数据。 Page data for pages in the same domain is shared between views in a session; 同一域中页面的页面数据在会话中的视图之间共享; if two pages are in different domains or different sessions, they don't share data. 如果两个页面位于不同的域或不同的会话中,则它们不共享数据。 Exiting the browser closes all open session(s), possibly saving part of the session(s) (eg histories, current pages, sessionStorage ) so that a session manager can re-open them. 退出浏览器会关闭所有打开的会话,可能会保存部分会话(例如历史记录,当前页面, sessionStorage ),以便会话管理器可以重新打开它们。 Session cookies are cookies that are discarded when a session is closed; 会话cookie是会话结束时丢弃的cookie; in other words, session cookies are non-persistant. 换句话说,会话cookie是非持久性的。 Though a session cookie may hold a session ID, the two concepts are orthogonal (sense 4; session cookies can hold things other than session IDs, and session IDs can be stored in persistant cookies). 虽然会话cookie可以包含会话ID, 但这两个概念是正交的 (感知4;会话cookie可以包含会话ID以外的内容,会话ID可以存储在持久性cookie中)。

Whether two different views are in the same collection depends on the browser. 两个不同的视图是否在同一个集合中取决于浏览器。 For example, one browser may consider a session to consist of all tabs within a single window; 例如,一个浏览器可以认为会话由单个窗口内的所有选项卡组成; separate windows are separate sessions. 单独的窗口是单独的会话。 IE8 lets users create new sessions via the "New session" menu item. IE8允许用户通过“新会话”菜单项创建新会话。 Otherwise, new windows and tabs are opened in the same session. 否则,将在同一会话中打开新窗口和选项卡。 Privacy modes also create new sessions. 隐私模式也会创建新会话。

In summary, browser sessions are indeed set by the browser, though it provides users various means of controlling browser sessions: creating new sessions, changing the history and current page in a view by browsing, saving and restoring sessions. 总之,浏览器会话确实由浏览器设置,但它为用户提供了各种控制浏览器会话的方法:通过浏览,保存和恢复会话来创建新会话,更改视图中的历史记录和当前页面。 A user could even change session data by editing sessions saved on disk, though this isn't a feature afforded by the browser. 用户甚至可以通过编辑保存在磁盘上的会话来更改会话数据,尽管这不是浏览器提供的功能。 None of this has anything to do with session hijacking. 这些都与会话劫持无关。 Server sessions are created and managed by the server, but users can (attempt to) switch server sessions by changing the session ID their browser passes back to the server, which is the basis for session hijacking. 服务器会话由服务器创建和管理,但用户可以(尝试)通过更改其浏览器传递回服务器的会话ID来切换服务器会话,这是会话劫持的基础。

See also PHP Session Fixation / Hijacking . 另请参见PHP会话固定/劫持

A user can change his session at any time. 用户可以随时更改其会话。 It's just a random string stored in a cookie in the users browser, and therefore it is very simple for the user to change it. 它只是存储在用户浏览器中的cookie中的随机字符串,因此用户更改它非常简单。

As the actual content of the session is stored on your server, you could for instance store the user's ip address, user agent or similar to make it harder to steal sessions from each other, by checking if this information still matches each time a new http request is made. 由于会话的实际内容存储在您的服务器上,您可以例如存储用户的IP地址,用户代理或类似内容,以便通过检查每次新http时此信息是否仍然匹配来更难以相互窃取会话提出要求。

No actually user can not change the actual session value at your website but can change the session id that is used to track the session this session id is stored on client browser by your website usually name "PHPSESSID" in cookie which are also known as session cookie. 实际上,用户无法更改您网站上的实际会话值,但可以更改用于跟踪会话的会话ID,该会话ID由您的网站存储在客户端浏览器上,通常在Cookie中名为“PHPSESSID”,也称为会话曲奇饼。 When a session is started on a site it stores the unique id corresponding to that session in the respective client browser in form of cookie named as "PHPSESSID". 当在站点上启动会话时,它以相应的客户端浏览器以名为“PHPSESSID”的cookie的形式存储与该会话相对应的唯一ID。 So if user is able to get PHPSESSID of any other user and it can replace his PHPSESSID with the victims PHPSESSID and it will result in session hijacking. 因此,如果用户能够获得任何其他用户的PHPSESSID,并且它可以用受害者PHPSESSID替换他的PHPSESSID,则会导致会话劫持。 I am using PHP context here. 我在这里使用PHP上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM