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/spec/lib/msf/base/sessions/meterpreter_spec.rb
Views: 11765
require 'spec_helper'1require 'rex/post/meterpreter/extensions/stdapi/net/interface'2require 'rex/post/meterpreter/extensions/stdapi/net/route'34RSpec.describe Msf::Sessions::Meterpreter do5before do6allow_any_instance_of(Rex::Post::Meterpreter::PacketDispatcher).to receive(:monitor_socket)7end89subject(:meterpreter) { described_class.new(StringIO.new(""), skip_ssl: true) }1011let(:v6_gateway) { "2607:f8b0:4004:0802::1014" }12let(:v4_gateway) { "192.168.3.1" }1314let(:v6_linklocal) { "fe80::d6c9:efff:fe53:53ff" }1516let(:routes) do17[18Rex::Post::Meterpreter::Extensions::Stdapi::Net::Route.new(19IPAddr.new("0.0.0.0").hton, # Subnet20IPAddr.new("0.0.0.0").hton, # Netmask21IPAddr.new("192.168.3.1").hton # Gateway22),23Rex::Post::Meterpreter::Extensions::Stdapi::Net::Route.new(24IPAddr.new("::").hton, # Subnet25IPAddr.new("::").hton, # Netmask26IPAddr.new(v6_gateway).hton # Gateway27)28]29end3031describe "#find_internet_connected_address" do3233subject(:connected_address) do34allow_message_expectations_on_nil35m = described_class.new(StringIO.new(""), skip_ssl: true)36allow(m).to receive_message_chain(:private_methods, :net)37allow(m).to receive_message_chain(:private_methods, :net, :config, :get_interfaces).and_return(interfaces)38allow(m).to receive_message_chain(:private_methods, :net, :config, :get_routes).and_return(routes)39m.session_host = session_host4041m.send(:find_internet_connected_address)42end4344let(:interfaces) do45ifaces = []46interface_config.each_with_index { |iface_hash, idx|47ifaces << Rex::Post::Meterpreter::Extensions::Stdapi::Net::Interface.new(48index: idx,49mac_addr: "00:11:22:33:44:%02x"%idx,50mac_name: "eth0",51mtu: 1500,52flags: 0,53addrs: iface_hash[:ips],54netmasks: iface_hash[:masks],55scopes: [ "" ]56)57}5859ifaces60end6162let(:session_host) { "99.99.99.99" }6364context "with an address that matches #session_host" do65let(:interface_config) do66[67{ ips: [ "192.168.10.1" ], masks: [ "255.255.255.0" ], },68{ ips: [ "192.168.11.1" ], masks: [ "255.255.255.0" ], },69{ ips: [ "192.168.12.1" ], masks: [ "255.255.255.0" ], },70{ ips: [ session_host ], masks: [ "255.255.255.0" ], },71{ ips: [ "192.168.14.1" ], masks: [ "255.255.255.0" ], },72{ ips: [ "192.168.16.1" ], masks: [ "255.255.255.0" ], },73]74end75it "returns nil" do76expect(connected_address).to be_nil77end78end7980# All the rest of these assume session_host does not match any81# interface's addresses8283context "one interface with one IPv4 address" do84let(:interface_config) do85[ { ips: [ "10.2.3.4" ], masks: [ "255.255.255.0" ], } ]86end87it "returns that address" do88expect(connected_address).to eq("10.2.3.4")89end90end9192context "one interface with one IPv6 address" do93let(:interface_config) do94[95{ ips: [ v6_linklocal ], masks: [ "ffff:ffff:ffff:ffff::" ], },96]97end98it "returns that address" do99expect(connected_address).to eq(v6_linklocal)100end101end102103context "one interface with mixed IP versions" do104context "first is correct" do105let(:interface_config) do106[107{ ips: [ "192.168.3.4" ], masks: [ "255.255.255.0" ], },108{ ips: [ v6_linklocal ], masks: [ "ffff:ffff:ffff:ffff::" ], },109]110end111it "returns first address" do112expect(connected_address).to eq("192.168.3.4")113end114end115context "second address is correct" do116let(:interface_config) do117[118{ ips: [ v6_linklocal ], masks: [ "ffff:ffff:ffff:ffff::" ], },119{ ips: [ "192.168.3.4" ], masks: [ "255.255.255.0" ], },120]121end122it "returns second address" do123expect(connected_address).to eq("192.168.3.4")124end125end126end127128context "one interface with multiple IPv4 addresses" do129context "first address is correct" do130let(:interface_config) do131[ {132ips: ["192.168.3.4", "10.2.3.4"],133masks: [ "255.255.255.0", "255.0.0.0"],134} ]135end136it "returns first address" do137expect(connected_address).to eq("192.168.3.4")138end139end140context "second address is correct" do141let(:interface_config) do142[ {143ips: [ "10.2.3.4", "192.168.3.4" ],144masks: [ "255.0.0.0", "255.255.255.0" ],145} ]146end147it "returns second address" do148expect(connected_address).to eq("192.168.3.4")149end150end151end152153end154155describe '#exit' do156it 'shuts down the session' do157allow(subject).to receive(:core).and_return(double('core'))158allow(subject).to receive(:console).and_return(double('console'))159160expect(subject.core).to receive(:shutdown)161expect(subject).to receive(:shutdown_passive_dispatcher)162expect(subject.console).to receive(:stop)163subject.exit164end165end166167end168169170