rule-updater.py 3.46 KB
Newer Older
1
#!/usr/local/bin/python2.7
2

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
"""
    Copyright (c) 2015 Ad Schellevis
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.

    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.

    --------------------------------------------------------------------------------------
29

30 31
    update suricata rules
"""
32

33
import os
34 35
import sys
import fcntl
36 37 38
from ConfigParser import ConfigParser
from lib import metadata
from lib import downloader
39
from lib import rule_source_directory
40

41 42
# check for a running update process, this may take a while so it's better to check...
try:
43
    lck = open('/tmp/suricata-rule-updater.py', 'w+')
44 45 46 47 48
    fcntl.flock(lck, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
    # already running, exit status 99
    sys.exit(99)

49 50
if __name__ == '__main__':
    # load list of configured rules from generated config
51
    enabled_rulefiles = dict()
52
    updater_conf = '/usr/local/etc/suricata/rule-updater.config'
53 54 55 56
    if os.path.exists(updater_conf):
        cnf = ConfigParser()
        cnf.read(updater_conf)
        for section in cnf.sections():
57
            if cnf.has_option(section, 'enabled') and cnf.getint(section, 'enabled') == 1:
58 59 60 61 62 63 64
                enabled_rulefiles[section.strip()] = {}
                # input filter
                if cnf.has_option(section, 'filter'):
                    enabled_rulefiles[section.strip()]['filter'] = cnf.get(section, 'filter').strip()
                else:
                    enabled_rulefiles[section.strip()]['filter'] = ""

65

66 67
    # download / remove rules
    md = metadata.Metadata()
68
    dl = downloader.Downloader(target_dir=rule_source_directory)
69 70
    for rule in md.list_rules():
        if 'url' in rule['source']:
71
            download_proto = str(rule['source']['url']).split(':')[0].lower()
72 73 74 75
            if dl.is_supported(download_proto):
                if rule['filename'] not in enabled_rulefiles:
                    try:
                        # remove configurable but unselected file
76 77
                        os.remove(('%s/%s' % (rule_source_directory, rule['filename'])).replace('//', '/'))
                    except OSError:
78 79
                        pass
                else:
80
                    input_filter = enabled_rulefiles[rule['filename']]['filter']
81 82
                    dl.download(proto=download_proto, url=rule['url'],
                                filename=rule['filename'], input_filter=input_filter)