-
Notifications
You must be signed in to change notification settings - Fork 192
/
wifi_password.cpp
72 lines (54 loc) · 2.07 KB
/
wifi_password.cpp
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
#include <iostream>
#include <vector>
#include <sstream>
#include <regex>
struct WifiProfile {
std::string ssid;
std::string password;
};
std::vector<WifiProfile> getWifiProfiles() {
std::vector<WifiProfile> wifiList;
std::string command = "netsh wlan show profiles";
std::array<char, 128> buffer;
std::string result;
FILE* pipe = _popen(command.c_str(), "r");
if (pipe) {
while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) {
result += buffer.data();
}
_pclose(pipe);
}
std::regex profileRegex(R"( All User Profile : (.*)\r\n)");
std::smatch match;
auto profileBegin = std::sregex_iterator(result.begin(), result.end(), profileRegex);
auto profileEnd = std::sregex_iterator();
for (std::sregex_iterator i = profileBegin; i != profileEnd; ++i) {
std::string profileName = (*i)[1].str();
std::string profileCommand = "netsh wlan show profile \"" + profileName + "\" key=clear";
std::string profileResult;
FILE* profilePipe = _popen(profileCommand.c_str(), "r");
if (profilePipe) {
while (fgets(buffer.data(), buffer.size(), profilePipe) != nullptr) {
profileResult += buffer.data();
}
_pclose(profilePipe);
}
std::regex passwordRegex(R"(Key Content : (.*)\r\n)");
std::smatch passwordMatch;
if (!std::regex_search(profileResult, passwordMatch, passwordRegex))
continue;
std::string password = passwordMatch[1].str();
WifiProfile wifiProfile;
wifiProfile.ssid = profileName;
wifiProfile.password = password;
wifiList.push_back(wifiProfile);
}
return wifiList;
}
int main() {
std::vector<WifiProfile> wifiList = getWifiProfiles();
for (const auto& wifiProfile : wifiList) {
std::cout << "SSID: " << wifiProfile.ssid << ", Password: " << wifiProfile.password << std::endl;
}
return 0;
}