You need to enable JavaScript to run this app. 如何将STIX 2对象转换为适配FastAPI的Pydantic模型
优惠活动
大模型
产品
解决方案
定价
更多

如何将STIX 2对象转换为适配FastAPI的Pydantic模型

STIX2对象转Pydantic模型问题

我正在使用FastAPI,需要将来自MITRE ATT&CK的不同STIX 2对象转换为对应的等效Pydantic模型,以便将其作为JSON响应返回。

我们以AttackPattern(攻击模式)对象为例,导入语句如下:

from stix2.v20.sdo import AttackPattern

AttackPattern类定义

该类的官方定义如下:

class AttackPattern(_DomainObject):
    """如需了解该对象属性的更多细节,可以参考STIX 2.0官方规范
    """

    _type = 'attack-pattern'
    _properties = OrderedDict([
        ('type', TypeProperty(_type, spec_version='2.0')),
        ('id', IDProperty(_type, spec_version='2.0')),
        ('created_by_ref', ReferenceProperty(valid_types='identity', spec_version='2.0')),
        ('created', TimestampProperty(default=lambda: NOW, precision='millisecond')),
        ('modified', TimestampProperty(default=lambda: NOW, precision='millisecond')),
        ('name', StringProperty(required=True)),
        ('description', StringProperty()),
        ('kill_chain_phases', ListProperty(KillChainPhase)),
        ('revoked', BooleanProperty(default=lambda: False)),
        ('labels', ListProperty(StringProperty)),
        ('external_references', ListProperty(ExternalReference)),
        ('object_marking_refs', ListProperty(ReferenceProperty(valid_types='marking-definition', spec_version='2.0'))),
        ('granular_markings', ListProperty(GranularMarking)),
    ])

    @property
    def properties(self):
        return self._properties

序列化后的JSON结构示例

该对象序列化后的JSON格式如下:

{
    "created": "2021-04-13T12:45:26.506Z",
    "created_by_ref": "identity--c78cb6e5-0c4b-4611-8297-d1b8b55e40b5",
    "description": "攻击者可能会尝试获取远程系统及其外设的详细信息,例如型号、角色和配置。攻击者可以利用远程系统信息发现得到的信息来锁定目标、规划后续攻击行为。例如,系统的运行角色和型号信息可以帮助攻击者判断该系统是否符合其攻击目标。此外,系统配置还可以用于确定后续使用的技术范围。系统信息请求通常使用自动化和管理协议实现,在正常运行过程中通常会被供应商软件自动请求。这类信息可用于调整管理操作,例如程序下载、系统或模块固件更新。攻击者可以通过直接调用系统API来获取同样的信息。",
    "external_references": [
        {
            "external_id": "T0888",
            "source_name": "mitre-ics-attack",
            "url": "https://collaborate.mitre.org/attackics/index.php/Technique/T0888"
        }
    ],
    "id": "attack-pattern--2fedbe69-581f-447d-8a78-32ee7db939a9",
    "kill_chain_phases": [
        {
            "kill_chain_name": "mitre-ics-attack",
            "phase_name": "discovery-ics"
        }
    ],
    "modified": "2021-04-13T12:45:26.506Z",
    "name": "远程系统信息发现",
    "object_marking_refs": [
        "marking-definition--fa42a846-8d90-4e51-bc29-71d5b4802168"
    ],
    "type": "attack-pattern",
    "x_mitre_data_sources": [
        "网络协议分析",
        "数据包捕获"
    ],
    "x_mitre_platforms": [
        "安全仪表系统/保护继电器",
        "现场控制器/RTU/PLC/IED"
    ]
}

核心问题

可以看到该对象还包含部分嵌套对象属性。请问基于AttackPattern对象本身或其__dict__表示,生成等效Pydantic模型的最佳方案是什么?

现有调试代码

我已经写了简单的测试类进行调试:

from stix2.v20.sdo import AttackPattern

from mitre.attack.ics.mitre_attack_ics import MitreAttackICS

if __name__ == '__main__':
    mitre_attack_ics = MitreAttackICS()

    techniques: list[AttackPattern] = mitre_attack_ics.get_techniques()
    technique = techniques[0]

    print(technique.serialize(sort_keys=True, indent=4))
    # print(technique.__dict__)
    # print(technique.object_properties())
    # print(technique.properties)

内容的提问来源于stack exchange,提问作者1Z10

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 15:24:04