mihari 3.8.0 → 3.9.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +6 -7
- data/config.ru +1 -1
- data/lib/mihari/analyzers/rule.rb +24 -20
- data/lib/mihari/commands/search.rb +3 -2
- data/lib/mihari/commands/web.rb +9 -5
- data/lib/mihari/database.rb +1 -1
- data/lib/mihari/errors.rb +2 -0
- data/lib/mihari/mixins/configuration.rb +12 -2
- data/lib/mihari/models/alert.rb +29 -54
- data/lib/mihari/models/artifact.rb +3 -0
- data/lib/mihari/structs/alert.rb +45 -0
- data/lib/mihari/structs/ipinfo.rb +3 -4
- data/lib/mihari/types.rb +6 -1
- data/lib/mihari/version.rb +1 -1
- data/lib/mihari/web/api.rb +43 -0
- data/lib/mihari/web/app.rb +48 -30
- data/lib/mihari/web/endpoints/alerts.rb +74 -0
- data/lib/mihari/web/endpoints/artifacts.rb +92 -0
- data/lib/mihari/web/endpoints/command.rb +32 -0
- data/lib/mihari/web/endpoints/configs.rb +22 -0
- data/lib/mihari/web/endpoints/ip_addresses.rb +27 -0
- data/lib/mihari/web/endpoints/sources.rb +18 -0
- data/lib/mihari/web/endpoints/tags.rb +38 -0
- data/lib/mihari/web/entities/alert.rb +23 -0
- data/lib/mihari/web/entities/artifact.rb +24 -0
- data/lib/mihari/web/entities/autonomous_system.rb +9 -0
- data/lib/mihari/web/entities/command.rb +14 -0
- data/lib/mihari/web/entities/config.rb +16 -0
- data/lib/mihari/web/entities/dns.rb +10 -0
- data/lib/mihari/web/entities/geolocation.rb +10 -0
- data/lib/mihari/web/entities/ip_address.rb +13 -0
- data/lib/mihari/web/entities/message.rb +9 -0
- data/lib/mihari/web/entities/reverse_dns.rb +9 -0
- data/lib/mihari/web/entities/source.rb +9 -0
- data/lib/mihari/web/entities/tag.rb +13 -0
- data/lib/mihari/web/entities/whois.rb +16 -0
- data/lib/mihari/web/public/grape.rb +73 -0
- data/lib/mihari/web/public/index.html +1 -1
- data/lib/mihari/web/public/redoc-static.html +53 -27
- data/lib/mihari/web/public/static/js/app.14008741.js +21 -0
- data/lib/mihari/web/public/static/js/app.14008741.js.map +1 -0
- data/lib/mihari/web/public/static/js/app.378da3dc.js +50 -0
- data/lib/mihari/web/public/static/js/app.378da3dc.js.map +1 -0
- data/lib/mihari/web/public/static/js/app.6b636b62.js +50 -0
- data/lib/mihari/web/public/static/js/app.6b636b62.js.map +1 -0
- data/lib/mihari/web/public/static/js/app.a862ebca.js +50 -0
- data/lib/mihari/web/public/static/js/app.a862ebca.js.map +1 -0
- data/lib/mihari.rb +3 -12
- data/mihari.gemspec +9 -6
- data/sig/lib/mihari/analyzers/rule.rbs +3 -3
- data/sig/lib/mihari/models/alert.rbs +3 -31
- data/sig/lib/mihari/structs/alert.rbs +27 -0
- data/sig/lib/mihari/web/app.rbs +2 -2
- metadata +132 -76
- data/lib/mihari/serializers/alert.rb +0 -14
- data/lib/mihari/serializers/artifact.rb +0 -18
- data/lib/mihari/serializers/autonomous_system.rb +0 -9
- data/lib/mihari/serializers/dns.rb +0 -11
- data/lib/mihari/serializers/geolocation.rb +0 -11
- data/lib/mihari/serializers/reverse_dns.rb +0 -11
- data/lib/mihari/serializers/tag.rb +0 -11
- data/lib/mihari/serializers/whois.rb +0 -11
- data/lib/mihari/web/controllers/alerts_controller.rb +0 -74
- data/lib/mihari/web/controllers/analyzers_controller.rb +0 -38
- data/lib/mihari/web/controllers/artifacts_controller.rb +0 -94
- data/lib/mihari/web/controllers/base_controller.rb +0 -22
- data/lib/mihari/web/controllers/command_controller.rb +0 -26
- data/lib/mihari/web/controllers/config_controller.rb +0 -13
- data/lib/mihari/web/controllers/ip_address_controller.rb +0 -21
- data/lib/mihari/web/controllers/sources_controller.rb +0 -12
- data/lib/mihari/web/controllers/tags_controller.rb +0 -30
- data/lib/mihari/web/helpers/json.rb +0 -53
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c12a4ec4b0c1eee79deba3e5f3a511edcea4a13d7d72dc8142dbd85821095f55
|
4
|
+
data.tar.gz: 4a1e388e55efff4b715caf2261990e6122afd4eb26aca0874a33d3025a16ba5b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8ead20235892bf77e222eff200f25539ffdd68cd21d14c5000d9196e518b9d2da2b461a2fe5d3dae4bc4bd899fb210f5f75b6330afad61826bc84dcfe40af23f
|
7
|
+
data.tar.gz: 4b5679794091a79adb426ef34c5396819c702d67ad84791dec1430eba0f007d1dcab5f7defc53b5dab06e8a0e174963c777fb05606f79a5f16c4377f07847f75
|
data/.github/workflows/test.yml
CHANGED
@@ -43,17 +43,16 @@ jobs:
|
|
43
43
|
|
44
44
|
steps:
|
45
45
|
- uses: actions/checkout@v2
|
46
|
-
- name: Set up Ruby 2.7
|
47
|
-
uses: ruby/setup-ruby@v1
|
48
|
-
with:
|
49
|
-
ruby-version: ${{ matrix.ruby }}
|
50
|
-
bundler-cache: true
|
51
46
|
|
52
47
|
- name: Install dependencies
|
53
48
|
run: |
|
54
49
|
sudo apt-get -yqq install libpq-dev libmysqlclient-dev
|
55
|
-
|
56
|
-
|
50
|
+
|
51
|
+
- name: Set up Ruby
|
52
|
+
uses: ruby/setup-ruby@v1
|
53
|
+
with:
|
54
|
+
ruby-version: ${{ matrix.ruby }}
|
55
|
+
bundler-cache: true
|
57
56
|
|
58
57
|
- name: Test with PostgreSQL
|
59
58
|
env:
|
data/config.ru
CHANGED
@@ -4,6 +4,30 @@ require "uuidtools"
|
|
4
4
|
|
5
5
|
module Mihari
|
6
6
|
module Analyzers
|
7
|
+
ANALYZER_TO_CLASS = {
|
8
|
+
"binaryedge" => BinaryEdge,
|
9
|
+
"censys" => Censys,
|
10
|
+
"circl" => CIRCL,
|
11
|
+
"crtsh" => Crtsh,
|
12
|
+
"dnpedia" => DNPedia,
|
13
|
+
"dnstwister" => DNSTwister,
|
14
|
+
"onyphe" => Onyphe,
|
15
|
+
"otx" => OTX,
|
16
|
+
"passivetotal" => PassiveTotal,
|
17
|
+
"pt" => PassiveTotal,
|
18
|
+
"pulsedive" => Pulsedive,
|
19
|
+
"securitytrails" => SecurityTrails,
|
20
|
+
"shodan" => Shodan,
|
21
|
+
"spyse" => Spyse,
|
22
|
+
"st" => SecurityTrails,
|
23
|
+
"urlscan" => Urlscan,
|
24
|
+
"virustotal_intelligence" => VirusTotalIntelligence,
|
25
|
+
"virustotal" => VirusTotal,
|
26
|
+
"vt_intel" => VirusTotalIntelligence,
|
27
|
+
"vt" => VirusTotal,
|
28
|
+
"zoomeye" => ZoomEye
|
29
|
+
}.freeze
|
30
|
+
|
7
31
|
class Rule < Base
|
8
32
|
include Mihari::Mixins::DisallowedDataValue
|
9
33
|
|
@@ -26,26 +50,6 @@ module Mihari
|
|
26
50
|
validate_analyzer_configurations
|
27
51
|
end
|
28
52
|
|
29
|
-
ANALYZER_TO_CLASS = {
|
30
|
-
"binaryedge" => BinaryEdge,
|
31
|
-
"censys" => Censys,
|
32
|
-
"circl" => CIRCL,
|
33
|
-
"crtsh" => Crtsh,
|
34
|
-
"dnpedia" => DNPedia,
|
35
|
-
"dnstwister" => DNSTwister,
|
36
|
-
"onyphe" => Onyphe,
|
37
|
-
"otx" => OTX,
|
38
|
-
"passivetotal" => PassiveTotal,
|
39
|
-
"pulsedive" => Pulsedive,
|
40
|
-
"securitytrails" => SecurityTrails,
|
41
|
-
"shodan" => Shodan,
|
42
|
-
"spyse" => Spyse,
|
43
|
-
"urlscan" => Urlscan,
|
44
|
-
"virustotal" => VirusTotal,
|
45
|
-
"virustotal_intelligence" => VirusTotalIntelligence,
|
46
|
-
"zoomeye" => ZoomEye
|
47
|
-
}.freeze
|
48
|
-
|
49
53
|
#
|
50
54
|
# Returns a list of artifacts matched with queries
|
51
55
|
#
|
@@ -10,6 +10,9 @@ module Mihari
|
|
10
10
|
desc "search [RULE]", "Search by a rule"
|
11
11
|
method_option :config, type: :string, desc: "Path to the config file"
|
12
12
|
def search_by_rule(rule)
|
13
|
+
# load configuration
|
14
|
+
load_configuration
|
15
|
+
|
13
16
|
# convert str(YAML) to hash or str(path/YAML file) to hash
|
14
17
|
rule = load_rule(rule)
|
15
18
|
|
@@ -77,8 +80,6 @@ module Mihari
|
|
77
80
|
# @return [nil]
|
78
81
|
#
|
79
82
|
def run_rule_analyzer(analyzer, ignore_old_artifacts: false, ignore_threshold: 0)
|
80
|
-
load_configuration
|
81
|
-
|
82
83
|
analyzer.ignore_old_artifacts = ignore_old_artifacts
|
83
84
|
analyzer.ignore_threshold = ignore_threshold
|
84
85
|
|
data/lib/mihari/commands/web.rb
CHANGED
@@ -6,19 +6,23 @@ module Mihari
|
|
6
6
|
def self.included(thor)
|
7
7
|
thor.class_eval do
|
8
8
|
desc "web", "Launch the web app"
|
9
|
-
method_option :port, type: :numeric, default: 9292
|
10
|
-
method_option :host, type: :string, default: "localhost"
|
9
|
+
method_option :port, type: :numeric, default: 9292, desc: "Hostname to listen on"
|
10
|
+
method_option :host, type: :string, default: "localhost", desc: "Port to listen on"
|
11
|
+
method_option :threads, type: :string, default: "0:16", desc: "min:max threads to use"
|
12
|
+
method_option :verbose, type: :boolean, default: true, desc: "Report each request"
|
11
13
|
method_option :config, type: :string, desc: "Path to the config file"
|
12
14
|
def web
|
13
|
-
port = options["port"]
|
14
|
-
host = options["host"]
|
15
|
+
port = options["port"]
|
16
|
+
host = options["host"]
|
17
|
+
threads = options["threads"]
|
18
|
+
verbose = options["verbose"]
|
15
19
|
|
16
20
|
load_configuration
|
17
21
|
|
18
22
|
# set rack env as production
|
19
23
|
ENV["RACK_ENV"] ||= "production"
|
20
24
|
|
21
|
-
Mihari::App.run!(port: port, host: host)
|
25
|
+
Mihari::App.run!(port: port, host: host, threads: threads, verbose: verbose)
|
22
26
|
end
|
23
27
|
end
|
24
28
|
end
|
data/lib/mihari/database.rb
CHANGED
data/lib/mihari/errors.rb
CHANGED
@@ -80,10 +80,20 @@ module Mihari
|
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
83
|
+
#
|
84
|
+
# Load configuration file
|
85
|
+
#
|
86
|
+
# @param [String] path
|
87
|
+
#
|
88
|
+
# @return [Hash]
|
89
|
+
#
|
83
90
|
def _load_config(path)
|
84
|
-
|
91
|
+
unless Pathname(path).exist?
|
92
|
+
puts "#{path} does not exist".colorize(:red)
|
93
|
+
raise FileNotFoundError
|
94
|
+
end
|
85
95
|
|
86
|
-
YAML.safe_load(path, symbolize_names: true)
|
96
|
+
YAML.safe_load(File.read(path), symbolize_names: true)
|
87
97
|
end
|
88
98
|
end
|
89
99
|
end
|
data/lib/mihari/models/alert.rb
CHANGED
@@ -13,92 +13,67 @@ module Mihari
|
|
13
13
|
#
|
14
14
|
# Search alerts
|
15
15
|
#
|
16
|
-
# @param [
|
17
|
-
# @param [String, nil] description
|
18
|
-
# @param [String, nil] source
|
19
|
-
# @param [String, nil] tag_name
|
20
|
-
# @param [String, nil] title
|
21
|
-
# @param [DateTime, nil] from_at
|
22
|
-
# @param [DateTime, nil] to_at
|
23
|
-
# @param [Integer, nil] limit
|
24
|
-
# @param [Integer, nil] page
|
16
|
+
# @param [Structs::Alert::SearchFilterWithPagination] filter
|
25
17
|
#
|
26
18
|
# @return [Array<Hash>]
|
27
19
|
#
|
28
|
-
def search(
|
29
|
-
limit = limit.to_i
|
20
|
+
def search(filter)
|
21
|
+
limit = filter.limit.to_i
|
30
22
|
raise ArgumentError, "limit should be bigger than zero" unless limit.positive?
|
31
23
|
|
32
|
-
page = page.to_i
|
24
|
+
page = filter.page.to_i
|
33
25
|
raise ArgumentError, "page should be bigger than zero" unless page.positive?
|
34
26
|
|
35
27
|
offset = (page - 1) * limit
|
36
28
|
|
37
|
-
relation = build_relation(
|
38
|
-
artifact_data: artifact_data,
|
39
|
-
title: title,
|
40
|
-
description: description,
|
41
|
-
source: source,
|
42
|
-
tag_name: tag_name,
|
43
|
-
from_at: from_at,
|
44
|
-
to_at: to_at
|
45
|
-
)
|
29
|
+
relation = build_relation(filter.without_pagination)
|
46
30
|
|
47
31
|
# TODO: improve queires
|
48
32
|
alert_ids = relation.limit(limit).offset(offset).order(id: :desc).pluck(:id).uniq
|
49
|
-
|
50
|
-
|
51
|
-
alerts.map do |alert|
|
52
|
-
json = Serializers::AlertSerializer.new(alert).as_json
|
53
|
-
json[:artifacts] = json[:artifacts] || []
|
54
|
-
json[:tags] = json[:tags] || []
|
55
|
-
json
|
56
|
-
end
|
33
|
+
includes(:artifacts, :tags).where(id: [alert_ids]).order(id: :desc)
|
57
34
|
end
|
58
35
|
|
59
36
|
#
|
60
37
|
# Count alerts
|
61
38
|
#
|
62
39
|
# @param [String, nil] artifact_data
|
63
|
-
# @param [String, nil] description
|
64
|
-
# @param [String, nil] source
|
65
|
-
# @param [String, nil] tag_name
|
66
|
-
# @param [String, nil] title
|
67
|
-
# @param [DateTime, nil] from_at
|
68
|
-
# @param [DateTime, nil] to_at
|
69
40
|
#
|
70
41
|
# @return [Integer]
|
71
42
|
#
|
72
|
-
def count(
|
73
|
-
relation = build_relation(
|
74
|
-
artifact_data: artifact_data,
|
75
|
-
title: title,
|
76
|
-
description: description,
|
77
|
-
source: source,
|
78
|
-
tag_name: tag_name,
|
79
|
-
from_at: from_at,
|
80
|
-
to_at: to_at
|
81
|
-
)
|
43
|
+
def count(filter)
|
44
|
+
relation = build_relation(filter)
|
82
45
|
relation.distinct("alerts.id").count
|
83
46
|
end
|
84
47
|
|
85
48
|
private
|
86
49
|
|
87
|
-
def build_relation(
|
88
|
-
|
50
|
+
def build_relation(filter)
|
51
|
+
artifact_ids = []
|
52
|
+
artifact = Artifact.includes(:autonomous_system, :dns_records, :reverse_dns_names)
|
53
|
+
artifact = artifact.where(data: filter.artifact_data) if filter.artifact_data
|
54
|
+
artifact = artifact.where(autonomous_system: { asn: filter.asn }) if filter.asn
|
55
|
+
artifact = artifact.where(dns_records: { value: filter.dns_record }) if filter.dns_record
|
56
|
+
artifact = artifact.where(reverse_dns_names: { name: filter.reverse_dns_name }) if filter.reverse_dns_name
|
57
|
+
# get artifact ids if there is any valid filter for artifact
|
58
|
+
if filter.has_valid_artifact_filters
|
59
|
+
artifact_ids = artifact.pluck(:id)
|
60
|
+
# set invalid ID if nothing is matched with the filters
|
61
|
+
artifact_ids = [-1] if artifact_ids.empty?
|
62
|
+
end
|
89
63
|
|
64
|
+
relation = self
|
90
65
|
relation = relation.includes(:artifacts, :tags)
|
91
66
|
|
92
|
-
relation = relation.where(artifacts: {
|
93
|
-
relation = relation.where(tags: { name: tag_name }) if tag_name
|
67
|
+
relation = relation.where(artifacts: { id: artifact_ids }) unless artifact_ids.empty?
|
68
|
+
relation = relation.where(tags: { name: filter.tag_name }) if filter.tag_name
|
94
69
|
|
95
|
-
relation = relation.where(source: source) if source
|
96
|
-
relation = relation.where(title: title) if title
|
70
|
+
relation = relation.where(source: filter.source) if filter.source
|
71
|
+
relation = relation.where(title: filter.title) if filter.title
|
97
72
|
|
98
|
-
relation = relation.filter(description: { like: "%#{description}%" }) if description
|
73
|
+
relation = relation.filter(description: { like: "%#{filter.description}%" }) if filter.description
|
99
74
|
|
100
|
-
relation = relation.filter(created_at: { gte: from_at }) if from_at
|
101
|
-
relation = relation.filter(created_at: { lte: to_at }) if to_at
|
75
|
+
relation = relation.filter(created_at: { gte: filter.from_at }) if filter.from_at
|
76
|
+
relation = relation.filter(created_at: { lte: filter.to_at }) if filter.to_at
|
102
77
|
|
103
78
|
relation
|
104
79
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "json"
|
2
|
+
require "dry/struct"
|
3
|
+
|
4
|
+
module Mihari
|
5
|
+
module Structs
|
6
|
+
module Alert
|
7
|
+
class SearchFilter < Dry::Struct
|
8
|
+
attribute? :artifact_data, Types::String.optional
|
9
|
+
attribute? :description, Types::String.optional
|
10
|
+
attribute? :source, Types::String.optional
|
11
|
+
attribute? :tag_name, Types::String.optional
|
12
|
+
attribute? :title, Types::String.optional
|
13
|
+
attribute? :from_at, Types::DateTime.optional
|
14
|
+
attribute? :to_at, Types::DateTime.optional
|
15
|
+
attribute? :asn, Types::Int.optional
|
16
|
+
attribute? :dns_record, Types::String.optional
|
17
|
+
attribute? :reverse_dns_name, Types::String.optional
|
18
|
+
|
19
|
+
def has_valid_artifact_filters
|
20
|
+
!(artifact_data || asn || dns_record || reverse_dns_name).nil?
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class SearchFilterWithPagination < SearchFilter
|
25
|
+
attribute? :page, Types::Int.default(1)
|
26
|
+
attribute? :limit, Types::Int.default(10)
|
27
|
+
|
28
|
+
def without_pagination
|
29
|
+
SearchFilter.new(
|
30
|
+
artifact_data: artifact_data,
|
31
|
+
description: description,
|
32
|
+
from_at: from_at,
|
33
|
+
source: source,
|
34
|
+
tag_name: tag_name,
|
35
|
+
title: title,
|
36
|
+
to_at: to_at,
|
37
|
+
asn: asn,
|
38
|
+
dns_record: dns_record,
|
39
|
+
reverse_dns_name: reverse_dns_name
|
40
|
+
)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/mihari/types.rb
CHANGED
@@ -9,6 +9,7 @@ module Mihari
|
|
9
9
|
Hash = Strict::Hash
|
10
10
|
String = Strict::String
|
11
11
|
Double = Strict::Float | Strict::Integer
|
12
|
+
DateTime = Strict::DateTime
|
12
13
|
|
13
14
|
DataTypes = Types::String.enum(*ALLOWED_DATA_TYPES)
|
14
15
|
|
@@ -21,11 +22,15 @@ module Mihari
|
|
21
22
|
"onyphe",
|
22
23
|
"otx",
|
23
24
|
"passivetotal",
|
25
|
+
"pt",
|
24
26
|
"pulsedive",
|
25
27
|
"securitytrails",
|
26
28
|
"shodan",
|
29
|
+
"st",
|
27
30
|
"virustotal_intelligence",
|
28
|
-
"virustotal"
|
31
|
+
"virustotal",
|
32
|
+
"vt_intel",
|
33
|
+
"vt"
|
29
34
|
)
|
30
35
|
end
|
31
36
|
end
|
data/lib/mihari/version.rb
CHANGED
@@ -0,0 +1,43 @@
|
|
1
|
+
# Entities
|
2
|
+
require "mihari/web/entities/message"
|
3
|
+
|
4
|
+
require "mihari/web/entities/autonomous_system"
|
5
|
+
require "mihari/web/entities/command"
|
6
|
+
require "mihari/web/entities/config"
|
7
|
+
require "mihari/web/entities/dns"
|
8
|
+
require "mihari/web/entities/geolocation"
|
9
|
+
require "mihari/web/entities/ip_address"
|
10
|
+
require "mihari/web/entities/reverse_dns"
|
11
|
+
require "mihari/web/entities/source"
|
12
|
+
require "mihari/web/entities/tag"
|
13
|
+
require "mihari/web/entities/whois"
|
14
|
+
|
15
|
+
require "mihari/web/entities/artifact"
|
16
|
+
|
17
|
+
require "mihari/web/entities/alert"
|
18
|
+
|
19
|
+
# Endpoints
|
20
|
+
require "mihari/web/endpoints/alerts"
|
21
|
+
require "mihari/web/endpoints/artifacts"
|
22
|
+
require "mihari/web/endpoints/command"
|
23
|
+
require "mihari/web/endpoints/configs"
|
24
|
+
require "mihari/web/endpoints/ip_addresses"
|
25
|
+
require "mihari/web/endpoints/sources"
|
26
|
+
require "mihari/web/endpoints/tags"
|
27
|
+
|
28
|
+
module Mihari
|
29
|
+
class API < Grape::API
|
30
|
+
prefix "api"
|
31
|
+
format :json
|
32
|
+
|
33
|
+
mount Endpoints::Alerts
|
34
|
+
mount Endpoints::Artifacts
|
35
|
+
mount Endpoints::Command
|
36
|
+
mount Endpoints::Configs
|
37
|
+
mount Endpoints::IPAddresses
|
38
|
+
mount Endpoints::Sources
|
39
|
+
mount Endpoints::Tags
|
40
|
+
|
41
|
+
add_swagger_documentation(api_version: "v1", info: { title: "Mihari API" })
|
42
|
+
end
|
43
|
+
end
|
data/lib/mihari/web/app.rb
CHANGED
@@ -2,46 +2,47 @@
|
|
2
2
|
|
3
3
|
require "launchy"
|
4
4
|
require "rack"
|
5
|
+
require "rack/contrib"
|
5
6
|
require "rack/handler/puma"
|
6
|
-
require "
|
7
|
+
require "rack/cors"
|
7
8
|
|
8
|
-
require "
|
9
|
+
require "grape"
|
10
|
+
require "grape-entity"
|
11
|
+
require "grape-swagger"
|
12
|
+
require "grape-swagger-entity"
|
9
13
|
|
10
|
-
require "mihari/web/
|
11
|
-
|
12
|
-
require "mihari/web/controllers/alerts_controller"
|
13
|
-
require "mihari/web/controllers/analyzers_controller"
|
14
|
-
require "mihari/web/controllers/artifacts_controller"
|
15
|
-
require "mihari/web/controllers/command_controller"
|
16
|
-
require "mihari/web/controllers/config_controller"
|
17
|
-
require "mihari/web/controllers/ip_address_controller"
|
18
|
-
require "mihari/web/controllers/sources_controller"
|
19
|
-
require "mihari/web/controllers/tags_controller"
|
14
|
+
require "mihari/web/api"
|
20
15
|
|
21
16
|
module Mihari
|
22
|
-
class App
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
17
|
+
class App
|
18
|
+
def initialize
|
19
|
+
@filenames = ["", ".html", "index.html", "/index.html"]
|
20
|
+
@rack_static = ::Rack::Static.new(
|
21
|
+
-> { [404, {}, []] },
|
22
|
+
root: File.expand_path("./public", __dir__),
|
23
|
+
urls: ["/"]
|
24
|
+
)
|
28
25
|
end
|
29
26
|
|
30
|
-
use Mihari::Controllers::AlertsController
|
31
|
-
use Mihari::Controllers::AnalyzersController
|
32
|
-
use Mihari::Controllers::ArtifactsController
|
33
|
-
use Mihari::Controllers::CommandController
|
34
|
-
use Mihari::Controllers::ConfigController
|
35
|
-
use Mihari::Controllers::IPAddressController
|
36
|
-
use Mihari::Controllers::SourcesController
|
37
|
-
use Mihari::Controllers::TagsController
|
38
|
-
|
39
27
|
class << self
|
40
|
-
def
|
28
|
+
def instance
|
29
|
+
@instance ||= Rack::Builder.new do
|
30
|
+
use Rack::Cors do
|
31
|
+
allow do
|
32
|
+
origins "*"
|
33
|
+
resource "*", headers: :any, methods: [:get, :post, :put, :delete, :options]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
run App.new
|
38
|
+
end.to_app
|
39
|
+
end
|
40
|
+
|
41
|
+
def run!(port: 9292, host: "localhost", threads: "0:16", verbose: false)
|
41
42
|
url = "http://#{host}:#{port}"
|
42
43
|
|
43
|
-
Rack::Handler::Puma.run
|
44
|
-
Launchy.open
|
44
|
+
Rack::Handler::Puma.run(instance, Port: port, Host: host, Threads: threads, Verbose: verbose) do |server|
|
45
|
+
Launchy.open(url) if ENV["RACK_ENV"] != "development"
|
45
46
|
|
46
47
|
[:INT, :TERM].each do |sig|
|
47
48
|
trap(sig) do
|
@@ -51,5 +52,22 @@ module Mihari
|
|
51
52
|
end
|
52
53
|
end
|
53
54
|
end
|
55
|
+
|
56
|
+
def call(env)
|
57
|
+
# api
|
58
|
+
api_response = API.call(env)
|
59
|
+
|
60
|
+
# Check if the App wants us to pass the response along to others
|
61
|
+
if api_response[1]["X-Cascade"] == "pass"
|
62
|
+
# static files
|
63
|
+
request_path = env["PATH_INFO"]
|
64
|
+
@filenames.each do |path|
|
65
|
+
response = @rack_static.call(env.merge("PATH_INFO" => request_path + path))
|
66
|
+
return response if response[0] != 404
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
api_response
|
71
|
+
end
|
54
72
|
end
|
55
73
|
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Mihari
|
4
|
+
module Endpoints
|
5
|
+
class Alerts < Grape::API
|
6
|
+
namespace :alerts do
|
7
|
+
desc "Search alerts", {
|
8
|
+
is_array: true,
|
9
|
+
success: Entities::Alert,
|
10
|
+
failure: [{ code: 404, message: "Not found", model: Entities::Message }]
|
11
|
+
}
|
12
|
+
params do
|
13
|
+
optional :page, type: Integer
|
14
|
+
optional :artifact, type: String
|
15
|
+
optional :description, type: String
|
16
|
+
optional :source, type: String
|
17
|
+
optional :tag, type: String
|
18
|
+
|
19
|
+
optional :fromAt, type: DateTime
|
20
|
+
optional :toAt, type: DateTime
|
21
|
+
|
22
|
+
optional :asn, type: Integer
|
23
|
+
optional :dnsRecord, type: String
|
24
|
+
optional :reverseDnsName, type: String
|
25
|
+
end
|
26
|
+
get "/" do
|
27
|
+
filter = params.to_h.to_snake_keys
|
28
|
+
|
29
|
+
# set page & limit
|
30
|
+
page = filter["page"] || 1
|
31
|
+
filter["page"] = page.to_i
|
32
|
+
|
33
|
+
limit = 10
|
34
|
+
filter["limit"] = 10
|
35
|
+
|
36
|
+
# normalize keys
|
37
|
+
filter["artifact_data"] = filter["artifact"]
|
38
|
+
filter["tag_name"] = filter["tag"]
|
39
|
+
|
40
|
+
# symbolize hash keys
|
41
|
+
filter = filter.to_h.transform_keys(&:to_sym)
|
42
|
+
|
43
|
+
search_filter_with_pagenation = Structs::Alert::SearchFilterWithPagination.new(**filter)
|
44
|
+
alerts = Mihari::Alert.search(search_filter_with_pagenation)
|
45
|
+
total = Mihari::Alert.count(search_filter_with_pagenation.without_pagination)
|
46
|
+
|
47
|
+
present({ alerts: alerts, total: total, current_page: page, page_size: limit }, with: Entities::AlertsWithPagination)
|
48
|
+
end
|
49
|
+
|
50
|
+
desc "Delete an alert", {
|
51
|
+
success: Entities::Message,
|
52
|
+
failure: [{ code: 404, message: "Not found", model: Entities::Message }]
|
53
|
+
}
|
54
|
+
params do
|
55
|
+
requires :id, type: Integer
|
56
|
+
end
|
57
|
+
delete "/:id" do
|
58
|
+
id = params["id"].to_i
|
59
|
+
|
60
|
+
begin
|
61
|
+
alert = Mihari::Alert.find(id)
|
62
|
+
rescue ActiveRecord::RecordNotFound
|
63
|
+
error!({ message: "ID:#{id} is not found" }, 404)
|
64
|
+
end
|
65
|
+
|
66
|
+
alert.destroy
|
67
|
+
|
68
|
+
status 204
|
69
|
+
present({ message: "" }, with: Entities::Message)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|