Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
import platform
import sys
from wsl_utils import isRunningOnWsl, convertWindowsPath2Wsl

CONFIG_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "config.json")

Expand Down Expand Up @@ -198,6 +199,10 @@ def load_config():
else:
cfg = {**_DEFAULT, **cfg}

# converting windows path if we are running in wsl
if isRunningOnWsl():
cfg["db_dir"] = convertWindowsPath2Wsl(cfg["db_dir"])

# 将相对路径转为绝对路径
base = os.path.dirname(os.path.abspath(__file__))
for key in ("keys_file", "decrypted_dir", "decoded_image_dir"):
Expand Down
4 changes: 4 additions & 0 deletions find_all_keys.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import functools
import platform
import sys
from wsl_utils import isRunningOnWsl


@functools.lru_cache(maxsize=1)
Expand All @@ -9,6 +10,9 @@ def _load_impl():
if system == "windows":
import find_all_keys_windows as impl
return impl
if isRunningOnWsl():
import find_all_keys_wsl as impl
return impl
if system == "linux":
import find_all_keys_linux as impl
return impl
Expand Down
41 changes: 41 additions & 0 deletions find_all_keys_wsl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
## 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


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():
callWindowsScriptCommand = f"""
import sys
sys.path.append(r'{WINDOWS_DIR}')
from find_all_keys_windows import get_pids

res = get_pids()
print(res)
"""
result = subprocess.run(["python.exe", "-c", callWindowsScriptCommand], capture_output=True, text=True)

if result.returncode != 0:
raise Exception("Error while getting the pids on windows")
try:
resString = result.stdout.strip().split("\n")[-1]
return ast.literal_eval(resString)
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"
subprocess.run(["python.exe", windows_full_path])
5 changes: 5 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import json
import os
import sys
from wsl_utils import isRunningOnWsl, convertWindowsPath2Wsl

import functools
print = functools.partial(print, flush=True)
Expand Down Expand Up @@ -34,6 +35,10 @@ def ensure_keys(keys_file, db_dir):
keys = {}
# 检查密钥是否匹配当前 db_dir(防止切换账号后误复用旧密钥)
saved_dir = keys.pop("_db_dir", None)

if isRunningOnWsl():
saved_dir = convertWindowsPath2Wsl(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}")
Expand Down
13 changes: 13 additions & 0 deletions wsl_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import platform
import os
import subprocess

def isRunningOnWsl():
return (
platform.system().lower() == "linux"
and os.path.exists('/proc/sys/fs/binfmt_misc/WSLInterop')
)

def convertWindowsPath2Wsl(windowsPath):
result = subprocess.run(['wslpath', '-u', windowsPath], capture_output=True, text=True, check=True)
return result.stdout.strip()