Class: Mihari::Structs::Shodan::Response

Inherits:
Dry::Struct
  • Object
show all
Includes:
MemoWise
Defined in:
lib/mihari/structs/shodan.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#matchesArray<Match> (readonly)

Returns:



127
# File 'lib/mihari/structs/shodan.rb', line 127

attribute :matches, Types.Array(Match)

#totalInteger (readonly)

Returns:

  • (Integer)


131
# File 'lib/mihari/structs/shodan.rb', line 131

attribute :total, Types::Int

Class Method Details

.from_dynamic!(d) ⇒ Result

Parameters:

  • d (Hash)

Returns:

  • (Result)


232
233
234
235
236
237
238
# File 'lib/mihari/structs/shodan.rb', line 232

def from_dynamic!(d)
  d = Types::Hash[d]
  new(
    matches: d.fetch("matches", []).map { |x| Match.from_dynamic!(x) },
    total: d.fetch("total")
  )
end

Instance Method Details

#artifactsArray<Mihari::Models::Artifact>

Returns:



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/mihari/structs/shodan.rb', line 202

def artifacts
  matches.map do |match|
     = (match.ip_str)

    ports = collect_ports_by_ip(match.ip_str).map { |port| Models::Port.new(number: port) }
    reverse_dns_names = collect_hostnames_by_ip(match.ip_str).map do |name|
      Models::ReverseDnsName.new(name:)
    end
    cpes = collect_cpes_by_ip(match.ip_str).map { |name| Models::CPE.new(name:) }
    vulnerabilities = collect_vulns_by_ip(match.ip_str).map { |name| Models::Vulnerability.new(name:) }

    Mihari::Models::Artifact.new(
      data: match.ip_str,
      metadata:,
      autonomous_system: match.autonomous_system,
      geolocation: match.location.geolocation,
      ports:,
      reverse_dns_names:,
      cpes:,
      vulnerabilities:
    )
  end
end

#collect_cpes_by_ip(ip) ⇒ Array<String>

Collect CPE from matches

Parameters:

  • ip (String)

Returns:

  • (Array<String>)


183
184
185
# File 'lib/mihari/structs/shodan.rb', line 183

def collect_cpes_by_ip(ip)
  select_matches_by_ip(ip).map(&:cpe).flatten.uniq
end

#collect_hostnames_by_ip(ip) ⇒ Array<String>

Collect hostnames from matches

Parameters:

  • ip (String)

Returns:

  • (Array<String>)


172
173
174
# File 'lib/mihari/structs/shodan.rb', line 172

def collect_hostnames_by_ip(ip)
  select_matches_by_ip(ip).map(&:hostnames).flatten.uniq
end

#collect_metadata_by_ip(ip) ⇒ Array<Hash>

Collect metadata from matches

Parameters:

  • ip (String)

Returns:

  • (Array<Hash>)


150
151
152
# File 'lib/mihari/structs/shodan.rb', line 150

def (ip)
  select_matches_by_ip(ip).map(&:metadata)
end

#collect_ports_by_ip(ip) ⇒ Array<String>

Collect ports from matches

Parameters:

  • ip (String)

Returns:

  • (Array<String>)


161
162
163
# File 'lib/mihari/structs/shodan.rb', line 161

def collect_ports_by_ip(ip)
  select_matches_by_ip(ip).map(&:port)
end

#collect_vulns_by_ip(ip) ⇒ Array<String>

Collect vulnerabilities from matches

Parameters:

  • ip (String)

Returns:

  • (Array<String>)


194
195
196
197
# File 'lib/mihari/structs/shodan.rb', line 194

def collect_vulns_by_ip(ip)
  # NOTE: vuln keys = CVE IDs
  select_matches_by_ip(ip).map { |match| match.vulns.keys }.flatten.uniq
end

#select_matches_by_ip(ip) ⇒ Array<Mihari::Structs::Shodan::Match>

Parameters:

  • ip (String)

Returns:



138
139
140
# File 'lib/mihari/structs/shodan.rb', line 138

def select_matches_by_ip(ip)
  matches.select { |match| match.ip_str == ip }
end