def test_clean_whitespace(self):
     """Clean up whitespace."""
     options = DotMap()
     rules = Rules(options)
     lines = [
         [
             ' Whitespace is removed from either end of the line. ',
             'Whitespace is removed from either end of the line.',
         ],
         [
             '   Whitespace is removed from either end of the line.   ',
             'Whitespace is removed from either end of the line.',
         ],
         [
             'Multiple spaces          are merged into a space.',
             'Multiple spaces are merged into a space.',
         ],
         [
             '\tTabs are merged\t\tinto single spaces.\t\t',
             'Tabs are merged into single spaces.',
         ],
         [
             '\tMixed whitespace is      \t treated the same.\t\t',
             'Mixed whitespace is treated the same.',
         ],
     ]
     for line in lines:
         self.assertEqual(rules.clean_whitespace(line[0]), line[1])
Example #2
0
    def __init__(self):
        """
            Initialize type and info_msg attributes which respectively represent
            the type of extracted information (here format strings - formatStr),
            and the message to display when the rule is initialized.
        """

        Rules.__init__(self)
        self.type = "format string"
        self.info_msg = "-> Format string analysis "
Example #3
0
    def __init__(self):
        """
            Initialize type and info_msg attributes which respectively represent
            the type of extracted information (here file path - path),
            and the message to display when the rule is initialized.
        """

        Rules.__init__(self)
        self.type = "path"
        self.info_msg = "-> Path analysis "
Example #4
0
    def __init__(self):
        """
            Initialize type and info_msg attributes which respectively represent
            the type of extracted information (here identifiers - Id),
            and the message to display when the rule is initialized.
        """

        Rules.__init__(self)
        self.type = "id"
        self.info_msg = "-> Identifiers analysis "
Example #5
0
    def __init__(self):
        """
            Initialize type and info_msg attributes which respectively represent
            the type of extracted information (here format strings - formatStr),
            and the message to display when the rule is initialized.
        """

        Rules.__init__(self)
        self.type = "format string"
        self.info_msg = "-> Format string analysis "
Example #6
0
    def __init__(self):
        """
            Initialize type and info_msg attributes which respectively represent
            the type of extracted information (here identifiers - Id),
            and the message to display when the rule is initialized.
        """

        Rules.__init__(self)
        self.type = "id"
        self.info_msg = "-> Identifiers analysis "
Example #7
0
    def __init__(self):
        """
            Initialize type and info_msg attributes which respectively represent
            the type of extracted information (here URLs - url),
            and the message to display when the rule is initialized.
        """

        Rules.__init__(self)
        self.type = "url"
        self.info_msg = "-> Url analysis "
Example #8
0
    def __init__(self):
        """
            Initialize type and info_msg attributes which respectively represent
            the type of extracted information (here format ip addresses - IpAddr),
            and the message to display when the rule is initialized.
        """

        Rules.__init__(self)
        self.type = "ip"
        self.info_msg = "-> IP adresses analysis "
Example #9
0
    def __init__(self):
        """
            Initialize type and info_msg attributes which respectively represent
            the type of extracted information (here format ip addresses - IpAddr),
            and the message to display when the rule is initialized.
        """

        Rules.__init__(self)
        self.type = "ip"
        self.info_msg = "-> IP adresses analysis "
Example #10
0
    def __init__(self):
        """
            Initialize type and info_msg attributes which respectively represent
            the type of extracted information (here command line - cmd), and
            the message to display when the rule is initialized.
        """

        Rules.__init__(self)
        self.type = "cmd"
        self.info_msg = "-> Cmd analysis "
Example #11
0
    def __init__(self):
        """
            Initialize type and info_msg attributes which respectively represent
            the type of extracted information (here command line - cmd), and
            the message to display when the rule is initialized.
        """

        Rules.__init__(self)
        self.type = "cmd"
        self.info_msg = "-> Cmd analysis "
Example #12
0
 def rules(self):
   ret = []
   for i in range(self.model.invisibleRootItem().rowCount()):
     categoryItem = self.model.invisibleRootItem().child(i)
     r = Rules()
     r.name = categoryItem.text()
     for j in range(categoryItem.rowCount()):
       ruleItem = categoryItem.child(j)
       r.rules.append(ruleItem.text())
     ret.append(r)
   return ret
Example #13
0
 def __init__(self, path, options):
     """Constructor.
     @type  path: str
     @param path: Path of output file to be generated.
     @type  options: DotMap
     @param options: Compile options parsed from the config file.
     """
     self.handle = None
     self.options = options
     self.path = path
     self.rules = Rules(options)
Example #14
0
    def __init__(self, malware_path):
        """
            Initialize type and info_msg attributes which respectively represent
            the type of extracted information (here elf symbols - symbol),
            and the message to display when the rule is initialized.
            This class also contains a malware_path value containing the absolute
            path of the analyzed binary.

            :param malware_path: Absolute path of the analyzed binary.
            :type: string
        """

        Rules.__init__(self)
        self.type = "symbol"
        self.info_msg = "-> Symbols analysis "
        self.malware_path = malware_path
