diff --git a/lib/msf/ui/console/command_dispatcher/core.rb b/lib/msf/ui/console/command_dispatcher/core.rb index 6b92168728..27d5d7a336 100644 --- a/lib/msf/ui/console/command_dispatcher/core.rb +++ b/lib/msf/ui/console/command_dispatcher/core.rb @@ -858,139 +858,144 @@ class Core # which session a given subnet should route through. # def cmd_route(*args) - args << 'print' if args.length == 0 + begin + args << 'print' if args.length == 0 - action = args.shift - case action - when "add", "remove", "del" - subnet = args.shift - subnet, cidr_mask = subnet.split("/") + action = args.shift + case action + when "add", "remove", "del" + subnet = args.shift + subnet, cidr_mask = subnet.split("/") - if Rex::Socket.is_ip_addr?(args.first) - netmask = args.shift - elsif Rex::Socket.is_ip_addr?(subnet) - netmask = Rex::Socket.addr_ctoa(cidr_mask, v6: Rex::Socket.is_ipv6?(subnet)) - end - - netmask = args.shift if netmask.nil? - gateway_name = args.shift - - if (subnet.nil? || netmask.nil? || gateway_name.nil?) - print_error("Missing arguments to route #{action}.") - return false - end - - case gateway_name - when /local/i - gateway = Rex::Socket::Comm::Local - when /^(-1|[0-9]+)$/ - session = framework.sessions.get(gateway_name) - if session.kind_of?(Msf::Session::Comm) - gateway = session - elsif session.nil? - print_error("Not a session: #{gateway_name}") - return false - else - print_error("Cannot route through the specified session (not a Comm)") - return false - end - else - print_error("Invalid gateway") - return false - end - - msg = "Route " - if action == "remove" or action == "del" - worked = Rex::Socket::SwitchBoard.remove_route(subnet, netmask, gateway) - msg << (worked ? "removed" : "not found") - else - worked = Rex::Socket::SwitchBoard.add_route(subnet, netmask, gateway) - msg << (worked ? "added" : "already exists") - end - print_status(msg) - - when "get" - if (args.length == 0) - print_error("You must supply an IP address.") - return false - end - - comm = Rex::Socket::SwitchBoard.best_comm(args[0]) - - if ((comm) and - (comm.kind_of?(Msf::Session))) - print_line("#{args[0]} routes through: Session #{comm.sid}") - else - print_line("#{args[0]} routes through: Local") - end - - - when "flush" - Rex::Socket::SwitchBoard.flush_routes - - when "print" - # IPv4 Table - tbl_ipv4 = Table.new( - Table::Style::Default, - 'Header' => "IPv4 Active Routing Table", - 'Prefix' => "\n", - 'Postfix' => "\n", - 'Columns' => - [ - 'Subnet', - 'Netmask', - 'Gateway', - ], - 'ColProps' => - { - 'Subnet' => { 'MaxWidth' => 17 }, - 'Netmask' => { 'MaxWidth' => 17 }, - }) - - # IPv6 Table - tbl_ipv6 = Table.new( - Table::Style::Default, - 'Header' => "IPv6 Active Routing Table", - 'Prefix' => "\n", - 'Postfix' => "\n", - 'Columns' => - [ - 'Subnet', - 'Netmask', - 'Gateway', - ], - 'ColProps' => - { - 'Subnet' => { 'MaxWidth' => 17 }, - 'Netmask' => { 'MaxWidth' => 17 }, - }) - - # Populate Route Tables - Rex::Socket::SwitchBoard.each { |route| - if (route.comm.kind_of?(Msf::Session)) - gw = "Session #{route.comm.sid}" - else - gw = route.comm.name.split(/::/)[-1] + if Rex::Socket.is_ip_addr?(args.first) + netmask = args.shift + elsif Rex::Socket.is_ip_addr?(subnet) + netmask = Rex::Socket.addr_ctoa(cidr_mask, v6: Rex::Socket.is_ipv6?(subnet)) end - tbl_ipv4 << [ route.subnet, route.netmask, gw ] if Rex::Socket.is_ipv4?(route.netmask) - tbl_ipv6 << [ route.subnet, route.netmask, gw ] if Rex::Socket.is_ipv6?(route.netmask) - } + netmask = args.shift if netmask.nil? + gateway_name = args.shift - # Print Route Tables - print(tbl_ipv4.to_s) if tbl_ipv4.rows.length > 0 - print(tbl_ipv6.to_s) if tbl_ipv6.rows.length > 0 + if (subnet.nil? || netmask.nil? || gateway_name.nil?) + print_error("Missing arguments to route #{action}.") + return false + end - if (tbl_ipv4.rows.length + tbl_ipv6.rows.length) < 1 - print_status("There are currently no routes defined.") - elsif (tbl_ipv4.rows.length < 1) && (tbl_ipv6.rows.length > 0) - print_status("There are currently no IPv4 routes defined.") - elsif (tbl_ipv4.rows.length > 0) && (tbl_ipv6.rows.length < 1) - print_status("There are currently no IPv6 routes defined.") + case gateway_name + when /local/i + gateway = Rex::Socket::Comm::Local + when /^(-1|[0-9]+)$/ + session = framework.sessions.get(gateway_name) + if session.kind_of?(Msf::Session::Comm) + gateway = session + elsif session.nil? + print_error("Not a session: #{gateway_name}") + return false + else + print_error("Cannot route through the specified session (not a Comm)") + return false + end + else + print_error("Invalid gateway") + return false + end + + msg = "Route " + if action == "remove" or action == "del" + worked = Rex::Socket::SwitchBoard.remove_route(subnet, netmask, gateway) + msg << (worked ? "removed" : "not found") + else + worked = Rex::Socket::SwitchBoard.add_route(subnet, netmask, gateway) + msg << (worked ? "added" : "already exists") + end + print_status(msg) + + when "get" + if (args.length == 0) + print_error("You must supply an IP address.") + return false + end + + comm = Rex::Socket::SwitchBoard.best_comm(args[0]) + + if ((comm) and + (comm.kind_of?(Msf::Session))) + print_line("#{args[0]} routes through: Session #{comm.sid}") + else + print_line("#{args[0]} routes through: Local") + end + + + when "flush" + Rex::Socket::SwitchBoard.flush_routes + + when "print" + # IPv4 Table + tbl_ipv4 = Table.new( + Table::Style::Default, + 'Header' => "IPv4 Active Routing Table", + 'Prefix' => "\n", + 'Postfix' => "\n", + 'Columns' => + [ + 'Subnet', + 'Netmask', + 'Gateway', + ], + 'ColProps' => + { + 'Subnet' => { 'MaxWidth' => 17 }, + 'Netmask' => { 'MaxWidth' => 17 }, + }) + + # IPv6 Table + tbl_ipv6 = Table.new( + Table::Style::Default, + 'Header' => "IPv6 Active Routing Table", + 'Prefix' => "\n", + 'Postfix' => "\n", + 'Columns' => + [ + 'Subnet', + 'Netmask', + 'Gateway', + ], + 'ColProps' => + { + 'Subnet' => { 'MaxWidth' => 17 }, + 'Netmask' => { 'MaxWidth' => 17 }, + }) + + # Populate Route Tables + Rex::Socket::SwitchBoard.each { |route| + if (route.comm.kind_of?(Msf::Session)) + gw = "Session #{route.comm.sid}" + else + gw = route.comm.name.split(/::/)[-1] + end + + tbl_ipv4 << [ route.subnet, route.netmask, gw ] if Rex::Socket.is_ipv4?(route.netmask) + tbl_ipv6 << [ route.subnet, route.netmask, gw ] if Rex::Socket.is_ipv6?(route.netmask) + } + + # Print Route Tables + print(tbl_ipv4.to_s) if tbl_ipv4.rows.length > 0 + print(tbl_ipv6.to_s) if tbl_ipv6.rows.length > 0 + + if (tbl_ipv4.rows.length + tbl_ipv6.rows.length) < 1 + print_status("There are currently no routes defined.") + elsif (tbl_ipv4.rows.length < 1) && (tbl_ipv6.rows.length > 0) + print_status("There are currently no IPv4 routes defined.") + elsif (tbl_ipv4.rows.length > 0) && (tbl_ipv6.rows.length < 1) + print_status("There are currently no IPv6 routes defined.") + end + + else + cmd_route_help end - - else - cmd_route_help + rescue => error + elog("#{error}\n\n#{error.backtrace.join("\n")}") + print_error(error.message) end end