Path: blob/master/modules/post/osx/manage/vpn.rb
19813 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6include Msf::Post::File78STR_CONNECTED = '* (Connected)'9STR_DISCONNECTED = '* (Disconnected)'1011def initialize(info = {})12super(13update_info(14info,15'Name' => 'OSX VPN Manager',16'Description' => %q{17This module lists VPN connections and tries to connect to them using stored credentials.18},19'License' => MSF_LICENSE,20'Author' => [21'Peter Toth <globetother[at]gmail.com>'22],23'Platform' => [ 'osx' ],24'SessionTypes' => [ 'shell', 'meterpreter' ],25'Actions' => [26[ 'LIST', { 'Description' => 'Show a list of VPN connections' } ],27[ 'CONNECT', { 'Description' => 'Connect to a VPN using stored credentials' } ],28[ 'DISCONNECT', { 'Description' => 'Disconnect from a VPN' } ]29],30'DefaultAction' => 'LIST',31'Notes' => {32'Stability' => [CRASH_SAFE],33'SideEffects' => [],34'Reliability' => []35}36)37)3839register_options(40[41OptString.new('VPN_CONNECTION', [true, 'Name of VPN connection. `set ACTION LIST` to get a list.', 'OSX_VPN']),42OptString.new('SCUTIL_PATH', [true, 'Path to the scutil executable.', '/usr/sbin/scutil']),43OptString.new('NETWORKSETUP_PATH', [true, 'Path to the networksetup executable.', '/usr/sbin/networksetup'])44]45)46end4748def run49fail_with(Failure::BadConfig, 'Invalid action') if action.nil?5051scutil_path = datastore['SCUTIL_PATH'].shellescape52networksetup_path = datastore['NETWORKSETUP_PATH'].shellescape53vpn_name = datastore['VPN_CONNECTION']5455if !file?(scutil_path)56print_error('Aborting, scutil binary not found.')57return58end5960if !file?(networksetup_path)61print_error('Aborting, networksetup binary not found.')62return63end6465# Fetch the list of configured VPN connections66cmd_list = "#{scutil_path} --nc list"67vprint_status(cmd_list)68vpn_data = cmd_exec(cmd_list)69connected_names = parse_vpn_connection_names(vpn_data, :connected)70disconnected_names = parse_vpn_connection_names(vpn_data, :disconnected)7172if action.name == 'LIST'73if !connected_names.empty?74print_status('VPN Connections Status: UP')75connected_names.each do |vpn_name|76print_good(' ' + vpn_name)77end78end79if !disconnected_names.empty?80print_status('VPN Connections Status: DOWN')81disconnected_names.each do |vpn_name|82print_good(' ' + vpn_name)83end84end85elsif action.name == 'CONNECT'86if connected_names.include?(vpn_name)87print_status("#{vpn_name} already connected")88return89end9091unless disconnected_names.include?(vpn_name)92print_error("#{vpn_name} not found")93return94end9596cmd_up = "#{networksetup_path} -connectpppoeservice '#{vpn_name}'"97vprint_status(cmd_up)98cmd_exec(cmd_up)99elsif action.name == 'DISCONNECT'100if disconnected_names.include?(vpn_name)101print_status("#{vpn_name} already disconnected")102return103end104105unless connected_names.include?(vpn_name)106print_error("#{vpn_name} not found")107return108end109110identifier = parse_vpn_connection_identifier(vpn_data, vpn_name)111unless identifier112print_error("Could not parse #{vpn_name} identifier")113return114end115cmd_down = "#{scutil_path} --nc stop #{identifier}"116vprint_status(cmd_down)117cmd_exec(cmd_down)118end119end120121def parse_vpn_connection_names(data, type = :connected)122lines = data.lines123connection_names = []124comp_str = type == :connected ? STR_CONNECTED : STR_DISCONNECTED125126lines.each do |line|127if line.start_with?(comp_str) && line =~ /"(.*)"/128connection_names << ::Regexp.last_match(1)129end130end131return connection_names132end133134def parse_vpn_connection_identifier(data, vpn_name)135lines = data.lines136lines.each do |line|137line.strip!138next if line.empty?139140if line.include?(vpn_name) && line =~ /([0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12})/141identifier = ::Regexp.last_match(1)142return identifier143end144end145return nil146end147end148149150