#!/usr/bin/env python3
"""检查微信进程并尝试attach"""
import frida
import sys

mgr = frida.get_device_manager()
device = mgr.add_remote_device('192.168.0.12:27042')
print(f'Device: {device.name}')

# 列出所有微信相关进程
procs = [p for p in device.enumerate_processes() if 'tencent.mm' in p.name]
print(f'\n微信相关进程:')
for p in procs:
    print(f'  PID={p.pid} Name={p.name}')

# 主进程是 com.tencent.mm（不带后缀）
main = [p for p in procs if p.name == 'com.tencent.mm']
if main:
    print(f'\n主进程 PID: {main[0].pid}')
else:
    print('\n主进程未找到，检查应用列表...')
    apps = [a for a in device.enumerate_applications() if 'tencent.mm' in a.identifier]
    for a in apps:
        print(f'  App: {a.identifier} PID={a.pid}')
    if apps and apps[0].pid > 0:
        print(f'\n使用应用PID: {apps[0].pid}')
    else:
        print('\n微信可能未在前台运行，尝试打开微信...')
        # 通过push进程确认微信存在
        push = [p for p in procs if ':push' in p.name]
        if push:
            print(f'  push进程存在，微信已安装但可能在后台')
            print('  请先打开微信到前台')
        sys.exit(1)
