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/scripts/meterpreter/autoroute.rb
Views: 11766
##1# WARNING: Metasploit no longer maintains or accepts meterpreter scripts.2# If you'd like to improve this script, please try to port it as a post3# module instead. Thank you.4##567#8# Meterpreter script for setting up a route from within a9# Meterpreter session, without having to background the10# current session.1112# Default options13session = client14subnet = nil15netmask = "255.255.255.0"16print_only = false17remove_route = false18remove_all_routes = false1920# Options parsing21@@exec_opts = Rex::Parser::Arguments.new(22"-h" => [false, "Help and usage"],23"-s" => [true, "Subnet (IPv4, for example, 10.10.10.0)"],24"-n" => [true, "Netmask (IPv4, for example, 255.255.255.0"],25"-p" => [false, "Print active routing table. All other options are ignored"],26"-d" => [false, "Delete the named route instead of adding it"],27"-D" => [false, "Delete all routes (does not require a subnet)"]28)2930# Defines usage31def usage()32print_status "Usage: run autoroute [-r] -s subnet -n netmask"33print_status "Examples:"34print_status " run autoroute -s 10.1.1.0 -n 255.255.255.0 # Add a route to 10.10.10.1/255.255.255.0"35print_status " run autoroute -s 10.10.10.1 # Netmask defaults to 255.255.255.0"36print_status " run autoroute -s 10.10.10.1/24 # CIDR notation is also okay"37print_status " run autoroute -p # Print active routing table"38print_status " run autoroute -d -s 10.10.10.1 # Deletes the 10.10.10.1/255.255.255.0 route"39print_status "Use the \"route\" and \"ipconfig\" Meterpreter commands to learn about available routes"40print_error "Deprecation warning: This script has been replaced by the post/multi/manage/autoroute module"41end424344@@exec_opts.parse(args) { |opt, idx, val|45v = val.to_s.strip46case opt47when "-h"48usage49raise Rex::Script::Completed50when "-s"51if v =~ /[0-9\x2e]+\x2f[0-9]{1,2}/52subnet,cidr = v.split("\x2f")53netmask = Rex::Socket.addr_ctoa(cidr.to_i)54else55subnet = v56end57when "-n"58if (0..32) === v.to_i59netmask = Rex::Socket.addr_ctoa(v.to_i)60else61netmask = v62end63when "-p"64print_only = true65when "-d"66remove_route = true67when "-D"68remove_all_routes = true69end70}7172def delete_all_routes73if Rex::Socket::SwitchBoard.routes.size > 074routes = []75Rex::Socket::SwitchBoard.each do |route|76routes << {:subnet => route.subnet, :netmask => route.netmask}77end78routes.each {|route_opts| delete_route(route_opts)}7980print_status "Deleted all routes"81else82print_status "No routes have been added yet"83end84raise Rex::Script::Completed85end8687# Identical functionality to command_dispatcher/core.rb, and88# nearly identical code89def print_routes90if Rex::Socket::SwitchBoard.routes.size > 091tbl = Msf::Ui::Console::Table.new(92Msf::Ui::Console::Table::Style::Default,93'Header' => "Active Routing Table",94'Prefix' => "\n",95'Postfix' => "\n",96'Columns' =>97[98'Subnet',99'Netmask',100'Gateway',101],102'ColProps' =>103{104'Subnet' => { 'Width' => 17 },105'Netmask' => { 'Width' => 17 },106})107ret = []108109Rex::Socket::SwitchBoard.each { |route|110if (route.comm.kind_of?(Msf::Session))111gw = "Session #{route.comm.sid}"112else113gw = route.comm.name.split(/::/)[-1]114end115tbl << [ route.subnet, route.netmask, gw ]116}117print tbl.to_s118else119print_status "No routes have been added yet"120end121raise Rex::Script::Completed122end123124# Yet another IP validator. I'm sure there's some Rex125# function that can just do this.126def check_ip(ip=nil)127return false if(ip.nil? || ip.strip.empty?)128begin129rw = Rex::Socket::RangeWalker.new(ip.strip)130(rw.valid? && rw.length == 1) ? true : false131rescue132false133end134end135136# Adds a route to the framework instance137def add_route(opts={})138subnet = opts[:subnet]139netmask = opts[:netmask] || "255.255.255.0" # Default class C140Rex::Socket::SwitchBoard.add_route(subnet, netmask, session)141end142143# Removes a route to the framework instance144def delete_route(opts={})145subnet = opts[:subnet]146netmask = opts[:netmask] || "255.255.255.0" # Default class C147Rex::Socket::SwitchBoard.remove_route(subnet, netmask, session)148end149150# Validates the command options151def validate_cmd(subnet=nil,netmask=nil)152if subnet.nil?153print_error "Missing -s (subnet) option"154return false155end156157unless(check_ip(subnet))158print_error "Subnet invalid (must be IPv4)"159usage160return false161end162163if(netmask and !(Rex::Socket.addr_atoc(netmask)))164print_error "Netmask invalid (must define contiguous IP addressing)"165usage166return false167end168169if(netmask and !check_ip(netmask))170print_error "Netmask invalid"171return usage172end173true174end175176if print_only177print_routes()178raise Rex::Script::Completed179end180181if remove_all_routes182delete_all_routes()183raise Rex::Script::Completed184end185186raise Rex::Script::Completed unless validate_cmd(subnet,netmask)187188if remove_route189print_status("Deleting route to %s/%s..." % [subnet,netmask])190route_result = delete_route(:subnet => subnet, :netmask => netmask)191else192print_status("Adding a route to %s/%s..." % [subnet,netmask])193route_result = add_route(:subnet => subnet, :netmask => netmask)194end195196if route_result197print_good "%s route to %s/%s via %s" % [198(remove_route ? "Deleted" : "Added"),199subnet,netmask,client.sock.peerhost200]201else202print_error "Could not %s route" % [(remove_route ? "delete" : "add")]203end204205if Rex::Socket::SwitchBoard.routes.size > 0206print_status "Use the -p option to list all active routes"207end208209210211