def get_package_bean_v2(stix_file_path): try: with codecs.open(stix_file_path, 'r', encoding='utf-8') as fp: content = fp.read() doc = json.loads(content) package_bean = StixFiles.PackageBean() package_bean.package_id = doc['id'] if ('spec_version' in doc): package_bean.version = doc['spec_version'] else: package_bean.version = '2.1' stip_sns = _is_produced_by_stip_sns_v2(doc) package_bean.related_packages = None produced_str = None if stip_sns: package_bean.package_name = stip_sns['name'] package_bean.description = stip_sns['description'] package_bean.is_created_by_sns = True package_bean.sns_type = _get_stip_sns_type_v2(stip_sns) if package_bean.sns_type != StixFiles.STIP_SNS_TYPE_V2_POST: package_bean.is_post_sns = False package_bean.related_packages = [] if const.STIP_STIX2_PROP_OBJECT_REF in stip_sns: package_bean.related_packages.append(stip_sns[const.STIP_STIX2_PROP_OBJECT_REF]) if const.STIP_STIX2_PROP_ATTACHMENT_REFS in stip_sns: for ref in stip_sns[const.STIP_STIX2_PROP_ATTACHMENT_REFS]: package_bean.related_packages.append(ref['bundle']) if len(package_bean.related_packages) == 0: package_bean.related_packages = None produced_str = stip_sns['created'] else: package_bean.package_name = None package_bean.description = None report = _get_report_object(doc) if report: package_bean.package_name = report['name'] if 'description' in report: package_bean.description = report['description'] produced_str = report['created'] if not package_bean.package_name: package_bean.package_name = package_bean.package_id if not package_bean.description: package_bean.description = 'Post: %s' % (package_bean.package_id) package_bean.is_created_by_sns = False package_bean.is_post_sns = True _set_stix_bean_from_doc_v2(package_bean, doc) if produced_str: package_bean.produced = stix2_str_to_datetime(produced_str) else: package_bean.produced = datetime.datetime.now(tz=pytz.utc) return package_bean except Exception as e: raise Exception('Can\'t parse STIX. ' + e.message)
def get_package_bean_v1(stix_file_path): doc = STIXPackage.from_xml(stix_file_path) try: package_bean = StixFiles.PackageBean() package_bean.is_post_sns = True package_bean.is_created_by_sns = False sns_type = None if _is_produced_by_stip_sns_v1(doc): package_bean.is_created_by_sns = True sns_type = _get_stip_sns_type_v1(doc) if sns_type != StixFiles.STIP_SNS_TYPE_ORIGIN: package_bean.is_post_sns = False try: package_bean.related_packages = [] for related_package in doc.related_packages: package_bean.related_packages.append(related_package.item.id_) except TypeError: package_bean.related_packages = None package_bean.package_id = doc.id_ package_bean.version = doc._version package_bean.produced = _get_produced_time_stix_1_x(doc) package_bean.package_name = doc.stix_header.title package_bean.sns_type = sns_type try: package_bean.description = doc.stix_header.description.value if package_bean.description is None: package_bean.description = '' except BaseException: package_bean.description = '' _set_stix_bean_from_doc_v1(package_bean, doc) if package_bean.sns_user_name == '': package_bean.sns_user_name = _get_sns_user_name_from_instance( package_bean.sns_instance) return package_bean except Exception: pass
def get_package_bean(stix_file_path): package_bean = StixFiles.PackageBean() #STIX 1.1 parse try: doc = STIXPackage.from_xml(stix_file_path) package_bean.is_post_sns = True package_bean.is_created_by_sns = False sns_type = None #S-TIP SNS で作成された STIX であるか? if is_produced_by_stip_sns(doc) == True: #SNS 産である package_bean.is_created_by_sns = True sns_type = get_stip_sns_type(doc) #origin 投稿以外は表示しない if sns_type != StixFiles.STIP_SNS_TYPE_ORIGIN: package_bean.is_post_sns = False #realted_packages探す try: package_bean.related_packages = [] for related_package in doc.related_packages: package_bean.related_packages.append(related_package.item.id_) except TypeError: package_bean.related_packages = None package_bean.package_id = doc.id_ package_bean.version = doc._version package_bean.produced = get_produced_time_stix_1_x(doc) package_bean.package_name = doc.stix_header.title package_bean.sns_type = sns_type try: package_bean.description = doc.stix_header.description.value if package_bean.description is None: package_bean.description = '' except: package_bean.description = '' #S-TIP SNS 作成の STIX から pacakge_bean の値をセットする set_stix_bean_from_doc(package_bean, doc) #SNS 産以外は sns_user_name が設定されていないので instance 名から取得する if package_bean.sns_user_name == '': package_bean.sns_user_name = get_sns_user_name_from_instance( package_bean.sns_instance) return package_bean except Exception: pass #STIX 2.0 parse try: with codecs.open(stix_file_path, 'r', 'utf-8') as fp: content = fp.read() doc = json.loads(content) package_bean.package_name = None #最初に見つかったtypeがreportのnameをpackage_nameとする #また、STIX 2.0 では package の timestampの格納場所がないのでNoneとする produced_str = None for d in doc['objects']: if d['type'] == 'report': package_bean.package_name = d['name'] produced_str = d['created'] package_bean.package_id = doc['id'] if doc.has_key('spec_version') == True: package_bean.version = doc['spec_version'] else: #STIX 2.1 には spec_version がない package_bean.version = '2.1' #Produced Time は Report の produced time if produced_str is not None: package_bean.produced = stix2_str_to_datetime(produced_str) else: package_bean.produced = datetime.datetime.now() package_bean.is_post_sns = True package_bean.is_created_by_sns = False package_bean.related_packages = None return package_bean except Exception as e: traceback.print_exc() raise Exception('Can\'t parse STIX. ' + e.message)