Class: ActiveStix::AttackPattern

Inherits:
ApplicationRecord show all
Defined in:
app/models/active_stix/attack_pattern.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.expected_keysObject



26
27
28
29
30
31
32
# File 'app/models/active_stix/attack_pattern.rb', line 26

def self.expected_keys
  [
      'external_references',
      'kill_chain_phases',
      'object_marking_refs'
  ]
end

.external_references(attack_pattern, obj) ⇒ Object



47
48
49
50
51
52
# File 'app/models/active_stix/attack_pattern.rb', line 47

def self.external_references(attack_pattern, obj)
  obj['external_references'].each do |er|
    external_reference = ActiveStix::ExternalReference.ingest_json(er, obj['id'])
    attack_pattern.external_references << external_reference unless ActiveStix::ReferenceItem.find_by(external_reference_id: external_reference.id, referrer_id: attack_pattern.id)
  end
end

.ingest_json(obj) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/active_stix/attack_pattern.rb', line 34

def self.ingest_json(obj)
  attack_pattern = find_or_create_by(stix_id: obj['id'], name: obj['name'], description: obj['description'])

  expected_keys.each do |expected_key|
    if obj.has_key?(expected_key)
      send(expected_key, *[attack_pattern, obj])
    end
  end

  attack_pattern.save
  attack_pattern
end

.kill_chain_phases(attack_pattern, obj) ⇒ Object



54
55
56
57
58
59
# File 'app/models/active_stix/attack_pattern.rb', line 54

def self.kill_chain_phases(attack_pattern, obj)
  obj['kill_chain_phases'].each do |kc|
    phase = ActiveStix::Phase.ingest_json(kc)
    attack_pattern.phases << phase unless ActiveStix::KillChainPhase.find_by(attack_pattern_ref: attack_pattern.stix_id, phase_id: phase.id)
  end
end

.object_marking_refs(attack_pattern, obj) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'app/models/active_stix/attack_pattern.rb', line 61

def self.object_marking_refs(attack_pattern, obj)
  # obj['object_marking_refs'].each do |mr|
  #   marking_definition = ActiveStix::MarkingDefinition.create_by_id(mr)
  #
  #   #ensure no duplicate entries
  #   attack_pattern.marking_definitions << marking_definition unless ActiveStix::ReferenceObjectMarkingAttack.find_by(marking_definition_id: marking_definition.id, attack_pattern_id: attack_pattern.id)
  #
  # end todo
end

Instance Method Details

#as_stixObject



133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/models/active_stix/attack_pattern.rb', line 133

def as_stix
  as_json(only: []).tap do |hash|
    hash["id"] = stix_id
    hash["type"] = type
    hash["created"] = created_at.rfc3339(3)
    hash["modified"] = updated_at.rfc3339(3)
    hash["external_references"] = external_references.collect {|ref| ref.as_stix}
    hash["name"] = name
    hash["description"] = description
    hash["kill_chain_phases"] = phases.collect {|phase| phase.as_stix}
  end
end

#convert_to_jsonObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/models/active_stix/attack_pattern.rb', line 87

def convert_to_json
  external_refs_arr = []
  external_references.each do |x|
    external_refs_arr << x.convert_to_json
  end

  phase_arr = []
  phases.each do |x|
    phase_arr << x.convert_to_json
  end

  # marking_def_arr = []
  # marking_definitions.each do |x|
  #   marking_def_arr << x.convert_to_json
  # end todo

  data_sources_arr = []
  data_sources.each do |x|
    data_sources_arr << x.convert_to_json
  end

  platform_arr = []
  platforms.each do |x|
    platform_arr << x.convert_to_json
  end


  perm_arr = []
  permissions_requireds.each do |x|
    perm_arr << x.convert_to_json
  end

  {
      :external_references => external_refs_arr,
      :object_marking_refs => marking_def_arr,
      :modified => updated_at.to_s,
      :kill_chain_phases => phase_arr,
      :id => stix_id,
      :name => name,
      :x_mitre_version => versions.first.convert_to_json,
      :type => "attack-pattern",
      :description => description,
  }
end

#indicator_relationshipsObject



72
73
74
75
76
77
78
79
80
# File 'app/models/active_stix/attack_pattern.rb', line 72

def indicator_relationships
  relationships = []
  ActiveStix::Relationship.where(target: self, relationship_type: "uses", source_type: "ActiveStix::Indicator").or(
      ActiveStix::Relationship.where(target: self, relationship_type: "indicates", source_type: "ActiveStix::Indicator")
  ).each do |rel|
    relationships << rel
  end
  relationships
end

#indicatorsObject



82
83
84
# File 'app/models/active_stix/attack_pattern.rb', line 82

def indicators
  indicator_relationships.collect {|rel| rel.source}
end

#targets(identity) ⇒ Object



146
147
148
# File 'app/models/active_stix/attack_pattern.rb', line 146

def targets(identity)
  ActiveStix::Relationship.relate(self, identity, "targets")
end

#targets?(identity) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'app/models/active_stix/attack_pattern.rb', line 22

def targets?(identity)
  ActiveStix::Relationship.where(source: self, target: identity).any?
end

#typeObject



18
19
20
# File 'app/models/active_stix/attack_pattern.rb', line 18

def type
  'attack-pattern'
end