예제 #1
0
 def __init__(self, filename=NSECONFIG):
     """
     Constructor don't load configuration file itselfs, use load() method after creation.
     """
     self.filename = filename
     self.downloader = DownloaderManager()
     self.reset()
예제 #2
0
 def __init__(self, filename = NSECONFIG):
     """
     Constructor don't load configuration file itselfs, use load() method after creation.
     """
     self.filename = filename
     self.downloader = DownloaderManager()
     self.reset()
예제 #3
0
class ScriptConfig(object):
    """
    Hold NSE Facilitator configuration settings
    """
    def __init__(self, filename=NSECONFIG):
        """
        Constructor don't load configuration file itselfs, use load() method after creation.
        """
        self.filename = filename
        self.downloader = DownloaderManager()
        self.reset()

    def __repr__(self):
        return "<ScriptConfig: %d sources>" % len(self.sources)

    def reset(self):
        """
        Reset configuration to empty state
        """
        self.sources = []
        self.install = []
        self.settings = []
        self.comments = []

    def get_proxies(self):
        """
        Return proxies dictionary
        """
        proxies = dict()
        for s in self.settings:
            s.apply(proxies)
        return proxies

    def set_proxies(self, proxies):
        """
        Set proxies settings
        """
        self.remove_item('HTTPPROXY')
        self.remove_item('FTPPROXY')
        if proxies.has_key('http'):
            self.add_item('HTTPPROXY', proxies['http'])
        if proxies.has_key('ftp'):
            self.add_item('FTPPROXY', proxies['ftp'])

    def download(self, url):
        """
        Download specified url from Web
        """
        proxies = self.get_proxies()
        self.downloader.set_proxies(proxies)
        return self.downloader.download(url)

    def install_script(self, name, data):
        """
        Install script with given name and contest using install items
        """
        if not data:
            return None
        for i in self.install:
            installed_path = i.install(name, data)
            if installed_path:
                return installed_path
        return None

    def set_default(self):
        """
        Reset configurtion to default state: take Nmap fetch directories as source
        """
        self.reset()
        dirs = [d for d in NmapFetchScripts().fetchdirs() if os.path.exists(d)]
        self.install = [
            InstallDirItem(d, self) for d in dirs if os.path.isdir(d)
        ]
        self.sources = [SourceDirItem(d, self) for d in dirs]

    def get_sources(self):
        """
        Return list of sources without dublications
        """
        res = list(self.install)
        res.extend([x for x in self.sources if not x in self.install])
        return res

    def get_dirs(self):
        """
        Get source directories
        """
        return [x.path for x in self.sources if type(x) == SourceDirItem]

    def add_item(self, type_, path):
        """
        Add new configuration item with specified type and path
        """
        sources = {
            'FILE': SourceFileItem,
            'DIR': SourceDirItem,
            'URL': SourceURLItem,
            'URLBASE': SourceURLBaseItem
        }
        install = {'INSTALLDIR': (InstallDirItem, SourceDirItem)}
        settings = {'HTTPPROXY': HTTPProxyItem, 'FTPPROXY': FTPProxyItem}
        if type_ in sources:
            self.sources.append(sources[type_](path, self))
        elif type_ in install:
            self.install.append(install[type_][0](path, self))
            self.sources.append(install[type_][1](path, self))
        elif type_ in settings:
            self.remove_item(type_)
            self.settings.append(settings[type_](path, self))

    def remove_item(self, type_, path=None):
        """
        Remove item with specified type and path if exists
        """
        def with_path(s):
            return s.type != type_ or s.path != path

        def without_path(s):
            return s.type != type_

        if path:
            f = with_path
        else:
            f = without_path
        self.sources = filter(f, self.sources)
        self.install = filter(f, self.install)
        self.settings = filter(f, self.settings)

    def load_from_text(self, data):
        """
        Load configuration from string representation
        """
        self.reset()
        for line in data.splitlines():
            line = line.lstrip()
            if not line or line.startswith('#'):  # empty or commented line
                self.comments.append(line)
                continue
            type_ = line.split()[0]
            path = line[line.find(type_) + len(type_):].strip()
            self.add_item(type_, path)

    def save_to_text(self):
        """
        Save configuration to string representation
        """
        res = []
        res.append("\n".join([x.save() for x in self.install]))
        res.append("\n")
        res.append("\n".join(
            [x.save() for x in self.sources if not x in self.install]))
        res.append("\n")
        res.append("\n".join([x.save() for x in self.settings]))
        res.append("\n".join(self.comments))
        return "".join(res)

    def load(self):
        """
        Load configuration from own file name
        """
        if not self.filename:
            return
        try:
            f = open(self.filename, "r")
            data = f.read()
            f.close()
            self.load_from_text(data)
        except IOError:
            self.set_default()

    def save(self):
        """
        Save configuration to own file name
        """
        if not self.filename:
            return
        f = open(self.filename, "w")
        f.write(self.save_to_text())
        f.close()

    def create_script_list(self, callback=None):
        """
        Create script list from all reloaded sources

        Callback must be in form callback(src, all, current) where src is the current
        source, all and current are tuples with progress of whole operation
        and current source parsing in form (current item, total items)
        """
        def subcallback(callback, src, all):
            def subcallback_impl(current):
                callback(src, all, current)

            if not callback:
                return None
            return subcallback_impl

        scripts = []
        for i, src in enumerate(self.sources):
            subcall = subcallback(callback, src, (i, len(self.sources)))
            if subcall: subcall(None)  # send current=None for every new source
            scripts.extend(src.reload_scripts(subcall))
        return scripts
