Ejemplo n.º 1
0
def test_sighting_all_required_properties():
    now = dt.datetime(2016, 4, 6, 20, 6, 37, tzinfo=pytz.utc)

    s = stix2.Sighting(
        type='sighting',
        id=SIGHTING_ID,
        created=now,
        modified=now,
        sighting_of_ref=INDICATOR_ID,
        where_sighted_refs=["identity--8cc7afd6-5455-4d2b-a736-e614ee631d99"])
    assert str(s) == EXPECTED_SIGHTING
Ejemplo n.º 2
0
def sighting_maker(**kwargs):
    sighting = stix2.Sighting(**kwargs)
    flag = itemtofile(sighting)
    return flag, sighting
    contact_information="*****@*****.**",
    labels=["Cyber Security"],
    sectors=["technology"]
)

indicator = stix2.Indicator(
    id="indicator--9299f726-ce06-492e-8472-2b52ccb53191",
    created_by_ref="identity--39012926-a052-44c4-ae48-caaf4a10ee6e",
    created="2017-02-27T13:57:10.515Z",
    modified="2017-02-27T13:57:10.515Z",
    name="Malicious URL",
    description="This URL is potentially associated with malicious activity and is listed on several blacklist sites.",
    labels=["malicious-activity"],
    pattern="[url:value = 'http://paypa1.banking.com']",
    valid_from="2015-06-29T09:10:15.915Z"
)

sighting = stix2.Sighting(
    id="sighting--8356e820-8080-4692-aa91-ecbe94006833",
    created_by_ref="identity--5206ba14-478f-4b0b-9a48-395f690c20a2",
    created="2017-02-28T19:37:11.213Z",
    modified="2017-02-28T19:37:11.213Z",
    first_seen="2017-02-27T21:37:11.213Z",
    last_seen="2017-02-27T21:37:11.213Z",
    count=1,
    sighting_of_ref="indicator--9299f726-ce06-492e-8472-2b52ccb53191",
    where_sighted_refs=["identity--5206ba14-478f-4b0b-9a48-395f690c20a2"]
)

bundle = stix2.Bundle(objects=[indicator, identityAlpha, identityBeta, sighting])
Ejemplo n.º 4
0
def stix_bundle(objs, rel=True, sight=True):
    objects = ()
    ids = []
    for o in objs:
        if not o.object_id.id in ids:
            ids.append(o.object_id.id)
        if o.object_type.name == "report":
            r = Report.objects.get(id=o.id)
            for i in r.object_refs.all().values_list("id", flat=True):
                if i in ids:
                    ids.append(i)
        if rel:
            rels = Relationship.objects.filter(
                Q(source_ref=o.object_id)\
                |Q(target_ref=o.object_id)\
            )
            lists = list(rels.values_list("object_id", flat=True)) + \
                    list(rels.values_list("source_ref", flat=True)) + \
                    list(rels.values_list("target_ref", flat=True))
            for i in lists:
                if not i in ids:
                    ids.append(i)
        if sight:
            sights = Sighting.objects.filter(
                Q(where_sighted_refs=o.object_id)\
                |Q(sighting_of_ref=o.object_id)\
            )
            lists = list(sights.values_list("object_id", flat=True)) + \
                    list(sights.values_list("sighting_of_ref", flat=True))
            for i in lists:
                if not i in ids:
                    ids += i
    oids = STIXObjectID.objects.filter(id__in=ids)
    for oid in oids:
        obj = myforms.get_obj_from_id(oid)
        if obj.object_type.name == 'identity':
            i = stix2.Identity(
                id=obj.object_id.object_id,
                name=obj.name,
                identity_class=obj.identity_class,
                description=obj.description,
                #sectors=[str(s.value) for s in obj.sectors.all()],
                sectors=[str(l.value) for l in obj.labels.all()],
                created=obj.created,
                modified=obj.modified,
            )
            objects += (i, )
        elif obj.object_type.name == 'attack-pattern':
            a = stix2.AttackPattern(
                id=obj.object_id.object_id,
                name=obj.name,
                description=obj.description,
                created=obj.created,
                modified=obj.modified,
            )
            objects += (a, )
        elif obj.object_type.name == 'malware':
            m = stix2.Malware(
                id=obj.object_id.object_id,
                name=obj.name,
                description=obj.description,
                labels=[str(l.value) for l in obj.labels.all()],
                created=obj.created,
                modified=obj.modified,
            )
            objects += (m, )
        elif obj.object_type.name == 'indicator':
            i = stix2.Indicator(
                id=obj.object_id.object_id,
                name=obj.name,
                description=obj.description,
                labels=[str(l.value) for l in obj.labels.all()],
                pattern=[str(p.value) for p in obj.pattern.all()],
                created=obj.created,
                modified=obj.modified,
            )
            objects += (i, )
        elif obj.object_type.name == 'threat-actor':
            t = stix2.ThreatActor(
                id=obj.object_id.object_id,
                name=obj.name,
                description=obj.description,
                labels=[str(l.value) for l in obj.labels.all()],
                aliases=[str(a.name) for a in obj.aliases.all()],
                created=obj.created,
                modified=obj.modified,
            )
            objects += (t, )
        elif obj.object_type.name == 'relationship':
            r = stix2.Relationship(
                id=obj.object_id.object_id,
                relationship_type=obj.relationship_type.name,
                description=obj.description,
                source_ref=obj.source_ref.object_id,
                target_ref=obj.target_ref.object_id,
                created=obj.created,
                modified=obj.modified,
            )
            objects += (r, )
        elif obj.object_type.name == 'sighting':
            s = stix2.Sighting(
                id=obj.object_id.object_id,
                sighting_of_ref=obj.sighting_of_ref.object_id,
                where_sighted_refs=[
                    str(w.object_id) for w in obj.where_sighted_refs.all()
                ],
                first_seen=obj.first_seen,
                last_seen=obj.last_seen,
                created=obj.created,
                modified=obj.modified,
            )
            objects += (s, )
        elif obj.object_type.name == 'report':
            r = stix2.Report(
                id=obj.object_id.object_id,
                labels=[str(l.value) for l in obj.labels.all()],
                name=obj.name,
                description=obj.description,
                published=obj.published,
                object_refs=[str(r.object_id) for r in obj.object_refs.all()],
                created=obj.created,
                modified=obj.modified,
            )
            objects += (r, )
    bundle = stix2.Bundle(*objects)
    return bundle
    last_observed="2017-02-27T21:37:11.213Z",
    number_observed=1,
    created_by_ref="identity--7865b6d2-a4af-45c5-b582-afe5ec376c33",
    objects={
        "0": {
            "type": "windows-registry-key",
            "key":
            "HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Services\\WSALG2"
        }
    })

