Ejemplo n.º 1
0
    def t106(
            cls,
            sso_version: int,
            app_id: int,
            sub_app_id: int,
            app_client_version: int,
            uin: int,
            salt: int,
            password_md5: bytes,
            guid: bytes,
            tgtgt_key: bytes,
            ip: bytes = bytes(4),
            save_password: bool = True,
            login_type: int = 1,  # password login
    ) -> "Packet[()]":
        key = md5(
            Packet.build(password_md5, bytes(4),
                         struct.pack(">I", salt or uin))).digest()

        body = Packet.build(
            struct.pack(
                ">HIIIIQ",
                4,  # tgtgt version
                cls._random_int32(),
                sso_version,
                app_id,
                app_client_version,
                uin or salt,
            ),
            struct.pack(">I", int(time.time())),
            ip,
            struct.pack(">?", save_password),
            struct.pack(">16s", password_md5),
            tgtgt_key,
            struct.pack(">I?", 0, bool(guid)),
            guid or struct.pack(
                ">IIII",
                cls._random_int32(),
                cls._random_int32(),
                cls._random_int32(),
                cls._random_int32(),
            ),
            struct.pack(">II", sub_app_id, login_type),
            cls._pack_lv(str(uin).encode()),
            struct.pack(">H", 0),  # not found in source
        )

        data = qqtea_encrypt(bytes(body), key)
        return cls._pack_tlv(0x106, data)
Ejemplo n.º 2
0
 def t144(
     cls,
     imei: bytes,
     bootloader: str,
     proc_version: str,
     codename: str,
     incremental: str,
     fingerprint: str,
     boot_id: str,
     android_id: str,
     baseband: str,
     inner_version: str,
     os_type: bytes,
     os_version: bytes,
     network_type: int,
     sim_info: bytes,
     apn: bytes,
     is_guid_from_file_null: bool,
     is_guid_available: bool,
     is_guid_changed: bool,
     guid_flag: int,
     build_model: bytes,
     guid: bytes,
     build_brand: bytes,
     tgtgt_key: bytes,
 ) -> "Packet[()]":
     return cls._pack_tlv(
         0x144,
         qqtea_encrypt(
             bytes(
                 Packet.build(
                     struct.pack(">H", 5),  # tlv count
                     cls.t109(imei),
                     cls.t52d(
                         bootloader,
                         proc_version,
                         codename,
                         incremental,
                         fingerprint,
                         boot_id,
                         android_id,
                         baseband,
                         inner_version,
                     ),
                     cls.t124(os_type, os_version, network_type, sim_info,
                              apn),
                     cls.t128(
                         is_guid_from_file_null,
                         is_guid_available,
                         is_guid_changed,
                         guid_flag,
                         build_model,
                         guid,
                         build_brand,
                     ),
                     cls.t16e(build_model),
                 )),
             tgtgt_key,
         ),
     )
Ejemplo n.º 3
0
    def build(
        cls,
        uin: int,
        body_type: int,
        body: Union[bytes, Packet],
        ksso_version: int = 0xA,
        key: Optional[bytes] = None,
        extra_data: bytes = b"",
    ) -> "CSsoDataPacket":
        """Build CSSOPacket head and append body.

        Packet body was encrypted in `CSSOData::serialize`.

        Note:
            Source: `CSSOHead::serialize_verFull`
        """
        return cls().write_with_length(
            Packet.build(
                struct.pack(">IB", ksso_version, body_type),
                struct.pack(">I",
                            len(extra_data) + 4),
                extra_data,
                bytes([0]),
                struct.pack(">I",
                            len(str(uin)) + 4),
                str(uin).encode(),
                qqtea_encrypt(bytes(body), key) if key else body,
            ),
            offset=4,
        )
Ejemplo n.º 4
0
 def encrypt(
     self, data: Union[bytes, Packet], key: Union[bytes, Packet]
 ) -> Packet:
     return Packet.build(
         struct.pack(">H", len(self.ticket)),
         self.ticket,
         qqtea_encrypt(bytes(data), bytes(key)),
     )
