diff --git a/README.md b/README.md index 34d17a4..58f4263 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ Windows: - Windows 10/11 - 微信正在运行 - 需要管理员权限(读取进程内存) +- 如果你使用 WSL 运行 main.py,请确保 Windows 和 WSL 中都已安装 Python,并配置好相应的依赖库。 Linux: @@ -83,7 +84,7 @@ python3 main.py decrypt "wechat_process": "Weixin.exe" } ``` - +如果您使用 WSL (Windows 子系统) 运行该脚本,请确保按照上述示例使用 Windows 文件格式。 Linux 版 `config.json` 示例: ```json diff --git a/config.py b/config.py index 97efc03..953ee44 100644 --- a/config.py +++ b/config.py @@ -7,6 +7,7 @@ import os import platform import sys +from wsl_utils import is_running_on_wsl, convert_windows_path_to_wsl CONFIG_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "config.json") @@ -198,6 +199,11 @@ def load_config(): else: cfg = {**_DEFAULT, **cfg} + # converting windows path if we are running in wsl + if is_running_on_wsl(): + for key in ("db_dir", "keys_file", "decrypted_dir", "decoded_image_dir"): + cfg[key] = convert_windows_path_to_wsl(cfg[key]) + # 将相对路径转为绝对路径 base = os.path.dirname(os.path.abspath(__file__)) for key in ("keys_file", "decrypted_dir", "decoded_image_dir"): diff --git a/find_all_keys.py b/find_all_keys.py index eba2191..f18c086 100644 --- a/find_all_keys.py +++ b/find_all_keys.py @@ -1,6 +1,7 @@ import functools import platform import sys +from wsl_utils import is_running_on_wsl @functools.lru_cache(maxsize=1) @@ -9,6 +10,9 @@ def _load_impl(): if system == "windows": import find_all_keys_windows as impl return impl + if is_running_on_wsl(): + import find_all_keys_wsl as impl + return impl if system == "linux": import find_all_keys_linux as impl return impl diff --git a/find_all_keys_wsl.py b/find_all_keys_wsl.py new file mode 100644 index 0000000..bf31036 --- /dev/null +++ b/find_all_keys_wsl.py @@ -0,0 +1,49 @@ +## This script is a simple wsl port of the existing windows script +## We can not access the memory of a wechat process running on windows from wsl +## So we simply call the windows version of python to find the keys +## Requires python to be installed on both wsl and windows with the relevant dependencies + +import subprocess +import os +import ast +import json + + +BASEPATH = os.path.dirname(os.path.abspath(__file__)) +# convert to a windows format: +try: + WINDOWS_DIR = subprocess.check_output(["wslpath", "-w", BASEPATH], text=True).strip() +except subprocess.CalledProcessError as e: + raise Exception(f"Error: Could not translate WSL path to Windows path: {e}") + + +def get_pids(): + result_flag = "#RESULT: " + call_windows_script_command = f""" +import sys +sys.path.append({json.dumps(WINDOWS_DIR)}) +from find_all_keys_windows import get_pids + +res = get_pids() +print('{result_flag}' + str(res)) +""" + result = subprocess.run(["python.exe", "-c", call_windows_script_command], capture_output=True, text=True) + + if result.returncode != 0: + raise Exception(f"Error while getting the pids on windows: {result.stderr}") + try: + output_lines = result.stdout.strip().split("\n") + result_line = next((line for line in output_lines if line.startswith(result_flag)), None) + if result_line is None: + raise ValueError + result_string = result_line[len(result_flag):].strip() + return ast.literal_eval(result_string) + except (SyntaxError, ValueError, IndexError) as e: + raise Exception(f"Error while parsing windows output: {e}\nRaw output: {result.stdout}") + + +def main(): + windows_full_path = rf"{WINDOWS_DIR}\find_all_keys_windows.py" + result = subprocess.run(["python.exe", windows_full_path]) + if result.returncode != 0: + raise Exception("error during key extraction on windows") diff --git a/main.py b/main.py index 90b16ee..2360464 100644 --- a/main.py +++ b/main.py @@ -7,6 +7,7 @@ import json import os import sys +from wsl_utils import is_running_on_wsl, convert_windows_path_to_wsl import functools print = functools.partial(print, flush=True) @@ -34,6 +35,10 @@ def ensure_keys(keys_file, db_dir): keys = {} # 检查密钥是否匹配当前 db_dir(防止切换账号后误复用旧密钥) saved_dir = keys.pop("_db_dir", None) + + if is_running_on_wsl(): + saved_dir = convert_windows_path_to_wsl(saved_dir) + if saved_dir and os.path.normcase(os.path.normpath(saved_dir)) != os.path.normcase(os.path.normpath(db_dir)): print(f"[!] 密钥文件对应的目录已变更,需要重新提取") print(f" 旧: {saved_dir}") diff --git a/wsl_utils.py b/wsl_utils.py new file mode 100644 index 0000000..3614d76 --- /dev/null +++ b/wsl_utils.py @@ -0,0 +1,19 @@ +import platform +import os +import subprocess + +def is_running_on_wsl(): + return ( + platform.system().lower() == "linux" + and os.path.exists('/proc/sys/fs/binfmt_misc/WSLInterop') + ) + +def convert_windows_path_to_wsl(windows_path): + if not (windows_path and ("\\" in windows_path or (len(windows_path) > 1 and windows_path[1] == ":"))): + return windows_path + try: + result = subprocess.run(['wslpath', '-u', windows_path], capture_output=True, text=True, check=True) + return result.stdout.strip() + except subprocess.CalledProcessError as e: + raise Exception(f"Failed to convert windows path '{windows_path}': {e}") +