Example #1
0
def getTypeObject(name=None, id=None):
    """ Open and read data from type name passed by user, then create Type Object from data
        Rule Objects are also created and added to Type Object.

        :return: Type Object created from data read in heimdall/conf/rules/type.yml.
        :rtype: Type Object

        ..seealso:: heimdall.core.type.Type, heimdall.core.rule.Rule
    """
    from core.type import Type
    from core.rule import Rule
    from core.yml import open_yml

    filepath = path.join(__tdir__, name + '.yml')
    type_data = open_yml(path=filepath)
    if type_data:
        type_data['path'] = filepath
        type = Type(**type_data)
        for r in type_data['rules']:
            r['type'] = type.name
            if id and id != -1:
                for i in id:
                    if str(i) == str(r['id']):
                        type.rules.append(Rule(**r))
                        type.all = False
            else:
                type.rules.append(Rule(**r))
                type.all = True
        return type
    def save_population_operation(self):
        population = RulePopulation(self.STARTING_SYMBOL)
        for rule_model in self.population_editor.rules:
            if rule_model.is_terminal:
                parent, terminal_word = Rule.from_human_friendly_representation(
                    population.symbol_shift(),
                    population.starting_symbol,
                    population.universal_symbol,
                    rule_model.parent,
                    rule_model.left_child,
                    rule_model.right_child
                )
                terminal_symbol = self.translator.word_to_symbol(terminal_word)

                population.add_rule(Rule(parent, terminal_symbol), self.randomizer)
            else:
                parent, left, right = Rule.from_human_friendly_representation(
                    population.symbol_shift(),
                    population.starting_symbol,
                    population.universal_symbol,
                    rule_model.parent,
                    rule_model.left_child,
                    rule_model.right_child
                )

                population.add_rule(Rule(parent, left, right), self.randomizer)

        name = os.path.basename(self.population_editor.population_path).split('.')[0]
        path = os.path.dirname(self.population_editor.population_path)
        self.simulation_executor.save_population_data(population, path, name)
Example #3
0
def getRuleObject(type=None, id=None):
    """ Open and read data from type name passed by user, then create Rule Object from data filtered by id.

        :return: Rule Object created from data read in heimdall/conf/rules/type.yml.
        :rtype: Rule Object

        ..seealso:: heimdall.core.rule.Rule
    """
    from core.rule import Rule
    from core.exceptions import RuleDoesNotExist
    from core.yml import open_yml

    filepath = path.join(__tdir__, type + '.yml')
    type_data = open_yml(path=filepath)

    try:
        if type_data:
            for r in type_data['rules']:
                if str(id) == str(r.get('id')):
                    r['type'] = type_data['name']
                    return Rule(**r)
            else:
                raise RuleDoesNotExist(
                    'Rule from type %s with id %s Does not exist!' %
                    (type_data.get('name'), id))
    except RuleDoesNotExist as rde:
        print rde
        exit(rde.code)
Example #4
0
def getAllTypesObjects():
    """ Open and read data from heimdall/conf/rules/type.yaml, then create Type Objects from data.
        Rule Objects are also created and added to Type Objects.

        :return: list of all Type Objects available.
        :rtype: list of Type Objects

        ..seealso:: heimdall.core.type.Type, heimdall.core.rule.Rule
    """
    from core.type import Type
    from core.rule import Rule
    from core.yml import open_yml

    type_availables = []
    for type in __typesfiles__:
        filepath = path.join(__tdir__, type)
        type_data = open_yml(path=filepath)
        if type_data:
            type_data['path'] = filepath
            type = Type(**type_data)
            for r in type_data['rules']:
                r['type'] = type.name
                type.rules.append(Rule(**r))
                type.all = True
            type_availables.append(type)
    return type_availables
Example #5
0
def add_rule(**kwargs):
    """ Create a Rule Object from information passed by user, and return a TypeObject who contain the new rule Object.

        Keyword Args:
            type (list of one str): rule's type (same as type yaml file) passed by user
            desc (list of one str): rule's description passed by user
            auditcmd (list of str): rule's auditcmd passed by user
            auditscript (list of str): rule's auditscript passed by user
            remedcmd (list of str): rule's remedcmd passed by user
            remedscript (list of str): rule's remedscript passed by use

        :return: updated Type with the new Rule Object
        :rtype: Type Object

        ..seealso: heimdall.core.rule.Rule, heimdall.core.type.Type, heimdall.conf.rules.getTypeObject
    """
    from core.rule import Rule
    # Create new dict with no empty values AND string format if value is list of one element
    newrule = {}
    for k, v in kwargs.iteritems():
        if not v:  # no value passed for this key, just pass
            pass
        elif 'audit' in k:  # key can be 'auditcmd' or 'auditscript', merge into 'audit' key.
            newrule['audit'] = kwargs[k]
        elif 'remed' in k:  # key can be 'remedcmd' or 'remedscript', merge into 'remed' key.
            newrule['remed'] = kwargs[k]
        else:  # type and desc are list of one element
            newrule[k] = kwargs[k][0]

    rule = Rule(**newrule)

    from conf.rules import getTypeObject

    t = getTypeObject(rule.type)  # Retrieve Type Object from rule type attribute
    rule.id = t.get_new_id()  # Setting new id
    from utils.wiki import create_wikirule
    create_wikirule(rule)
    rule.check_wikipage()
    t.add_rule(rule)

    return t
    def get_rule(self, rule):
        """Return specific rule from name and its execution timeout."""

        # Bind the rule options to the function call
        # There may be multiple conditions defined per rule
        rule_obj = Rule(self.bind_options(self.rules, rule),
                        map(lambda x: self.bind_options(self.conditions, x),
                            rule["conditions"]),
                        name=rule["rule_name"])

        # Get timeout from rule-specific config or from default value
        timeout = rule.get("timeout") or config["DEFAULT_RULE_TIMEOUT"]

        return (rule_obj, timeout)
Example #7
0
def getAllRulesObjects():
    """ Open and read data from heimdall/conf/rules/type.yaml, then create Rule Objects from data.

        :return: list of all Rule Objects available.
        :rtype: list of Rule Objects

        ..seealso:: heimdall.core.rule.Rule
    """
    from core.rule import Rule
    from core.yml import open_yml

    rules_availables = []
    for type in __typesfiles__:
        filepath = path.join(__tdir__, type)
        type_data = open_yml(path=filepath)
        if type_data:
            for r in type_data['rules']:
                r['type'] = type_data['name']  # Adding type for format_output
                rules_availables.append(Rule(**r))
    return rules_availables
Example #8
0
 def _generate_rule(self, global_itemset: ItemSet, clause: ItemSet):
   result = global_itemset.new_remove(*clause.items)
   rule = Rule.create_fule(clause, result)
   return rule
 def json_decoder(self, json, randomizer):
     for jsonized_rule in json[1:]:
         rule = Rule.json_decoder(jsonized_rule[1:])
         super().add_rule(rule, randomizer)
         self._add_new_rule_probability(rule, jsonized_rule[0])
 def json_decoder(self, json, randomizer):
     for jsonized_rule in json[1:]:
         self.add_rule(Rule.json_decoder(jsonized_rule), randomizer)
Example #11
0
def test_rules():
    rules = Rule().rules
    rules_list = Rule().rules()
    assert isinstance(rules, object)
    assert isinstance(rules_list, dict)
    assert isinstance(rules_list['CVI_10001'], object)
Example #12
0
def test_vulnerabilities():
    vulnerabilities = Rule().vulnerabilities
    assert isinstance(vulnerabilities, list)
    assert 'SQLI' in vulnerabilities