Ejemplo n.º 5
0
 def encrypt(
     cls, data: Union[bytes, Packet], key: Union[bytes, Packet]
 ) -> Packet:
     return Packet.build(
         struct.pack(">BB", 2, 1),
         key,
         struct.pack(
             ">HHH",
             305,
             1,  # oicq.wlogin_sdk.tools.EcdhCrypt.sKeyVersion
             len(cls.client_public_key),
         ),
         cls.client_public_key,
         qqtea_encrypt(bytes(data), cls.share_key),
     )
Ejemplo n.º 6
0
 def t400(
     cls,
     g: bytes,
     uin: int,
     guid: bytes,
     dpwd: bytes,
     app_id: int,
     sub_app_id: int,
     rand_seed: bytes,
     _version: int = 1,
 ) -> "Packet[()]":
     data = Packet.build(
         struct.pack(">HQ", _version, uin),
         guid,
         dpwd,
         struct.pack(">III", app_id, sub_app_id, int(time.time())),
         rand_seed,
     )
     return cls._pack_tlv(0x400, qqtea_encrypt(bytes(data), g))
Ejemplo n.º 7
0
def encode_exchange_emp_15(
    seq: int,
    session_id: bytes,
    uin: int,
    g: bytes,
    dpwd: bytes,
    no_pic_sig: bytes,
    encrypted_a1: bytes,
    rand_seed: bytes,
    wt_session_ticket: bytes,
    wt_session_ticket_key: bytes,
) -> Packet:
    """Build exchange emp request packet.

    command id: ``0x810 = 2064``

    sub command id: ``15``

    command name: ``wtlogin.exchange_emp``

    Note:
        Source: oicq.wlogin_sdk.request.aa

    Args:
        seq (int): Packet sequence.
        session_id (bytes): Session ID.
        ksid (bytes): KSID of client.
        uin (int): User QQ number.
        g (bytes): Siginfo g.
        dpwd (bytes): Siginfo dpwd.
        no_pic_sig (bytes): Siginfo no pic sig.
        encrypted_a1 (bytes): Siginfo Encrypted A1.
        rand_seed (bytes): Siginfo random seed.
        wt_session_ticket (bytes): Siginfo session ticket.
        wt_session_ticket_key (bytes): Siginfo session ticket key.

    Returns:
        Packet: Exchange emp packet.
    """
    COMMAND_ID = 2064
    SUB_COMMAND_ID = 15
    COMMAND_NAME = "wtlogin.exchange_emp"

    APK_ID = APK_INFO.apk_id
    APK_VERSION = APK_INFO.version
    APK_SIGN = APK_INFO.apk_sign
    APK_BUILD_TIME = APK_INFO.build_time
    APP_ID = APK_INFO.app_id
    SUB_APP_ID = APK_INFO.sub_app_id
    APP_CLIENT_VERSION = 0
    SDK_VERSION = APK_INFO.sdk_version
    SSO_VERSION = APK_INFO.sso_version
    BITMAP = APK_INFO.bitmap
    MAIN_SIGMAP = APK_INFO.main_sigmap
    SUB_SIGMAP = APK_INFO.sub_sigmap

    GUID = DEVICE.guid
    GUID_SRC = 1
    GUID_CHANGE = 0
    GUID_FLAG = 0
    GUID_FLAG |= GUID_SRC << 24 & 0xFF000000
    GUID_FLAG |= GUID_CHANGE << 8 & 0xFF00
    LOCAL_ID = 2052  # oicq.wlogin_sdk.request.t.v
    IP_BYTES: bytes = ipaddress.ip_address(DEVICE.ip_address).packed
    NETWORK_TYPE = (DEVICE.apn == "wifi") + 1

    data = Packet.build(
        struct.pack(">HH", SUB_COMMAND_ID, 24),
        TlvEncoder.t18(APP_ID, APP_CLIENT_VERSION, uin),
        TlvEncoder.t1(uin, int(time.time()), IP_BYTES),
        TlvEncoder._pack_tlv(0x106, encrypted_a1),
        TlvEncoder.t116(BITMAP, SUB_SIGMAP),
        TlvEncoder.t100(SSO_VERSION, APP_ID, SUB_APP_ID, APP_CLIENT_VERSION,
                        MAIN_SIGMAP),
        TlvEncoder.t107(),
        # TlvEncoder.t108(KSID),  # null when first time login
        TlvEncoder.t144(
            DEVICE.imei.encode(),
            DEVICE.bootloader,
            DEVICE.proc_version,
            DEVICE.version.codename,
            DEVICE.version.incremental,
            DEVICE.fingerprint,
            DEVICE.boot_id,
            DEVICE.android_id,
            DEVICE.baseband,
            DEVICE.version.incremental,
            DEVICE.os_type.encode(),
            DEVICE.version.release.encode(),
            NETWORK_TYPE,
            DEVICE.sim.encode(),
            DEVICE.apn.encode(),
            False,
            True,
            False,
            GUID_FLAG,
            DEVICE.model.encode(),
            DEVICE.guid,
            DEVICE.brand.encode(),
            DEVICE.tgtgt,
        ),
        TlvEncoder.t142(APK_ID),
        # TlvEncoder.t112(),
        TlvEncoder.t145(DEVICE.guid),
        # TlvEncoder.t166(1),
        TlvEncoder.t16a(no_pic_sig),
        TlvEncoder.t154(seq),
        TlvEncoder.t141(DEVICE.sim.encode(), NETWORK_TYPE,
                        DEVICE.apn.encode()),
        TlvEncoder.t8(LOCAL_ID),
        TlvEncoder.t511([
            "tenpay.com",
            "openmobile.qq.com",
            "docs.qq.com",
            "connect.qq.com",
            "qzone.qq.com",
            "vip.qq.com",
            "gamecenter.qq.com",
            "qun.qq.com",
            "game.qq.com",
            "qqweb.qq.com",
            "office.qq.com",
            "ti.qq.com",
            "mail.qq.com",
            "mma.qq.com",
        ]),  # com.tencent.mobileqq.msf.core.auth.l
        TlvEncoder.t147(APP_ID, APK_VERSION.encode(), APK_SIGN),
        # TlvEncoder.t172(),
        TlvEncoder.t177(APK_BUILD_TIME, SDK_VERSION),
        TlvEncoder.t400(g, uin, GUID, dpwd, 1, APP_ID, rand_seed),
        TlvEncoder.t187(DEVICE.mac_address.encode()),
        TlvEncoder.t188(DEVICE.android_id.encode()),
        TlvEncoder.t194(DEVICE.imsi_md5) if DEVICE.imsi_md5 else b"",
        # TlvEncoder.t201(),
        TlvEncoder.t202(DEVICE.wifi_bssid.encode(), DEVICE.wifi_ssid.encode()),
        TlvEncoder.t516(),
        TlvEncoder.t521(),
        TlvEncoder.t525(TlvEncoder.t536([])),
    )
    session = EncryptSession(wt_session_ticket)
    oicq_packet = OICQRequest.build_encoded(
        uin,
        COMMAND_ID,
        session.encrypt(data, wt_session_ticket_key),
        session.id,
    )
    packet = UniPacket.build(uin,
                             seq,
                             COMMAND_NAME,
                             session_id,
                             2,
                             oicq_packet,
                             key=bytes(16))
    return packet
