class NetworkManager(object): def __init__( self, networkmanager_config_path='/etc/NetworkManager/NetworkManager.conf', unmanaged_interfaces=[]): self.interfaces = pyw.interfaces() self.netcards = { interface: NetworkCard(interface) for interface in pyw.winterfaces() } self.nm_config_file = networkmanager_config_path self.file_handler = None self.unmanaged_interfaces_setup(unmanaged_interfaces) def unmanaged_check(self, interface): ok_status = ["unmanaged", "unavailable"] for line in check_output(["nmcli", "dev"]).split("\n"): try: args = line.split()[:4] iface, type, status, connection = args if interface == iface: return status in ok_status else: continue except: pass return True def unmanaged_interfaces_setup(self, unmanaged_interfaces): for iface in unmanaged_interfaces: if iface in self.interfaces: self.set_mac_and_unmanage(iface, self.netcards[iface].get_mac(), True) def iptables_redirect(self, from_if, to_if): card = self.get_netcard(from_if) # Get NetCard object if card is not None: NetUtils().accept_forwarding(from_if) if not card.is_virtual(): NetUtils().set_postrouting_interface(to_if) NetUtils().add_routing_rule(card.get_subnet(), card.get_mask(), card.get_ip()) def configure_interface(self, interface, ip, netmask=None, broadcast=None, mtu=1800): NetUtils().interface_config(interface, ip, netmask, broadcast) NetUtils().set_interface_mtu(interface, mtu) def set_mac_and_unmanage(self, interface, mac, retry=False, virtInterfaces=0): card = self.get_netcard(interface) # Runs at least once, if retry is flagged # it will try to reset the interface and repeat the process while (True): if card is not None: if not card.set_mac(mac): return False if not self.unmanaged_check(interface) or virtInterfaces > 0: if not self.network_manager_ignore(interface, mac, virtInterfaces): return False os.system("service network-manager restart" ) # Restarting NetworkManager service if pyw.macget(card.card) == mac: return True if not retry: break print "[-] Unable to set mac and unmanage, resetting interface and retrying." retry = False try: card = NetworkCard(interface) if card.get_mode() != 'managed': card.set_mode('managed') except: return False return False # NetworkManager is usually a conflicting process, # but we can configure it to ignore the interface # we use as access point or to sniff packets def network_manager_ignore(self, interface, mac_address, virtInterfaces=0): if virtInterfaces > 0: mac_address = mac_address[:-1] + "0" interface_ignore_string = interface for i in range(virtInterfaces): interface_ignore_string += ",mac:{}".format(mac_address[:-1] + str(i + 1)) interface_ignore_string += ",interface-name:{}_{}".format( interface, i) try: ignore_config = dedent(""" [main] plugins=ifupdown,keyfile [ifupdown] managed=false [keyfile] unmanaged-devices=mac:{mac_address},interface-name:{ignore_interfaces} """.format( mac_address=mac_address, ignore_interfaces=interface_ignore_string)) self.cleanup_filehandler() self.file_handler = FileHandler(self.nm_config_file) self.file_handler.write(ignore_config) self.netcards[interface].set_managed(True) except Exception as e: print e return False return True def get_netcard(self, interface): netcard = None try: try: netcard = self.netcards[interface] except KeyError: # Check if it was plugged in at runtime self.netcards = { interface: NetworkCard(interface) for interface in pyw.winterfaces() } netcard = self.netcards[interface] except KeyError: print "[-] Interface: '{}' does not exist".format(interface) return None return netcard def cleanup_filehandler(self): if self.file_handler: self.file_handler.restore_file() self.file_handler = None os.system("service network-manager restart") def reset_interfaces(self): restart_services = False for card in [ card for card in self.netcards if not self.netcards[card].is_virtual ]: self.netcards[card].set_mac(self.netcards[card].original_mac) self.netcards[card].set_mode('managed') if self.netcards[card].is_managed(): restart_services = True if restart_services: os.system("service networking restart") os.system("service network-manager restart") def cleanup(self): NetUtils().flush_iptables() self.cleanup_filehandler() self.reset_interfaces()
class NetworkManager(object): def __init__( self, networkmanager_config_path='/etc/NetworkManager/NetworkManager.conf' ): self.interfaces = pyw.interfaces() self.netcards = { interface: NetworkCard(interface) for interface in pyw.winterfaces() } self.nm_config_file = networkmanager_config_path self.file_handler = None def iptables_redirect(self, from_if, to_if): card = self.get_netcard(from_if) # Get NetCard object if card != None: NetUtils().flush_iptables() NetUtils().accept_forwarding(from_if) NetUtils().set_postrouting_interface(to_if) NetUtils().add_routing_rule(card.get_subnet(), card.get_mask(), card.get_ip()) def configure_interface(self, interface, ip, netmask=None, broadcast=None, mtu=1800): card = self.get_netcard(interface) # Get NetCard object if card != None: card.ifconfig(ip, netmask, broadcast) card.set_mtu_size(mtu) def set_mac_and_unmanage(self, interface, mac, retry=False): card = self.get_netcard(interface) # Runs at least once, if retry is flagged # it will try to reset the interface and repeat the process while (1): if card != None: if not card.set_mac(mac): return False if not self.network_manager_ignore(interface, mac): return False os.system("service NetworkManager restart" ) # Restarting NetworkManager service if pyw.macget(card.card) == mac: return True if not retry: break print "[-] Unable to set mac and unmanage, resetting interface and retrying." retry = False card = NetworkCard(interface) if card.get_mode() != 'managed': card.set_mode('managed') return False # NetworkManager is usually a conflicting process, # but we can configure it to ignore the interface # we use as access point or to sniff packets def network_manager_ignore(self, interface, mac_address): try: ignore_config = dedent(""" [main] plugins=ifupdown,keyfile [ifupdown] managed=false [keyfile] unmanaged-devices=mac:{mac_address},interface-name:{interface} """.format(mac_address=mac_address, interface=interface)) self.cleanup_filehandler() self.file_handler = FileHandler(self.nm_config_file) self.file_handler.write(ignore_config) except Exception as e: print e return False return True def get_netcard(self, interface): netcard = None try: try: netcard = self.netcards[interface] except KeyError: # Check if it was plugged in at runtime self.netcards = { interface: NetworkCard(interface) for interface in pyw.winterfaces() } netcard = self.netcards[interface] except KeyError: print "[-] Interface: '{}' does not exist".format(interface) return None return netcard def cleanup_filehandler(self): if self.file_handler: self.file_handler.restore_file() self.file_handler = None def reset_interfaces(self): for card in self.netcards: self.netcards[card].set_mac(self.netcards[card].original_mac) self.netcards[card].set_mode('managed') def cleanup(self): NetUtils().flush_iptables() self.cleanup_filehandler() self.reset_interfaces()
class NetworkManager(object): def __init__(self, networkmanager_config_path='/etc/NetworkManager/NetworkManager.conf', unmanaged_interfaces = []): self.interfaces = pyw.interfaces() self.netcards = { interface: NetworkCard(interface) for interface in pyw.winterfaces() } self.nm_config_file = networkmanager_config_path self.file_handler = None self.unmanaged_interfaces_setup(unmanaged_interfaces) def unmanaged_check(self, interface): ok_status = ["unmanaged", "unavailable"] for line in check_output(["nmcli", "dev"]).split("\n"): try: args = line.split()[:4] iface, type, status, connection = args if interface == iface: return status in ok_status else: continue except: pass return True def unmanaged_interfaces_setup(self, unmanaged_interfaces): for iface in unmanaged_interfaces: if iface in self.interfaces: self.set_mac_and_unmanage(iface, self.netcards[iface].get_mac(), True) def iptables_redirect(self, from_if, to_if): card = self.get_netcard(from_if) # Get NetCard object if card is not None: NetUtils().accept_forwarding(from_if) if not card.is_virtual(): NetUtils().set_postrouting_interface(to_if) NetUtils().add_routing_rule(card.get_subnet(), card.get_mask(), card.get_ip()) def configure_interface(self, interface, ip, netmask=None, broadcast=None, mtu=1800): NetUtils().interface_config(interface, ip, netmask, broadcast) NetUtils().set_interface_mtu(interface, mtu) def set_mac_and_unmanage(self, interface, mac, retry = False, virtInterfaces = 0): card = self.get_netcard(interface) # Runs at least once, if retry is flagged # it will try to reset the interface and repeat the process while(True): if card is not None: if not card.set_mac(mac): return False if not self.unmanaged_check(interface) or virtInterfaces > 0: if not self.network_manager_ignore(interface, mac, virtInterfaces): return False os.system("service network-manager restart") # Restarting NetworkManager service if pyw.macget(card.card) == mac: return True if not retry: break print "[-] Unable to set mac and unmanage, resetting interface and retrying." retry = False try: card = NetworkCard(interface) if card.get_mode() != 'managed': card.set_mode('managed') except: return False return False # NetworkManager is usually a conflicting process, # but we can configure it to ignore the interface # we use as access point or to sniff packets def network_manager_ignore(self, interface, mac_address, virtInterfaces = 0): if virtInterfaces > 0: mac_address = mac_address[:-1] + "0" interface_ignore_string = interface for i in range(virtInterfaces): interface_ignore_string += ",mac:{}".format(mac_address[:-1] + str(i + 1)) interface_ignore_string += ",interface-name:{}_{}".format(interface, i) try: ignore_config = dedent( """ [main] plugins=ifupdown,keyfile [ifupdown] managed=false [keyfile] unmanaged-devices=mac:{mac_address},interface-name:{ignore_interfaces} """.format( mac_address=mac_address, ignore_interfaces=interface_ignore_string )) self.cleanup_filehandler() self.file_handler = FileHandler(self.nm_config_file) self.file_handler.write(ignore_config) self.netcards[interface].set_managed(True) except Exception as e: print e return False return True def get_netcard(self, interface): netcard = None try: try: netcard = self.netcards[interface] except KeyError: # Check if it was plugged in at runtime self.netcards = { interface: NetworkCard(interface) for interface in pyw.winterfaces() } netcard = self.netcards[interface] except KeyError: print "[-] Interface: '{}' does not exist".format(interface) return None return netcard def cleanup_filehandler(self): if self.file_handler: self.file_handler.restore_file() self.file_handler = None os.system("service network-manager restart") def reset_interfaces(self): restart_services = False for card in [card for card in self.netcards if not self.netcards[card].is_virtual]: self.netcards[card].set_mac(self.netcards[card].original_mac) self.netcards[card].set_mode('managed') if self.netcards[card].is_managed(): restart_services = True if restart_services: os.system("service networking restart") os.system("service network-manager restart") def cleanup(self): NetUtils().flush_iptables() self.cleanup_filehandler() self.reset_interfaces()