Enable or disable monitor mode on a WiFi interface.
(self, params: dict)
| 874 | # ========================================================================= |
| 875 | |
| 876 | def toggle_monitor_mode(self, params: dict) -> dict: |
| 877 | """Enable or disable monitor mode on a WiFi interface.""" |
| 878 | import re |
| 879 | |
| 880 | action = params.get('action', 'start') |
| 881 | interface = params.get('interface', '') |
| 882 | kill_processes = params.get('kill_processes', False) |
| 883 | |
| 884 | # Validate interface name (alphanumeric, underscore, dash only) |
| 885 | if not interface or not re.match(r'^[a-zA-Z][a-zA-Z0-9_-]*$', interface): |
| 886 | return {'status': 'error', 'message': 'Invalid interface name'} |
| 887 | |
| 888 | airmon_path = self._get_tool_path('airmon-ng') |
| 889 | iw_path = self._get_tool_path('iw') |
| 890 | |
| 891 | if action == 'start': |
| 892 | if airmon_path: |
| 893 | try: |
| 894 | # Get interfaces before |
| 895 | def get_wireless_interfaces(): |
| 896 | interfaces = set() |
| 897 | try: |
| 898 | for iface in os.listdir('/sys/class/net'): |
| 899 | if os.path.exists(f'/sys/class/net/{iface}/wireless') or 'mon' in iface: |
| 900 | interfaces.add(iface) |
| 901 | except OSError: |
| 902 | pass |
| 903 | return interfaces |
| 904 | |
| 905 | interfaces_before = get_wireless_interfaces() |
| 906 | |
| 907 | # Kill interfering processes if requested |
| 908 | if kill_processes: |
| 909 | subprocess.run([airmon_path, 'check', 'kill'], |
| 910 | capture_output=True, timeout=10) |
| 911 | |
| 912 | # Start monitor mode |
| 913 | result = subprocess.run([airmon_path, 'start', interface], |
| 914 | capture_output=True, text=True, timeout=15) |
| 915 | output = result.stdout + result.stderr |
| 916 | |
| 917 | time.sleep(1) |
| 918 | interfaces_after = get_wireless_interfaces() |
| 919 | |
| 920 | # Find the new monitor interface |
| 921 | new_interfaces = interfaces_after - interfaces_before |
| 922 | monitor_iface = None |
| 923 | |
| 924 | if new_interfaces: |
| 925 | for iface in new_interfaces: |
| 926 | if 'mon' in iface: |
| 927 | monitor_iface = iface |
| 928 | break |
| 929 | if not monitor_iface: |
| 930 | monitor_iface = list(new_interfaces)[0] |
| 931 | |
| 932 | # Try to parse from airmon-ng output |
| 933 | if not monitor_iface: |
no test coverage detected