Ejemplo n.º 8
0
def encode_login_request20(
    seq: int,
    key: bytes,
    session_id: bytes,
    ksid: bytes,
    uin: int,
    t104: bytes,
    g: bytes,
) -> Packet:
    """Build device lock login request packet.

    Called in ``oicq.wlogin_sdk.request.WtloginHelper.GetStWithoutPasswd``.

    command id: ``0x810 = 2064``

    sub command id: ``20``

    command name: ``wtlogin.login``

    Note:
        Source: oicq.wlogin_sdk.request.p

    Args:
        seq (int): Packet sequence.
        key (bytes): 16 bits key used to decode the response.
        session_id (bytes): Session ID.
        ksid (bytes): KSID of client.
        uin (int): User QQ number.
        t104 (bytes): T104 response data.
        g (bytes): md5 of (guid + dpwd + t402).

    Returns:
        Packet: Login packet.
    """
    COMMAND_ID = 2064
    SUB_COMMAND_ID = 20
    COMMAND_NAME = "wtlogin.login"

    SUB_APP_ID = APK_INFO.sub_app_id
    BITMAP = APK_INFO.bitmap
    SUB_SIGMAP = APK_INFO.sub_sigmap

    LOCAL_ID = 2052  # oicq.wlogin_sdk.request.t.v

    data = Packet.build(
        struct.pack(">HH", SUB_COMMAND_ID, 4),  # packet num
        TlvEncoder.t8(LOCAL_ID),
        TlvEncoder.t104(t104),
        TlvEncoder.t116(BITMAP, SUB_SIGMAP),
        TlvEncoder.t401(g),
    )
    oicq_packet = OICQRequest.build_encoded(uin, COMMAND_ID,
                                            ECDH.encrypt(data, key), ECDH.id)
    sso_packet = CSsoBodyPacket.build(
        seq,
        SUB_APP_ID,
        COMMAND_NAME,
        DEVICE.imei,
        session_id,
        ksid,
        oicq_packet,
    )
    # encrypted by 16-byte zero. Reference: ``CSSOData::serialize``
    packet = CSsoDataPacket.build(uin, 2, sso_packet, key=bytes(16))
    return packet
