feat: implement Zalo database scanner to extract and dump table schemas via ADB
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
import os
|
||||
import subprocess
|
||||
import logging
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||
|
||||
def run_adb_command(command: str):
|
||||
"""Chạy lệnh ADB shell và trả về kết quả"""
|
||||
full_cmd = f"adb shell {command}"
|
||||
try:
|
||||
result = subprocess.run(full_cmd, shell=True, check=True, capture_output=True, text=True)
|
||||
return result.stdout.strip()
|
||||
except subprocess.CalledProcessError as e:
|
||||
logging.error(f"Lỗi khi chạy lệnh: {full_cmd}")
|
||||
logging.error(e.stderr)
|
||||
return None
|
||||
|
||||
def scan_zalo_databases():
|
||||
# Kiểm tra xem có quyền Root không (su)
|
||||
test_root = run_adb_command("su -c 'echo root_ok'")
|
||||
if test_root != "root_ok":
|
||||
logging.error("Thiết bị chưa được cấp quyền Root cho ADB, hoặc không nhận lệnh su!")
|
||||
return
|
||||
|
||||
logging.info("1. Đang liệt kê các file database của Zalo...")
|
||||
zalo_db_path = "/data/data/com.zing.zalo/databases"
|
||||
|
||||
# Liệt kê các file .db
|
||||
files_output = run_adb_command(f"su -c 'ls -l {zalo_db_path}'")
|
||||
logging.info(f"Các file trong {zalo_db_path}:\n{files_output}\n")
|
||||
|
||||
# Tìm kiếm các file .db chính xác
|
||||
db_list = run_adb_command(f"su -c 'ls {zalo_db_path}/*.db'")
|
||||
if not db_list:
|
||||
logging.error("Không tìm thấy file .db nào của Zalo!")
|
||||
return
|
||||
|
||||
db_files = db_list.split('\n')
|
||||
logging.info(f"Đã tìm thấy {len(db_files)} file database. Đang phân tích các bảng (tables)...")
|
||||
|
||||
# Mở file để ghi schema
|
||||
with open("zalo_schema_dump.txt", "w", encoding="utf-8") as f:
|
||||
f.write("=== DUMP CẤU TRÚC DATABASE ZALO ===\n")
|
||||
|
||||
# Duyệt qua từng DB và lấy cấu trúc bảng
|
||||
for db in db_files:
|
||||
db = db.strip()
|
||||
if not db: continue
|
||||
|
||||
logging.info(f"\n--- Phân tích File: {db} ---")
|
||||
# Sử dụng sqlite3 có sẵn trên Android để liệt kê bảng
|
||||
tables = run_adb_command(f"su -c 'sqlite3 {db} \".tables\"'")
|
||||
|
||||
if tables:
|
||||
logging.info(f"Các bảng trong {db}:")
|
||||
logging.info(tables)
|
||||
|
||||
# Nếu thấy bảng có vẻ giống tin nhắn (như messages, chat, msg...)
|
||||
if "msg" in tables.lower() or "chat" in tables.lower() or "message" in tables.lower():
|
||||
logging.info(f"-> PHÁT HIỆN DB TIỀM NĂNG: {db}. Đang trích xuất cấu trúc...")
|
||||
schema = run_adb_command(f"su -c 'sqlite3 {db} \".schema\"'")
|
||||
with open("zalo_schema_dump.txt", "a", encoding="utf-8") as f:
|
||||
f.write(f"\n--- Schema của {db} ---\n")
|
||||
f.write(schema if schema else "Không thể đọc schema")
|
||||
logging.info(f"Đã lưu schema của {db} vào file zalo_schema_dump.txt")
|
||||
else:
|
||||
logging.warning(f"Không thể đọc bảng từ {db} (Có thể bị khóa hoặc mã hóa)")
|
||||
|
||||
if __name__ == "__main__":
|
||||
logging.info("BẮT ĐẦU QUÉT DATABASE ZALO")
|
||||
# Đảm bảo kết nối ADB tới máy giả lập/điện thoại
|
||||
subprocess.run("adb connect 10.160.138.2:5555", shell=True, capture_output=True)
|
||||
scan_zalo_databases()
|
||||
logging.info("Hoàn tất quét! Hãy kiểm tra file zalo_schema_dump.txt (nếu có)")
|
||||
Reference in New Issue
Block a user