基于Python的移动设备图案锁暴力破解:键盘输入模拟绘制方案问询
用Python实现键盘映射模拟移动设备图案锁输入
一、适用的Python库
- pynput:用于监听键盘输入,捕获用户按下的按键序列;
- pyautogui:用于模拟鼠标拖动操作,将键盘输入转化为屏幕上的图案绘制手势。
这两个库无需依赖ADB,仅通过电脑端操作即可实现需求,适合无法开启USB调试的场景。
二、3×3图案锁的键盘映射逻辑
将键盘数字键1-9与图案锁的3×3网格一一对应(匹配常规电话键盘布局):
1(左上) 2(中上) 3(右上) 4(左中) 5(中间) 6(右中) 7(左下) 8(中下) 9(右下)
用户按下数字键时记录序列,按Enter触发绘制,按Esc终止程序。
三、实现代码示例
首先安装依赖:
pip install pynput pyautogui
核心代码:
import pyautogui from pynput.keyboard import Key, Listener # 需根据你的设备投屏/显示的图案锁位置修改坐标 # 可通过运行`pyautogui.displayMousePosition()`获取实时坐标 pattern_points = { '1': (150, 250), '2': (250, 250), '3': (350, 250), '4': (150, 350), '5': (250, 350), '6': (350, 350), '7': (150, 450), '8': (250, 450), '9': (350, 450) } current_pattern = [] def on_press(key): try: # 捕获数字键,添加到图案序列 if hasattr(key, 'char') and key.char in pattern_points: current_pattern.append(key.char) print(f"添加点: {key.char}") # 按Enter执行图案绘制 elif key == Key.enter: if len(current_pattern) >= 4: draw_pattern(current_pattern) else: print("图案长度不足,至少需4个点") current_pattern.clear() # 按Esc退出监听 elif key == Key.esc: return False except AttributeError: pass def draw_pattern(pattern): print(f"绘制图案: {'->'.join(pattern)}") # 移动到第一个点并按下左键 start_x, start_y = pattern_points[pattern[0]] pyautogui.moveTo(start_x, start_y, duration=0.2) pyautogui.mouseDown() # 依次拖动到后续点 for point in pattern[1:]: x, y = pattern_points[point] pyautogui.moveTo(x, y, duration=0.15) # 松开左键完成绘制 pyautogui.mouseUp() print("图案绘制完成") # 启动键盘监听器 with Listener(on_press=on_press) as listener: listener.join()
四、关键注意事项
- 坐标校准:运行
pyautogui.displayMousePosition(),将鼠标移到图案锁的每个点上,记录对应的(x,y)坐标替换pattern_points中的值; - 投屏适配:如果使用手机投屏工具,需将坐标调整为投屏窗口内的相对位置,可结合
pygetwindow获取窗口偏移量进行自动校准; - 速度调整:修改
duration参数控制鼠标移动速度,尽量贴近真实手势的速度,避免被系统判定为异常操作。
五、替代方案
如果需要更精准的设备端模拟,且能临时开启USB调试,可结合adb shell input swipe命令实现,但需提前获取图案点的屏幕绝对坐标:
# 示例:从(100,200)拖动到(300,400) adb shell input swipe 100 200 300 400 150
内容的提问来源于stack exchange,提问作者Hari
相关产品推荐
相关产品推荐

