php中session会话的安全性简单理解
获取会话ID的方式很多,攻击者可以通过查看明文通信来获取,所以把会话ID放在URL中或者放在通过未加密连接传输的Cookie中是很危险的;还有在URL中(作为_get()参数)传递会话ID也是不安全的,因为浏览器历史缓存中会存储URL,这样就很容易被读取。(可以考虑使用ssh进行加密传输)
主要的安全措施有以下两个方面。
1、防止攻击者获取用户的会话ID。
获取会话ID的方式很多,攻击者可以通过查看明文通信来获取,所以把会话ID放在URL中或者放在通过未加密连接传输的Cookie中是很危险的;还有在URL中(作为_get()参数)传递会话ID也是不安全的,因为浏览器历史缓存中会存储URL,这样就很容易被读取。(可以考虑使用ssh进行加密传输)
还有一种更为隐蔽的攻击手段,攻击者通过一个被脚本攻击突破的Web站点,把被突破的这个站点上的用户重新定向到另一个站点,然后在重新定向的站点的URL中插入以下代码:
?PHPSESSID=213456465412312365465412312;
最后发送到Web应用程序。当用户查看Web应用程序时,PHP会发现没有与这个会话ID相关联的数据并且会创建一些数据。用户不知道发生了什么,但攻击者却知道了会话ID,就可以利用这个会话ID进入应用程序。
要防止这种攻击,有两种方法。
(1)检查php教程.ini中是否打开了session.use_only_cookie。如果是这种情况,PHP会拒绝基于URL的会话ID。
(2)当启动会话时,在会话数据中放一个变量,这个变量表示会话是用户创建的;如果发现会话数据中没有这个变量,那就说明会话ID是假的,就可以调用session_regenerate_id函数,给现有会话分配一个新的会话ID。
示例:
通过判断变量是否存在来确定会话ID的真假,如果存在,则说明会话ID是真的,否则是假的,并使用session_regenerate_id()函数对会话ID进行更改,重新给会话创建一个新的会话ID,
代码如下:
复制代码 代码如下:
session_start () ;
if (!isset ( $_SESSION['shili1'] )) { //判断shili1变量是否配置
$old_id = session_id () ; //原来的会话ID的变量名
session_regenerate_id () ; //获取一个新的会话ID
$new_id = session_id () ; //新的会话ID的变量名
echo "old : $old_id
" ; //输出原来的会话ID
echo "new : $new_id
" ; //输出新的会话ID
$_SESSION['shili1'] = TRUE ; }
?>
运行结果如图所示:
这只是一个示例,输出会话ID是为了更好的理解和应用这个函数,而在程序设计中是不需要输出会话ID的。
2、限制攻击者获取会话ID。
限制攻击者获取会话ID的方法如下。
(1)使用一个函数(md5)计算User-Agent头加上某些附加字符串数据后的散列值(hash)。(散列函数(hash function)接受一个任意大的数据集,并且将它转换为一个看起来完全不同的数据,这个数据很短。产生的散列值是完全不可重现的,也不可能由另一个输入产生。)
在User-Agent字符串后面添加一些数据,攻击者就无法通过对常见的代理值计算md5编码来试探User-Agent字符串。
(2)将这个经过编码的字符串保存在用户的会话数据中。
(3)每次从这个用户接收到请求时,检查这个散列值。
此方案的代码如下:
复制代码 代码如下:
define ( ‘ua_seed','webapp' ) ;
session_start () ;
if ( !isset($_SESSION['user_agent'] )){
$_SESSION['user_agent'] = md5 ( $_SERVER['HTTP_USER_AGENT'].ua_seed );
}else{
if ($_SESSION['user_agent'] != md5($_SERVER['HTTP_USER_AGENT'].ua_seed)){} }
?>
通过给攻击者制造一些麻烦,使攻击者即使获取了会话ID,也无法进行破坏,能够减少对系统造成的损失

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.