Beispiel #1
0
def _read_config_file(cf, _globals=globals(), _locals=locals(), interactive=True):
    """Read a config file: execute a python file while loading scapy, that may contain
    some pre-configured values.
    
    If _globals or _locals are specified, they will be updated with the loaded vars.
    This allows an external program to use the function. Otherwise, vars are only available
    from inside the scapy console.
    
    params:
    - _globals: the globals() vars
    - _locals: the locals() vars
    - interactive: specified whether or not errors should be printed using the scapy console or
    raised.

    ex, content of a config.py file:
        'conf.verb = 42\n'
    Manual loading:
        >>> _read_config_file("./config.py"))
        >>> conf.verb
        42
    """
    log_loading.debug("Loading config file [%s]", cf)
    try:
        exec(compile(open(cf).read(), cf, 'exec'), _globals, _locals)
    except IOError as e:
        if interactive:
            raise
        log_loading.warning("Cannot read config file [%s] [%s]", cf, e)
    except Exception as e:
        if interactive:
            raise
        log_loading.exception("Error during evaluation of config file [%s]", cf)
Beispiel #2
0
Datei: main.py Projekt: ii0/scapy
def _read_config_file(cf, _globals=globals(), _locals=locals(), interactive=True):
    """Read a config file: execute a python file while loading scapy, that may contain
    some pre-configured values.

    If _globals or _locals are specified, they will be updated with the loaded vars.
    This allows an external program to use the function. Otherwise, vars are only available
    from inside the scapy console.

    params:
    - _globals: the globals() vars
    - _locals: the locals() vars
    - interactive: specified whether or not errors should be printed using the scapy console or
    raised.

    ex, content of a config.py file:
        'conf.verb = 42\n'
    Manual loading:
        >>> _read_config_file("./config.py"))
        >>> conf.verb
        42
    """
    log_loading.debug("Loading config file [%s]", cf)
    try:
        exec(compile(open(cf).read(), cf, 'exec'), _globals, _locals)
    except IOError as e:
        if interactive:
            raise
        log_loading.warning("Cannot read config file [%s] [%s]", cf, e)
    except Exception as e:
        if interactive:
            raise
        log_loading.exception("Error during evaluation of config file [%s]", cf)
Beispiel #3
0
 def restart_npf(self):
     """ Restart npf incase the wireless network adapter gone"""
     log_loading.debug("Restart npf..")
     subprocess.Popen(['net','stop','npf'],
                     stdin=subprocess.PIPE,
                     stdout=subprocess.PIPE).communicate()
     subprocess.Popen(['net','start','npf'],
                     stdin=subprocess.PIPE,
                     stdout=subprocess.PIPE).communicate()
    def _update_pcapdata(self):
        """Supplement more info from pypcap and the Windows registry"""

        # XXX: We try eth0 - eth29 by bruteforce and match by IP address,
        # because only the IP is available in both pypcap and dnet.
        # This may not work with unorthodox network configurations and is
        # slow because we have to walk through the Windows registry.
        for n in range(30):
            guess = "eth%s" % n
            win_name = pcapdnet.pcap.ex_name(guess)
            if win_name.endswith("}"):
                try:
                    uuid = win_name[win_name.index("{"):win_name.index("}") +
                                    1]
                    keyname = r"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\%s" % uuid
                    try:
                        key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
                                              keyname)
                    except WindowsError:
                        log_loading.debug(
                            "Couldn't open 'HKEY_LOCAL_MACHINE\\%s' (for guessed pcap iface name '%s')."
                            % (keyname, guess))
                        continue
                    try:
                        fixed_ip = _winreg.QueryValueEx(
                            key, "IPAddress")[0][0].encode("utf-8")
                    except (WindowsError, UnicodeDecodeError, IndexError):
                        fixed_ip = None
                    try:
                        dhcp_ip = _winreg.QueryValueEx(
                            key, "DhcpIPAddress")[0].encode("utf-8")
                    except (WindowsError, UnicodeDecodeError, IndexError):
                        dhcp_ip = None
                    # "0.0.0.0" or None means the value is not set (at least not correctly).
                    # If both fixed_ip and dhcp_ip are set, fixed_ip takes precedence
                    if fixed_ip is not None and fixed_ip != "0.0.0.0":
                        ip = fixed_ip
                    elif dhcp_ip is not None and dhcp_ip != "0.0.0.0":
                        ip = dhcp_ip
                    else:
                        continue
                except IOError:
                    continue
                else:
                    if ip == self.ip:
                        self.pcap_name = guess
                        self.win_name = win_name
                        self.uuid = uuid
                        break
        else:
            raise PcapNameNotFoundError
