b3570de4b0
Almost complete converting script into module. Do not attempt to run as module yet...
99 lines
4.4 KiB
Python
99 lines
4.4 KiB
Python
# Copyright (c) 2024 Anoduck
|
|
#
|
|
# This software is released under the MIT License.
|
|
# https://opensource.org/licenses/MIT
|
|
|
|
import os
|
|
import sys
|
|
from scapy.config import Conf as scapyconfig
|
|
from faker import Faker
|
|
|
|
fake = Faker()
|
|
|
|
|
|
# -------------------------------------------------------------------
|
|
# ███╗ ██╗███████╗████████╗██████╗ ███████╗██╗ ██╗
|
|
# ████╗ ██║██╔════╝╚══██╔══╝██╔══██╗██╔════╝██║ ██║
|
|
# ██╔██╗ ██║█████╗ ██║ ██║ ██║█████╗ ██║ ██║
|
|
# ██║╚██╗██║██╔══╝ ██║ ██║ ██║██╔══╝ ╚██╗ ██╔╝
|
|
# ██║ ╚████║███████╗ ██║ ██████╔╝███████╗ ╚████╔╝
|
|
# ╚═╝ ╚═══╝╚══════╝ ╚═╝ ╚═════╝ ╚══════╝ ╚═══╝
|
|
# ----------------------------------------------------------------
|
|
class NetDev(object):
|
|
|
|
def create_if(self) -> bool:
|
|
try:
|
|
os.system(f'ip link set {self.interface} up')
|
|
os.system(
|
|
f'iw dev {self.interface} interface add {self.mon_crtd} type monitor')
|
|
self.log.debug('Created {0}'.format(self.mon_crtd))
|
|
os.system(f'ip link set {self.mon_crtd} down')
|
|
os.system(f'ip link set {self.mon_crtd} address {self.macaddr}')
|
|
self.log.debug('Set device address to {0}'.format(self.macaddr))
|
|
os.system(f'ip link set {self.mon_crtd} up')
|
|
self.log.debug('Set device up')
|
|
os.system('iw set reg US')
|
|
self.log.debug('Set device registry to US')
|
|
self.log.info('Device is fully configured and up')
|
|
return True
|
|
except os.error as e:
|
|
self.log.debug('Failed to create {0}'.format(self.interface), e)
|
|
sys.exit(1)
|
|
|
|
def switch_if(self) -> bool:
|
|
try:
|
|
os.system(f'ip link set {self.interface} down')
|
|
self.log.debug('Set device down')
|
|
os.system(f'ip link set {self.interface} address {self.macaddr}')
|
|
self.log.debug('Set device address to {0}'.format(self.macaddr))
|
|
# (below) setting registry is known to cause issues.
|
|
# os.system('iw set reg US')
|
|
# self.log.debug('Set device registry to US')
|
|
os.system(f'iw dev {self.interface} set type monitor')
|
|
self.log.debug('{0} switched to monitor'.format(self.interface))
|
|
os.system(f'ip link set {self.interface} up')
|
|
scapyconfig.iface = self.interface
|
|
self.log.info('Set scapy config self.name to: {0}'.format(
|
|
self.interface))
|
|
self.log.info('Device is fully configured and up')
|
|
return True
|
|
except os.error as e:
|
|
self.log.debug('Failed to switch ', self.interface, ' type', e)
|
|
print('Failed to change ', self.interface, ' mode', e)
|
|
sys.exit(1)
|
|
|
|
def start_monitor(self, interface, mon_type, log) -> tuple:
|
|
"""
|
|
Starts a monitor self.name based on the given arguments.
|
|
|
|
Args:
|
|
interface (str): The name of the interface
|
|
to create the monitor interface from.
|
|
mon_type (str): The type of monitor interface
|
|
to create or switch to.
|
|
Possible values are "create" or "switch".
|
|
|
|
Returns:
|
|
str: The name of the created or switched monitor interface.
|
|
|
|
"""
|
|
self.interface = interface
|
|
self.mon_type = mon_type
|
|
self.log = log
|
|
self.macaddr = fake.mac_address()
|
|
self.mon_crtd = f'{self.interface}mon'
|
|
log.debug('mac_address: {0}'.format(self.macaddr))
|
|
log.debug('Monitor Type: {0}'.format(self.mon_type))
|
|
log.info('Starting monitor interface')
|
|
if self.mon_type == 'create':
|
|
self.create_if()
|
|
mon_if = self.mon_crtd
|
|
return mon_if, self.macaddr
|
|
elif self.mon_type == 'switch':
|
|
self.switch_if()
|
|
mon_if = self.interface
|
|
return mon_if, self.macaddr
|
|
else:
|
|
Exception('Invalid monitor type')
|
|
log.debug('Invalid monitor type')
|
|
sys.exit(1)
|