mihari 4.0.0 → 4.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1a42a8897840d2f2268f88c6734c1633642256ef08667c39f45f5d03ac13874f
4
- data.tar.gz: 89bdd5aa38f833e3158464915ec46c05e8c0bb6f17d34fe397d1590c59a57d7c
3
+ metadata.gz: f96f1f1e70601518505d5b48aba5a9e0c60f1555e9d7c96ca307a32f5214a568
4
+ data.tar.gz: 87910483603ccf914b867bede2ea287d456c9bde59ccfebd2324c42b8b1c6928
5
5
  SHA512:
6
- metadata.gz: f62927fdb96eabffd99106bf03178e6eb573ee5e58b8fa2bb94b6b6b2ea898324fd65656bbb128aab67196ad03da959603dd73108c8840e883560d673c8afa5f
7
- data.tar.gz: 2e752fa7ab93691ad6c38ec534af3bab14e27f2b66127dc00fc016f26554ea29fc2c743d2bdaef93d85e816d8a47e54d2d41bb6dafaabf711cfb2c833c3ff0ff
6
+ metadata.gz: 71fc241abdc3c41f28d49e7a2004957edaf0f09adc2f96b634af9dc5ea6de02fb2377fe0f88fbdb3c72816b2f72ebb4f6747e959a0dc7b13fff8377806bc57db
7
+ data.tar.gz: 9270f526e57e69df875f5a4cdacc0905cf8e8635f4fcea734f9a52b4f8b2e81a732a1990c4def0c53623ee57b60e7c40ee249a1b9501f866b1aec64fbbc1b47c
@@ -16,7 +16,7 @@ module Mihari
16
16
  results = results.map { |result| Structs::Shodan::Result.from_dynamic!(result) }
17
17
  results.map do |result|
18
18
  matches = result.matches || []
19
- matches.map { |match| build_artifact match }
19
+ matches.map { |match| build_artifact(match, matches) }
20
20
  end.flatten.uniq(&:data)
21
21
  end
22
22
 
@@ -73,14 +73,27 @@ module Mihari
73
73
  responses
74
74
  end
75
75
 
76
+ #
77
+ # Collect metadata from matches
78
+ #
79
+ # @param [Array<Structs::Shodan::Match>] matches
80
+ # @param [String] ip
81
+ #
82
+ # @return [Array<Hash>]
83
+ #
84
+ def collect_metadata_by_ip(matches, ip)
85
+ matches.select { |match| match.ip_str == ip }.map(&:metadata)
86
+ end
87
+
76
88
  #
77
89
  # Build an artifact from a Shodan search API response
78
90
  #
79
91
  # @param [Structs::Shodan::Match] match
92
+ # @param [Array<Structs::Shodan::Match>] matches
80
93
  #
81
94
  # @return [Artifact]
82
95
  #
83
- def build_artifact(match)
96
+ def build_artifact(match, matches)
84
97
  as = nil
85
98
  as = AutonomousSystem.new(asn: normalize_asn(match.asn)) unless match.asn.nil?
86
99
 
@@ -92,10 +105,12 @@ module Mihari
92
105
  )
93
106
  end
94
107
 
108
+ metadata = collect_metadata_by_ip(matches, match.ip_str)
109
+
95
110
  Artifact.new(
96
111
  data: match.ip_str,
97
112
  source: source,
98
- metadata: match.metadata,
113
+ metadata: metadata,
99
114
  autonomous_system: as,
100
115
  geolocation: geolocation
101
116
  )
@@ -11,7 +11,7 @@ module Mihari
11
11
  #
12
12
  def to_h
13
13
  symbolized_data = data.deep_symbolize_keys
14
- h = { id: id, created_at: created_at, yaml: symbolized_data.to_yaml }
14
+ h = { id: id, created_at: created_at, yaml: data.to_yaml }
15
15
  h.merge symbolized_data
16
16
  end
17
17
 
@@ -108,6 +108,12 @@ module Mihari
108
108
  # @return [Mihari::Rule]
109
109
  #
110
110
  def to_model
111
+ rule = Mihari::Rule.find(id)
112
+ rule.title = title
113
+ rule.description = description
114
+ rule.data = data
115
+ rule
116
+ rescue ActiveRecord::RecordNotFound
111
117
  Mihari::Rule.new(
112
118
  id: id,
113
119
  title: title,
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mihari
4
- VERSION = "4.0.0"
4
+ VERSION = "4.1.0"
5
5
  end
@@ -112,6 +112,41 @@ module Mihari
112
112
  present model.to_h, with: Entities::Rule
113
113
  end
114
114
 
115
+ desc "Update a rule", {
116
+ success: Entities::Rule,
117
+ summary: "Update a rule"
118
+ }
119
+ put "/" do
120
+ id = params["id"].to_s
121
+
122
+ begin
123
+ Mihari::Rule.find(id)
124
+ rescue ActiveRecord::RecordNotFound
125
+ error!({ message: "ID:#{id} is not found" }, 404)
126
+ end
127
+
128
+ rule = Structs::Rule::Rule.new(params)
129
+
130
+ begin
131
+ rule.validate!
132
+ rescue RuleValidationError
133
+ error!({ message: "Data format is invalid", details: rule.errors.to_h }, 400) if rule.errors?
134
+
135
+ # when NoMethodError occurs
136
+ error!({ message: "Data format is invalid" }, 400)
137
+ end
138
+
139
+ begin
140
+ model = rule.to_model
141
+ model.save
142
+ rescue ActiveRecord::RecordNotUnique
143
+ error!({ message: "ID:#{rule.id} is already registered" }, 400)
144
+ end
145
+
146
+ status 201
147
+ present model.to_h, with: Entities::Rule
148
+ end
149
+
115
150
  desc "Delete a rule", {
116
151
  success: Entities::Message,
117
152
  failure: [{ code: 404, message: "Not found", model: Entities::Message }],
@@ -1 +1 @@
1
- <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="icon" href="/static/favicon.ico"><title>Mihari</title><link href="/static/js/app.49ab738a.js" rel="preload" as="script"></head><body><noscript><strong>We're sorry but Mihari doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div><script src="/static/js/app.49ab738a.js"></script></body></html>
1
+ <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="icon" href="/static/favicon.ico"><title>Mihari</title><link href="/static/js/app.cb1fa7be.js" rel="preload" as="script"></head><body><noscript><strong>We're sorry but Mihari doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div><script src="/static/js/app.cb1fa7be.js"></script></body></html>