Ejemplo n.º 9
0
def encode_login_request2_captcha(
    seq: int,
    key: bytes,
    session_id: bytes,
    ksid: bytes,
    uin: int,
    captcha: str,
    sign: bytes,
    t104: bytes,
) -> Packet:
    """Build submit captcha request packet.

    Called in ``oicq.wlogin_sdk.request.WtloginHelper.CheckPictureAndGetSt``.

    command id: ``0x810 = 2064``

    sub command id: ``2``

    command name: ``wtlogin.login``

    Note:
        Source: oicq.wlogin_sdk.request.n

    Args:
        seq (int): Packet sequence.
        key (bytes): 16 bits key used to decode the response.
        session_id (bytes): Session ID.
        ksid (bytes): KSID of client.
        uin (int): User QQ number.
        captcha (str): Captcha image result.
        sign (bytes): Signature of the captcha.
        t104 (bytes): TLV 104 data.

    Returns:
        Packet: Login packet.
    """
    COMMAND_ID = 2064
    SUB_COMMAND_ID = 2
    COMMAND_NAME = "wtlogin.login"

    SUB_APP_ID = APK_INFO.sub_app_id
    BITMAP = APK_INFO.bitmap
    SUB_SIGMAP = APK_INFO.sub_sigmap

    LOCAL_ID = 2052  # oicq.wlogin_sdk.request.t.v

    data = Packet.build(
        struct.pack(">HH", SUB_COMMAND_ID, 4),  # packet num
        TlvEncoder.t2(captcha.encode(), sign),
        TlvEncoder.t8(LOCAL_ID),
        TlvEncoder.t104(t104),
        TlvEncoder.t116(BITMAP, SUB_SIGMAP),
    )
    oicq_packet = OICQRequest.build_encoded(uin, COMMAND_ID,
                                            ECDH.encrypt(data, key), ECDH.id)
    sso_packet = CSsoBodyPacket.build(
        seq,
        SUB_APP_ID,
        COMMAND_NAME,
        DEVICE.imei,
        session_id,
        ksid,
        oicq_packet,
    )
    # encrypted by 16-byte zero. Reference: ``CSSOData::serialize``
    packet = CSsoDataPacket.build(uin, 2, sso_packet, key=bytes(16))
    return packet
