/ ctiger / netdev.py
netdev.py
 1  # Copyright (c) 2024 Anoduck
 2  # 
 3  # This software is released under the MIT License.
 4  # https://opensource.org/licenses/MIT
 5  
 6  import os
 7  import sys
 8  from scapy.config import Conf as scapyconfig
 9  from faker import Faker
10  
11  fake = Faker()
12  
13  
14  # -------------------------------------------------------------------
15  # ███╗   ██╗███████╗████████╗██████╗ ███████╗██╗   ██╗
16  # ████╗  ██║██╔════╝╚══██╔══╝██╔══██╗██╔════╝██║   ██║
17  # ██╔██╗ ██║█████╗     ██║   ██║  ██║█████╗  ██║   ██║
18  # ██║╚██╗██║██╔══╝     ██║   ██║  ██║██╔══╝  ╚██╗ ██╔╝
19  # ██║ ╚████║███████╗   ██║   ██████╔╝███████╗ ╚████╔╝
20  # ╚═╝  ╚═══╝╚══════╝   ╚═╝   ╚═════╝ ╚══════╝  ╚═══╝
21  # ----------------------------------------------------------------
22  class NetDev(object):
23  
24      def create_if(self) -> bool:
25          try:
26              os.system(f'ip link set {self.interface} up')
27              os.system(
28                  f'iw dev {self.interface} interface add {self.mon_crtd} type monitor')
29              self.log.debug('Created {0}'.format(self.mon_crtd))
30              os.system(f'ip link set {self.mon_crtd} down')
31              os.system(f'ip link set {self.mon_crtd} address {self.macaddr}')
32              self.log.debug('Set device address to {0}'.format(self.macaddr))
33              os.system(f'ip link set {self.mon_crtd} up')
34              self.log.debug('Set device up')
35              os.system('iw set reg US')
36              self.log.debug('Set device registry to US')
37              self.log.info('Device is fully configured and up')
38              return True
39          except os.error as e:
40              self.log.debug('Failed to create {0}'.format(self.interface), e)
41              sys.exit(1)
42  
43      def switch_if(self) -> bool:
44          try:
45              os.system(f'ip link set {self.interface} down')
46              self.log.debug('Set device down')
47              os.system(f'ip link set {self.interface} address {self.macaddr}')
48              self.log.debug('Set device address to {0}'.format(self.macaddr))
49              # (below) setting registry is known to cause issues.
50              # os.system('iw set reg US')
51              # self.log.debug('Set device registry to US')
52              os.system(f'iw dev {self.interface} set type monitor')
53              self.log.debug('{0} switched to monitor'.format(self.interface))
54              os.system(f'ip link set {self.interface} up')
55              scapyconfig.iface = self.interface
56              self.log.info('Set scapy config self.name to: {0}'.format(
57                  self.interface))
58              self.log.info('Device is fully configured and up')
59              return True
60          except os.error as e:
61              self.log.debug('Failed to switch ',  self.interface,  ' type', e)
62              print('Failed to change ',  self.interface,  ' mode', e)
63              sys.exit(1)
64  
65      def start_monitor(self, interface, mon_type, log) -> tuple:
66          """
67          Starts a monitor self.name based on the given arguments.
68  
69          Args:
70              interface (str): The name of the interface
71            to create the monitor interface from.
72              mon_type (str): The type of monitor interface
73            to create or switch to.
74                  Possible values are "create" or "switch".
75  
76          Returns:
77                  str: The name of the created or switched monitor interface.
78  
79          """
80          self.interface = interface
81          self.mon_type = mon_type
82          self.log = log
83          self.macaddr = fake.mac_address()
84          self.mon_crtd = f'{self.interface}mon'
85          log.debug('mac_address: {0}'.format(self.macaddr))
86          log.debug('Monitor Type: {0}'.format(self.mon_type))
87          log.info('Starting monitor interface')
88          if self.mon_type == 'create':
89              self.create_if()
90              mon_if = self.mon_crtd
91              return mon_if, self.macaddr
92          elif self.mon_type == 'switch':
93              self.switch_if()
94              mon_if = self.interface
95              return mon_if, self.macaddr
96          else:
97              Exception('Invalid monitor type')
98              log.debug('Invalid monitor type')
99              sys.exit(1)