sighting = stix2.Sighting(
    id="sighting--779c4ae8-e134-4180-baa4-03141095d971",
    created_by_ref="identity--987eeee1-413a-44ac-96cc-0a8acdcc2f2c",
    created="2017-02-28T19:37:11.213Z",
    modified="2017-02-28T19:37:11.213Z",
    first_seen="2017-02-28T19:07:24.856Z",
    last_seen="2017-02-28T19:07:24.856Z",
    count=1,
    sighting_of_ref="malware--ae560258-a5cb-4be8-8f05-013d6712295f",
    where_sighted_refs=["identity--987eeee1-413a-44ac-96cc-0a8acdcc2f2c"],
    observed_data_refs=[
        "observed-data--cf8eaa41-6f4c-482e-89b9-9cd2d6a83cb1",
        "observed-data--a0d34360-66ad-4977-b255-d9e1080421c4"
    ])

bundle = stix2.Bundle(objects=[
    identityPym, identityOscorp, malware, observedDataFile, observedDataRegKey,
    sighting
])
Ejemplo n.º 6
0
                                      interoperability=True)
 campaign = stix2.Campaign(**CAMPAIGN_KWARGS, interoperability=True)
 course_of_action = stix2.CourseOfAction(**COURSE_OF_ACTION_KWARGS,
                                         interoperability=True)
 identity = stix2.Identity(**IDENTITY_KWARGS, interoperability=True)
 indicator = stix2.Indicator(**INDICATOR_KWARGS, interoperability=True)
 intrusion_set = stix2.IntrusionSet(**INTRUSION_SET_KWARGS,
                                    interoperability=True)
 malware = stix2.Malware(**MALWARE_KWARGS, interoperability=True)
 marking_definition = stix2.MarkingDefinition(**MARKING_DEFINITION_KWARGS,
                                              interoperability=True)
 observed_data = stix2.ObservedData(**OBSERVED_DATA_KWARGS,
                                    interoperability=True)
 relationship = stix2.Relationship(**RELATIONSHIP_KWARGS,
                                   interoperability=True)
 sighting = stix2.Sighting(**SIGHTING_KWARGS, interoperability=True)
 threat_actor = stix2.ThreatActor(**THREAT_ACTOR_KWARGS,
                                  interoperability=True)
 tool = stix2.Tool(**TOOL_KWARGS)  #, interoperability=True),
 vulnerability = stix2.Vulnerability(**VULNERABILITY_KWARGS,
                                     interoperability=True)
 report = stix2.Report(**REPORT_KWARGS, interoperability=True)
 bundle = stix2.Bundle(**BUNDLE_KWARGS,
                       interoperability=True,
                       objects=[
                           attack_pattern, campaign, course_of_action,
                           identity, indicator, intrusion_set, malware,
                           marking_definition, observed_data, tool,
                           relationship, sighting, threat_actor,
                           vulnerability, report
                       ])
