标签

Honeymoon - Thomas Ng

归档

近期文章

php实现游客状态获取web端快手cookie

Webman框架,本文章php实现游客状态获取web端快手cookie

不懂的可以评论或联系我邮箱:owen@owenzhang.com

著作权归OwenZhang所有。商业转载请联系OwenZhang获得授权,非商业转载请注明出处。

调用:

$cookie = KuaishouService::instance()->getCookie();

KuaishouService.php代码如下:

<?php

/**
 * 快手服务类
 */

namespace app\service;

class KuaishouService
{

    public static ?KuaishouService $_instance = null;

    /**
     * @return KuaishouService|mixed
     */
    public static function instance(): ?KuaishouService
    {
        if (!static::$_instance) static::$_instance = new self();
        return static::$_instance;
    }

    /**
     * @notes : web端cookie生成,游客状态
     *          模拟加密生成(基于逆向分析)
     * @return string
     * @throws \Random\RandomException
     * @author: OwenZhang
     * @time  : 2026/1/29 10:06
     */
    public function getCookie(): string
    {
        // 这些函数需要根据实际的JS逆向结果来实现
        $cookies = [
            '_did'              => 'web_' . mt_rand(1000000000, 9999999999) . strtoupper(bin2hex(random_bytes(4))),
            'kpf'               => 'PC_WEB',
            'clientid'          => '3',
            'did'               => 'web_' . bin2hex(random_bytes(16)),
            'kwpsecproductname' => 'kuaishou-vision',
            'kwssectoken'       => $this->simulateKwssectoken(),
            'kwscode'           => $this->simulateKwscode(),
            'ktrace-context'    => $this->simulateKtraceContext(),
            'kpn'               => 'KUAISHOU_VISION'
        ];

        // 构建Cookie字符串
        $cookieStr = '';
        foreach ($cookies as $key => $value) {
            $cookieStr .= $key . '=' . $value . '; ';
        }

        return rtrim($cookieStr, '; ');
    }

    private function simulateKwssectoken()
    {
        // 模拟生成kwssectoken
        $timestamp = time();
        $random    = bin2hex(random_bytes(24));
        $data      = $timestamp . ':' . $random;

        // Base64编码并清理
        $base64 = base64_encode($data);
        return str_replace(['+', '/', '='], ['', '', ''], $base64);
    }

    private function simulateKwscode()
    {
        // 模拟生成kwscode
        $data = [
            'ts'      => round(microtime(true) * 1000),
            'nonce'   => bin2hex(random_bytes(16)),
            'device'  => bin2hex(random_bytes(8)),
            'version' => '1.0.0'
        ];

        $json   = json_encode($data);
        $base64 = base64_encode($json . '|' . md5($json));

        // 确保有=填充
        while (strlen($base64) % 4 != 0) {
            $base64 .= '=';
        }

        return $base64;
    }

    private function simulateKtraceContext()
    {
        $traceId  = bin2hex(random_bytes(8));
        $spanId   = bin2hex(random_bytes(8));
        $parentId = bin2hex(random_bytes(8));

        $part1 = base64_encode("1.0|{$traceId}|{$parentId}|" . mt_rand(100000, 999999));
        $part2 = base64_encode("1.0|{$spanId}|" . mt_rand(100000, 999999) . '|' . mt_rand(100000, 999999));

        return "1|{$part1}|{$part2}|0|webservice-user-growth-node|webservice|true|src-Js";
    }

}

Buy me a cup of coffee 🙂

觉得对你有帮助,就给我打赏吧,谢谢!

微信赞赏码链接,点击跳转:

php实现游客状态获取web端快手cookie插图

Tags: