def match_observables(observables): # Remove empty observables observables = [observable for observable in observables if observable] extended_query = set(observables) | set(derive(observables)) added_entities = set() data = {"matches": [], "unknown": set(observables), "entities": [], "known": [], "neighbors": []} for o in Observable.objects(value__in=list(extended_query)): data['known'].append(o.info()) del_from_set(data['unknown'], o.value) for link, node in (o.incoming()): if isinstance(node, Observable): if (link.src.value not in extended_query or link.dst.value not in extended_query) and node.tags: data['neighbors'].append((link.info(), node.info())) for o, i in Indicator.search(extended_query): o = Observable.add_text(o) match = i.info() match.update({"observable": o.info(), "related": [], "suggested_tags": set()}) for nodes in i.neighbors().values(): for l, node in nodes: # add node name and link description to indicator node_data = {"entity": node.type, "name": node.name, "link_description": l.description or l.tag} match["related"].append(node_data) # uniquely add node information to related entitites if node.name not in added_entities: nodeinfo = node.info() nodeinfo['type'] = node.type data["entities"].append(nodeinfo) added_entities.add(node.name) o_tags = o.get_tags() [match["suggested_tags"].add(tag) for tag in node.generate_tags() if tag not in o_tags] data["matches"].append(match) del_from_set(data["unknown"], o.value) return data
o6 = Observable.add_text("http://hrakrue-home.de/87yte55/6t45eyv.exe") Link.connect(o6, bartalex_callback2) Link.connect(o6, bartalex).add_history("Queries") Link.connect(o6, dridex).add_history("Drops") o7 = Observable.add_text("http://kdojinyhb.wz.cz/87yte55/6t45eyv.exe") o8 = Observable.add_text("http://kdojinyhb.wz.cz/87yte55/6t45eyv.exe2") o9 = Observable.add_text("http://zeuscpanel.com/gate.php") o9.tag('zeus') t1 = Observable.add_text("http://toto.com") t2 = Observable.add_text("Http://tata.com") t3 = Observable.add_text("hxxp://tomchop[.]me") l = Link.connect(t1, t2) print "Links", Link.objects(src=t1) t2.delete() print "Links", Link.objects(src=t1) test = "http://soccersisters.net/mg.jpg" for i in Indicator.objects(): if i.match(test): for type, nodes in i.neighbors().items(): print " {}".format(type) for l, node in nodes: print {"type": type, "link": l.info(), "node": node.info()} print "Test with the following:" print o3.value print o7.value print t1.value
o6 = Observable.add_text("http://hrakrue-home.de/87yte55/6t45eyv.exe") Link.connect(o6, bartalex_callback2) Link.connect(o6, bartalex).add_history('testrun', 'Queries') Link.connect(o6, dridex).add_history('testrun', 'Drops') o7 = Observable.add_text("http://kdojinyhb.wz.cz/87yte55/6t45eyv.exe") o8 = Observable.add_text("http://kdojinyhb.wz.cz/87yte55/6t45eyv.exe2") o9 = Observable.add_text("http://zeuscpanel.com/gate.php") o9.tag('zeus') t1 = Observable.add_text("http://toto.com") t2 = Observable.add_text("Http://tata.com") t3 = Observable.add_text("hxxp://tomchop[.]me") l = Link.connect(t1, t2) print "Links", Link.objects(src=t1) t2.delete() print "Links", Link.objects(src=t1) test = "http://soccersisters.net/mg.jpg" for i in Indicator.objects(): if i.match(test): for type, nodes in i.neighbors().items(): print " {}".format(type) for l, node in nodes: print {"type": type, "link": l.info(), "node": node.info()} print "Test with the following:" print o3.value print o7.value print t1.value
def match_observables(observables, save_matches=False, fetch_neighbors=True): # Remove empty observables observables, extended_query = derive(observables) observables = list(observables) data = { "matches": [], "unknown": set(observables), "entities": {}, "known": [], "neighbors": [], } # add to "known" for o in Observable.objects(value__in=list(extended_query)): data['known'].append(o.info()) del_from_set(data['unknown'], o.value) if fetch_neighbors: for link, node in (o.incoming()): if isinstance(node, Observable): if (link.src.value not in extended_query or link.dst.value not in extended_query) and node.tags: data['neighbors'].append((link.info(), node.info())) for nodes in o.neighbors("Entity").values(): for l, node in nodes: # add node name and link description to indicator node_data = { "entity": node.type, "name": node.name, "link_description": l.description } # uniquely add node information to related entitites ent = data['entities'].get(node.name, node.info()) if 'matches' not in ent: ent['matches'] = {"observables": []} if 'observables' not in ent['matches']: ent['matches']['observables'] = [] info = node.info() o_info = o.info() info['matched_observable'] = { "value": o_info['value'], "tags": [t['name'] for t in o_info['tags']], "human_url": o_info['human_url'], "url": o_info['url'], "context": o_info['context'] } if info not in ent['matches']['observables']: ent['matches']['observables'].append(info) data['entities'][node.name] = ent # add to "matches" for o, i in Indicator.search(extended_query): if save_matches: o = Observable.add_text(o) else: o = Observable.guess_type(o)(value=o) try: o.validate() except ObservableValidationError: pass try: o = Observable.objects.get(value=o.value) except Exception: pass match = i.info() match.update({ "observable": o.info() if o.id else o.value, "related": [], "suggested_tags": set() }) for nodes in i.neighbors("Entity").values(): for l, node in nodes: # add node name and link description to indicator node_data = { "entity": node.type, "name": node.name, "link_description": l.description } match["related"].append(node_data) # uniquely add node information to related entitites ent = data['entities'].get(node.name, node.info()) if 'matches' not in ent: ent['matches'] = {"indicators": []} if 'indicators' not in ent['matches']: ent['matches']['indicators'] = [] info = i.info() info['matched_observable'] = o.value if info not in ent['matches']['indicators']: ent['matches']['indicators'].append(info) data['entities'][node.name] = ent o_tags = o.get_tags() for tag in node.generate_tags(): if tag not in o_tags: match["suggested_tags"].add(tag) data["matches"].append(match) data['entities'] = data['entities'].values() return data
def match_observables(observables, save_matches=False, fetch_neighbors=True): # Remove empty observables observables = [refang(observable) for observable in observables if observable] extended_query = set(observables) | set(derive(observables)) data = { "matches": [], "unknown": set(observables), "entities": {}, "known": [], "neighbors": [], } # add to "known" for o in Observable.objects(value__in=list(extended_query)): data['known'].append(o.info()) del_from_set(data['unknown'], o.value) if fetch_neighbors: for link, node in (o.incoming()): if isinstance(node, Observable): if (link.src.value not in extended_query or link.dst.value not in extended_query) and node.tags: data['neighbors'].append((link.info(), node.info())) for nodes in o.neighbors("Entity").values(): for l, node in nodes: # add node name and link description to indicator node_data = {"entity": node.type, "name": node.name, "link_description": l.description} # uniquely add node information to related entitites ent = data['entities'].get(node.name, node.info()) if 'matches' not in ent: ent['matches'] = {"observables": []} if 'observables' not in ent['matches']: ent['matches']['observables'] = [] info = node.info() o_info = o.info() info['matched_observable'] = { "value": o_info['value'], "tags": [t['name'] for t in o_info['tags']], "human_url": o_info['human_url'], "url": o_info['url'] } if info not in ent['matches']['observables']: ent['matches']['observables'].append(info) data['entities'][node.name] = ent # add to "matches" for o, i in Indicator.search(extended_query): if save_matches: o = Observable.add_text(o) else: o = Observable.guess_type(o)(value=o) try: o.validate() except ObservableValidationError: pass try: o = Observable.objects.get(value=o.value) except Exception: pass match = i.info() match.update({"observable": o.info(), "related": [], "suggested_tags": set()}) for nodes in i.neighbors("Entity").values(): for l, node in nodes: # add node name and link description to indicator node_data = {"entity": node.type, "name": node.name, "link_description": l.description} match["related"].append(node_data) # uniquely add node information to related entitites ent = data['entities'].get(node.name, node.info()) if 'matches' not in ent: ent['matches'] = {"indicators": []} if 'indicators' not in ent['matches']: ent['matches']['indicators'] = [] info = i.info() info['matched_observable'] = o.value if info not in ent['matches']['indicators']: ent['matches']['indicators'].append(info) data['entities'][node.name] = ent o_tags = o.get_tags() [match["suggested_tags"].add(tag) for tag in node.generate_tags() if tag not in o_tags] data["matches"].append(match) data['entities'] = data['entities'].values() return data