---
name: penetration-testing
description: Ethical hacking and security testing methodologies using penetration testing tools, exploit frameworks, and manual security validation. Use when assessing application security posture and identifying exploitable vulnerabilities.
---
# Penetration Testing
## Overview
Systematic security testing to identify, exploit, and document vulnerabilities in applications, networks, and infrastructure through simulated attacks.
## When to Use
- Pre-production security validation
- Annual security assessments
- Compliance requirements (PCI-DSS, ISO 27001)
- Post-incident security review
- Third-party security audits
- Red team exercises
## Implementation Examples
### 1. **Automated Penetration Testing Framework**
```python
# pentest_framework.py
import requests
import socket
import subprocess
import json
from typing import List, Dict
from dataclasses import dataclass, asdict
from datetime import datetime
@dataclass
class Finding:
severity: str
category: str
target: str
vulnerability: str
evidence: str
remediation: str
cvss_score: float
class PenetrationTester:
def __init__(self, target: str):
self.target = target
self.findings: List[Finding] = []
def test_sql_injection(self, url: str) -> None:
"""Test for SQL injection vulnerabilities"""
print(f"Testing SQL injection on {url}")
payloads = [
"' OR '1'='1",
"'; DROP TABLE users--",
"' UNION SELECT NULL, NULL, NULL--",
"1' AND 1=1--",
"admin'--"
]
for payload in payloads:
try:
response = requests.get(
url,
params={'id': payload},
timeout=5
)
# Check for SQL errors
sql_errors = [
'mysql_fetch_array',
'SQLServer JDBC Driver',
'ORA-01756',
'PostgreSQL',
'sqlite3.OperationalError'
]
for error in sql_errors:
if error in response.text:
self.findings.append(Finding(
severity='critical',
category='SQL Injection',
target=url,
vulnerability=f'SQL Injection detected with payload: {payload}',
evidence=f'Error message: {error}',
remediation='Use parameterized queries or prepared statements',
cvss_score=9.8
))
break
except Exception as e:
print(f"Error testing {url}: {e}")
def test_xss(self, url: str) -> None:
"""Test for Cross-Site Scripting vulnerabilities"""
print(f"Testing XSS on {url}")
payloads = [
"",
"
",
"javascript:alert('XSS')",
"