#!/usr/bin/env python3 # -*- coding: utf-8 -*- import subprocess import os import sys import re import struct import argparse import logging from pyquery import PyQuery from pysqlcipher3 import dbapi2 as sqlite from hashlib import md5 import wechat # noqa: setup logger color logger = logging.getLogger("wechat") def subproc_call(cmd, timeout=None): """ Execute a command with timeout, and return STDOUT and STDERR Args: cmd(str): the command to execute. timeout(float): timeout in seconds. Returns: output(bytes), retcode(int). If timeout, retcode is -1. """ try: output = subprocess.check_output( cmd, stderr=subprocess.STDOUT, shell=True, timeout=timeout) return output, 0 except subprocess.TimeoutExpired as e: logger.warn("Command '{}' timeout!".format(cmd)) if e.output: logger.warn(e.output.decode('utf-8')) return e.output, -1 else: return "", -1 except subprocess.CalledProcessError as e: logger.warn("Command '{}' failed, return code={}".format(cmd, e.returncode)) logger.warn(e.output.decode('utf-8')) return e.output, e.returncode except Exception: logger.warn("Command '{}' failed to run.".format(cmd)) return "", -2 def subproc_succ(cmd): """ Execute cmd and expect it succeeds. """ output, ret = subproc_call(cmd) assert ret == 0 RES_DIR = "/mnt/sdcard/tencent/MicroMsg" MM_DIR = "/data/data/com.tencent.mm" def get_uin(): candidates = [] try: uin = None out, _ = subproc_call(f"adb shell cat {MM_DIR}/shared_prefs/system_config_prefs.xml") for line in out.decode('utf-8').split("\n"): if "default_uin" in line: line = PyQuery(line) uin = line.attr["value"] break uin = int(uin) except Exception: logger.warning("uin not found in system_config_prefs.xml") else: candidates.append(uin) logger.info(f"found uin={uin} in system_config_prefs.xml") try: uin = None out, _ = subproc_call(f"adb shell cat {MM_DIR}/shared_prefs/com.tencent.mm_preferences.xml") for line in out.decode('utf-8').split("\n"): if "last_login_uin" in line: line = PyQuery(line) uin = line.text() break uin = int(uin) except Exception: logger.warning("uin not found in com.tencent.mm_preferences.xml") else: candidates.append(uin) logger.info(f"found uin={uin} in com.tencent.mm_preferences.xml") try: uin = None out, _ = subproc_call(f"adb shell cat {MM_DIR}/shared_prefs/auth_info_key_prefs.xml") for line in out.decode('utf-8').split("\n"): if "auth_uin" in line: line = PyQuery(line) uin = line.attr["value"] break uin = int(uin) except Exception: logger.warning("uin not found in auth_info_key_prefs.xml") else: candidates.append(uin) logger.info(f"found uin={uin} in auth_info_key_prefs.xml") candidates = list({x for x in candidates if x != 0}) logger.info(f"Possible uin: {candidates}") return candidates def get_imei(): candidates = [] class Parcel(object): # https://gist.github.com/ktnr74/60ac7bcc2cd17b43f2cb def __init__(self, text): if text.startswith(b'Result: Parcel(') and text.endswith(b'\')'): self.data = b''.join([struct.pack('