Ejemplo n.º 10
0
def encode_login_request9(
    seq: int,
    key: bytes,
    session_id: bytes,
    ksid: bytes,
    uin: int,
    password_md5: bytes,
) -> Packet:
    """Build main login request packet.

    Called in ``oicq.wlogin_sdk.request.WtloginHelper.GetStWithPasswd``.

    command id: ``0x810 = 2064``

    sub command id: ``9``

    command name: ``wtlogin.login``

    Note:
        Source: oicq.wlogin_sdk.request.k

    Args:
        seq (int): Packet sequence.
        key (bytes): 16 bits key used to decode the response.
        session_id (bytes): Session ID.
        ksid (bytes): KSID of client.
        uin (int): User QQ number.
        password_md5 (bytes): User QQ password md5 hash.

    Returns:
        Packet: Login packet.
    """
    COMMAND_ID = 2064
    SUB_COMMAND_ID = 9
    COMMAND_NAME = "wtlogin.login"

    APK_ID = APK_INFO.apk_id
    APK_VERSION = APK_INFO.version
    APK_SIGN = APK_INFO.apk_sign
    APK_BUILD_TIME = APK_INFO.build_time
    APP_ID = APK_INFO.app_id
    SUB_APP_ID = APK_INFO.sub_app_id
    APP_CLIENT_VERSION = 0
    SDK_VERSION = APK_INFO.sdk_version
    SSO_VERSION = APK_INFO.sso_version
    BITMAP = APK_INFO.bitmap
    MAIN_SIGMAP = APK_INFO.main_sigmap
    SUB_SIGMAP = APK_INFO.sub_sigmap

    GUID_SRC = 1
    GUID_CHANGE = 0
    GUID_FLAG = 0
    GUID_FLAG |= GUID_SRC << 24 & 0xFF000000
    GUID_FLAG |= GUID_CHANGE << 8 & 0xFF00
    CAN_WEB_VERIFY = 130  # oicq.wlogin_sdk.request.k.K
    LOCAL_ID = 2052  # oicq.wlogin_sdk.request.t.v
    IP_BYTES: bytes = ipaddress.ip_address(DEVICE.ip_address).packed
    NETWORK_TYPE = (DEVICE.apn == "wifi") + 1

    data = Packet.build(
        struct.pack(">HH", SUB_COMMAND_ID, 23),  # packet num
        TlvEncoder.t18(APP_ID, APP_CLIENT_VERSION, uin),
        TlvEncoder.t1(uin, int(time.time()), IP_BYTES),
        TlvEncoder.t106(
            SSO_VERSION,
            APP_ID,
            SUB_APP_ID,
            APP_CLIENT_VERSION,
            uin,
            0,
            password_md5,
            DEVICE.guid,
            DEVICE.tgtgt,
        ),
        TlvEncoder.t116(BITMAP, SUB_SIGMAP),
        TlvEncoder.t100(SSO_VERSION, APP_ID, SUB_APP_ID, APP_CLIENT_VERSION,
                        MAIN_SIGMAP),
        TlvEncoder.t107(),
        # TlvEncoder.t108(KSID),  # null when first time login
        # TlvEncoder.t104(),
        TlvEncoder.t142(APK_ID),
        TlvEncoder.t144(
            DEVICE.imei.encode(),
            DEVICE.bootloader,
            DEVICE.proc_version,
            DEVICE.version.codename,
            DEVICE.version.incremental,
            DEVICE.fingerprint,
            DEVICE.boot_id,
            DEVICE.android_id,
            DEVICE.baseband,
            DEVICE.version.incremental,
            DEVICE.os_type.encode(),
            DEVICE.version.release.encode(),
            NETWORK_TYPE,
            DEVICE.sim.encode(),
            DEVICE.apn.encode(),
            False,
            True,
            False,
            GUID_FLAG,
            DEVICE.model.encode(),
            DEVICE.guid,
            DEVICE.brand.encode(),
            DEVICE.tgtgt,
        ),
        TlvEncoder.t145(DEVICE.guid),
        TlvEncoder.t147(APP_ID, APK_VERSION.encode(), APK_SIGN),
        # TlvEncoder.t166(1),
        # TlvEncoder.t16a(),
        TlvEncoder.t154(seq),
        TlvEncoder.t141(DEVICE.sim.encode(), NETWORK_TYPE,
                        DEVICE.apn.encode()),
        TlvEncoder.t8(LOCAL_ID),
        TlvEncoder.t511([
            "tenpay.com",
            "openmobile.qq.com",
            "docs.qq.com",
            "connect.qq.com",
            "qzone.qq.com",
            "vip.qq.com",
            "gamecenter.qq.com",
            "qun.qq.com",
            "game.qq.com",
            "qqweb.qq.com",
            "office.qq.com",
            "ti.qq.com",
            "mail.qq.com",
            "mma.qq.com",
        ]),  # com.tencent.mobileqq.msf.core.auth.l
        # TlvEncoder.t172(),
        # TlvEncoder.t185(1),  # when sms login, is_password_login == 3
        # TlvEncoder.t400(),  # null when first time login
        TlvEncoder.t187(DEVICE.mac_address.encode()),
        TlvEncoder.t188(DEVICE.android_id.encode()),
        TlvEncoder.t194(DEVICE.imsi_md5) if DEVICE.imsi_md5 else b"",
        TlvEncoder.t191(CAN_WEB_VERIFY),
        # TlvEncoder.t201(),
        TlvEncoder.t202(DEVICE.wifi_bssid.encode(), DEVICE.wifi_ssid.encode()),
        TlvEncoder.t177(APK_BUILD_TIME, SDK_VERSION),
        TlvEncoder.t516(),
        TlvEncoder.t521(),
        TlvEncoder.t525(TlvEncoder.t536([])),
        # TlvEncoder.t318()  # not login in by qr
    )
    oicq_packet = OICQRequest.build_encoded(uin, COMMAND_ID,
                                            ECDH.encrypt(data, key), ECDH.id)
    sso_packet = CSsoBodyPacket.build(
        seq,
        SUB_APP_ID,
        COMMAND_NAME,
        DEVICE.imei,
        session_id,
        ksid,
        oicq_packet,
    )
    # encrypted by 16-byte zero. Reference: ``CSSOData::serialize``
    packet = CSsoDataPacket.build(uin, 2, sso_packet, key=bytes(16))
    return packet