Ejemplo n.º 7
0
def stix_bundle(rep):
    objects = ()
    for ref in rep.object_refs.all():
        obj = myforms.get_obj_from_id(ref)
        if obj.object_type.name == 'identity':
            i = stix2.Identity(
                id=obj.object_id.object_id,
                name=obj.name,
                identity_class=obj.identity_class,
                description=obj.description,
                #sectors=[str(s.value) for s in obj.sectors.all()],
                sectors=[str(l.value) for l in obj.labels.all()],
                created=obj.created,
                modified=obj.modified,
            )
            objects += (i, )
        elif obj.object_type.name == 'attack-pattern':
            a = stix2.AttackPattern(
                id=obj.object_id.object_id,
                name=obj.name,
                description=obj.description,
                created=obj.created,
                modified=obj.modified,
            )
            objects += (a, )
        elif obj.object_type.name == 'malware':
            m = stix2.Malware(
                id=obj.object_id.object_id,
                name=obj.name,
                description=obj.description,
                labels=[str(l.value) for l in obj.labels.all()],
                created=obj.created,
                modified=obj.modified,
            )
            objects += (m, )
        elif obj.object_type.name == 'threat-actor':
            t = stix2.ThreatActor(
                id=obj.object_id.object_id,
                name=obj.name,
                description=obj.description,
                labels=[str(l.value) for l in obj.labels.all()],
                aliases=[str(a.name) for a in obj.aliases.all()],
                created=obj.created,
                modified=obj.modified,
            )
            objects += (t, )
        elif obj.object_type.name == 'relationship':
            r = stix2.Relationship(
                id=obj.object_id.object_id,
                relationship_type=obj.relationship_type.name,
                description=obj.description,
                source_ref=obj.source_ref.object_id,
                target_ref=obj.target_ref.object_id,
                created=obj.created,
                modified=obj.modified,
            )
            objects += (r, )
        elif obj.object_type.name == 'sighting':
            s = stix2.Sighting(
                id=obj.object_id.object_id,
                sighting_of_ref=obj.sighting_of_ref.object_id,
                where_sighted_refs=[
                    str(w.object_id) for w in obj.where_sighted_refs.all()
                ],
                first_seen=obj.first_seen,
                last_seen=obj.last_seen,
                created=obj.created,
                modified=obj.modified,
            )
            objects += (s, )
    report = stix2.Report(
        id=rep.object_id.object_id,
        labels=[str(l.value) for l in rep.labels.all()],
        name=rep.name,
        description=rep.description,
        published=rep.published,
        object_refs=[str(r.object_id) for r in rep.object_refs.all()],
        created=obj.created,
        modified=obj.modified,
    )
    objects += (report, )
    bundle = stix2.Bundle(*objects)
    return bundle
