bro_hub 0.1.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +21 -0
- data/README.md +204 -87
- data/app/controllers/bro_hub/apm/application_controller.rb +26 -0
- data/app/controllers/bro_hub/apm/deploys_controller.rb +7 -0
- data/app/controllers/bro_hub/apm/endpoints_controller.rb +8 -0
- data/app/controllers/bro_hub/apm/errors_controller.rb +15 -0
- data/app/controllers/bro_hub/apm/findings_controller.rb +8 -0
- data/app/controllers/bro_hub/apm/overview_controller.rb +7 -0
- data/app/controllers/bro_hub/apm/queries_controller.rb +11 -0
- data/app/controllers/bro_hub/apm/status_controller.rb +9 -0
- data/app/controllers/bro_hub/application_controller.rb +16 -0
- data/app/controllers/bro_hub/audit_events_controller.rb +56 -0
- data/app/controllers/bro_hub/cache/application_controller.rb +55 -0
- data/app/controllers/bro_hub/cache/audit_events_controller.rb +40 -0
- data/app/controllers/bro_hub/cache/flushes_controller.rb +34 -0
- data/app/controllers/bro_hub/cache/keys_controller.rb +41 -0
- data/app/controllers/bro_hub/cache/purges_controller.rb +40 -0
- data/app/controllers/bro_hub/env/application_controller.rb +27 -0
- data/app/controllers/bro_hub/env/audit_controller.rb +10 -0
- data/app/controllers/bro_hub/env/home_controller.rb +6 -0
- data/app/controllers/bro_hub/env/proposals_controller.rb +86 -0
- data/app/controllers/bro_hub/env/reviews_controller.rb +72 -0
- data/app/controllers/bro_hub/feature_controller.rb +21 -0
- data/app/controllers/bro_hub/safe/application_controller.rb +24 -0
- data/app/controllers/bro_hub/safe/flagged_commands_controller.rb +14 -0
- data/app/controllers/bro_hub/safe/reviews_controller.rb +39 -0
- data/app/controllers/bro_hub/safe/rules_controller.rb +18 -0
- data/app/controllers/bro_hub/safe/sessions_controller.rb +63 -0
- data/app/controllers/bro_hub/safe/unlocks_controller.rb +14 -0
- data/app/helpers/bro_hub/apm/application_helper.rb +104 -0
- data/app/helpers/bro_hub/application_helper.rb +28 -18
- data/app/helpers/bro_hub/cache/application_helper.rb +63 -0
- data/app/helpers/bro_hub/env/application_helper.rb +67 -0
- data/app/helpers/bro_hub/safe/application_helper.rb +59 -0
- data/app/jobs/bro_hub/safe/purge_job.rb +10 -0
- data/app/models/bro_hub/application_record.rb +5 -0
- data/app/models/bro_hub/audit_event.rb +40 -0
- data/app/models/bro_hub/cache/audit_event.rb +15 -0
- data/app/models/bro_hub/env/audit_event.rb +72 -0
- data/app/models/bro_hub/safe/command.rb +22 -0
- data/app/models/bro_hub/safe/review.rb +22 -0
- data/app/models/bro_hub/safe/session.rb +57 -0
- data/app/models/bro_hub/safe/unlock.rb +15 -0
- data/app/views/bro_hub/apm/deploys/index.html.erb +21 -0
- data/app/views/bro_hub/apm/endpoints/index.html.erb +27 -0
- data/app/views/bro_hub/apm/errors/index.html.erb +22 -0
- data/app/views/bro_hub/apm/errors/not_found.html.erb +3 -0
- data/app/views/bro_hub/apm/errors/show.html.erb +43 -0
- data/app/views/bro_hub/apm/findings/index.html.erb +24 -0
- data/app/views/bro_hub/apm/overview/show.html.erb +59 -0
- data/app/views/bro_hub/apm/queries/index.html.erb +37 -0
- data/app/views/bro_hub/apm/shared/_page_styles.html.erb +15 -0
- data/app/views/bro_hub/apm/status/show.html.erb +47 -0
- data/app/views/bro_hub/audit_events/index.html.erb +38 -0
- data/app/views/bro_hub/audit_events/show.html.erb +54 -0
- data/app/views/bro_hub/cache/audit_events/index.html.erb +39 -0
- data/app/views/bro_hub/cache/audit_events/show.html.erb +48 -0
- data/app/views/bro_hub/cache/flushes/new.html.erb +19 -0
- data/app/views/bro_hub/cache/keys/index.html.erb +36 -0
- data/app/views/bro_hub/cache/keys/show.html.erb +57 -0
- data/app/views/bro_hub/cache/purges/new.html.erb +17 -0
- data/app/views/bro_hub/cache/purges/preview.html.erb +37 -0
- data/app/views/bro_hub/cache/shared/_store_tabs.html.erb +9 -0
- data/app/views/bro_hub/cache/shared/unsupported.html.erb +3 -0
- data/app/views/bro_hub/env/audit/index.html.erb +31 -0
- data/app/views/bro_hub/env/home/index.html.erb +25 -0
- data/app/views/bro_hub/env/proposals/_new_key_row.html.erb +7 -0
- data/app/views/bro_hub/env/proposals/new.html.erb +99 -0
- data/app/views/bro_hub/env/reviews/new.html.erb +29 -0
- data/app/views/bro_hub/env/reviews/show.html.erb +122 -0
- data/app/views/bro_hub/env/shared/_page_styles.html.erb +57 -0
- data/app/views/bro_hub/env/shared/_script.html.erb +114 -0
- data/app/views/bro_hub/env/shared/not_ready.html.erb +17 -0
- data/app/views/bro_hub/safe/flagged_commands/index.html.erb +22 -0
- data/app/views/bro_hub/safe/rules/show.html.erb +59 -0
- data/app/views/bro_hub/safe/sessions/index.html.erb +51 -0
- data/app/views/bro_hub/safe/sessions/show.html.erb +92 -0
- data/app/views/bro_hub/safe/unlocks/index.html.erb +25 -0
- data/app/views/bro_hub/shared/schema_missing.html.erb +25 -0
- data/app/views/layouts/bro_hub/application.html.erb +11 -2
- data/config/routes/apm.rb +10 -0
- data/config/routes/cache.rb +17 -0
- data/config/routes/env.rb +10 -0
- data/config/routes/safe.rb +10 -0
- data/config/routes.rb +12 -3
- data/db/migrate/20261129000000_create_bro_hub_tables.rb +85 -0
- data/lib/bro_hub/apm/api.rb +88 -0
- data/lib/bro_hub/apm/configuration.rb +58 -0
- data/lib/bro_hub/apm/local_status.rb +49 -0
- data/lib/bro_hub/apm.rb +28 -0
- data/lib/bro_hub/audit.rb +41 -0
- data/lib/bro_hub/cache/audit.rb +15 -0
- data/lib/bro_hub/cache/configuration.rb +105 -0
- data/lib/bro_hub/cache/errors.rb +26 -0
- data/lib/bro_hub/cache/redis_store.rb +220 -0
- data/lib/bro_hub/cache.rb +38 -0
- data/lib/bro_hub/configuration.rb +19 -4
- data/lib/bro_hub/controller_base.rb +3 -6
- data/lib/bro_hub/engine.rb +60 -1
- data/lib/bro_hub/env/audit.rb +17 -0
- data/lib/bro_hub/env/configuration.rb +72 -0
- data/lib/bro_hub/env/credentials.rb +135 -0
- data/lib/bro_hub/env/deployments.rb +45 -0
- data/lib/bro_hub/env/differ.rb +58 -0
- data/lib/bro_hub/env/editor.rb +104 -0
- data/lib/bro_hub/env/encrypted_blob.rb +18 -0
- data/lib/bro_hub/env/environment.rb +74 -0
- data/lib/bro_hub/env.rb +36 -0
- data/lib/bro_hub/features.rb +13 -0
- data/lib/bro_hub/layout.rb +2 -5
- data/lib/bro_hub/registry.rb +5 -0
- data/lib/bro_hub/safe/cipher.rb +46 -0
- data/lib/bro_hub/safe/commands.rb +25 -0
- data/lib/bro_hub/safe/configuration.rb +191 -0
- data/lib/bro_hub/safe/encrypted_type.rb +16 -0
- data/lib/bro_hub/safe/errors.rb +48 -0
- data/lib/bro_hub/safe/executor.rb +142 -0
- data/lib/bro_hub/safe/guards/const_guard.rb +42 -0
- data/lib/bro_hub/safe/guards/credentials_guard.rb +31 -0
- data/lib/bro_hub/safe/guards/env_guard.rb +90 -0
- data/lib/bro_hub/safe/guards/file_guard.rb +28 -0
- data/lib/bro_hub/safe/guards/guarded.rb +51 -0
- data/lib/bro_hub/safe/guards/irb_context.rb +23 -0
- data/lib/bro_hub/safe/guards/method_guard.rb +32 -0
- data/lib/bro_hub/safe/guards/network_guard.rb +98 -0
- data/lib/bro_hub/safe/guards/sql_guard.rb +26 -0
- data/lib/bro_hub/safe/parsed_command.rb +66 -0
- data/lib/bro_hub/safe/recorder.rb +82 -0
- data/lib/bro_hub/safe/shield.rb +83 -0
- data/lib/bro_hub/safe/supervisor.rb +81 -0
- data/lib/bro_hub/safe/terminal.rb +55 -0
- data/lib/bro_hub/safe/trail.rb +24 -0
- data/lib/bro_hub/safe/validator.rb +74 -0
- data/lib/bro_hub/safe.rb +134 -0
- data/lib/bro_hub/schema.rb +36 -0
- data/lib/bro_hub/section.rb +14 -10
- data/lib/bro_hub/testing.rb +62 -0
- data/lib/bro_hub/version.rb +1 -1
- data/lib/bro_hub.rb +32 -6
- data/lib/generators/bro_hub/install/install_generator.rb +93 -69
- data/lib/generators/bro_hub/install/templates/bro_hub.rb.tt +115 -0
- data/lib/generators/bro_hub/uninstall/templates/drop_bro_hub_tables.rb.tt +16 -0
- data/lib/generators/bro_hub/uninstall/uninstall_generator.rb +57 -0
- data/lib/tasks/bro_hub.rake +9 -0
- metadata +155 -10
- data/lib/bro_hub/root_middleware.rb +0 -17
- data/lib/generators/bro_hub/install/templates/bro_hub.rb +0 -11
- data/lib/generators/bro_hub/installable.rb +0 -63
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
module BroHub::Safe
|
|
2
|
+
module Guards
|
|
3
|
+
# Runtime backstop for config.forbidden_methods, and the only check for config.runtime_only_methods. The
|
|
4
|
+
# validator catches `eval(...)` written out, this catches `send(:eval, ...)`, `method(:eval).call`, and friends.
|
|
5
|
+
module MethodGuard
|
|
6
|
+
def self.install
|
|
7
|
+
(BroHub::Safe.config.forbidden_methods + BroHub::Safe.config.runtime_only_methods).uniq.each do |entry|
|
|
8
|
+
owner_name, separator, method_name = entry.partition(/[#.]/)
|
|
9
|
+
owner = owner_name.safe_constantize or next
|
|
10
|
+
scope = separator == "." ? :singleton : :instance
|
|
11
|
+
install_one(owner, method_name.to_sym, scope, entry)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.install_one(target, name, scope, entry)
|
|
16
|
+
owner = scope == :singleton ? target.singleton_class : target
|
|
17
|
+
visibility = %i[ public protected private ].find { |v| owner.send(:"#{v}_instance_methods", false).include?(name) } or return
|
|
18
|
+
|
|
19
|
+
guard = Module.new
|
|
20
|
+
guard.define_method(name) do |*args, **kwargs, &block|
|
|
21
|
+
if BroHub::Safe.executor.executing_user_command? && BroHub::Safe.executor.from_console?(caller_locations(1, 1))
|
|
22
|
+
BroHub::Safe.stop!(ForbiddenCommand.new("Calling #{entry} is not allowed."))
|
|
23
|
+
end
|
|
24
|
+
super(*args, **kwargs, &block)
|
|
25
|
+
end
|
|
26
|
+
guard.send(visibility, name)
|
|
27
|
+
Guards.built << guard
|
|
28
|
+
owner.prepend(guard)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
require "socket"
|
|
2
|
+
require "uri"
|
|
3
|
+
|
|
4
|
+
module BroHub::Safe
|
|
5
|
+
module Guards
|
|
6
|
+
# Stops connections to protected systems (Redis, Elasticsearch, ...) while the network
|
|
7
|
+
# resource is locked. It checks new connections and writes on existing ones, so a
|
|
8
|
+
# connection pool opened before the command started doesn't get a free pass.
|
|
9
|
+
module NetworkGuard
|
|
10
|
+
class << self
|
|
11
|
+
def check!(host:, port:)
|
|
12
|
+
return unless BroHub::Safe.executor.executing_user_command? && BroHub::Safe.shield.protects?(:network)
|
|
13
|
+
return unless protected?(host, port)
|
|
14
|
+
|
|
15
|
+
BroHub::Safe.stop!(ProtectedResource.new(:network, "(#{host}:#{port})"))
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Existing connection: look at who it is connected to.
|
|
19
|
+
def check_socket!(socket)
|
|
20
|
+
address = socket.respond_to?(:remote_address) ? socket.remote_address : socket.to_io.remote_address
|
|
21
|
+
check!(host: address.ip_address, port: address.ip_port) if address.ip?
|
|
22
|
+
rescue SocketError, Errno::ENOTCONN, IOError
|
|
23
|
+
nil # not connected (yet): nothing to protect
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def protected?(host, port)
|
|
27
|
+
targets.any? do |target|
|
|
28
|
+
target[:port] == port.to_i && (target[:host] == host.to_s || target[:addresses].include?(host.to_s))
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def reset!
|
|
33
|
+
@targets = nil
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
def targets
|
|
38
|
+
@targets ||= BroHub::Safe.config.protected_urls.map do |url|
|
|
39
|
+
host, port = host_and_port(url)
|
|
40
|
+
{ host: host, port: port.to_i, addresses: resolve(host, port) }
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def resolve(host, port)
|
|
45
|
+
Addrinfo.getaddrinfo(host, port, nil, :STREAM).map(&:ip_address)
|
|
46
|
+
rescue SocketError
|
|
47
|
+
[]
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def host_and_port(url)
|
|
51
|
+
uri = URI.parse(url.to_s)
|
|
52
|
+
return [ uri.host, uri.port ] if uri.host
|
|
53
|
+
|
|
54
|
+
host, _, port = url.to_s.rpartition(":")
|
|
55
|
+
[ host, port ]
|
|
56
|
+
rescue URI::InvalidURIError
|
|
57
|
+
host, _, port = url.to_s.rpartition(":")
|
|
58
|
+
[ host, port ]
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
module Connect
|
|
63
|
+
def initialize(host, port, *args, **kwargs)
|
|
64
|
+
NetworkGuard.check!(host: host, port: port)
|
|
65
|
+
super
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
module Writes
|
|
70
|
+
%i[ write write_nonblock syswrite ].each do |name|
|
|
71
|
+
define_method(name) do |*args, **kwargs, &block|
|
|
72
|
+
NetworkGuard.check_socket!(self) if BroHub::Safe.executor.executing_user_command?
|
|
73
|
+
super(*args, **kwargs, &block)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
module SocketConnect
|
|
79
|
+
%i[ connect connect_nonblock ].each do |name|
|
|
80
|
+
define_method(name) do |remote, *args, **kwargs|
|
|
81
|
+
if BroHub::Safe.executor.executing_user_command?
|
|
82
|
+
address = remote.is_a?(Addrinfo) ? remote : Addrinfo.new(remote)
|
|
83
|
+
NetworkGuard.check!(host: address.ip_address, port: address.ip_port) if address.ip?
|
|
84
|
+
end
|
|
85
|
+
super(remote, *args, **kwargs)
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def self.install
|
|
91
|
+
TCPSocket.prepend(Connect)
|
|
92
|
+
BasicSocket.prepend(Writes)
|
|
93
|
+
Socket.prepend(SocketConnect)
|
|
94
|
+
OpenSSL::SSL::SSLSocket.prepend(Writes) if defined?(OpenSSL::SSL::SSLSocket)
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
module BroHub::Safe
|
|
2
|
+
module Guards
|
|
3
|
+
# Every statement ActiveRecord runs goes through the adapter's #log. A command that
|
|
4
|
+
# mentions one of BroHub's tables (to read, change or delete the audit trail) is stopped here.
|
|
5
|
+
module SqlGuard
|
|
6
|
+
# The console tables and the audit table every feature shares.
|
|
7
|
+
TABLES = /bro_hub_/i
|
|
8
|
+
|
|
9
|
+
# Prepends the statement check, and protects the adapter's raw driver connection (which runs SQL without #log)
|
|
10
|
+
# while locked. Both are defined on AbstractAdapter itself (with_raw_connection privately).
|
|
11
|
+
def self.install
|
|
12
|
+
ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend(self)
|
|
13
|
+
Guarded.install(ActiveRecord::ConnectionAdapters::AbstractAdapter, %i[raw_connection with_raw_connection], :credentials)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
def log(sql, *args, **kwargs, &block)
|
|
18
|
+
if BroHub::Safe.executor.executing_user_command? && sql.to_s.match?(TABLES)
|
|
19
|
+
BroHub::Safe.stop!(TamperingAttempt.new("The audit trail can't be touched from the console."))
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
super
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
require "ripper"
|
|
2
|
+
|
|
3
|
+
module BroHub::Safe
|
|
4
|
+
# A lenient lexical view of a console command. It uses Ripper's tokenizer, which keeps
|
|
5
|
+
# producing tokens for code that doesn't parse, so a syntax trick can't hide a constant
|
|
6
|
+
# or method name from the validations.
|
|
7
|
+
class ParsedCommand
|
|
8
|
+
attr_reader :source
|
|
9
|
+
|
|
10
|
+
def initialize(source)
|
|
11
|
+
@source = Array(source).join("\n")
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def tokens
|
|
15
|
+
@tokens ||= Ripper.lex(source).map { |(_pos, type, text, _state)| [ type, text ] }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Constant paths as written: "ActiveRecord::Encryption::Config" (leading :: dropped).
|
|
19
|
+
def constant_paths
|
|
20
|
+
@constant_paths ||= begin
|
|
21
|
+
paths = []
|
|
22
|
+
significant = tokens.reject { |type, _| type == :on_sp || type == :on_ignored_nl || type == :on_nl }
|
|
23
|
+
significant.each_with_index do |(type, text), i|
|
|
24
|
+
next unless type == :on_const
|
|
25
|
+
|
|
26
|
+
previous = significant[i - 1]
|
|
27
|
+
after_scope = i > 0 && previous == [ :on_op, "::" ]
|
|
28
|
+
qualified = after_scope && i > 1 && significant[i - 2][0] == :on_const
|
|
29
|
+
if qualified && paths.any?
|
|
30
|
+
paths[-1] = "#{paths.last}::#{text}"
|
|
31
|
+
else
|
|
32
|
+
paths << text
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
paths
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Names after `class`/`module`: the things this command (re)opens.
|
|
40
|
+
def reopened_constants
|
|
41
|
+
@reopened_constants ||= begin
|
|
42
|
+
significant = tokens.reject { |type, _| type == :on_sp }
|
|
43
|
+
significant.each_with_index.filter_map do |(type, text), i|
|
|
44
|
+
next unless type == :on_kw && %w[ class module ].include?(text)
|
|
45
|
+
|
|
46
|
+
name = +""
|
|
47
|
+
significant[(i + 1)..].each do |t, x|
|
|
48
|
+
break unless t == :on_const || (t == :on_op && x == "::")
|
|
49
|
+
name << x
|
|
50
|
+
end
|
|
51
|
+
name.delete_prefix("::").presence
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Identifiers that might be method calls, plus backtick/%x shell execution as "`".
|
|
57
|
+
def method_names
|
|
58
|
+
@method_names ||= tokens.filter_map do |type, text|
|
|
59
|
+
case type
|
|
60
|
+
when :on_ident, :on_const, :on_kw then text
|
|
61
|
+
when :on_backtick then "`"
|
|
62
|
+
end
|
|
63
|
+
end.uniq
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
module BroHub::Safe
|
|
2
|
+
# Writes the audit trail. Everything here runs as the system (never as a user command),
|
|
3
|
+
# and silences SQL logging so the trail doesn't spill into the console.
|
|
4
|
+
class Recorder
|
|
5
|
+
attr_reader :session, :unlock
|
|
6
|
+
|
|
7
|
+
def start_session(username:, reason:, source: "console", client_ip: nil)
|
|
8
|
+
quietly do
|
|
9
|
+
@session = Session.create!(
|
|
10
|
+
username: username, reason: reason, source: source, client_ip: client_ip,
|
|
11
|
+
hostname: Socket.gethostname, pid: Process.pid, started_at: Time.current
|
|
12
|
+
)
|
|
13
|
+
Trail.record(:session_started, session: @session, reason: reason, via: source)
|
|
14
|
+
end
|
|
15
|
+
BroHub::Safe.notify(:session_started, session: @session)
|
|
16
|
+
@session
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def finish_session
|
|
20
|
+
return unless @session
|
|
21
|
+
|
|
22
|
+
quietly do
|
|
23
|
+
end_unlock
|
|
24
|
+
@session.update!(ended_at: Time.current)
|
|
25
|
+
end
|
|
26
|
+
@session = nil
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# resources: array of symbols, e.g. [:all] or [:credentials]
|
|
30
|
+
def start_unlock(resources, justification)
|
|
31
|
+
quietly do
|
|
32
|
+
end_unlock
|
|
33
|
+
@unlock = session.unlocks.create!(
|
|
34
|
+
resources: resources.join(","), justification: justification, started_at: Time.current
|
|
35
|
+
)
|
|
36
|
+
Trail.record(:sensitive_unlocked, session: session, reason: justification, resources: resources.map(&:to_s))
|
|
37
|
+
end
|
|
38
|
+
BroHub::Safe.notify(:unlocked, session: session, unlock: @unlock)
|
|
39
|
+
@unlock
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def end_unlock
|
|
43
|
+
return unless @unlock
|
|
44
|
+
|
|
45
|
+
quietly { @unlock.update!(ended_at: Time.current) }
|
|
46
|
+
@unlock = nil
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Records a command before it runs, linked to the current unlock window if any.
|
|
50
|
+
def record_command(statements)
|
|
51
|
+
quietly do
|
|
52
|
+
session.commands.create!(statements: Array(statements).join("\n").strip, unlock: unlock)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def flag(command, status, reason)
|
|
57
|
+
quietly do
|
|
58
|
+
command.update!(status: status, flag_reason: reason)
|
|
59
|
+
Trail.record(:command_flagged, session: session, reason: reason, status: status.to_s, command_id: command.id)
|
|
60
|
+
end
|
|
61
|
+
BroHub::Safe.notify(:command_flagged, session: session, command: command, status: status, reason: reason)
|
|
62
|
+
command
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
private
|
|
66
|
+
def quietly(&block)
|
|
67
|
+
BroHub::Safe.executor.run_as_system do
|
|
68
|
+
if BroHub::Safe.debug
|
|
69
|
+
ensure_connected(&block)
|
|
70
|
+
else
|
|
71
|
+
ActiveRecord::Base.logger.silence { ensure_connected(&block) }
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def ensure_connected
|
|
77
|
+
connection = Session.connection
|
|
78
|
+
connection.reconnect! unless connection.active?
|
|
79
|
+
yield
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
module BroHub::Safe
|
|
2
|
+
# The shield decides what is protected right now. Everything starts locked. unlock! asks for
|
|
3
|
+
# a justification, records it, and opens the requested resources until lock!.
|
|
4
|
+
#
|
|
5
|
+
# Resources:
|
|
6
|
+
# encrypted_data - Active Record encrypted attributes read as ciphertext
|
|
7
|
+
# credentials - Rails credentials, their keys, secret_key_base
|
|
8
|
+
# env - ENV values whose names look secret
|
|
9
|
+
# files - master.key, .env files
|
|
10
|
+
# network - connections to config.protected_urls
|
|
11
|
+
class Shield
|
|
12
|
+
RESOURCES = %i[ encrypted_data credentials env files network ].freeze
|
|
13
|
+
|
|
14
|
+
def initialize
|
|
15
|
+
@unlocked = Set.new
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def locked?
|
|
19
|
+
@unlocked.empty?
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def unlocked?(resource)
|
|
23
|
+
@unlocked.include?(:all) || @unlocked.include?(resource)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Is this resource protected from the console right now?
|
|
27
|
+
def protects?(resource)
|
|
28
|
+
BroHub::Safe.config.protected_resources.include?(resource) && !unlocked?(resource)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def unlock(resources = [ :all ])
|
|
32
|
+
unknown = resources - RESOURCES - BroHub::Safe.config.protected_resources - [ :all ]
|
|
33
|
+
raise ArgumentError, "Unknown resource #{unknown.first.inspect}. Choose from #{(RESOURCES | BroHub::Safe.config.protected_resources).join(', ')}." if unknown.any?
|
|
34
|
+
|
|
35
|
+
BroHub::Safe.executor.run_as_system do
|
|
36
|
+
justification = Terminal.ask(BroHub::Safe.config.unlock_prompt)
|
|
37
|
+
BroHub::Safe.recorder.start_unlock(resources, justification)
|
|
38
|
+
@unlocked = Set.new(resources)
|
|
39
|
+
Terminal.say(BroHub::Safe.config.unlocked_message, :yellow)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def lock(silent: false)
|
|
44
|
+
BroHub::Safe.executor.run_as_system do
|
|
45
|
+
was_unlocked = !locked?
|
|
46
|
+
BroHub::Safe.recorder.end_unlock if BroHub::Safe.recorder.session
|
|
47
|
+
@unlocked = Set.new
|
|
48
|
+
Terminal.say(BroHub::Safe.config.locked_message, :yellow) if was_unlocked && !silent
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Runs the block with the protections that work by wrapping execution switched on.
|
|
53
|
+
def protect(&block)
|
|
54
|
+
if protects?(:encrypted_data) && encryption_available?
|
|
55
|
+
ActiveRecord::Encryption.protecting_encrypted_data(&block)
|
|
56
|
+
else
|
|
57
|
+
block.call
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def install
|
|
62
|
+
Guards::SqlGuard.install
|
|
63
|
+
Guards::NetworkGuard.install if BroHub::Safe.config.protected_urls.any?
|
|
64
|
+
Guards::CredentialsGuard.install
|
|
65
|
+
Guards::EnvGuard.install
|
|
66
|
+
Guards::FileGuard.install
|
|
67
|
+
Guards::ConstGuard.install
|
|
68
|
+
Guards::MethodGuard.install
|
|
69
|
+
install_irb
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
private
|
|
73
|
+
def install_irb
|
|
74
|
+
IRB::Context.prepend(Guards::IrbContext)
|
|
75
|
+
IRB::Command.register(:unlock!, Commands::Unlock)
|
|
76
|
+
IRB::Command.register(:lock!, Commands::Lock)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def encryption_available?
|
|
80
|
+
defined?(ActiveRecord::Encryption) && ActiveRecord::Encryption.respond_to?(:protecting_encrypted_data)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
require "ipaddr"
|
|
2
|
+
|
|
3
|
+
module BroHub::Safe
|
|
4
|
+
# Entry point for a console (or runner) process: installs the protections once, then
|
|
5
|
+
# identifies the person, asks why, and opens a session.
|
|
6
|
+
class Supervisor
|
|
7
|
+
def run_console
|
|
8
|
+
install
|
|
9
|
+
start
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def run_runner
|
|
13
|
+
start(source: "runner", reason: runner_reason, username: runner_username, interactive: false)
|
|
14
|
+
BroHub::Safe.recorder.record_command(runner_statements)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def install
|
|
18
|
+
return if @installed
|
|
19
|
+
|
|
20
|
+
# Loaded ahead of time: without this, the first command in a real console raised
|
|
21
|
+
# "uninitialized constant ActiveRecord::QueryMethods::Relation" (console1984 hit the same).
|
|
22
|
+
require "active_record/relation"
|
|
23
|
+
BroHub::Safe.shield.install
|
|
24
|
+
BroHub::Safe.lock_down! if BroHub::Safe.config.freeze_internals
|
|
25
|
+
@installed = true
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Fails closed: without the console tables nothing could be recorded, so no session starts.
|
|
29
|
+
def start(source: "console", reason: nil, username: nil, interactive: true)
|
|
30
|
+
raise SchemaMissing unless BroHub::Schema.ready?
|
|
31
|
+
|
|
32
|
+
BroHub::Safe.shield.lock(silent: true)
|
|
33
|
+
Terminal.say(BroHub::Safe.config.welcome_message, :yellow) if interactive
|
|
34
|
+
username ||= current_username
|
|
35
|
+
reason ||= session_reason(username)
|
|
36
|
+
BroHub::Safe.recorder.start_session(username: username, reason: reason, source: source, client_ip: current_client_ip)
|
|
37
|
+
at_exit { stop }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def stop
|
|
41
|
+
BroHub::Safe.recorder.finish_session
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def current_username
|
|
45
|
+
BroHub::Safe.config.username_resolver.call.to_s.strip.presence ||
|
|
46
|
+
(BroHub::Safe.config.ask_for_username_if_empty && Terminal.interactive? && Terminal.ask("Please, enter your name:")) ||
|
|
47
|
+
raise(MissingUsername)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# nil when unknown or not an IP address, so a bad value can't put junk into the audit trail.
|
|
51
|
+
def current_client_ip
|
|
52
|
+
ip = BroHub::Safe.config.client_ip_resolver&.call.to_s.strip
|
|
53
|
+
IPAddr.new(ip).to_s if ip.present?
|
|
54
|
+
rescue IPAddr::Error
|
|
55
|
+
nil
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
private
|
|
59
|
+
# CONSOLE_REASON answers without a terminal; otherwise ask, and refuse to continue without one.
|
|
60
|
+
def session_reason(username)
|
|
61
|
+
env_reason = ENV["CONSOLE_REASON"].to_s.squish.first(255).presence
|
|
62
|
+
return env_reason if env_reason
|
|
63
|
+
raise MissingReason unless Terminal.interactive?
|
|
64
|
+
|
|
65
|
+
Terminal.ask("#{username}, why are you using this console today?")
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Runners are usually automation: don't refuse, but record that nobody said who or why.
|
|
69
|
+
def runner_reason
|
|
70
|
+
ENV["CONSOLE_REASON"].to_s.squish.first(255).presence || "(no reason given)"
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def runner_username
|
|
74
|
+
BroHub::Safe.config.username_resolver.call.to_s.strip.presence || "(unattributed)"
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def runner_statements
|
|
78
|
+
"rails runner #{ARGV.join(' ')}".strip
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
module BroHub::Safe
|
|
2
|
+
# Talking to the person at the console.
|
|
3
|
+
module Terminal
|
|
4
|
+
COLORS = { green: 32, yellow: 33, red: 31, blue: 34 }.freeze
|
|
5
|
+
|
|
6
|
+
class << self
|
|
7
|
+
# Tests silence console output.
|
|
8
|
+
attr_accessor :muted
|
|
9
|
+
|
|
10
|
+
def interactive?
|
|
11
|
+
$stdin.tty?
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def say(message, color = nil)
|
|
15
|
+
return if muted
|
|
16
|
+
|
|
17
|
+
text = "\n#{message}\n"
|
|
18
|
+
$stdout.puts(color && $stdout.tty? ? "\e[#{COLORS.fetch(color)}m#{text}\e[0m" : text)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Something worth the person's attention, on stderr so it doesn't mix with output they pipe.
|
|
22
|
+
def alert(message)
|
|
23
|
+
$stderr.puts("SafeBro: #{message}") unless muted
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Asks until it gets a non-blank answer. Raises NotInteractive when there's nobody to ask.
|
|
27
|
+
def ask(question)
|
|
28
|
+
raise NotInteractive unless interactive?
|
|
29
|
+
|
|
30
|
+
say(question, :green)
|
|
31
|
+
answer = nil
|
|
32
|
+
with_plain_output do
|
|
33
|
+
loop do
|
|
34
|
+
line = Reline.readline("> ", false)
|
|
35
|
+
raise NotInteractive, "No answer given." if line.nil?
|
|
36
|
+
|
|
37
|
+
answer = line.strip
|
|
38
|
+
break unless answer.empty?
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
answer
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
# IRB colorizes what you type; the answer should look like plain text.
|
|
46
|
+
def with_plain_output
|
|
47
|
+
original = Reline.output_modifier_proc
|
|
48
|
+
Reline.output_modifier_proc = nil
|
|
49
|
+
yield
|
|
50
|
+
ensure
|
|
51
|
+
Reline.output_modifier_proc = original
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module BroHub::Safe
|
|
2
|
+
# The key moments of a session, copied to BroHub's shared audit log so they show up next to what the other
|
|
3
|
+
# features record: a session starting, the console being unlocked, a command being flagged, a verdict.
|
|
4
|
+
#
|
|
5
|
+
# The console tables stay the full record (every command, every unlock window). This is the short version,
|
|
6
|
+
# and it never holds a command, its output or a reviewer's notes. It is best effort: the console tables are
|
|
7
|
+
# written first, so a failing audit write must not stop a console from opening.
|
|
8
|
+
module Trail
|
|
9
|
+
SOURCE = "safe".freeze
|
|
10
|
+
|
|
11
|
+
module_function
|
|
12
|
+
|
|
13
|
+
def record(event, session:, user: session.username, reason: nil, **details)
|
|
14
|
+
BroHub::Audit.record(
|
|
15
|
+
source: SOURCE, event: event, user: user.to_s,
|
|
16
|
+
context: session.hostname, target: "session ##{session.id}", reference: session.id.to_s,
|
|
17
|
+
reason: reason, ip: session.client_ip, **details
|
|
18
|
+
)
|
|
19
|
+
rescue StandardError => error
|
|
20
|
+
Rails.logger&.error("BroHub::Safe: could not copy #{event} to the BroHub audit log (#{error.class}: #{error.message})")
|
|
21
|
+
nil
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
module BroHub::Safe
|
|
2
|
+
# Static analysis of a console command, before it runs. Each validation raises
|
|
3
|
+
# ForbiddenCommand (blocked) or SuspiciousCommand (runs, but is flagged).
|
|
4
|
+
#
|
|
5
|
+
# This is a speed bump for honest mistakes and a tripwire for dishonest attempts. It is not
|
|
6
|
+
# a sandbox: Ruby is too dynamic for that. The runtime guards (see Guards) cover the most
|
|
7
|
+
# obvious ways around it, and the rest is what the audit trail is for.
|
|
8
|
+
class Validator
|
|
9
|
+
class Validation
|
|
10
|
+
def initialize(config)
|
|
11
|
+
@config = config
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
attr_reader :config
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# class Foo / module Foo for a protected Foo or Foo::Bar.
|
|
18
|
+
class ForbiddenReopening < Validation
|
|
19
|
+
def validate(command, locked:)
|
|
20
|
+
command.reopened_constants.each do |name|
|
|
21
|
+
if config.forbidden_reopening.any? { |forbidden| name == forbidden || name.start_with?("#{forbidden}::") }
|
|
22
|
+
raise ForbiddenCommand, "Reopening #{name} is not allowed."
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
class ForbiddenConstantReference < Validation
|
|
29
|
+
def validate(command, locked:)
|
|
30
|
+
forbidden = config.forbidden_constants.fetch(:always, [])
|
|
31
|
+
forbidden += config.forbidden_constants.fetch(:locked, []) if locked
|
|
32
|
+
command.constant_paths.each do |path|
|
|
33
|
+
match = forbidden.find { |f| path == f || path.start_with?("#{f}::") }
|
|
34
|
+
raise ForbiddenCommand, "Referencing #{match} is not allowed#{' while locked' unless config.forbidden_constants.fetch(:always, []).include?(match)}." if match
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Method names from forbidden_methods ("Kernel#eval" -> eval), wherever they appear.
|
|
40
|
+
class ForbiddenMethods < Validation
|
|
41
|
+
def validate(command, locked:)
|
|
42
|
+
names = config.forbidden_methods.map { |entry| entry.split(/[#.]/, 2).last }.uniq
|
|
43
|
+
used = command.method_names & names
|
|
44
|
+
raise ForbiddenCommand, "Calling #{used.first} is not allowed." if used.any?
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
class SuspiciousTerms < Validation
|
|
49
|
+
def validate(command, locked:)
|
|
50
|
+
term = config.suspicious_terms.find { |t| command.source.include?(t) }
|
|
51
|
+
raise SuspiciousCommand, "Command mentions #{term}." if term
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
VALIDATIONS = [ ForbiddenReopening, ForbiddenConstantReference, ForbiddenMethods, SuspiciousTerms ].freeze
|
|
56
|
+
|
|
57
|
+
def initialize(config = BroHub::Safe.config)
|
|
58
|
+
@validations = VALIDATIONS.map { |klass| klass.new(config) }
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Raises on the first failing validation. Forbidden beats suspicious, so run every
|
|
62
|
+
# validation and only then raise a suspicion.
|
|
63
|
+
def validate(source, locked: true)
|
|
64
|
+
command = ParsedCommand.new(source)
|
|
65
|
+
suspicion = nil
|
|
66
|
+
@validations.each do |validation|
|
|
67
|
+
validation.validate(command, locked: locked)
|
|
68
|
+
rescue SuspiciousCommand => error
|
|
69
|
+
suspicion ||= error
|
|
70
|
+
end
|
|
71
|
+
raise suspicion if suspicion
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|