installRules.py 4.08 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

Ad Schellevis's avatar
Ad Schellevis committed
30
    Install suricata ruleset into opnsense.rules directory
31 32
"""
import os.path
33 34
import lib.rulecache
from lib import rule_source_directory
35

36 37 38
if __name__ == '__main__':
    RuleCache = lib.rulecache.RuleCache()

39 40
    rule_target_dir = ('%s../opnsense.rules' % rule_source_directory)
    rule_yaml_list = ('%s../installed_rules.yaml' % rule_source_directory)
41

42
    rule_config_fn = ('%s../rules.config' % rule_source_directory)
43
    # parse OPNsense rule config
44
    rule_updates = RuleCache.list_local_changes()
45 46 47 48 49 50 51

    # create target rule directory if not existing
    if not os.path.exists(rule_target_dir):
        os.mkdir(rule_target_dir, 0o755)

    # install ruleset
    all_installed_files = []
52
    for filename in RuleCache.list_local():
53
        output_data = []
54
        for rule_info_record in RuleCache.list_rules(filename=filename):
55 56 57 58 59 60 61 62 63 64 65 66
            # default behavior, do not touch rule, only copy to output
            rule = rule_info_record['rule']
            # change rule if in rule rule updates
            if rule_info_record['metadata'] is not None and 'sid' in rule_info_record['metadata'] \
                    and rule_info_record['metadata']['sid'] in rule_updates:
                # search last comment marker
                for i in range(len(rule_info_record['rule'])):
                    if rule[i] != '#':
                        break

                # generate altered rule
                if 'enabled' in rule_updates[rule_info_record['metadata']['sid']]:
67
                    # enabled / disabled in configuration
68
                    if (rule_updates[rule_info_record['metadata']['sid']]['enabled']) == '0':
69
                        rule = ('#%s' % rule[i:])
70 71
                    else:
                        rule = rule[i:]
72 73 74 75 76 77 78
                if 'action' in rule_updates[rule_info_record['metadata']['sid']]:
                    # (new) action in configuration
                    new_action = rule_updates[rule_info_record['metadata']['sid']]['action']
                    if rule[0] == '#':
                        rule = '#%s %s' % (new_action, ' '.join(rule.split(' ')[1:]))
                    else:
                        rule = '%s %s' % (new_action, ' '.join(rule.split(' ')[1:]))
79 80 81 82 83

            output_data.append(rule)

        # write data to file
        all_installed_files.append(filename.split('/')[-1])
84
        open('%s/%s' % (rule_target_dir, filename.split('/')[-1]), 'wb').write('\n'.join(output_data))
85 86

    # flush all written rule filenames into yaml file
87
    with open(rule_yaml_list, 'wb') as f_out:
88 89 90 91
        f_out.write('%YAML 1.1\n')
        f_out.write('---\n')
        f_out.write('rule-files:\n')
        for installed_file in all_installed_files:
92
            f_out.write(' - %s\n' % installed_file)