Example #1
0
def run_validator(pattern):
    """
    Validates a pattern against the STIX Pattern grammar.  Error messages are
    returned in a list.  The test passed if the returned list is empty.
    """
    start = ''
    if isinstance(pattern, six.string_types):
        start = pattern[:2]
        pattern = InputStream(pattern)

    if not start:
        start = pattern.readline()[:2]
        pattern.seek(0)

    parseErrListener = STIXPatternErrorListener()

    lexer = STIXPatternLexer(pattern)
    # it always adds a console listener by default... remove it.
    lexer.removeErrorListeners()

    stream = CommonTokenStream(lexer)

    parser = STIXPatternParser(stream)

    # it always adds a console listener by default... remove it.
    parser.removeErrorListeners()
    parser.addErrorListener(parseErrListener)

    # To improve error messages, replace "<INVALID>" in the literal
    # names with symbolic names.  This is a hack, but seemed like
    # the simplest workaround.
    for i, lit_name in enumerate(parser.literalNames):
        if lit_name == u"<INVALID>":
            parser.literalNames[i] = parser.symbolicNames[i]

    tree = parser.pattern()
    inspection_listener = InspectionListener()

    # replace with easier-to-understand error message
    if not (start[0] == '[' or start == '(['):
        parseErrListener.err_strings[0] = "FAIL: Error found at line 1:0. " \
                                          "input is missing square brackets"

    # validate observed objects
    if len(parseErrListener.err_strings) == 0:
        ParseTreeWalker.DEFAULT.walk(inspection_listener, tree)
        patt_data = inspection_listener.pattern_data()
        obj_validator_results = object_validator.verify_object(patt_data)

        if obj_validator_results:
            parseErrListener.err_strings.extend(obj_validator_results)

    return parseErrListener.err_strings
Example #2
0
def run_validator(pattern, stix_version=DEFAULT_VERSION):
    """
    Validates a pattern against the STIX Pattern grammar.  Error messages are
    returned in a list.  The test passed if the returned list is empty.
    """
    start = ''
    if isinstance(pattern, six.string_types):
        start = leading_characters(pattern, 2)
        pattern = InputStream(pattern)

    if not start:
        start = leading_characters(pattern.readline(), 2)
        pattern.seek(0)

    if stix_version == '2.1':
        return run_validator21(pattern, start)
    else:
        return run_validator20(pattern, start)
def run_validator(pattern):
    """
    Validates a pattern against the STIX Pattern grammar.  Error messages are
    returned in a list.  The test passed if the returned list is empty.
    """

    start = ''
    if isinstance(pattern, six.string_types):
        start = pattern[:2]
        pattern = InputStream(pattern)

    if not start:
        start = pattern.readline()[:2]
        pattern.seek(0)

    parseErrListener = STIXPatternErrorListener()

    lexer = STIXPatternLexer(pattern)
    # it always adds a console listener by default... remove it.
    lexer.removeErrorListeners()

    stream = CommonTokenStream(lexer)

    parser = STIXPatternParser(stream)
    parser.buildParseTrees = False
    # it always adds a console listener by default... remove it.
    parser.removeErrorListeners()
    parser.addErrorListener(parseErrListener)

    # To improve error messages, replace "<INVALID>" in the literal
    # names with symbolic names.  This is a hack, but seemed like
    # the simplest workaround.
    for i, lit_name in enumerate(parser.literalNames):
        if lit_name == u"<INVALID>":
            parser.literalNames[i] = parser.symbolicNames[i]

    parser.pattern()

    # replace with easier-to-understand error message
    if not (start[0] == '[' or start == '(['):
        parseErrListener.err_strings[0] = "FAIL: Error found at line 1:0. " \
                                          "input is missing square brackets"

    return parseErrListener.err_strings
def create_pattern_object(pattern, module_suffix="", module_name=""):
    """
    Validates a pattern against the STIX Pattern grammar.  Error messages are
    returned in a list.  The test passed if the returned list is empty.
    """

    start = ''
    if isinstance(pattern, six.string_types):
        start = pattern[:2]
        pattern = InputStream(pattern)

    if not start:
        start = pattern.readline()[:2]
        pattern.seek(0)

    parseErrListener = STIXPatternErrorListener()

    lexer = STIXPatternLexer(pattern)
    # it always adds a console listener by default... remove it.
    lexer.removeErrorListeners()

    stream = CommonTokenStream(lexer)

    parser = STIXPatternParser(stream)

    parser.buildParseTrees = True
    # it always adds a console listener by default... remove it.
    parser.removeErrorListeners()
    parser.addErrorListener(parseErrListener)

    # To improve error messages, replace "<INVALID>" in the literal
    # names with symbolic names.  This is a hack, but seemed like
    # the simplest workaround.
    for i, lit_name in enumerate(parser.literalNames):
        if lit_name == u"<INVALID>":
            parser.literalNames[i] = parser.symbolicNames[i]

    tree = parser.pattern()
    builder = STIXPatternVisitorForSTIX2(module_suffix, module_name)
    return builder.visit(tree)
Example #5
0
def run_validator(pattern, stix_version=DEFAULT_VERSION):
    """
    Validates a pattern against the STIX Pattern grammar.  Error messages are
    returned in a list.  The test passed if the returned list is empty.
    """
    if isinstance(pattern, six.string_types):
        pattern_str = pattern
        pattern = InputStream(pattern)

    else:
        pattern_str = pattern.readline()
        pattern.seek(0)

    if stix_version == '2.1':
        err_messages = run_validator21(pattern)
    else:
        err_messages = run_validator20(pattern)

    if not brackets_check(pattern_str):
        err_messages.insert(
            0,
            "FAIL: Error found at line 1:0. input is missing square brackets")

    return err_messages
 def execute(self, lexer:Lexer, input:InputStream, startIndex:int):
     requiresSeek = False
     stopIndex = input.index
     try:
         for lexerAction in self.lexerActions:
             if isinstance(lexerAction, LexerIndexedCustomAction):
                 offset = lexerAction.offset
                 input.seek(startIndex + offset)
                 lexerAction = lexerAction.action
                 requiresSeek = (startIndex + offset) != stopIndex
             elif lexerAction.isPositionDependent:
                 input.seek(stopIndex)
                 requiresSeek = False
             lexerAction.execute(lexer)
     finally:
         if requiresSeek:
             input.seek(stopIndex)
Example #7
0
 def execute(self, lexer: Lexer, input: InputStream, startIndex: int):
     requiresSeek = False
     stopIndex = input.index
     try:
         for lexerAction in self.lexerActions:
             if isinstance(lexerAction, LexerIndexedCustomAction):
                 offset = lexerAction.offset
                 input.seek(startIndex + offset)
                 lexerAction = lexerAction.action
                 requiresSeek = (startIndex + offset) != stopIndex
             elif lexerAction.isPositionDependent:
                 input.seek(stopIndex)
                 requiresSeek = False
             lexerAction.execute(lexer)
     finally:
         if requiresSeek:
             input.seek(stopIndex)