Example #15
0
    def __init__(self, malware_path):
        """
            Initialize type and info_msg attributes which respectively represent
            the type of extracted information (here elf symbols - symbol),
            and the message to display when the rule is initialized.
            This class also contains a malware_path value containing the absolute
            path of the analyzed binary.

            :param malware_path: Absolute path of the analyzed binary.
            :type: string
        """

        Rules.__init__(self)
        self.type = "symbol"
        self.info_msg = "-> Symbols analysis "
        self.malware_path = malware_path
 def _create_rules_prose_first_char(self):
     """Create a rules object for inserting characters.
     @rtype:  lib.rules.Rules
     @return: Rules object.
     """
     options = DotMap()
     options.compile.paragraph.mode = 'prose'
     options.compile.paragraph.tabFirst.title = False
     options.compile.paragraph.tabFirst.chapter = True
     options.compile.paragraph.tabFirst.section = True
     return Rules(options)
Example #17
0
class _TextStrategyCompiler(BaseStrategyCompiler):
    """Compile to a plain text file."""
    def __init__(self, path, options):
        """Constructor.
        @type  path: str
        @param path: Path of output file to be generated.
        @type  options: DotMap
        @param options: Compile options parsed from the config file.
        """
        self.handle = None
        self.options = options
        self.path = path
        self.rules = Rules(options)

    def __enter__(self):
        """Create and open a new document."""
        self.handle = open(self.path, 'w')
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        """Close the open file handle."""
        self.handle.close()

    def _blank_lines(self, state):
        """Determine the number of blank lines to insert before this one.
        :param lib.state.State state: Formatting state of current line of text.
        :return str: Line breaks to add as a prefix to the current line.
        """
        blank_lines = ''
        if state.is_first_paragraph:
            if state.markup.is_chapter or state.markup.is_section:
                blank_lines = '\n'
            else:
                blank_lines = '\n\n'
        elif state.markup.token:
            if (state.markup.token == MarkupToken.chapter
                    or state.markup.token == MarkupToken.section):
                blank_lines = '\n\n'
            elif state.markup.token == MarkupToken.section_break:
                blank_lines = '\n'
        return blank_lines

    def _format(self, line, state):
        """Format the line of text for the target document type.
        @type  line: str
        @param line: Proze formatted line to be written to the document.
        @type  state: lib.state.State
        @param state: Formatting state of the current line of text.
        @rtype:  str
        @return: Formatted line for insertion into the document.
        """
        line = self.rules.clean_whitespace(line)
        blank_lines = self._blank_lines(state)
        if state.markup.is_markup_line:
            line = blank_lines + self._parse_structural_markup(line, state)
        else:
            first_char = self.rules.first_character(state, use_spaces=True)
            line = blank_lines + first_char + line
            line = self._strip_bold_italics(line)
        return line

    def _parse_structural_markup(self, line, state):
        """Process lines of structural markup.
        @type  line: str
        @param line: Proze formatted line to be written to the document.
        @type  state: lib.state.State
        @param state: Formatting state of the current line of text.
        @rtype:  str
        @return: Line after structural markup changes are applied.
        """
        if state.markup.token == MarkupToken.author:
            line = re.sub(state.markup.token, 'by', line, flags=re.I)
        elif state.markup.token == MarkupToken.chapter:
            line = re.sub(state.markup.token, '', line, flags=re.I)
        elif state.markup.token == MarkupToken.section:
            line = re.sub(state.markup.token, '', line, flags=re.I)
        elif state.markup.token == MarkupToken.section_break:
            line = line.strip()
        elif state.markup.token == MarkupToken.title:
            line = re.sub(state.markup.token, '', line, flags=re.I)
        return line.strip()

    def _split_on_line_length(self, line):
        """Split into multiple lines if longer than MAX_LINE_LENGTH.
        @type  line: str:
        @param line: Text to be split.
        @rtype:  list
        @return: Split line.
        """
        lines = []
        curr = line
        while len(curr) > MAX_LINE_LENGTH:
            index = MAX_LINE_LENGTH
            while curr[index] not in [' ', '-'] and index >= 0:
                index -= 1
            if index == 0:
                # Force a hard mid-word split for really long words.
                index = MAX_LINE_LENGTH
            lines.append(curr[0:index])
            curr = curr[index + 1:]
        lines.append(curr)
        return lines

    def _strip_bold_italics(self, line):
        """Remove bold and italic markup.
        @type  line: str
        @param line: Proze formatted line to be written to the document.
        @rtype:  str
        @return: Line after bold and italic markup is removed.
        """
        line = re.sub('\*', '', line)
        line = re.sub('__', '', line)
        return line

    def write(self, line, state):
        """Write a line of text to the output document.
        @type  line: str
        @param line: Formatted line to be written to the document.
        @type  state: lib.state.State
        @param state: Formatting state of the current line of text.
        """
        if not state.is_blank:
            line = self._format(line, state)
            lines = self._split_on_line_length(line)
            for partial in lines:
                self.handle.write(partial + '\n')