Ejemplo n.º 8
0
def stix_bundle(objs, mask=True):
    objects = ()
    for obj in objs:
        oid = obj.object_id.object_id
        dscr = ""
        if not mask and hasattr(obj, "description"):
            dscr = obj.description
        if obj.object_type.name == 'attack-pattern':
            a = stix2.AttackPattern(
                id=oid,
                name=obj.name,
                description=dscr,
                created=obj.created,
                modified=obj.modified,
                kill_chain_phases=stix2killchain(obj),
            )
            objects += (a, )
        elif obj.object_type.name == 'campaign':
            c = stix2.Campaign(
                id=oid,
                name=obj.name,
                description=dscr,
                aliases=[str(a.name) for a in obj.aliases.all()],
                created=obj.created,
                modified=obj.modified,
                first_seen=obj.first_seen,
                last_seen=obj.last_seen,
            )
            objects += (c, )
        elif obj.object_type.name == 'course-of-action':
            c = stix2.CourseOfAction(
                id=oid,
                name=obj.name,
                description=dscr,
                created=obj.created,
                modified=obj.modified,
            )
            objects += (c, )
        elif obj.object_type.name == 'identity':
            name = obj.name
            if mask:
                name = oid
                label = obj.labels.all()
                if label.count() >= 1:
                    name = str(obj.id)
                    if label[0].alias:
                        name += '-' + label[0].alias
                    else:
                        name += '-' + label[0].value
            i = stix2.Identity(
                id=oid,
                name=name,
                identity_class=obj.identity_class,
                description=dscr,
                sectors=[str(s.value) for s in obj.sectors.all()],
                labels=[str(l.value) for l in obj.labels.all()],
                created=obj.created,
                modified=obj.modified,
            )
            objects += (i, )
        elif obj.object_type.name == 'indicator':
            pattern = "[]"
            if not mask and obj.pattern:
                pattern = obj.pattern.pattern
            i = stix2.Indicator(
                id=oid,
                name=obj.name,
                description=dscr,
                labels=[str(l.value) for l in obj.labels.all()],
                pattern=pattern,
                created=obj.created,
                modified=obj.modified,
                valid_from=obj.valid_from,
                valid_until=obj.valid_until,
            )
            objects += (i, )
        elif obj.object_type.name == 'intrusion-set':
            i = stix2.IntrusionSet(
                id=oid,
                name=obj.name,
                description=dscr,
                aliases=[str(a.name) for a in obj.aliases.all()],
                created=obj.created,
                modified=obj.modified,
                first_seen=obj.first_seen,
                #last_seen=obj.last_seen,
            )
            objects += (i, )
        elif obj.object_type.name == 'malware':
            m = stix2.Malware(
                id=oid,
                name=obj.name,
                description=dscr,
                labels=[str(l.value) for l in obj.labels.all()],
                created=obj.created,
                modified=obj.modified,
                kill_chain_phases=stix2killchain(obj),
            )
            objects += (m, )
        elif obj.object_type.name == 'observed-data':
            obs = {}
            for o in obj.observable_objects.all():
                ob = None
                if o.type.name == "file":
                    f = FileObject.objects.get(id=o.id)
                    ob = stix2.File(name=f.name)
                elif o.type.name == "ipv4-addr":
                    i = IPv4AddressObject.objects.get(id=o.id)
                    ob = stix2.IPv4Address(value=i.value)
                elif o.type.name == "url":
                    u = URLObject.objects.get(id=o.id)
                    ob = stix2.URL(value=u.value)
                elif o.type.name == "domain-name":
                    dn = DomainNameObject.objects.get(id=o.id)
                    ob = stix2.DomainName(value=dn.value)
                if ob and not mask:
                    obs[str(o.id)] = json.loads(str(ob))
            od = stix2.ObservedData(
                id=oid,
                created=obj.created,
                modified=obj.modified,
                first_observed=obj.first_observed,
                last_observed=obj.last_observed,
                number_observed=obj.number_observed,
                objects=obs,
            )
            objects += (od, )
        elif obj.object_type.name == 'report':
            created_by = None
            if obj.created_by_ref:
                created_by = obj.created_by_ref.object_id
            r = stix2.Report(
                id=oid,
                labels=[str(l.value) for l in obj.labels.all()],
                name=obj.name,
                description=dscr,
                published=obj.published,
                object_refs=[str(r.object_id) for r in obj.object_refs.all()],
                created_by_ref=created_by,
                created=obj.created,
                modified=obj.modified,
            )
            objects += (r, )
        elif obj.object_type.name == 'threat-actor':
            t = stix2.ThreatActor(
                id=oid,
                name=obj.name,
                description=dscr,
                labels=[str(l.value) for l in obj.labels.all()],
                aliases=[str(a.name) for a in obj.aliases.all()],
                created=obj.created,
                modified=obj.modified,
            )
            objects += (t, )
        elif obj.object_type.name == 'tool':
            t = stix2.Tool(
                id=oid,
                name=obj.name,
                description=dscr,
                labels=[str(l.value) for l in obj.labels.all()],
                created=obj.created,
                modified=obj.modified,
                kill_chain_phases=stix2killchain(obj),
            )
            objects += (t, )
        elif obj.object_type.name == 'vulnerability':
            v = stix2.Vulnerability(
                id=oid,
                name=obj.name,
                description=dscr,
                created=obj.created,
                modified=obj.modified,
            )
            objects += (v, )
        elif obj.object_type.name == 'relationship':
            r = stix2.Relationship(
                id=oid,
                relationship_type=obj.relationship_type.name,
                description=dscr,
                source_ref=obj.source_ref.object_id,
                target_ref=obj.target_ref.object_id,
                created=obj.created,
                modified=obj.modified,
            )
            objects += (r, )
        elif obj.object_type.name == 'sighting':
            s = stix2.Sighting(
                id=oid,
                sighting_of_ref=obj.sighting_of_ref.object_id,
                where_sighted_refs=[
                    str(w.object_id.object_id)
                    for w in obj.where_sighted_refs.all()
                ],
                observed_data_refs=[
                    str(od.object_id.object_id)
                    for od in obj.observed_data_refs.all()
                ],
                first_seen=obj.first_seen,
                last_seen=obj.last_seen,
                created=obj.created,
                modified=obj.modified,
            )
            objects += (s, )
    bundle = stix2.Bundle(*objects)
    return bundle