Extract YARA signatures embedded in Yara/Yara indicatorItem nodes.
        This is done regardless of logic structure in the OpenIOC.
482787
def get_embedded_yara(self, iocid):
        
        ioc_obj = self.iocs[iocid]
        ids_to_process = set([])
        signatures = ''
        for elem in ioc_obj.top_level_indicator.xpath('.//IndicatorItem[Context/@search = "Yara/Yara"]'):
            signature = elem.findtext('Content')
            signatures = signatures + '\n' + signature
        if signatures:
            signatures += '\n'
        return signatures
888782
def process_module(self, yam):
        """"""
        for ann in yam.search(("ietf-yang-metadata", "annotation")):
            self.process_annotation(ann)
        for ch in yam.i_children[:]:
            if ch.keyword == "rpc":
                self.process_rpc(ch)
            elif ch.keyword == "notification":
                self.process_notification(ch)
            else:
                continue
            yam.i_children.remove(ch)
        self.process_children(yam, "//nc:*", 1)
639975
def extract_builton(self, built_on, regex):
        
        pattern = re.compile(regex, re.M | re.I)
        match = pattern.search(built_on)
        if match and len(match.groups()) >= 1:
            node_name = match.group(1)
        else:
            msg = "Node name not extracted, using builtOn as it is: " + regex + ":" + built_on
            logger.warning(msg)
            node_name = built_on

        return node_name
318466
def verify(xml):
    ''''''
    try:
        from cStringIO import cStringIO as StringIO 
    except ImportError:
        from StringIO import StringIO
    
    xml = xml.encode('utf-8', 'xmlcharrefreplace')
    tree = etree.parse(StringIO(xml))
    
    try:
        '''Isolating the Signature element'''
        signature_tree = tree.xpath('xmldsig:Signature',
                                    namespaces=NAMESPACES)[0]
    
        '''Removing the Signature element from the main tree to compute a new 
        digest value with the actual content of the XML.'''
        signature_tree.getparent().remove(signature_tree)
        signed_info = _generate_signed_info(etree.tostring(tree))
        
        '''Constructing the key from the Modulus and Exponent stored in the
        Signature/KeyInfo/KeyValue/RSAKeyValue element'''                                 
        from Crypto.PublicKey import RSA
        modulus = signature_tree.xpath(KEY_INFO_PATH + '/xmldsig:Modulus',
                                       namespaces=NAMESPACES)[0].text
        exponent = signature_tree.xpath(KEY_INFO_PATH + '/xmldsig:Exponent',
                                        namespaces=NAMESPACES)[0].text        
        public_key = RSA.construct((b64d(modulus), b64d(exponent)))
    
        '''Isolating the signature value to verify'''
        signature = signature_tree.xpath('xmldsig:SignatureValue',
                                         namespaces=NAMESPACES)[0].text
    except IndexError:
        raise RuntimeError('XML does not contain a properly formatted ' \
                           ' Signature element')
    
    verifier = PKCS1_v1_5.PKCS115_SigScheme(public_key)
    return verifier.verify(SHA.new(c14n(signed_info)),
                           binascii.a2b_base64(signature))
1103700
def parse_yaml(self, y):
        '''

        '''
        self.connector_id = y['connectorId']
        self.name = y['name']
        if 'transMethod' in y:
            self.trans_method = y['transMethod']
        else:
            self.trans_method = ''
        if RTS_EXT_NS_YAML + 'comment' in y:
            self.comment = y[RTS_EXT_NS_YAML + 'comment']
        else:
            self.comment = ''
        if RTS_EXT_NS_YAML + 'visible' in y:
            visible = y[RTS_EXT_NS_YAML + 'visible']
            if visible == True or visible == 'true' or visible == '1':
                self.visible = True
            else:
                self.visible = False
        if 'sourceServicePort' not in y:
            raise InvalidServicePortConnectorNodeError
        self.source_service_port = \
                TargetPort().parse_yaml(y['sourceServicePort'])
        if 'targetServicePort' not in y:
            raise InvalidServicePortConnectorNodeError
        self.target_service_port = \
                TargetPort().parse_yaml(y['targetServicePort'])
        if RTS_EXT_NS_YAML + 'properties' in y:
            for p in y[RTS_EXT_NS_YAML + 'properties']:
                if 'value' in p:
                    value = p['value']
                else:
                    value = None
                self._properties[p['name']] = value
        return self
