Educational Demo for CSCE Software Security Class
This project demonstrates replay attacks and their prevention mechanisms in a practical, hands-on way. It includes both vulnerable and secure implementations to illustrate the difference.
A replay attack is a form of network attack where valid data transmission is maliciously or fraudulently repeated or delayed. An attacker intercepts (or captures) legitimate communication and "replays" it later to:
- Gain unauthorized access
- Duplicate transactions
- Bypass authentication mechanisms
- Financial: Replaying a money transfer request multiple times
- Authentication: Reusing captured session tokens
- IoT: Replaying unlock commands to smart locks
csce_demo/
βββ main.py # Interactive menu for running demos
βββ server.py # Vulnerable server (NO replay protection)
βββ server_secure.py # Secure server (WITH replay protection)
βββ client.py # Legitimate client application
βββ attacker.py # Replay attack simulator
βββ pyproject.toml # Python dependencies
βββ README.md # This file
- Python 3.10+
- Flask (for HTTP server)
- Requests (for HTTP client)
# Install dependencies
pip install flask requests
# Or using uv (if available)
uv add flask requests# Interactive menu (recommended for first-time users)
python main.pyThe interactive menu provides three demonstrations:
- Vulnerable Server Demo - Shows successful replay attack
- Secure Server Demo - Shows replay attack prevention
- Side-by-Side Comparison - Run both servers simultaneously
You can also run components individually:
# Terminal 1: Start vulnerable server
python server.py
# Terminal 2: Run legitimate client
python client.py
# Terminal 3: Launch replay attack
python attacker.pyFor secure server testing:
# Terminal 1: Start secure server
python server_secure.py
# Terminal 2: Modify client/attacker to use port 5001Client β Server: POST /login (username, password)
Server β Client: 200 OK (token: abc123)
Client β Server: POST /transfer (token: abc123, to: bob, amount: $100)
Server β Client: 200 OK (transfer successful)
Attacker β Server: POST /transfer (token: abc123, to: attacker, amount: $100)
Server β Client: 200 OK (transfer successful) β οΈ VULNERABILITY!
The attacker uses the same token without needing credentials!
The secure server implements two layers of defense:
# Request must include current timestamp
{
"to": "bob",
"amount": 100,
"timestamp": 1705401234.567 # Current Unix timestamp
}Server validates:
- Timestamp is within acceptable window (60 seconds)
- Rejects requests with old or future timestamps
# Request must include unique nonce
{
"to": "bob",
"amount": 100,
"nonce": "4f8a3c2d1b9e7f6a" # Unique random value
}Server validates:
- Nonce hasn't been used before
- Tracks used nonces in memory
- Cleans up expired nonces (5-minute window)
# Secure request format
{
"to": "bob",
"amount": 100,
"timestamp": 1705401234.567,
"nonce": "4f8a3c2d1b9e7f6a"
}Even if an attacker captures this request, they cannot:
- Replay immediately: Same nonce will be rejected
- Replay later: Timestamp will be too old
- Modify timestamp: Would need to generate new nonce
1. Alice logs in β Token: abc123
2. Alice transfers $100 to Bob β Success
3. Attacker captures token: abc123
4. Attacker transfers $50 to self β Success β οΈ
5. Attacker transfers $50 to self β Success β οΈ
6. Attacker transfers $50 to self β Success β οΈ
Result: Alice loses $250 total (1 legitimate + 3 replay attacks)
1. Alice logs in β Token: xyz789
2. Alice transfers $100 (nonce: n1, time: t1) β Success
3. Attacker captures token & request
4. Attacker replays exact request β BLOCKED (duplicate nonce)
5. Attacker tries with old timestamp β BLOCKED (timestamp expired)
6. Attacker tries with new timestamp β BLOCKED (still needs valid nonce)
Result: Alice loses only $100 (legitimate transaction only) β
- Tokens can be intercepted (man-in-the-middle, network sniffing)
- Without context, same token = same access
- Unlimited validity = unlimited risk
- Timestamp: Limits replay window
- Nonce: Prevents duplicate requests
- Token Expiry: Limits token lifetime
- HTTPS: Encrypts communication (not shown in demo)
- Request Signing: Cryptographic verification (advanced)
- Always use HTTPS/TLS
- Implement both timestamp AND nonce validation
- Use short token expiration (5-15 minutes)
- Consider request signing with HMAC/JWT
- Log and monitor for replay attempt patterns
- Implement rate limiting
# In vulnerable system
1. Login and capture token
2. Logout
3. Use captured token β Still works! β οΈ
# In secure system
1. Login and capture token
2. Logout
3. Use captured token β Rejected β
# In vulnerable system
1. Capture request at 10:00 AM
2. Replay at 10:05 AM β Still works! β οΈ
# In secure system
1. Capture request at 10:00 AM (timestamp: t1)
2. Replay at 10:05 AM β Rejected (timestamp too old) β
# Attacker tries to be clever
1. Capture: {to: "bob", amount: 100, nonce: "n1", time: t1}
2. Modify: {to: "attacker", amount: 1000, nonce: "n1", time: t2}
3. Replay β Rejected (nonce already used) β
This demo is designed for classroom use:
- Duration: 20-30 minutes for full demonstration
- Prerequisites: Basic understanding of HTTP, REST APIs
- Learning Objectives:
- Understand replay attack mechanics
- Learn defense mechanisms (timestamp, nonce)
- Appreciate defense-in-depth security
- Why is timestamp validation alone insufficient?
- What are the trade-offs of nonce storage?
- How does HTTPS/TLS relate to replay protection?
- What other attacks might this system be vulnerable to?
- How would you implement this in a distributed system?
- Modify the attacker to try bypassing timestamp validation
- Implement request signing using HMAC
- Add rate limiting to prevent brute-force
- Create a distributed nonce store using Redis
- Implement token refresh mechanisms
This code is intentionally vulnerable for educational purposes.
- DO NOT use the vulnerable server in production
- DO NOT use this code as a template for real applications
- Always follow security best practices in production code
- This demo simplifies many real-world complexities
Educational use only. For CSCE Software Security class demonstrations.
Remember: Security is not a feature you add at the endβit must be designed in from the start. This demo shows why security by design matters! π‘οΈ