65 lines
2.1 KiB
Python
65 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
从浏览器登录中提取加密密码(Python Playwright 版本)
|
|
|
|
使用方式:
|
|
python extract_encrypted_password.py <account> <password> [app_code]
|
|
|
|
示例:
|
|
python extract_encrypted_password.py admin 123456 workSystem
|
|
"""
|
|
import sys
|
|
from playwright.sync_api import sync_playwright
|
|
|
|
|
|
def extract_encrypted_password(account: str, password: str, app_code: str = 'dr') -> str:
|
|
"""通过 UI 登录拦截提取加密密码"""
|
|
encrypted_password = {}
|
|
|
|
def handle_request(request):
|
|
if '/oauth/Login' in request.url and request.method == 'POST':
|
|
post_data = request.post_data
|
|
if post_data:
|
|
# POST 数据格式: account=xxx&password=xxx&origin=hash&...
|
|
if 'password=' in post_data:
|
|
import re
|
|
match = re.search(r'password=([^&]+)', post_data)
|
|
if match:
|
|
encrypted_password['value'] = match.group(1)
|
|
|
|
with sync_playwright() as p:
|
|
browser = p.chromium.launch(headless=True)
|
|
context = browser.new_context()
|
|
page = context.new_page()
|
|
page.on('request', handle_request)
|
|
|
|
login_url = f"http://127.0.0.1:3000/jnpf_app_{app_code}/login"
|
|
page.goto(login_url, wait_until='domcontentloaded', timeout=30000)
|
|
page.wait_for_timeout(2000)
|
|
|
|
page.fill('#form_item_account', account)
|
|
page.fill('#form_item_password', password)
|
|
page.locator('button:has-text("登 录")').click()
|
|
|
|
# 等待密码在请求中捕获即可(不等页面跳转)
|
|
page.wait_for_timeout(3000)
|
|
|
|
browser.close()
|
|
|
|
if not encrypted_password:
|
|
raise RuntimeError('未能拦截到登录请求')
|
|
return encrypted_password['value']
|
|
|
|
|
|
if __name__ == '__main__':
|
|
if len(sys.argv) < 3:
|
|
print(f'用法: python {sys.argv[0]} <account> <password> [app_code]', file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
account = sys.argv[1]
|
|
password = sys.argv[2]
|
|
app_code = sys.argv[3] if len(sys.argv) > 3 else 'dr'
|
|
|
|
result = extract_encrypted_password(account, password, app_code)
|
|
print(result)
|