1210865
def get_identities(self, item):
        

        # question
        user = self.get_sh_identity(item, self.get_field_author())
        yield user

        # answers
        if 'answers' in item['data']:
            for answer in item['data']['answers']:
                # avoid "answered_by" : "This post is a wiki" corner case
                if type(answer['answered_by']) is dict:
                    user = self.get_sh_identity(answer['answered_by'])
                    yield user
                if 'comments' in answer:
                    for comment in answer['comments']:
                        commenter = self.get_sh_identity(comment)
                        yield commenter
140892
def get_signature_candidate(lines):
    
    # non empty lines indexes
    non_empty = [i for i, line in enumerate(lines) if line.strip()]

    # if message is empty or just one line then there is no signature
    if len(non_empty) <= 1:
        return []

    # we don't expect signature to start at the 1st line
    candidate = non_empty[1:]
    # signature shouldn't be longer then SIGNATURE_MAX_LINES
    candidate = candidate[-SIGNATURE_MAX_LINES:]

    markers = _mark_candidate_indexes(lines, candidate)
    candidate = _process_marked_candidate_indexes(candidate, markers)

    # get actual lines for the candidate instead of indexes
    if candidate:
        candidate = lines[candidate[0]:]
        return candidate

    return []
1355501
def _extract_from_included(self, data):
        
        return (item for item in self.included_data
                if item['type'] == data['type'] and
                str(item['id']) == str(data['id']))
132720
def yara_match(file_path, rules):
    """"""
    print('New Extracted File: {:s}'.format(file_path))
    print('Mathes:')
    pprint(rules.match(file_path))
1121253
def parse_yaml(self, y):
        ''''''
        self._reset()
        self.id = y['id']
        self.path_uri = y['pathUri']
        if 'activeConfigurationSet' in y:
            self.active_configuration_set = y['activeConfigurationSet']
        else:
            self.active_configuration_set = ''
        self.instance_name = y['instanceName']
        self.compositeType = comp_type.from_string(y['compositeType'])
        required = y['isRequired']
        if required == True or required == 'true' or required == '1':
            self.is_required = True
        else:
            self.is_required = False
        if RTS_EXT_NS_YAML + 'comment' in y:
            self.comment = y[RTS_EXT_NS_YAML + 'comment']
        self.visible = False
        if RTS_EXT_NS_YAML + 'visible' in y:
            visible = y.get(RTS_EXT_NS_YAML + 'visible')
            if visible == True or visible == 'true' or visible == 'True':
                self.visible = True

        # Get the children
        if 'dataPorts' in y:
            for p in y.get('dataPorts'):
                self._data_ports.append(DataPort().parse_yaml(p))
        if 'servicePorts' in y:
            for p in y.get('servicePorts'):
                self._service_ports.append(ServicePort().parse_yaml(p))
        if 'configurationSets' in y:
            for p in y.get('configurationSets'):
                self._config_sets.append(ConfigurationSet().parse_yaml(p))
        if 'executionContexts' in y:
            for p in y.get('executionContexts'):
                self._exec_contexts.append(ExecutionContext().parse_yaml(p))
        if 'participants' in y:
            for p in y.get('participants'):
                self._participants.append(Participant().parse_yaml(p))

        # Extended profile children
        if RTS_EXT_NS_YAML + 'location' in y:
            l = y[RTS_EXT_NS_YAML + 'location']
            self._location = Location().parse_yaml(l)
        if RTS_EXT_NS_YAML + 'properties' in y:
            for p in y.get(RTS_EXT_NS_YAML + 'properties'):
                if 'value' in p:
                    value = p['value']
                else:
                    value = None
                self._properties[p['name']] = value

        return self
463148
def _GetSignatureScanner(cls, specification_store):
    
    signature_scanner = pysigscan.scanner()
    signature_scanner.set_scan_buffer_size(cls._SCAN_BUFFER_SIZE)

    for format_specification in specification_store.specifications:
      for signature in format_specification.signatures:
        pattern_offset = signature.offset

        if pattern_offset is None:
          signature_flags = pysigscan.signature_flags.NO_OFFSET
        elif pattern_offset < 0:
          pattern_offset *= -1
          signature_flags = pysigscan.signature_flags.RELATIVE_FROM_END
        else:
          signature_flags = pysigscan.signature_flags.RELATIVE_FROM_START

        signature_scanner.add_signature(
            signature.identifier, pattern_offset, signature.pattern,
            signature_flags)

    return signature_scanner
