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/rex/parser/dbeaver.rb
Views: 11779
module Rex1module Parser2# @author Kali-Team3module Dbeaver45module Error6class DbeaverError < StandardError7end89class ParserError < DbeaverError10end1112class DecryptionError < ParserError13end14end1516SECRET_KEY = 'sdf@!#$verf^wv%6Fwe%$$#FFGwfsdefwfe135s$^H)dg'.freeze17AES_KEY = "\xBA\xBBJ\x9FwJ\xB8S\xC9l-e=\xFETJ".freeze18# decrypt_dbeaver_credentials19#20# @param credentials_config_data [String]21# @return [String] plaintext22def decrypt_dbeaver_credentials(credentials_config_data)23aes = OpenSSL::Cipher.new('AES-128-CBC')24begin25aes.decrypt26aes.key = AES_KEY27plaintext = aes.update(credentials_config_data)28plaintext << aes.final29rescue OpenSSL::Cipher::CipherError => e30raise Error::DecryptionError, 'Unable to decrypt dbeaver credentials'31end32return plaintext[plaintext.index('{"')..]33end3435# parse_credentials36#37# @param credentials_config_data [String]38# @return [Hash] result_hashmap39def parse_credentials(credentials_config_data)40decrypt_data = decrypt_dbeaver_credentials(credentials_config_data)41result_hashmap = Hash.new42begin43result_hashmap = JSON.parse(decrypt_data)44rescue ::JSON::ParserError => e45raise Error::ParserError, "[parse_credentials] #{e.class} - #{e}"46end47return result_hashmap48end4950# parse_data_sources51#52# @param data_sources_data [String]53# @param credentials_config_data [String]54# @return [Hash] result_hashmap55def parse_data_sources(data_sources_data, credentials_config_data)56credentials = parse_credentials(credentials_config_data)57result_hashmap = Hash.new58if credentials.empty?59return result_hashmap60end6162begin63data_sources = JSON.parse(data_sources_data)64connections = data_sources['connections']65if connections.nil? || connections.empty?66return result_hashmap67end6869connections.each do |data_source_id, item|70next if item['configuration'].nil?7172result_hashmap[data_source_id] = Hash[73'name' => item['name'] || '',74'provider' => item['provider'] || '',75'host' => item['configuration']['host'] || '',76'port' => item['configuration']['port'] || '',77'user' => credentials.key?(data_source_id) ? credentials[data_source_id]['#connection']['user'] : '',78'password' => credentials.key?(data_source_id) ? credentials[data_source_id]['#connection']['password'] : '',79'database' => item['configuration']['database'] || '',80'url' => item['configuration']['url'] || '',81'type' => item['configuration']['type'] || ''82]83end84rescue ::JSON::ParserError => e85raise Error::ParserError, "[parse_data_sources] #{e.class} - #{e}"86end87return result_hashmap88end8990# decrypt_dbeaver_6_1_391#92# @param base64_string [String]93# @return [String]94def decrypt_dbeaver_6_1_3(base64_string)95plaintext = ''96if base64_string.nil?97return plaintext98end99100data = Rex::Text.decode_base64(base64_string)101for i in 0..data.length - 3102xor_data = Rex::Text.xor(data[i], SECRET_KEY[i % SECRET_KEY.length])103plaintext += xor_data104end105return plaintext106end107108# parse_data_sources_xml109#110# @param data_sources_data [String]111# @return [Hash] result_hashmap112def parse_data_sources_xml(data_sources_data)113mxml = REXML::Document.new(data_sources_data).root114unless mxml115raise Error::ParserError, '[parse_data_sources_xml] XML parsing error'116end117result_hashmap = Hash.new118mxml.elements.to_a('//data-sources//data-source//connection//').each do |node|119next unless node.name == 'connection'120121data_source_id = node.parent.attributes['id']122result_hashmap[data_source_id] = Hash[123'name' => node.parent.attributes['name'] || '',124'provider' => node.parent.attributes['provider'] || '',125'host' => node.attributes['host'] || '',126'port' => node.attributes['port'] || '',127'user' => node.attributes['user'] || '',128'password' => decrypt_dbeaver_6_1_3(node.attributes['password']),129'database' => node.attributes['database'] || '',130'url' => node.attributes['url'] || '',131'type' => node.attributes['type'] || ''132]133end134return result_hashmap135end136137end138end139end140141142