Ejemplo n.º 1
0
    def __init__(self, macs=None):
        super().__init__()

        self._modules = ["antenna", "gps", "capture", "wifi"]
        self._params = meta.Params()
        if macs:
            self._params.macs = macs
        self._aps = APs()

        # Ensure we have root
        if os.getuid() != 0:
            print(
                "Error: this application needs root to run correctly. Please run as root."
            )
            exit(1)

        # WiFi
        module_logger.info("Initializing WiFi")
        # Set interface to first
        iface = interface.get_first_interface()
        if iface is not None:
            self._params.iface = iface
        else:
            module_logger.error("No valid wireless interface available")
            exit(1)

        # Start the command loop - these need to be the last lines in the initializer
        self._update_prompt()
        self.cmdloop('Welcome to Localizer Shell...')
Ejemplo n.º 2
0
    def setUpClass(cls):
        if localizer.meta.iface is None:
            localizer.meta.iface = interface.get_first_interface()
        if localizer.meta.path is None:
            localizer.meta.path = tempfile.gettempdir()

        # Speed up tests
        localizer.meta.duration = 5
Ejemplo n.º 3
0
    def _build_capture(capture_section, meta_section):
        """
        Use a dictionary from configparser to build a capture object

        :param capture_section: A dictionary of key and values with capture properties
        :type capture_section: dict
        :param meta_section: A dictionary of key and values with default properties
        :type meta_section: dict
        :return: A Params object
        :rtype: Params()
        """

        try:
            if 'iface' in capture_section and capture_section['iface']:
                _iface = capture_section['iface']
            elif 'iface' in meta_section and meta_section['iface']:
                _iface = meta_section['iface']
            else:
                _iface = interface.get_first_interface()
                if not _iface:
                    raise ValueError(
                        "No valid interface provided or available on system")

            if 'duration' in capture_section:
                _duration = capture_section['duration']
            elif 'duration' in meta_section:
                _duration = meta_section['duration']
            else:
                _duration = capture.OPTIMAL_CAPTURE_DURATION

            if 'degrees' in capture_section:
                _degrees = capture_section['degrees']
            elif 'degrees' in meta_section:
                _degrees = meta_section['degrees']
            else:
                raise ValueError("No valid degrees")

            if 'bearing' in capture_section:
                _bearing = capture_section['bearing']
            elif 'bearing' in meta_section:
                _bearing = meta_section['bearing']
            else:
                raise ValueError("No valid bearing")

            if 'hop_int' in capture_section:
                _hop_int = capture_section['hop_int']
            elif 'hop_int' in meta_section:
                _hop_int = meta_section['hop_int']
            else:
                _hop_int = interface.OPTIMAL_BEACON_INT

            if 'hop_dist' in capture_section:
                _hop_dist = capture_section['hop_dist']
            elif 'hop_dist' in meta_section:
                _hop_dist = meta_section['hop_dist']
            else:
                _hop_dist = interface.STD_CHANNEL_DISTANCE

            if 'capture' in capture_section:
                _capture = capture_section['capture']
            elif 'capture' in meta_section:
                _capture = meta_section['capture']
            else:
                raise ValueError("No valid capture name")

            if 'macs' in capture_section:
                _macs = capture_section['macs'].split(',')
            elif 'macs' in meta_section:
                _macs = meta_section['macs'].split(',')
            else:
                _macs = None

            if 'channel' in capture_section:
                _channel = capture_section['channel']
            elif 'channel' in meta_section:
                _channel = meta_section['channel']
            else:
                _channel = None

            if 'focused' in capture_section:
                _focused = tuple(capture_section['focused'].split(','))
            elif 'focused' in meta_section:
                _focused = tuple(meta_section['focused'].split(','))
            else:
                _focused = None

            cap = localizer.meta.Params(_iface, _duration, _degrees, _bearing,
                                        _hop_int, _hop_dist, _macs, _channel,
                                        _focused, _capture)
            # Validate iface
            module_logger.debug("Setting iface {}".format(_iface))
            cap.iface = _iface

            return cap

        except ValueError as e:
            module_logger.warning(e)
            return None
Ejemplo n.º 4
0
    def do_set(self, args):
        """
        Set a named parameter. All parameters require a value except for iface and macs
        - iface without a parameter will set the iface to the first system wireless iface found
        - macs without a parameter will delete the mac address whitelist

        :param args: Parameter name followed by new value
        :type args: str
        """

        split_args = args.split()
        if len(split_args) < 1:
            module_logger.error(
                "You must provide at least one argument".format(args))
        elif len(split_args) == 1:
            if split_args[0] == "iface":
                iface = interface.get_first_interface()

                if iface is not None:
                    self._params.iface = iface
                else:
                    module_logger.error(
                        "There are no wireless interfaces available.")
            elif split_args[0] == 'macs':
                self._params.macs = []
            else:
                module_logger.error("Parameters require a value".format(
                    split_args[0]))
        elif split_args[0] in meta.Params.VALID_PARAMS:
            try:
                param = split_args[0]
                value = split_args[1]
                # Validate certain parameters
                if split_args[0] == "iface":
                    self._params.iface = value
                elif param == "duration":
                    self._params.duration = value
                elif param == "degrees":
                    self._params.degrees = value
                elif param == "bearing":
                    self._params.bearing_magnetic = value
                elif param == "hop_int":
                    self._params.hop_int = value
                elif param == "hop_dist":
                    self._params.hop_dist = value
                elif param == "mac":
                    self._params.add_mac(value)
                elif param == "macs":
                    # Load macs from provided file
                    self._params.add_mac(localizer.load_macs(value))
                elif param == "channel":
                    self._params.channel = value
                elif param == "capture":
                    self._params.capture = value

                print("Parameter '{}' set to '{}'".format(param, value))

            except (ValueError, FileNotFoundError) as e:
                module_logger.error(e)
        else:
            module_logger.error("Invalid parameter '{}'".format(split_args[0]))

        self._update_prompt()
import os
import timeit
from subprocess import call

from localizer.interface import get_first_interface

DN = open(os.devnull, 'w')

num_loops = 500
default_hop_interval = 0.1

iface = get_first_interface()
channels = range(11)
curr_channel = 0


def change_channel():
    global curr_channel
    call(['iwconfig', iface, 'channel',
          str(channels[curr_channel])],
         stdout=DN,
         stderr=DN)
    curr_channel = (curr_channel + 1) % 11


total_time = timeit.timeit(change_channel, number=num_loops)
print("Average time: {} ({:.2f}x shorter than default_hop_interval)".format(
    total_time / num_loops, default_hop_interval / (total_time / num_loops)))
Ejemplo n.º 6
0
# Script can be run standalone to test the antenna
if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser(
        description="Test wifi adapter mode change and channel changing")
    parser.add_argument("duration",
                        help="Number of seconds to run the test",
                        type=int,
                        nargs='?',
                        default=localizer.meta.duration)
    parser.add_argument("iface",
                        help="Interface to test",
                        nargs='?',
                        default=interface.get_first_interface())
    parser.add_argument("interval",
                        help="Number of seconds between channel hops",
                        type=float,
                        nargs='?',
                        default=localizer.meta.hop_int)
    arguments = parser.parse_args()

    try:
        localizer.meta.duration = arguments.duration
        localizer.meta.iface = arguments.iface
        localizer.meta.hop_int = arguments.interval
    except ValueError:
        print("Invalid parameters")
        exit(1)