#
# Copyright (c) 2006-2016 Wade Alcorn - wade@bindshell.net
# Browser Exploitation Framework (BeEF) - http://beefproject.com
# See the file 'doc/COPYING' for copying permission
#
class Asus_rt_n12e_get_info < BeEF::Core::Command

  def self.options
    return [
      { 'name' => 'target_ip', 'ui_label' => 'Target Host', 'value' => 'router.asus.com' }
    ]
  end

  def post_execute
    save({'result' => @datastore['result']})

    configuration = BeEF::Core::Configuration.instance
    if configuration.get("beef.extension.network.enable") == true

      session_id = @datastore['beefhook']

      # log the network hosts
      if @datastore['results'] =~ /ip=(.+)&clients=(.+)&wanip=(.+)&netmask=(.+)&gateway=(.+)&dns=(.+)/
        ip = "#{$1}"
        clients = "#{$2}"
        wanip = "#{$3}"
        netmask = "#{$4}"
        gateway = "#{$5}"
        dns_servers = "#{$6}"

        if !ip.nil? && BeEF::Filters.is_valid_ip?(ip)
          print_debug("Hooked browser found Asus RT-N12E router [ip: #{ip}]")
          BeEF::Core::Models::NetworkHost.add(:hooked_browser_id => session_id, :ip => ip, :type => 'Asus RT-N12E Router')
          BeEF::Core::Models::NetworkService.add(:hooked_browser_id => session_id, :proto => 'http', :ip => ip, :port => 80, :type => 'HTTP Server')
        end
        clients.scan(/([\d\.]+,[:\dA-F]{17})/).flatten.each do |client|
          next if client.nil?
          if client.to_s =~ /^([\d\.]+),([:\dA-F]{17})$/
            ip = $1
            mac = $2
            if BeEF::Filters.is_valid_ip?(ip)
              print_debug("Hooked browser found router client [ip: #{ip}, mac: #{mac}]")
              BeEF::Core::Models::NetworkHost.add(:hooked_browser_id => session_id, :ip => ip, :mac => mac)
            end
          end
        end
        if !gateway.nil? && BeEF::Filters.is_valid_ip?(gateway)
          print_debug("Hooked browser found WAN gateway server [ip: #{gateway}]")
          BeEF::Core::Models::NetworkHost.add(:hooked_browser_id => session_id, :ip => gateway, :type => 'WAN Gateway')
        end
        if !dns_servers.nil? && dns_servers =~ /^([\d\. ]+)$/
          dns_servers.split(/ /).uniq.each do |dns|
            if BeEF::Filters.is_valid_ip?(dns)
              print_debug("Hooked browser found DNS server [ip: #{dns}]")
              BeEF::Core::Models::NetworkHost.add(:hooked_browser_id => session_id, :ip => dns, :type => 'DNS Server')
            end
          end
        end
      end
    end

  end

end

