Statutory Warning & Legal Disclaimer
Part 1: Server-Side Request Forgery (SSRF)
Understanding the Attack Vector
Attack Scenarios
1. Internal Network Reconnaissance
- User Input: http://localhost:8080/admin
- User Input: http://169.254.169.254/latest/meta-data/ (AWS metadata)
- User Input: http://internal-database:5432/
2. Protocol Smuggling
- gopher://: craft TCP payloads to talk to Redis, Memcached
- file://: read local files from the server
- dict://: query dictionary services
- ldap://: LDAP injection and enumeration
3. Cloud Metadata Exploitation
- AWS: 169.254.169.254
- GCP: 169.254.169.254
- Azure: 169.254.169.254
- DigitalOcean: 169.254.169.254
Defensive Strategies
Input Validation
Architecture Defenses
- Isolate fetch services in separate network segments
- Disable unnecessary URL schemes (gopher, file, dict)
- Implement request timeouts and size limits
- Use allowlists rather than blocklists for URLs
Part 2: Insecure Deserialization
The Vulnerability
Language-Specific Attack Vectors
Java Deserialization
- Apache Commons Collections
- Spring Framework
- Hibernate
- Groovy
PHP Object Injection
Python Pickle
Prevention
- JSON with schema validation (loses type information, gains safety)
- Protocol Buffers with strict schema enforcement
- XML with external entity processing disabled
- Signed/encrypted serialization with HMAC verification
Safe alternative using JSON
Part 3: Race Conditions and Time-of-Check to Time-of-Use (TOCTOU)
Understanding Race Conditions
Common Vulnerable Scenarios
1. File Upload Race Condition
2. Coupon/Discount Code Abuse
- Thread 1: Check if coupon used? → No
- Thread 2: Check if coupon used? → No
- Thread 1: Apply discount and mark used
- Thread 2: Apply discount and mark used (Double discount!)
3. Banking Transaction Race
Defensive Techniques
Atomic Operations
File System Defenses
- Use atomic file operations (move/rename)
- Create files with exclusive locks (O_EXCL)
- Validate content after write, not before
Idempotency Keys
Part 4: XML External Entity (XXE) Injection
The Vulnerability
Attack Vectors
1. File Disclosure
2. Remote Resource Inclusion
3. Billion Laughs (DoS)
Prevention
Disable External Entities
- Use JSON instead of XML when possible
- Disable DTD processing entirely
- Disable external entity resolution
- Implement strict input size limits
- Use updated XML parsers with secure defaults
Part 5: Template Injection (SSTI)
Server-Side Template Injection
Detection and Exploitation
Jinja2 (Python)
- {{ 7*7 }} (Test: returns 49)
- {{ config }} (Access configuration)
- {{ ''.__class__.__mro__[1].__subclasses__() }} (Class exploration)
Twig (PHP)
- {{ 7*7 }} (Basic test)
- {{ _self.env.getCacheFilename() }} (File operations)
Prevention
Sandbox Environments
- Use sandboxed template configurations
- Disable dangerous functions and filters
- Implement Content Security Policy headers
Part 6: HTTP Request Smuggling
The Attack
Attack Variants
1. CL.TE (Content-Length vs Transfer-Encoding)
2. TE.CL
3. TE.TE
Impact
- Bypass front-end security controls
- Access internal/admin endpoints
- Hijack user sessions
- Poison web caches
Prevention
- Use HTTP/2 end-to-end (eliminates ambiguity)
- Normalize requests at the front-end
- Reject ambiguous requests
- Ensure consistent parsing between proxy and application server
Part 7: Prototype Pollution (JavaScript)
The Vulnerability
Attack Mechanism
- ?__proto__.isAdmin=true
- ?constructor.prototype.isAdmin=true
- ?__proto__[role]=admin
Prevention
Object.create(null)
Input Sanitization
- Reject keys containing __proto__, constructor, prototype
- Use schema validation libraries (Joi, Yup)
- Freeze Object.prototype: Object.freeze(Object.prototype)
Part 8: Second-Order SQL Injection
Beyond Basic SQLi
Attack Flow
- Attacker registers with username: admin'--
- Application stores username in database (properly escaped)
- Later, admin panel queries: SELECT * FROM users WHERE username = '$user'
- If $user comes from the database without re-escaping: injection occurs
Prevention
- Use parameterized queries consistently
- Never assume stored data is safe
- Apply output encoding when displaying stored data
- Follow the principle of least privilege for database accounts
Read also: API Security & Rate Limiting
Understanding Web Security: Tokens, Rate Limiting, and Mitigation
An educational deep dive into API security, analyzing token structures, common rate-limiting bypasses, and robust defense mechanisms.Conclusion: Defense in Depth
- Input Validation: Validate at the perimeter and at each layer.
- Parameterized Queries: Never concatenate user input into commands.
- Least Privilege: Limit what compromised components can access.
- Security Headers: Implement CSP, HSTS, X-Frame-Options.
- Monitoring: Log and alert on suspicious patterns.
- Regular Testing: Automated scanning and manual penetration testing.