Repository: secdev/scapy

Issue:
Improper condition for scapy/arch/indows/_init__.py/NetworkInterfaceDict.load_from_powershell()
Scapy Version: v2.3.3-1008
System: Windows10
Python Version: 2.7.14

File: scapy/arch/indows/\_\_init\_\_.py  Line:647

There is:
    if _detect[0] and not _detect[2] and (not hasattr(self, "restarted_adapter") or self.restarted_adapter):
I suppose that should be:
    if _detect[0] and not _detect[2] and (not hasattr(self, "restarted_adapter") or not self.restarted_adapter):

Thanks,
Adam Karpierz



Discussion:
Indeed ! Thanks a lot for reporting

Resolving patch:
diff --git a/scapy/arch/windows/__init__.py b/scapy/arch/windows/__init__.py
--- a/scapy/arch/windows/__init__.py
+++ b/scapy/arch/windows/__init__.py
@@ -417,27 +417,32 @@ def update(self, data):
         # Other attributes are optional
         self._update_pcapdata()
 
+        try:
+            # Npcap loopback interface
+            if self.name == scapy.consts.LOOPBACK_NAME and conf.use_npcap:
+                # https://nmap.org/npcap/guide/npcap-devguide.html
+                self.mac = "00:00:00:00:00:00"
+                self.ip = "127.0.0.1"
+                conf.cache_ipaddrs[self.pcap_name] = socket.inet_aton(self.ip)
+                return
+            else:
+                self.mac = data['mac']
+        except KeyError:
+            pass
+
         try:
             self.ip = socket.inet_ntoa(get_if_raw_addr(self))
         except (TypeError, NameError):
             pass
 
         try:
+            # Windows native loopback interface
             if not self.ip and self.name == scapy.consts.LOOPBACK_NAME:
                 self.ip = "127.0.0.1"
                 conf.cache_ipaddrs[self.pcap_name] = socket.inet_aton(self.ip)
         except (KeyError, AttributeError, NameError) as e:
             print(e)
 
-        try:
-            if self.name == scapy.consts.LOOPBACK_NAME and conf.use_npcap:
-                # https://nmap.org/npcap/guide/npcap-devguide.html
-                self.mac = "00:00:00:00:00:00"
-            else:
-                self.mac = data['mac']
-        except KeyError:
-            pass
-
     def _update_pcapdata(self):
         if self.is_invalid():
             return
@@ -644,7 +649,7 @@ def _ask_user():
                         return False
                 return False
             _error_msg = "No match between your pcap and windows network interfaces found. "
-            if _detect[0] and not _detect[2] and (not hasattr(self, "restarted_adapter") or self.restarted_adapter):
+            if _detect[0] and not _detect[2] and not (hasattr(self, "restarted_adapter") and self.restarted_adapter):
                 warning("Scapy has detected that your pcap service is not running !")
                 if not conf.interactive or _ask_user():
                     succeed = pcap_service_start(askadmin=conf.interactive)
@@ -705,6 +710,7 @@ def remove_invalid_ifaces(self):
 
     def reload(self):
         """Reload interface list"""
+        self.restarted_adapter = False
         self.data.clear()
         self.load_from_powershell()
 


Test patch:
diff --git a/test/mock_windows.uts b/test/mock_windows.uts
--- a/test/mock_windows.uts
+++ b/test/mock_windows.uts
@@ -264,4 +264,18 @@ assert pcap_service_status()[2] == False
 = test pcap_service_start
 
 pcap_service_start()
-assert pcap_service_status()[2] == True
\ No newline at end of file
+assert pcap_service_status()[2] == True
+
+= Test auto-pcap start UI
+
+old_ifaces = IFACES.data
+
+@mock.patch("scapy.arch.windows.get_if_list")
+def _test_autostart_ui(mocked_getiflist):
+    mocked_getiflist.side_effect = lambda: []
+    IFACES.reload()
+    assert IFACES.data == {}
+
+_test_autostart_ui()
+
+IFACES.data = old_ifaces