def _regist_stix(content, community, via): stix_file_path = tempfile.mktemp(suffix='.xml') if not isinstance(content, str): content = content.decode('utf-8') with open(stix_file_path, 'w+t', encoding='utf-8') as fp: fp.write(content) regist(stix_file_path, community, via) return
def poll_11(self): auth_credentials_dict = self._get_auth_credentials_dict() self._client.set_auth_credentials(auth_credentials_dict) try: poll_parameters = tm11.PollParameters() poll_request = tm11.PollRequest( message_id=tm11.generate_message_id(), collection_name=self._collection_name, exclusive_begin_timestamp_label=self._start, inclusive_end_timestamp_label=self._end, poll_parameters=poll_parameters, ) last_requested = datetime.datetime.now(pytz.utc) http_resp = self._client.call_taxii_service2( self._address, self._path, const.VID_TAXII_XML_11, poll_request.to_xml(), port=self._port) taxii_message = libtaxii.get_message_from_http_response( http_resp, poll_request.message_id) try: if taxii_message.content_blocks is None: return 0 except AttributeError: return 0 count = 0 for cb in taxii_message.content_blocks: _, stix_file_path = tempfile.mkstemp(suffix='.xml') with open(stix_file_path, 'wb+') as fp: fp.write(cb.content) try: from ctirs.core.stix.regist import regist if self._community is not None: regist(stix_file_path, self._community, self._via) count += 1 except BaseException: traceback.print_exc() self._taxii.last_requested = last_requested self._taxii.save() return count finally: if 'cert_file' in auth_credentials_dict: try: os.remove(auth_credentials_dict['cert_file']) except Exception: pass if 'key_file' in auth_credentials_dict: try: os.remove(auth_credentials_dict['key_file']) except Exception: pass
def _regist_stix(content, community, via): # stixファイルを一時ファイルに出力 stix_file_path = tempfile.mktemp(suffix='.xml') with open(stix_file_path, 'w+t', encoding='utf-8') as fp: # cb.contentがstixの中身(contentの型はstr) fp.write(content.decode('utf-8')) # 登録 regist(stix_file_path, community, via) return
def poll_20(self): self.debug_print('>>> poll_20:enter') url = self.get_taxii20_objects_url() if url is None: self.debug_print('>>> poll_20: No base_url') return 0 headers = { 'Accept' : 'application/vnd.oasis.stix+json; version=2.0', } self.debug_print('poll_20: URL: %s' % (url)) self.debug_print('poll_20: request headers:%s' % (json.dumps(headers,indent=4))) resp = requests.get( url, headers = headers, auth = (self._username,self._password), verify = False, proxies = self._proxies ) self.debug_print('poll_20: response status_code:%s' % (resp.status_code)) #self.debug_print('poll_20: response headers') #for k,v in resp.headers.iteritems(): # self.debug_print('%s: %s' % (k,v)) #self.debug_print('----') #self.debug_print('poll_20: request resp.json():%s' % (json.dumps(resp.json(),indent=4))) #stix のリストで返却される count = 0 stixes = [] js =resp.json() #list で返却される場合と単体の場合がある if isinstance(js, list) == True: stixes = js else: stixes = [js] for j in stixes: #1つずつ file に保存して regist する #stixファイルを一時ファイルに出力 _,stix_file_path = tempfile.mkstemp(suffix='.json') with open(stix_file_path,'wb+') as fp: fp.write(json.dumps(j,indent=4)) #登録 try: from ctirs.core.stix.regist import regist if self._community is not None: regist(stix_file_path,self._community,self._via) count += 1 except: #taxiiの場合は途中で失敗しても処理を継続する traceback.print_exc() pass last_requested = datetime.datetime.now(pytz.utc) self._taxii.last_requested = last_requested self._taxii.save() return count
def poll_11(self): #request xml作成 poll_parameters = tm11.PollParameters() poll_request = tm11.PollRequest( message_id=tm11.generate_message_id(), collection_name=self._collection_name, exclusive_begin_timestamp_label=self._start, inclusive_end_timestamp_label=self._end, poll_parameters=poll_parameters, ) last_requested = datetime.datetime.now(pytz.utc) #from xml.dom.minidom import parseString #print( parseString(poll_request.to_xml()).toprettyxml(encoding='utf-8')) #request http_resp = self._client.call_taxii_service2( self._address, self._path, const.VID_TAXII_XML_11, poll_request.to_xml(), port=self._port) #taxii_message抽出 taxii_message = libtaxii.get_message_from_http_response(http_resp, poll_request.message_id) #content_blocks が None or 存在しない場合は登録ししない try: if taxii_message.content_blocks is None: return 0 except AttributeError: return 0 #content_blocksごとに登録処理 count = 0 for cb in taxii_message.content_blocks: #stixファイルを一時ファイルに出力 _,stix_file_path = tempfile.mkstemp(suffix='.xml') with open(stix_file_path,'wb+') as fp: #cb.contentがstixの中身 fp.write(cb.content) #登録 try: from ctirs.core.stix.regist import regist if self._community is not None: regist(stix_file_path,self._community,self._via) count += 1 except: #taxiiの場合は途中で失敗しても処理を継続する traceback.print_exc() #last_requested更新 self._taxii.last_requested = last_requested self._taxii.save() return count
def post_write_collection(content): debug_print('>>>enter post_write_collection') debug_print('>>>--- content start') debug_print(str(content)) debug_print('>>>--- content end') #stixファイルを一時ファイルに出力 _, stix_file_path = tempfile.mkstemp(suffix='.json') with open(stix_file_path, 'wb+') as fp: fp.write(content) print '>>>stix_file_path: ' + str(stix_file_path) #RSに登録 try: from ctirs.core.stix.regist import regist if _community is not None: regist(stix_file_path, _community, _via) print '>>>regist success:' print '>>>regist _community: ' + str(_community) print '>>>regist _via: ' + str(_via) except Exception as e: traceback.print_exc() raise e #stix解析し、返却値を作成する j = json.loads(content) r = {} r['id'] = j['id'] r['status'] = 'complete' r['request_timestamp'] = datetime.datetime.now( pytz.utc).strftime('%Y-%m-%dT%H:%M:%S.%fZ') total_count = 0 failure_count = 0 success_count = 0 pending_count = 0 successes = [] for object_ in j['objects']: total_count += 1 success_count += 1 successes.append(object_['id']) r['total_count'] = total_count r['success_count'] = success_count r['failure_count'] = failure_count r['pending_count'] = pending_count r['successes'] = successes return r
def create_content_block(self, content_block_entity, collection_ids, service_id): # content_balock_entity.contentに中身が格納される # with open('/home/terra/work/libtaxii/server/output/output_BabyTiger.xml','w', encoding='utf-8') as fp: # fp.write(content_block_entity.content) # ctirs に registする stix_file_path = tempfile.mktemp(suffix='.xml') with open(stix_file_path, 'wb+') as fp: fp.write(content_block_entity.content) try: from ctirs.core.stix.regist import regist regist(stix_file_path, self.community, self.via) except Exception as e: os.remove(stix_file_path) traceback.print_exc() raise e return content_block_entity
def upload_common(request, via): # 引数取得 community_id = get_upload_upload_community_id(request) community_name = get_upload_upload_community_name(request) package_name = get_upload_upload_package_name(request) stix = get_sharing_stix(request)[0] # stixファイルを一時ファイルに出力 stix_file_path = tempfile.mktemp(suffix='.xml') with open(stix_file_path, 'wb+') as fp: for chunk in stix: fp.write(chunk) # Communityドキュメントを取得する if community_id is not None: community = Communities.objects.get(id=community_id) elif community_name is not None: community = Communities.objects.get(name=community_name) else: raise Exception('Invalid community id or name.') # 登録処理 regist(stix_file_path, community, via, package_name=package_name) return
def poll_20(self): url = self.get_taxii20_objects_url() if url is None: return 0 headers = { 'Accept': 'application/vnd.oasis.stix+json; version=2.0', } resp = requests.get(url, headers=headers, auth=(self._username, self._password), verify=False, proxies=self._proxies) count = 0 stixes = [] js = resp.json() if isinstance(js, list): stixes = js else: stixes = [js] for j in stixes: _, stix_file_path = tempfile.mkstemp(suffix='.json') with open(stix_file_path, 'wb+') as fp: fp.write(json.dumps(j, indent=4)) try: from ctirs.core.stix.regist import regist if self._community is not None: regist(stix_file_path, self._community, self._via) count += 1 except BaseException: traceback.print_exc() last_requested = datetime.datetime.now(pytz.utc) self._taxii.last_requested = last_requested self._taxii.save() return count
def poll_20(taxii_client, protocol_version='2.0'): try: count = 0 fd = None js = _get_json_response(taxii_client, protocol_version) if 'objects' not in js: return 0 fd, stix_file_path = tempfile.mkstemp(suffix='.json') if protocol_version == '2.0': with open(stix_file_path, 'wb+') as fp: fp.write(json.dumps(js, indent=4).encode('utf-8')) elif protocol_version == '2.1': objects = _get_objects_21(taxii_client, protocol_version, [], js) if len(objects) == 0: return 0 bundle = Bundle(objects) with open(stix_file_path, 'wb+') as fp: fp.write(bundle.serialize(pretty=True).encode('utf-8')) from ctirs.core.stix.regist import regist if taxii_client._community is not None: regist(stix_file_path, taxii_client._community, taxii_client._via) count += 1 last_requested = datetime.datetime.now(pytz.utc) taxii_client._taxii.last_requested = last_requested taxii_client._taxii.save() return count except BaseException as e: traceback.print_exc() raise e finally: if fd is not None: try: os.close(fd) except Exception: pass
def post_language_contents(request, object_ref, ctirs_auth_user): try: j = json.loads(request.body) # S-TIP Identity 作成する stip_identity = _get_stip_identname(request.user) # bundle 作成 bundle = Bundle(stip_identity) # 参照元の obejct を取得 object_ = get_object(object_ref) if object_ is None: return error( Exception('No document. (object_ref=%s)' % (object_ref))) for language_content in j['language_contents']: selector_str = language_content['selector'] content_value = language_content['content'] language = language_content['language'] try: selector_elems = selector_str.split('.') last_elem = object_ # selector の 要素をチェックする if len(selector_elems) == 1: # selector が . でつながられていない場合 last_selector = selector_str last_elem = is_exist_objects(selector_str, last_elem) else: # selector が . でつながられている場合は最後までたどる for selector in selector_elems[:-1]: last_selector = selector last_elem = is_exist_objects(selector, last_elem) if last_elem is None: raise Exception('selector is invalid: ' + str(selector_str)) if isinstance(last_elem, list): # 空要素で初期化し、該当 index の要素だけ上書きする lc_lists = [''] * len(last_elem) lc_lists[get_list_index_from_selector( selector_elems[-1])] = content_value content = lc_lists selector = '.'.join(selector_elems[:-1]) elif isinstance(last_elem, dict): # 空辞書で初期化し、該当 index の要素だけ上書きする content = {} content[selector_elems[-1]] = content_value selector = '.'.join(selector_elems[:-1]) else: # list ではない content = content_value selector = last_selector except Exception as e: traceback.print_exc() raise e contents = {} contents[language] = {selector: content} language_content = LanguageContent( created_by_ref=stip_identity, object_ref=object_ref, object_modified=object_['modified'], contents=contents) bundle.objects.append(language_content) # viaを取得 via = Vias.get_via_rest_api_upload(uploader=ctirs_auth_user.id) community = Communities.get_default_community() # stixファイルを一時ファイルに出力 stix_file_path = tempfile.mktemp(suffix='.json') with open(stix_file_path, 'wb+') as fp: fp.write(bundle.serialize(indent=4, ensure_ascii=False)).encode() # 登録処理 regist(stix_file_path, community, via) resp = get_normal_response_json() bundle_json = json.loads(str(bundle)) resp['data'] = {'bundle': bundle_json} return JsonResponse(resp, status=201, safe=False) except Exception as e: traceback.print_exc() return error(e)