PHP 使用 rand() 函数生成令牌安全吗?
Web 应用经常需要创建一个难以被猜解的令牌,例如,会话令牌、CSRF 令牌或者在忘记密码功能中用于邮件重置密码使用的令牌。这些令牌本应该被进行加密保护,但实际中却经常被使用多次调用 rand函数并转换输出为字符串来表示。本文将展示预测使用 rand产生的令牌其实并不那么困难。
rand 是如何工作的
在 PHP 中,函数 rand 用于产生伪随机数,而初始化随机数的种子是由 srand 产生的。如果用户不选择调用 srand,那么 PHP 会将一个非常难以猜测的数字作为随机数生成器的种子。由 srand产生的种子将完全决定了 rand函数生成的随机数。
随机数生成器会保持由 srand初始化过后的状态,并在每次调用 rand后改变。这个状态与进程状态相关,所以两个进程通常不会返回相同的 rand随机数。
示例程序
我们使用的实例程序为 EZChatter,一个用于生成 CSRF 令牌的小程序,但在创建时没有很好的考虑安全性:
public static function gen($len = 5){ $token = ''; while($len--){ $choose = rand(0, 2); if ($choose === 0) $token .= chr(rand(ord('A'), ord('Z'))); else if($choose === 1) $token .= chr(rand(ord('a'), ord('z'))); else $token .= chr(rand(ord('0'), ord('9'))); } return $token;}
如你所见,上述代码首先调用 rand来决定是使用大写字母、小写字母还是数字,然后在选择一个特定的字母或数字。每当我们请求 index.php 时都会产生一个新的 CSRF 令牌,所以我们可以随意的发出请求以产生我们需要的令牌。我们的目标是预测分配给用户的令牌,这样我们就可以进行 CSRF 攻击了。
猜解种子
正如前文所说,随机数序列完全由种子所决定的,所以我们可以简单的尝试每一个可能的数字作为 srand的参数来猜测到正确的随机数生成器状态。但请注意,这在 Linux 上只有服务器进程是新创建的才能奏效,如果服务器已经产生了很多 rand调用,那么我们就需要在破解程序中重复同样次数的调用以获取到相同的状态。而在 Windows 上,随机数生成器的状态只与 srand的参数有关,所以就不需要进行一个重复的过程了。
如果想从一个新创建的进程中获取到一个令牌,那么可以使用下列的 PHP 脚本来进行破解:
for ($i = 0; $i < PHP_INT_MAX; $i++) { srand($i); if (Token::gen(10) == "2118Jx9w3e") { die("Found: $i \n"); }}
为了搜索 srand总计 4294967295 个可能的参数值,大概要花费12小时。然而,由于 PHP 仅仅只是调用了 glibc 的 rand函数,我们可以重新转换 PHP 代码为 C 来加快运行速度。这里给出两个版本的代码,一个调用的是 glibc rand,另一个模仿的是 Windows rand。它是基于 token.php中的 PHP 代码,使用 PHP 中的宏 ext/standard/rand.c来循环尝试找出可能的种子。在 Windows 上大约只需要十来分钟,Linux 上大约需要几个小时。
一旦攻击完成,就拥有了跟服务器处在相同状态下的随机数生成器,从而可以同服务器产生相同的令牌。通过将自己生成的令牌与服务器返回的令牌进行比对,就可以知道哪些令牌已经被分配给了用户,进而可以开始攻击。
Linux 上的状态攻击
在 Windows 上,猜解 srand参数和随机数生成器的状态是一回事,但在 Linux 上则不同。glibc 的 rand()保持一系列的数字,并决定下一个状态像下面这样:
state[i] = state[i-3] + state[i-31]return state[i] >> 1
所以每一个输出近似是之前 3 和 31 的结果之和。考虑如下令牌:
- 6ZF5kNgonV
- 9h3byovpGR
- gGt0A94U92
现在,该决定下一个随机数是否为大写字母、小写字母还是数字。这将由之前第 3 和 31 次的结果决定, gGt0A94U92中的 9 和 9h3byovpGR中的 y。所以,我们预计下一个 rand(0, 2)输出会近似于⌊10/10 + 25/26 × 3⌋ = 2 mod 3,这意味着我们将得到一个数字。下面假设我们可以预测到这个数字,则下一次调用 rand得到的数字将由之前第 3 次调用 rand得到的数字和之前第 31 次调用的 rand得到小写字母决定。这个数字将介于 ⌊2/3 + 1/3 × 10⌋ = 0 mod 10 和 ⌊3/3 + 2/3 × 10⌋ = 6 mod 10 之间。因此我们预计值将介于 0 到6,最终结果为 4:
- 43J2d2ew31
如你所见,我们没法通过这种方法精确的预测下一个随机数,但也很清楚,我们可以预测到大致的范围,你已经不可以称其为随机数了。另外也可以使用同样的方式猜解 glibc的随机数生成器的状态,尽管这里没有在进行尝试。
总结
应当使用安全加密的随机数生成器。如果使用 rand产生随机数,很多情况下是可以对其随机数生成器进行猜解的,这样令牌将变得可以被预测。在 Linux 上预测令牌会相对困难一些,但这仍然并不安全。Windows 上的随机数生成器相对更容易被利用,因为其随机数生成器状态可以在数分钟内被猜解出来。
*原文: sjoerdlangkemper.nl,FB小编xiaix编译,转自须注明来自FreeBuf黑客与极客(FreeBuf.COM)

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