Ejemplo n.º 11
0
def encode_login_request8(
    seq: int,
    key: bytes,
    session_id: bytes,
    ksid: bytes,
    uin: int,
    t104: bytes,
    t174: bytes,
) -> Packet:
    """Build sms request packet.

    Called in ``oicq.wlogin_sdk.request.WtloginHelper.RefreshSMSData``.

    command id: ``0x810 = 2064``

    sub command id: ``8``

    command name: ``wtlogin.login``

    Note:
        Source: oicq.wlogin_sdk.request.r

    Args:
        seq (int): Packet sequence.
        key (bytes): 16 bits key used to decode the response.
        session_id (bytes): Session ID.
        ksid (bytes): KSID of client.
        uin (int): User QQ number.
        t104 (bytes): TLV 104 data.
        t174 (bytes): TLV 174 data.

    Returns:
        Packet: Login packet.
    """
    COMMAND_ID = 2064
    SUB_COMMAND_ID = 8
    COMMAND_NAME = "wtlogin.login"

    SMS_APP_ID = 9
    SUB_APP_ID = APK_INFO.sub_app_id
    BITMAP = APK_INFO.bitmap
    SUB_SIGMAP = APK_INFO.sub_sigmap

    GUID_SRC = 1
    GUID_CHANGE = 0
    GUID_FLAG = 0
    GUID_FLAG |= GUID_SRC << 24 & 0xFF000000
    GUID_FLAG |= GUID_CHANGE << 8 & 0xFF00
    LOCAL_ID = 2052  # oicq.wlogin_sdk.request.t.v

    data = Packet.build(
        struct.pack(">HH", SUB_COMMAND_ID, 6),  # packet num
        TlvEncoder.t8(LOCAL_ID),
        TlvEncoder.t104(t104),
        TlvEncoder.t116(BITMAP, SUB_SIGMAP),
        TlvEncoder.t174(t174),
        TlvEncoder.t17a(SMS_APP_ID),
        TlvEncoder.t197(),
    )
    oicq_packet = OICQRequest.build_encoded(uin, COMMAND_ID,
                                            ECDH.encrypt(data, key), ECDH.id)
    sso_packet = CSsoBodyPacket.build(
        seq,
        SUB_APP_ID,
        COMMAND_NAME,
        DEVICE.imei,
        session_id,
        ksid,
        oicq_packet,
    )
    # encrypted by 16-byte zero. Reference: ``CSSOData::serialize``
    packet = CSsoDataPacket.build(uin, 2, sso_packet, key=bytes(16))
    return packet