Persistence on Linux

In this article we will describe some post-exploitation techniques allowing us to maintain our access to a Linux box.

Reverse shell

A reverse shell is neither discrete nor elegant, however it allow for quick backdoor access to the box.

Moreover, it does not require the machine to be visible on Internet, which can be useful if it is behind a NAT or a firewall.

Launch a connection to the attack server from our compromised machine:

This one-liner will try to open a shell to vps.hacker.com:8080 every 10 seconds.

We need a listener on the attacking server:

After some time, the shell is executed !

We can then put the reverse shell one-liner in rc.local or bashrc for cross-reboot persistence.

SSH backdoor

If a SSH service is enabled on the compromised host, a simple solution could be to add our public key to the authorized_keys file . We can then connect directly through SSH.

First, generate a SSH keypair on our attacking server:

After which we copy the public key to the compromised host:

We can log in anytime with:

Add user

Another solution could be to add a user. We can then use this user account to log in through SSH, FTP or any system service using local accounts on the machine.

This adds an “admin” user with password “lemmein”.

Encrypt a USB drive on Linux

USB thumbdrives often store confidential information. However, it is very easy to loose. Encrypt a USB drive helps protecting your data with a password.

Here is how to encrypt a USB drive with cryptsetup:

First, install the cryptsetup package:

Then, ensure the USB drive is not mounted (replace /dev/sdb1 with the USB drive partition):

Now, let’s create an encrypted filesystem on the partition (choose a strong password):

Then, mount the filesystem and format it as FAT:

Then, unmount the device:

Next time the USB drive is inserted, a password will be prompted.

Persistence on Windows

In this post, we will describe some persistence techniques on Windows boxes.

Registry key

Some registry keys such as Run allow to define programs to start after a reboot.

There are two instances of the Run registry key:

  • HKCU\Software\Microsoft\Windows\CurrentVersion\Run: Programs listed here will start when the current user logs in.
  • HKCU\Software\Microsoft\Windows\CurrentVersion\Run: Programs listed here will start when the computer starts.

The first instance only requires privileges of the current user, however the second one can only be set by users with administration privileges.

Let’s launch C:\Windows\agent.exe on each session start:

System Service

Services are programs started on each boot. They are started by default with maximum privileges (SYSTEM).

Example:

Scheduled tasks

Scheduled tasks are executed periodically.

Add user

Adding a Windows user is a simple yet efficient solution to maintain our access. Adding a user requires administration privileges.

Adding a local administrator

Adding a new local administrator “admin” with password “s3crEt”.

The local adminstrators group’s name can change with the system’s language (e.g “Administrateurs” in French).

Adding a domain administrator

If we have domain administration privileges, we can add another domain administrator:

Block facebook, twitter, linkedin widgets… with no software.

Social networks are omnipresent on the Internet. Most websites include social widgets, allowing to “like” or “bookmark” pages. However, these widget can help track your browser’s IP, user agent, history, gelocation etc. This happens even if you are not connected and don’t own an account on the social network.

To avoid this, we can edit the hosts file, which contains IP address / hostname associations.

On Windows, it is located at C:\Windows\System32\drivers\etc\hosts, on Linux it is at /etc/hosts and on Mac it is there: /private/etc/hosts.

Then, it is sufficient to add this line at the end of the file and save (you need administrator privileges to save).

Adapt the domain name list with any domain you would like to block.

Here is an example hosts file blocking facebook.com.

Now, you should see Facebook widgets disappear like by magic on every page you visit !

Add SSH key to server

First, generate a SSH RSA keypair:

The public key should be copied on the remote server you want to access though SSH:

The private key should be on every machine you want to connect from (usually, only your own).

Now, simply log in with ssh user@myserver.com