741124
def get_all_nodes(self, addr, is_syscall=None, anyaddr=False):
        
        results = [ ]

        for cfg_node in self.graph.nodes():
            if cfg_node.addr == addr or (anyaddr and
                                         cfg_node.size is not None and
                                         cfg_node.addr <= addr < (cfg_node.addr + cfg_node.size)
                                         ):
                if is_syscall and cfg_node.is_syscall:
                    results.append(cfg_node)
                elif is_syscall is False and not cfg_node.is_syscall:
                    results.append(cfg_node)
                else:
                    results.append(cfg_node)

        return results
1205223
def _get_node_key(self, node_dict_item):
        """"""
        s = tuple(sorted(node_dict_item['sources']))
        t = tuple(sorted(node_dict_item['targets']))
        return (s, t)
1339803
def extract_pattern(fmt):
    """"""
    class FakeDict(object):
        def __init__(self):
            self.seen_keys = set()

        def __getitem__(self, key):
            self.seen_keys.add(key)
            return ''

        def keys(self):
            return self.seen_keys

    fake = FakeDict()
    try:
        fmt % fake
    except TypeError:
        # Formatting error
        pass
    return set(fake.keys())
1358278
def _export_signatures(meta_graph):
  """"""
  named_signatures = tf_v1.get_collection(_SIGNATURE_COLLECTION)
  if not named_signatures:
    raise ValueError("No signatures present. Please call hub.add_signature(...)"
                     "at least once in the module_fn.")
  for key, signature in named_signatures:
    meta_graph.signature_def[key].CopyFrom(signature)
276086
def get_identities(self, item):
        

        field = self.get_field_author()
        yield self.get_sh_identity(item, field)
508878
def process_item_nodes(app, doctree, fromdocname):
    
    env = app.builder.env

    all_items = sorted(env.traceability_all_items, key=naturalsortkey)

    # Item matrix:
    # Create table with related items, printing their target references.
    # Only source and target items matching respective regexp shall be included
    for node in doctree.traverse(item_matrix):
        table = nodes.table()
        tgroup = nodes.tgroup()
        left_colspec = nodes.colspec(colwidth=5)
        right_colspec = nodes.colspec(colwidth=5)
        tgroup += [left_colspec, right_colspec]
        tgroup += nodes.thead('', nodes.row(
            '',
            nodes.entry('', nodes.paragraph('', 'Source')),
            nodes.entry('', nodes.paragraph('', 'Target'))))
        tbody = nodes.tbody()
        tgroup += tbody
        table += tgroup

        for source_item in all_items:
            if re.match(node['source'], source_item):
                row = nodes.row()
                left = nodes.entry()
                left += make_item_ref(app, env, fromdocname,
                                      env.traceability_all_items[source_item])
                right = nodes.entry()
                for target_item in all_items:
                    if (re.match(node['target'], target_item) and
                            are_related(
                                env, source_item, target_item, node['type'])):
                        right += make_item_ref(
                            app, env, fromdocname,
                            env.traceability_all_items[target_item])
                row += left
                row += right
                tbody += row

        node.replace_self(table)

    # Item list:
    # Create list with target references. Only items matching list regexp
    # shall be included
    for node in doctree.traverse(item_list):
        content = nodes.bullet_list()
        for item in all_items:
            if re.match(node['filter'], item):
                bullet_list_item = nodes.list_item()
                paragraph = nodes.paragraph()
                paragraph.append(
                    make_item_ref(app, env, fromdocname,
                                  env.traceability_all_items[item]))
                bullet_list_item.append(paragraph)
                content.append(bullet_list_item)

        node.replace_self(content)

    # Resolve item cross references (from ``item`` role)
    for node in doctree.traverse(pending_item_xref):
        # Create a dummy reference to be used if target reference fails
        new_node = make_refnode(app.builder,
                                fromdocname,
                                fromdocname,
                                'ITEM_NOT_FOUND',
                                node[0].deepcopy(),
                                node['reftarget'] + '??')
        # If target exists, try to create the reference
        if node['reftarget'] in env.traceability_all_items:
            item_info = env.traceability_all_items[node['reftarget']]
            try:
                new_node = make_refnode(app.builder,
                                        fromdocname,
                                        item_info['docname'],
                                        item_info['target']['refid'],
                                        node[0].deepcopy(),
                                        node['reftarget'])
            except NoUri:
                # ignore if no URI can be determined, e.g. for LaTeX output :(
                pass

        else:
            env.warn_node(
                'Traceability: item %s not found' % node['reftarget'], node)

        node.replace_self(new_node)