# HTML Injection Testing - Detailed Guide
> This file contains the detailed procedure and reference material extracted from `SKILL.md` for focused loading. The root skill defines activation, examples, safety constraints, and limitations.
## Purpose
Identify and exploit HTML injection vulnerabilities that allow attackers to inject malicious HTML content into web applications. This vulnerability enables attackers to modify page appearance, create phishing pages, and steal user credentials through injected forms.
## Outputs and Deliverables
1. **Vulnerability Report** - Identified injection points
2. **Exploitation Proof** - Demonstrated content manipulation
3. **Impact Assessment** - Potential phishing and defacement risks
4. **Remediation Guidance** - Input validation recommendations
Use a controlled lab and synthetic credentials for form demonstrations. Do not expose an injected form to real users or collect real passwords. Stored payloads require explicit cleanup verification. Reflection in response text is only a candidate: inspect parsed DOM, context and browser behavior; curl/grep cannot prove rendering or exploitability. Every automated request must stay within the exact authorized origin, bounded count/rate and response-size budget.
## Core Workflow
### Phase 1: Understanding HTML Injection
HTML injection occurs when user input is reflected in web pages without proper sanitization:
```html
Welcome,
?name=
Injected Content
Welcome,
Injected Content
```
Key differences from XSS:
- HTML injection: Only HTML tags are rendered
- XSS: JavaScript code is executed
- HTML injection is often stepping stone to XSS
Attack goals:
- Modify website appearance (defacement)
- Create fake login forms (phishing)
- Inject malicious links
- Display misleading content
### Phase 2: Identifying Injection Points
Map application for potential injection surfaces:
```
1. Search bars and search results
2. Comment sections
3. User profile fields
4. Contact forms and feedback
5. Registration forms
6. URL parameters reflected on page
7. Error messages
8. Page titles and headers
9. Hidden form fields
10. Cookie values reflected on page
```
Common vulnerable parameters:
```
?name=
?user=
?search=
?query=
?message=
?title=
?content=
?redirect=
?url=
?page=
```
### Phase 3: Basic HTML Injection Testing
Test with simple HTML tags:
```html
Test Injection
Bold TextItalic TextUnderlined Text
Red Text
Injected DIV
Injected paragraph
Line breaks
Click HereLegitimate Link
```
Testing workflow:
```bash
# Test basic injection
curl --get --data-urlencode 'q=
Test
' "http://127.0.0.1:8000/search"
# Check if HTML renders in response
curl --get --data-urlencode 'q=Bold' "http://127.0.0.1:8000/search" | grep -i "bold"
# Test in URL-encoded form
curl "http://127.0.0.1:8000/search?q=%3Ch1%3ETest%3C%2Fh1%3E"
```
### Phase 4: Types of HTML Injection
#### Stored HTML Injection
Payload persists in database:
```html
Name: John Doe
Bio:
",
"",
"",
]
for payload in payloads:
encoded = urllib.parse.quote(payload)
url = f"{target}?{param}={encoded}"
try:
response = requests.get(url, timeout=5, allow_redirects=False)
if payload.lower() in response.text.lower():
print(f"[+] Possible injection: {payload}")
elif "
" in response.text or "" in response.text:
print(f"[?] Partial reflection: {payload}")
except Exception as e:
print(f"[-] Error: {e}")
```
### Phase 10: Prevention and Remediation
Secure coding practices:
```php
// PHP: Escape output
echo htmlspecialchars($user_input, ENT_QUOTES, 'UTF-8');
// strip_tags is not an XSS defense and allowed tags retain unsafe attributes.
// For intended rich HTML, use a maintained HTML sanitizer with a restrictive policy.
```
```python
# Python: HTML escape
from html import escape
safe_output = escape(user_input)
# Python Flask: Auto-escaping
{{ user_input }} # Jinja2 escapes by default
{{ user_input | safe }} # Marks as safe (dangerous!)
```
```javascript
// JavaScript: Text content (safe)
element.textContent = userInput;
// JavaScript: innerHTML (dangerous!)
element.innerHTML = userInput; // Vulnerable!
// JavaScript: Sanitize
const clean = DOMPurify.sanitize(userInput);
element.innerHTML = clean;
```
Server-side protections:
- Input validation (whitelist allowed characters)
- Output encoding (context-aware escaping)
- Content Security Policy (CSP) headers
- Web Application Firewall (WAF) rules
## Quick Reference
### Common Test Payloads
| Payload | Purpose |
|---------|---------|
| `
Test
` | Basic rendering test |
| `Bold` | Simple formatting |
| `Link` | Link injection |
| `` | Image tag test |
| `
` | Style injection |
| `
` | Form hijacking |
### Injection Contexts
| Context | Test Approach |
|---------|---------------|
| URL parameter | `?param=
test
` |
| Form field | POST with HTML payload |
| Cookie value | Inject via document.cookie |
| HTTP header | Inject in Referer/User-Agent |
| File upload | HTML file with malicious content |
### Encoding Types
| Type | Example |
|------|---------|
| URL encoding | `%3Ch1%3E` = `
` |
| HTML entities | `<h1>` = `
` |
| Double encoding | `%253C` = `<` |
| Unicode | `\u003c` = `<` |
Encoding examples depend on the application decoding pipeline. CSS does not execute JavaScript concatenation, character references are not universally reparsed as markup, and split tag names are not equivalent to intact names. Treat examples as hypotheses, not guaranteed filter bypasses.
## Troubleshooting
| Issue | Solutions |
|-------|-----------|
| HTML not rendering | Check if output HTML-encoded; try encoding variations; verify HTML context |
| Payload stripped | Use encoding variations; try tag splitting; test null bytes; nested tags |
| XSS not working (HTML only) | JS filtered but HTML allowed; leverage phishing forms, meta refresh redirects |