Beispiel #5
0
    def _update_pcapdata(self):
        """Supplement more info from pypcap and the Windows registry"""

        # XXX: We try eth0 - eth29 by bruteforce and match by IP address,
        # because only the IP is available in both pypcap and dnet.
        # This may not work with unorthodox network configurations and is
        # slow because we have to walk through the Windows registry.
        for n in range(30):
            guess = "eth%s" % n
            win_name = pcapdnet.pcap.ex_name(guess)
            if win_name.endswith("}"):
                try:
                    uuid = win_name[win_name.index("{") : win_name.index("}") + 1]
                    keyname = r"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\%s" % uuid
                    try:
                        key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, keyname)
                    except WindowsError:
                        log_loading.debug(
                            "Couldn't open 'HKEY_LOCAL_MACHINE\\%s' (for guessed pcap iface name '%s')."
                            % (keyname, guess)
                        )
                        continue
                    try:
                        fixed_ip = _winreg.QueryValueEx(key, "IPAddress")[0][0].encode("utf-8")
                    except (WindowsError, UnicodeDecodeError, IndexError):
                        fixed_ip = None
                    try:
                        dhcp_ip = _winreg.QueryValueEx(key, "DhcpIPAddress")[0].encode("utf-8")
                    except (WindowsError, UnicodeDecodeError, IndexError):
                        dhcp_ip = None
                    # "0.0.0.0" or None means the value is not set (at least not correctly).
                    # If both fixed_ip and dhcp_ip are set, fixed_ip takes precedence
                    if fixed_ip is not None and fixed_ip != "0.0.0.0":
                        ip = fixed_ip
                    elif dhcp_ip is not None and dhcp_ip != "0.0.0.0":
                        ip = dhcp_ip
                    else:
                        continue
                except IOError:
                    continue
                else:
                    if ip == self.ip:
                        self.pcap_name = guess
                        self.win_name = win_name
                        self.uuid = uuid
                        break
        else:
            raise PcapNameNotFoundError
Beispiel #6
0
from scapy.error import log_loading
import logging
log = logging.getLogger("scapy.loading")

#log_loading.info("Please, report issues to https://github.com/phaethon/scapy")


def _import_star(m):
    #mod = __import__("." + m, globals(), locals())
    mod = importlib.import_module("scapy.layers." + m)
    for k, v in mod.__dict__.items():
        globals()[k] = v


for _l in ['l2', 'inet', 'inet6']:
    log_loading.debug("Loading layer %s" % _l)
    #print "load  ",_l
    _import_star(_l)

#def _import_star(m):
#mod = __import__("." + m, globals(), locals())
#    mod = importlib.import_module("scapy.layers." + m)
#    for k,v in mod.__dict__.items():
#        globals()[k] = v

#for _l in conf.load_layers:
#    log_loading.debug("Loading layer %s" % _l)
#    try:
#        _import_star(_l)
#    except Exception as e:
#        log.warning("can't import layer %s: %s" % (_l,e))
Beispiel #7
0
# This file is part of Scapy
# See http://www.secdev.org/projects/scapy for more information
# Copyright (C) Philippe Biondi <*****@*****.**>
# This program is published under a GPLv2 license

"""
All layers. Configurable with conf.load_layers.
"""

from __future__ import absolute_import
from scapy.config import conf
from scapy.error import log_loading
from scapy.main import load_layer
import logging
import scapy.modules.six as six

ignored = list(six.moves.builtins.__dict__) + ["sys"]
log = logging.getLogger("scapy.loading")

__all__ = []

for _l in conf.load_layers:
    log_loading.debug("Loading layer %s" % _l)
    try:
        load_layer(_l, globals_dict=globals(), symb_list=__all__)
    except Exception as e:
        log.warning("can't import layer %s: %s", _l, e)

del _l