예제 #4
0
class ScriptConfig(object):
    """
    Hold NSE Facilitator configuration settings
    """
    def __init__(self, filename = NSECONFIG):
        """
        Constructor don't load configuration file itselfs, use load() method after creation.
        """
        self.filename = filename
        self.downloader = DownloaderManager()
        self.reset()

    def __repr__(self):
        return "<ScriptConfig: %d sources>" % len(self.sources)
    
    def reset(self):
        """
        Reset configuration to empty state
        """
        self.sources = []
        self.install = []
        self.settings = []
        self.comments = []

    def get_proxies(self):
        """
        Return proxies dictionary
        """
        proxies = dict()
        for s in self.settings:
            s.apply(proxies)
        return proxies

    def set_proxies(self, proxies):
        """
        Set proxies settings
        """
        self.remove_item('HTTPPROXY')
        self.remove_item('FTPPROXY')
        if proxies.has_key('http'):
            self.add_item('HTTPPROXY', proxies['http'])
        if proxies.has_key('ftp'):
            self.add_item('FTPPROXY', proxies['ftp'])
        
    def download(self, url):
        """
        Download specified url from Web
        """
        proxies = self.get_proxies()
        self.downloader.set_proxies(proxies)
        return self.downloader.download(url)
    
    def install_script(self, name, data):
        """
        Install script with given name and contest using install items
        """
        if not data:
            return None
        for i in self.install:
            installed_path = i.install(name, data)
            if installed_path:
                return installed_path
        return None

    def set_default(self):
        """
        Reset configurtion to default state: take Nmap fetch directories as source
        """
        self.reset()
        dirs = [d for d in NmapFetchScripts().fetchdirs() if os.path.exists(d)]
        self.install = [InstallDirItem(d, self) for d in dirs if os.path.isdir(d)]
        self.sources = [SourceDirItem(d, self) for d in dirs]

    def get_sources(self):
        """
        Return list of sources without dublications
        """
        res = list(self.install)
        res.extend([x for x in self.sources if not x in self.install])
        return res

    def get_dirs(self):
        """
        Get source directories
        """
        return [x.path for x in self.sources if type(x) == SourceDirItem]

    def add_item(self, type_,  path):
        """
        Add new configuration item with specified type and path
        """
        sources = {
            'FILE' : SourceFileItem,
            'DIR'  : SourceDirItem,
            'URL'  : SourceURLItem,
            'URLBASE' : SourceURLBaseItem
        }
        install = {
            'INSTALLDIR' : (InstallDirItem, SourceDirItem)
        }
        settings = {
            'HTTPPROXY' : HTTPProxyItem,
            'FTPPROXY'  : FTPProxyItem
        }
        if type_ in sources:
            self.sources.append(sources[type_](path, self))
        elif type_ in install:
            self.install.append(install[type_][0](path, self))
            self.sources.append(install[type_][1](path, self))
        elif type_ in settings:
            self.remove_item(type_)
            self.settings.append(settings[type_](path, self))

    def remove_item(self, type_, path = None):
        """
        Remove item with specified type and path if exists
        """
        def with_path(s):
            return s.type != type_ or s.path != path
        def without_path(s):
            return s.type != type_
        if path:
            f = with_path
        else:
            f = without_path
        self.sources = filter(f, self.sources)
        self.install = filter(f, self.install)
        self.settings = filter(f, self.settings)
        
    def load_from_text(self, data):
        """
        Load configuration from string representation
        """
        self.reset()
        for line in data.splitlines():
            line = line.lstrip()
            if not line or line.startswith('#'): # empty or commented line
                self.comments.append(line)
                continue
            type_ = line.split()[0]
            path = line[line.find(type_) + len(type_):].strip()
            self.add_item(type_, path)

    def save_to_text(self):
        """
        Save configuration to string representation
        """
        res = []
        res.append("\n".join([x.save() for x in self.install]))
        res.append("\n")
        res.append("\n".join([x.save() for x in self.sources if not x in self.install]))
        res.append("\n")
        res.append("\n".join([x.save() for x in self.settings]))
        res.append("\n".join(self.comments))
        return "".join(res)
    
    def load(self):
        """
        Load configuration from own file name
        """
        if not self.filename:
            return
        try:
            f = open(self.filename, "r")
            data = f.read()
            f.close()
            self.load_from_text(data)
        except IOError:
            self.set_default()

    def save(self):
        """
        Save configuration to own file name
        """
        if not self.filename:
            return
        f = open(self.filename, "w")
        f.write(self.save_to_text())
        f.close()

    def create_script_list(self, callback = None):
        """
        Create script list from all reloaded sources

        Callback must be in form callback(src, all, current) where src is the current
        source, all and current are tuples with progress of whole operation
        and current source parsing in form (current item, total items)
        """
        def subcallback(callback, src, all):
            def subcallback_impl(current):
                callback(src, all, current)
            if not callback:
                return None
            return subcallback_impl
        scripts = []
        for i, src in enumerate(self.sources):
            subcall = subcallback(callback, src, (i, len(self.sources)))
            if subcall: subcall(None) # send current=None for every new source
            scripts.extend(src.reload_scripts(subcall))
        return scripts