diff --git a/README.md b/README.md index 7703380..1d17d8b 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ To-Do - [X] Add method to check and return only the connected SSID name and Password - [X] Use nmcli to retrieve passwords on linux rather than reading files (may not require sudo) - [X] Multithreading support for windows to imporove execution speed +- [ ] Add support for WPA-Enteprise About ----- diff --git a/changelog.txt b/changelog.txt index 91463e0..55344ee 100644 --- a/changelog.txt +++ b/changelog.txt @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 0.3.5b - 28-03-2021 +### Added + +### Changed +- Fix for detecting current network if NetworkManager not installed +- Other bugfixes. ## 0.3.4b - 26-03-2021 diff --git a/wifipasswords/__init__.py b/wifipasswords/__init__.py index 583427e..1663eee 100644 --- a/wifipasswords/__init__.py +++ b/wifipasswords/__init__.py @@ -25,7 +25,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see < https: // www.gnu.org/licenses/>. -__version__ = "0.3.4-beta" +__version__ = "0.3.5-beta" __licence__ = "GPLv3" # GNU General Public Licence v3 import platform diff --git a/wifipasswords/__main__.py b/wifipasswords/__main__.py index 3e51fd8..039d425 100644 --- a/wifipasswords/__main__.py +++ b/wifipasswords/__main__.py @@ -146,7 +146,7 @@ def cli(): if args['json'] == None: args['json'] = args['all'] print() - pw.save_json(os.path.join(args['json'], 'networks_data.json')) + pw.save_json(os.path.join(args['json'], 'networks_data.json'),data) print("JSON saved >> {}".format( os.path.join(args['json'], 'networks_data.json'))) print() diff --git a/wifipasswords/wifipasswords_linux.py b/wifipasswords/wifipasswords_linux.py index 4e5bab7..d15a448 100644 --- a/wifipasswords/wifipasswords_linux.py +++ b/wifipasswords/wifipasswords_linux.py @@ -91,18 +91,14 @@ def get_passwords(self) -> dict: pool.close() pool.join() - self.number_of_profiles = len(results) - self.data = results - ## check wpa_supplicant file, but only if the file exists and no networks were found from networkmanager # if network manager is being used there shouldn't be an active wpa_supplicant file elif os.path.isfile(self.wpa_supplicant_file_path): results = {} file_string = self._command_runner(['sudo','cat',self.wpa_supplicant_file_path]) - network_blocks = re.findall('(?<=network=)[^}]*(?=})', file_string) + network_blocks = re.findall('(?<=network={)[^}]*(?=})', file_string) for network_block in network_blocks: - block_stripped = network_block.strip().replace( - '\t', '').replace('\n', ' ').split(' ') + block_stripped = network_block.strip().replace('\t', '').split('\n') ssid = ' ' auth = ' ' psk = ' ' @@ -125,6 +121,8 @@ def get_passwords(self) -> dict: else: results = {} + self.number_of_profiles = len(results) + self.data = results return results @@ -191,35 +189,43 @@ def get_visible_networks(self, as_dictionary=False) -> str: def get_dns_config(self, as_dictionary=False) -> str: dns_dict = {} - interfaces = self._command_runner(['nmcli','-t','-f','DEVICE,CONNECTION','dev']).split('\n') - for interface in interfaces: - # try: - if len(interface.split(':')) == 2: - suffix = '' - type = 'None' - DNS = [] - interface_data = self._command_runner(['nmcli','-t','-f','IP4.DNS,IP4.DOMAIN','device','show',interface.split(':')[0]]).split('\n') - profile_data = self._command_runner(['nmcli','-t','-f','ipv4.dns,ipv4.ignore-auto-dns','c','s',interface.split(':')[1]]).split('\n') - for row in interface_data: - if 'IP4.DOMAIN' in row: - suffix = row.split(':')[1] - if 'IP4.DNS' in row: - DNS = row.split(':')[1].split(',') - for row in profile_data: - if 'ipv4.ignore-auto-dns' in row: - if row.split(':')[1] == 'yes': - type = 'Static' - elif row.split(':')[1] == 'no' and len(DNS) != 0: - type = 'DHCP' - dns_dict[interface.split(':')[0]] = {'type':type, 'DNS': DNS, 'suffix': suffix} - - if as_dictionary: - return dns_dict + ## uses nmcli - if doesnt exist return error message + if os.path.exists(self.nm_path): + interfaces = self._command_runner(['nmcli','-t','-f','DEVICE,CONNECTION','dev']).split('\n') + for interface in interfaces: + # try: + if len(interface.split(':')) == 2: + suffix = '' + type = 'None' + DNS = [] + interface_data = self._command_runner(['nmcli','-t','-f','IP4.DNS,IP4.DOMAIN','device','show',interface.split(':')[0]]).split('\n') + profile_data = self._command_runner(['nmcli','-t','-f','ipv4.dns,ipv4.ignore-auto-dns','c','s',interface.split(':')[1]]).split('\n') + for row in interface_data: + if 'IP4.DOMAIN' in row: + suffix = row.split(':')[1] + if 'IP4.DNS' in row: + DNS = row.split(':')[1].split(',') + for row in profile_data: + if 'ipv4.ignore-auto-dns' in row: + if row.split(':')[1] == 'yes': + type = 'Static' + elif row.split(':')[1] == 'no' and len(DNS) != 0: + type = 'DHCP' + dns_dict[interface.split(':')[0]] = {'type':type, 'DNS': DNS, 'suffix': suffix} + + if as_dictionary: + return dns_dict + else: + dns_string = '' + for k,v in dns_dict.items(): + dns_string = dns_string + f"Interface: {k} \n type: {v['type']} \n DNS: {v['DNS']} \n domain: {v['suffix']}" + '\n' + '\n' + return dns_string + else: - dns_string = '' - for k,v in dns_dict.items(): - dns_string = dns_string + f"Interface: {k} \n type: {v['type']} \n DNS: {v['DNS']} \n domain: {v['suffix']}" + '\n' + '\n' - return dns_string + if as_dictionary: + return {} + else: + return "Requires NetworkManager" def save_wpa_supplicant(self, path: str, data: dict = None, include_open: bool = True, @@ -289,29 +295,67 @@ def get_number_profiles(self) -> int: def get_currently_connected_ssids(self) -> list: connected_ssids = [] - connected_data = self._command_runner(['nmcli','-t','d']).split('\n') - - for row in connected_data: - try: - if row.split(':')[1] == 'wifi' and row.split(':')[2] == 'connected': - connected_ssids.append(row.split(':')[3]) - except: - pass + + #check if network manager is installed by checking config path, else use iwgetid + if os.path.exists(self.nm_path): + connected_data = self._command_runner(['nmcli', '-t', 'd']).split('\n') + for row in connected_data: + try: + if row.split(':')[1] == 'wifi' and row.split(':')[2] == 'connected': + connected_ssids.append(row.split(':')[3]) + except: + pass + + #if there is no nmcli, use iwgetid -r + else: + connected_data = self._command_runner(['iwgetid', '-r']).split('\n') + for row in connected_data: + try: + if row != '': + connected_ssids.append(row) + except: + pass + return connected_ssids - + def get_currently_connected_passwords(self) -> list: """ Returns a tuple of (ssid, psk) for each currently connected network. """ connected_passwords = [] - for ssid in self.get_currently_connected_ssids(): - psk = '' - key_content = self._command_runner(['nmcli','-t','-f','802-11-wireless-security.psk','c','s',ssid,'--show-secrets']) - if key_content != '': - for row in key_content.split('\n'): - if '802-11-wireless-security.psk' in row: - psk = row.split(':')[1] - connected_passwords.append((ssid,psk)) - + connected_ssids = self.get_currently_connected_ssids() + + if os.path.exists(self.nm_path): + for ssid in connected_ssids: + psk = '' + key_content = self._command_runner( + ['nmcli', '-t', '-f', '802-11-wireless-security.psk', 'c', 's', ssid, '--show-secrets']) + if key_content != '': + for row in key_content.split('\n'): + if '802-11-wireless-security.psk' in row: + psk = row.split(':')[1] + connected_passwords.append((ssid, psk)) + + elif os.path.isfile(self.wpa_supplicant_file_path): + file_string = self._command_runner( + ['sudo', 'cat', self.wpa_supplicant_file_path]) + network_blocks = re.findall('(?<=network=)[^}]*(?=})', file_string) + + #if matching a connected ssid + matched_blocks = [net for net in network_blocks if any( + xs in net for xs in connected_ssids)] + + for network_block in matched_blocks: + block_stripped = network_block.strip().replace( + '\t', '').replace('\n', ' ').split(' ') + ssid = '' + psk = '' + for row in block_stripped: + if 'ssid' in row: + ssid = row.split('ssid=')[1][1:-1] + if 'psk' in row: + psk = row.split('psk=')[1][1:-1] + connected_passwords.append((ssid, psk)) + return connected_passwords