Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/msf/core/auxiliary/prometheus.rb
Views: 11784
# -*- coding: binary -*-12module Msf3###4#5# This module provides methods for working with Prometheus node exporter6#7###8module Auxiliary::Prometheus9include Msf::Auxiliary::Report1011# returns username, password12def process_authorization(auth)13if auth['credentials']14# credential foobar15return '', auth['credentials']16elsif auth['credentials_file']17# type: Bearer18# credentials_file: /var/run/secrets/kubernetes.io/serviceaccount/token19return auth['type'], auth['credentials_file']20end21end2223# processes a generic URI for creds24def process_embedded_uri(uri, job_name, config_name)25uri = URI(uri)26cred = credential_data27cred[:port] = uri.port28cred[:address] = uri.host29cred[:username] = uri.user30cred[:private_data] = uri.password31cred[:service_name] = uri.scheme32create_credential_and_login(cred)33@table_creds << [34job_name,35config_name,36uri.host,37uri.port,38uri.user,39uri.password,40''41]42end4344def credential_data45{46# these 4 need to be changed every time47# address: thost,48# port: tport,49# username: username50# private_data: hash51protocol: 'tcp',52workspace_id: myworkspace_id,53origin_type: :service,54private_type: :password,55service_name: '',56module_fullname: fullname,57status: Metasploit::Model::Login::Status::UNTRIED58}59end6061def process_dns_sd_configs(job_name, dns_sd_configs)62dns_sd_configs['names']&.each do |name|63username = dns_sd_configs.dig('basic_auth', 'username')64password = dns_sd_configs.dig('basic_auth', 'password')65password = dns_sd_configs.dig('basic_auth', 'password_file') if dns_sd_configs.dig('basic_auth', 'password_file')66uri = URI("#{dns_sd_configs['scheme']}://#{name}")67cred = credential_data68cred[:port] = uri.port69cred[:address] = uri.host70cred[:username] = dns_sd_configs.dig('basic_auth', 'username')71cred[:private_data] = password72cred[:service_name] = dns_sd_configs['scheme']73create_credential_and_login(cred)74@table_creds << [75job_name,76'dns_sd_configs',77uri.host,78uri.port,79username,80password,81''82]83end84end8586def process_consul_sd_configs(job_name, consul_sd_configs)87uri = URI("#{consul_sd_configs['scheme']}://#{consul_sd_configs['server']}")88cred = credential_data89cred[:port] = uri.port90cred[:address] = uri.host91cred[:username] = ''92cred[:private_data] = consul_sd_configs['token']93cred[:service_name] = consul_sd_configs['scheme']94create_credential_and_login(cred)95@table_creds << [96job_name,97'consul_sd_configs',98uri.host,99uri.port,100'',101consul_sd_configs['token'],102"Path Prefix: #{consul_sd_configs['path_prefix']}"103]104end105106def process_kubernetes_sd_configs(job_name, kubernetes_sd_configs)107username = kubernetes_sd_configs.dig('basic_auth', 'username')108password = kubernetes_sd_configs.dig('basic_auth', 'password')109password = kubernetes_sd_configs.dig('basic_auth', 'password_file') if kubernetes_sd_configs.dig('basic_auth', 'password_file')110111uri = URI(kubernetes_sd_configs['api_server'])112cred = credential_data113cred[:port] = uri.port114cred[:address] = uri.host115cred[:username] = username116cred[:private_data] = password117cred[:service_name] = uri.scheme118create_credential_and_login(cred)119@table_creds << [120job_name,121'kubernetes_sd_configs',122uri.host,123uri.port,124username,125password,126"Role: #{kubernetes_sd_configs['role']}"127]128end129130def process_kuma_sd_configs(job_name, targets)131return if targets['server'].nil?132return unless targets['server'].include? '@'133134process_embedded_uri(targets['server'], job_name, 'kuma_sd_configs')135end136137def process_marathon_sd_configs(job_name, marathon_sd_configs)138marathon_sd_configs['servers']&.each do |servers|139uri = URI(servers)140cred = credential_data141cred[:port] = uri.port142cred[:address] = uri.host143cred[:username] = ''144cred[:private_data] = marathon_sd_configs['auth_token']145cred[:service_name] = uri.scheme146create_credential_and_login(cred)147@table_creds << [148job_name,149'marathon_sd_configs',150uri.host,151uri.port,152'',153marathon_sd_configs['auth_token'],154''155]156end157end158159def process_nomad_sd_configs(job_name, targets)160return if targets['server'].nil?161return unless targets['server'].include? '@'162163process_embedded_uri(targets['server'], job_name, 'nomad_sd_configs')164end165166def process_ec2_sd_configs(job_name, ec2_sd_configs)167cred = credential_data168cred[:port] = ''169cred[:address] = ''170cred[:username] = ec2_sd_configs['access_key']171cred[:private_data] = ec2_sd_configs['secret_key']172cred[:service_name] = ''173create_credential_and_login(cred)174@table_creds << [175job_name,176'ec2_sd_configs',177'',178'',179ec2_sd_configs['access_key'],180ec2_sd_configs['secret_key'],181"Region: #{ec2_sd_configs['region']}, Profile: #{ec2_sd_configs['profile']}"182]183end184185def process_lightsail_sd_configs(job_name, lightsail_sd_configs)186cred = credential_data187cred[:port] = ''188cred[:address] = ''189cred[:username] = lightsail_sd_configs['access_key']190cred[:private_data] = lightsail_sd_configs['secret_key']191cred[:service_name] = ''192create_credential_and_login(cred)193@table_creds << [194job_name,195'lightsail_sd_configs',196'',197'',198lightsail_sd_configs['access_key'],199lightsail_sd_configs['secret_key'],200"Region: #{lightsail_sd_configs['region']}, Profile: #{lightsail_sd_configs['profile']}"201]202end203204def process_azure_sd_configs(job_name, azure_sd_configs)205cred = credential_data206cred[:port] = azure_sd_configs['port']207cred[:address] = ''208cred[:username] = azure_sd_configs['client_id']209cred[:private_data] = azure_sd_configs['client_secret']210cred[:service_name] = azure_sd_configs['authentication_method']211create_credential_and_login(cred)212@table_creds << [213job_name,214'azure_sd_configs',215'',216azure_sd_configs['port'],217azure_sd_configs['client_id'],218azure_sd_configs['client_secret'],219"Environment: #{azure_sd_configs['environment']}, Subscription ID: #{azure_sd_configs['subscription_id']}, Resource Group: #{azure_sd_configs['resource_group']}, Tenant ID: #{azure_sd_configs['tenant_id']}"220]221end222223def process_http_sd_configs(job_name, http_sd_configs)224return if http_sd_configs['url'].nil?225return unless http_sd_configs['url'].include? '@'226227process_embedded_uri(http_sd_configs['url'], job_name, 'http_sd_configs')228end229230def process_digitalocean_sd_configs(job_name, digitalocean_sd_configs)231username, password = process_authorization(digitalocean_sd_configs['authorization'])232cred = credential_data233cred[:port] = ''234cred[:address] = ''235cred[:username] = username236cred[:private_data] = password237create_credential_and_login(cred)238@table_creds << [239job_name,240'digitalocean_sd_configs',241'',242'',243username,244password,245''246]247end248249def process_hetzner_sd_configs(job_name, hetzner_sd_configs)250username = hetzner_sd_configs.dig('basic_auth', 'username')251password = hetzner_sd_configs.dig('basic_auth', 'password')252253username, password = process_authorization(hetzner_sd_configs['authorization']) if hetzner_sd_configs.dig('authorization', 'credentials')254255cred = credential_data256cred[:port] = ''257cred[:address] = ''258cred[:username] = username259cred[:private_data] = password260create_credential_and_login(cred)261@table_creds << [262job_name,263'hetzner_sd_configs',264'',265'',266username,267password,268hetzner_sd_configs['role']269]270end271272def process_eureka_sd_configs(job_name, eureka_sd_configs)273return if eureka_sd_configs['server'].nil?274return unless eureka_sd_configs['server'].include? '@'275276process_embedded_uri(eureka_sd_configs['server'], job_name, 'eureka_sd_configs')277end278279def process_ovhcloud_sd_configs(job_name, ovhcloud_sd_configs)280cred = credential_data281cred[:port] = ''282cred[:address] = ovhcloud_sd_configs['endpoint']283cred[:username] = ovhcloud_sd_configs['application_key']284cred[:private_data] = ovhcloud_sd_configs['application_secret']285cred[:service_name] = ovhcloud_sd_configs['service']286create_credential_and_login(cred)287@table_creds << [288job_name,289'ovhcloud_sd_configs',290ovhcloud_sd_configs['endpoint'],291'',292ovhcloud_sd_configs['application_key'],293ovhcloud_sd_configs['application_secret'],294"Consumer Key: #{ovhcloud_sd_configs['consumer_key']}, Service: #{ovhcloud_sd_configs['service']}"295]296end297298def process_scaleway_sd_configs(job_name, scaleway_sd_configs)299cred = credential_data300cred[:port] = ''301cred[:address] = ''302cred[:username] = scaleway_sd_configs['access_key']303cred[:private_data] = scaleway_sd_configs['secret_key']304cred[:service_name] = scaleway_sd_configs['role']305create_credential_and_login(cred)306@table_creds << [307job_name,308'scaleway_sd_configs',309'',310'',311scaleway_sd_configs['access_key'],312scaleway_sd_configs['secret_key'],313"Project ID: #{scaleway_sd_configs['project_id']}, Role: #{scaleway_sd_configs['role']}"314]315end316317def process_linode_sd_configs(job_name, linode_sd_configs)318username, password = process_authorization(linode_sd_configs['authorization'])319cred = credential_data320cred[:port] = ''321cred[:address] = ''322cred[:username] = username323cred[:private_data] = password324create_credential_and_login(cred)325@table_creds << [326job_name,327'linode_sd_configs',328'',329'',330username,331password,332''333]334end335336def process_uyuni_sd_configs(job_name, uyuni_sd_configs)337uri = URI(uyuni_sd_configs['server'])338cred = credential_data339cred[:port] = uri.port340cred[:address] = uri.host341cred[:username] = uyuni_sd_configs['username']342cred[:private_data] = uyuni_sd_configs['password']343cred[:service_name] = uri.scheme344create_credential_and_login(cred)345@table_creds << [346job_name,347'uyuni_sd_configs',348uri.host,349uri.port,350uyuni_sd_configs['username'],351uyuni_sd_configs['password'],352''353]354end355356def process_ionos_sd_configs(job_name, ionos_sd_configs)357_username, password = process_authorization(ionos_sd_configs['authorization'])358# we may hit an issue here where we have a type stored in username, but use datacenter_id359# as the username360cred = credential_data361cred[:port] = ''362cred[:address] = ''363cred[:username] = ionos_sd_configs['datacenter_id']364cred[:private_data] = password365create_credential_and_login(cred)366@table_creds << [367job_name,368'ionos_sd_configs',369'',370'',371ionos_sd_configs['datacenter_id'],372password,373''374]375end376377def process_vultr_sd_configs(job_name, vultr_sd_configs)378username, password = process_authorization(vultr_sd_configs['authorization'])379cred = credential_data380cred[:port] = ''381cred[:address] = ''382cred[:username] = username383cred[:private_data] = password384create_credential_and_login(cred)385@table_creds << [386job_name,387'vultr_sd_configs',388'',389'',390username,391password,392''393]394end395396def prometheus_config_eater(yamlconf)397@table_creds = Rex::Text::Table.new(398'Header' => 'Credentials',399'Indent' => 2,400'Columns' =>401[402'Name',403'Config',404'Host',405'Port',406'Public/Username',407'Private/Password/Token',408'Notes'409]410)411412yamlconf['scrape_configs']&.each do |scrape|413# check for targets which have creds built in to the URL414if scrape['static_configs']415scrape['static_configs']&.each do |static|416static['targets']&.each do |target|417if target.include? '@'418uri = URI(target)419cred = credential_data420cred[:port] = uri.port421cred[:address] = uri.host422cred[:username] = uri.user423cred[:private_data] = uri.password424cred[:service_name] = uri.scheme425create_credential_and_login(cred)426@table_creds << [427scrape['job_name'],428'static_configs Target',429uri.host,430uri.port,431uri.user,432uri.password,433''434]435end436end437end438elsif scrape['dns_sd_configs']439scrape['dns_sd_configs']&.each do |dns_sd_configs|440# pass in basic_auth from the level above441if dns_sd_configs['basic_auth'].nil? && scrape['basic_auth']442dns_sd_configs['basic_auth'] = {}443dns_sd_configs['basic_auth']['username'] = scrape.dig('basic_auth', 'username') if scrape.dig('basic_auth', 'username')444dns_sd_configs['basic_auth']['password'] = scrape.dig('basic_auth', 'password') if scrape.dig('basic_auth', 'password')445dns_sd_configs['basic_auth']['password_file'] = scrape.dig('basic_auth', 'password_file') if scrape.dig('basic_auth', 'password_file')446end447448# pass in the 'scheme' from a level above to properly build the URI449if dns_sd_configs['scheme'].nil? && scrape['scheme']450dns_sd_configs['scheme'] = scrape['scheme']451end452453process_dns_sd_configs(scrape['job_name'], dns_sd_configs)454end455elsif scrape['consul_sd_configs']456scrape['consul_sd_configs']&.each do |consul_sd_configs|457process_consul_sd_configs(scrape['job_name'], consul_sd_configs)458end459elsif scrape['authorization']460username, password = process_authorization(scrape['authorization'])461cred = credential_data462cred[:port] = ''463cred[:address] = ''464cred[:username] = username465cred[:private_data] = password466create_credential_and_login(cred)467@table_creds << [468scrape['job_name'],469'authorization',470'',471'',472username,473password,474''475]476elsif scrape['kubernetes_sd_configs']477scrape['kubernetes_sd_configs']&.each do |kubernetes_sd_configs|478next unless kubernetes_sd_configs['api_server']479480# if scrape has basic auth, but the individual config doesn't481# add it to the individual config482if kubernetes_sd_configs['basic_auth'].nil? && scrape['basic_auth']483kubernetes_sd_configs['basic_auth'] = {}484kubernetes_sd_configs['basic_auth']['username'] = scrape.dig('basic_auth', 'username') if scrape.dig('basic_auth', 'username')485kubernetes_sd_configs['basic_auth']['password'] = scrape.dig('basic_auth', 'password') if scrape.dig('basic_auth', 'password')486kubernetes_sd_configs['basic_auth']['password'] = scrape.dig('basic_auth', 'password_file') if scrape.dig('basic_auth', 'password_file')487end488489process_kubernetes_sd_configs(scrape['job_name'], kubernetes_sd_configs)490end491elsif scrape['kuma_sd_configs']492scrape['kuma_sd_configs']&.each do |targets|493process_kuma_sd_configs(scrape['job_name'], targets)494end495elsif scrape['marathon_sd_configs']496scrape['marathon_sd_configs']&.each do |marathon_sd_configs|497process_marathon_sd_configs(scrape['job_name'], marathon_sd_configs)498end499elsif scrape['nomad_sd_configs']500scrape['nomad_sd_configs']&.each do |targets|501process_nomad_sd_configs(scrape['job_name'], targets)502end503elsif scrape['ec2_sd_configs']504scrape['ec2_sd_configs']&.each do |ec2_sd_configs|505process_ec2_sd_configs(scrape['job_name'], ec2_sd_configs)506end507elsif scrape['lightsail_sd_configs']508scrape['lightsail_sd_configs']&.each do |lightsail_sd_configs|509process_lightsail_sd_configs(scrape['job_name'], lightsail_sd_configs)510end511elsif scrape['azure_sd_configs']512scrape['azure_sd_configs']&.each do |azure_sd_configs|513process_azure_sd_configs(scrape['job_name'], azure_sd_configs)514end515elsif scrape['http_sd_configs']516scrape['http_sd_configs']&.each do |http_sd_configs|517process_http_sd_configs(scrape['job_name'], http_sd_configs)518end519elsif scrape['digitalocean_sd_configs']520scrape['digitalocean_sd_configs']&.each do |digitalocean_sd_configs|521process_digitalocean_sd_configs(scrape['job_name'], digitalocean_sd_configs)522end523elsif scrape['hetzner_sd_configs']524scrape['hetzner_sd_configs']&.each do |hetzner_sd_configs|525process_hetzner_sd_configs(scrape['job_name'], hetzner_sd_configs)526end527elsif scrape['eureka_sd_configs']528scrape['eureka_sd_configs']&.each do |eureka_sd_configs|529process_eureka_sd_configs(scrape['job_name'], eureka_sd_configs)530end531elsif scrape['ovhcloud_sd_configs']532scrape['ovhcloud_sd_configs']&.each do |ovhcloud_sd_configs|533process_ovhcloud_sd_configs(scrape['job_name'], ovhcloud_sd_configs)534end535elsif scrape['scaleway_sd_configs']536scrape['scaleway_sd_configs']&.each do |scaleway_sd_configs|537process_scaleway_sd_configs(scrape['job_name'], scaleway_sd_configs)538end539elsif scrape['linode_sd_configs']540scrape['linode_sd_configs']&.each do |linode_sd_configs|541process_linode_sd_configs(scrape['job_name'], linode_sd_configs)542end543elsif scrape['uyuni_sd_configs']544scrape['uyuni_sd_configs']&.each do |uyuni_sd_configs|545process_uyuni_sd_configs(scrape['job_name'], uyuni_sd_configs)546end547elsif scrape['ionos_sd_configs']548scrape['ionos_sd_configs']&.each do |ionos_sd_configs|549process_ionos_sd_configs(scrape['job_name'], ionos_sd_configs)550end551elsif scrape['vultr_sd_configs']552scrape['vultr_sd_configs']&.each do |vultr_sd_configs|553process_vultr_sd_configs(scrape['job_name'], vultr_sd_configs)554end555end556end557print_good(@table_creds.to_s) if !@table_creds.rows.empty?558end559560def process_results_page(page)561# data is in a strange 'label{optional_kv_hash-ish} value' format.562return nil if page.nil?563564results = []565page.scan(/^(?<name>\w+)(?:{(?<labels>[^}]+)})? (?<value>[\w.+-]+)/).each do |hit|566result = {}567value = { 'value' => hit[2], 'labels' => {} }568if hit[1]569hit[1].scan(/(?<key>[^=]+?)="(?<value>[^"]*)",?/).each do |label|570value['labels'][label[0]] = label[1]571end572end573result[hit[0]] = value574results.append(result)575end576return results577end578end579end580581582