# Malformed save files (.sav) allow to write files with arbitrary extensions and content in GoldSrc-based games.

Malformed .sav file bypasses extension check via substring in path, allowing arbitrary file write and DLL loading from a malicious server.

- Payout: $1,500
- Severity: high
- Program: Valve
- Found by: splatt581
- Disclosed: 2020-02-24
- Write-up quality: 8/10
- Original: https://hackerone.com/reports/458842
- Library page: https://bugbountylibrary.com/r/h1%3A458842
- Tags: area:os domain:desktop impact:integrity impact:rce kind:bounty platform:hackerone sev:high technique:chained vuln:file-upload vuln:path-traversal vuln:rce

**Target:** GoldSrc game engine (hl.exe) save file loader
**Root cause:** The save file loader checks for the substring '*.HL?' anywhere in the file path instead of at the end, allowing files with arbitrary extensions to be written by embedding '.HL1' in the middle of the path.
**Steps:**
1. Create a .sav file that unpacks a file named `test.HL1.dll` containing arbitrary content.
2. Place the .sav file in `%gamedir%/SAVE/`.
3. Launch the game and execute `load fakeresource` in the console.
4. The file `test.HL1.dll` is written to the SAVE directory.
5. For remote exploitation, a malicious server sends the .sav file to the client.
6. Server sends console commands via `SVC_StuffText` or `SVC_Director`: `_setgamedir %gamedir%_downloads;_restart`.
7. After reconnect, server sends: `logsdir SAVE/test.HL1/cl_dlls;log on;_setgamedir %gamedir%_downloads/SAVE/test.HL1;load fakeresource;_restart`.
8. The .dll is unpacked to `SAVE/test.HL1/cl_dlls/client.dll` and loaded into memory on restart.
**How found:** Observed that the save file loader checks for '.HL?' substring anywhere in the path, not just the extension, and that directory traversal is blocked but substring injection bypasses the check.
**Exploitation:** A malicious server delivers a crafted .sav file to the client, then uses console commands to set the game directory, create necessary folders, load the save to unpack a malicious DLL, and restart the engine to load the DLL into memory, achieving code execution.
**Escalation:** Chained file write with console command injection and engine restart to load the DLL as a client library.
**Insight:** When a file extension check uses substring matching instead of suffix matching, try inserting the expected substring in the middle of the filename to bypass the filter.

## Proof of concept (copied exactly from the original report)

**Console commands to unpack and load malicious DLL**

```
logsdir SAVE/test.HL1/cl_dlls;log on;_setgamedir %gamedir%_downloads/SAVE/test.HL1;load fakeresource;_restart
```

**Load command triggering malicious save unpack**

````
2) Launch the game client and type in the console ```load fakeresource```;

The file ```test.HL1.dll``` will appear in the folder ```SAVE```
````

**Server command to set gamedir and restart**

````
_setgamedir %gamedir%_downloads;_restart```. Let's take a closer look at this string:

* ```_setgamedir %gamedir%_downloads``` - sets the directory in which the .sav file is downloaded, for its subsequent load;
* ```_restart``` - applying the above changes.
````

