Skip to content

Commit aa7a904

Browse files
committed
Adding OWASP Code
1 parent ef04b8d commit aa7a904

24 files changed

+18544
-0
lines changed

Diff for: owasp-top10/.env

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
MONGODB_URI=mongodb://localhost:27017
2+

Diff for: owasp-top10/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
venv/

Diff for: owasp-top10/README.md

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# Educational Vulnerable Application
2+
3+
**WARNING: This application is intentionally vulnerable and meant for educational purposes only. DO NOT deploy this in any production environment.**
4+
5+
## Overview
6+
This application demonstrates common security vulnerabilities based on OWASP Top 10 (2021). It consists of two microservices:
7+
- Auth Service: Handles user authentication with intentional vulnerabilities
8+
- Profile Service: Manages user profile data with intentional vulnerabilities
9+
10+
## Intentional Vulnerabilities
11+
12+
### 1. Broken Access Control (A01:2021)
13+
- No role-based access control implementation
14+
- Direct object references without verification
15+
- Location: `auth_service/routes.py` - endpoint `/api/user/<id>`
16+
17+
### 2. Cryptographic Failures (A02:2021)
18+
- Passwords stored with weak hashing (MD5)
19+
- Sensitive data transmitted without encryption
20+
- Location: `auth_service/utils.py` - `hash_password()` function
21+
22+
### 3. Injection (A03:2021)
23+
- SQL injection vulnerability in login query
24+
- NoSQL injection in profile lookup
25+
- Location: `auth_service/routes.py` - `/login` endpoint
26+
- Location: `profile_service/routes.py` - `/profile` endpoint
27+
28+
### 4. Insecure Design (A04:2021)
29+
- No rate limiting on login attempts
30+
- Password reset without verification
31+
- Location: `auth_service/routes.py` - all endpoints
32+
33+
### 5. Security Misconfiguration (A05:2021)
34+
- Debug mode enabled
35+
- Default/weak credentials
36+
- Location: `config.py` - all configuration settings
37+
38+
### 6. Vulnerable Components (A06:2021)
39+
- Outdated dependencies in requirements.txt
40+
- Known vulnerable versions of packages
41+
42+
### 7. Authentication Failures (A07:2021)
43+
- Weak password requirements
44+
- Session tokens without expiry
45+
- Location: `auth_service/utils.py` - `validate_password()` function
46+
47+
### 8. Software and Data Integrity Failures (A08:2021)
48+
- No integrity checks on uploaded files
49+
- Unsecured deserialization
50+
- Location: `profile_service/routes.py` - `/upload` endpoint
51+
52+
### 9. Security Logging Failures (A09:2021)
53+
- No logging of security events
54+
- Sensitive data in logs
55+
- Location: Both services lack proper logging
56+
57+
### 10. Server-Side Request Forgery (A10:2021)
58+
- Unvalidated URL inputs
59+
- Location: `profile_service/routes.py` - `/fetch-avatar` endpoint
60+
61+
## Setup Instructions
62+
63+
1. Create virtual environment:
64+
```bash
65+
python -m venv venv
66+
source venv/bin/activate # Linux/Mac
67+
venv\Scripts\activate # Windows
68+
```
69+
70+
2. Install dependencies:
71+
```bash
72+
pip install -r requirements.txt
73+
```
74+
75+
3. Set up MongoDB:
76+
- Use local MongoDB instance or
77+
- Create free MongoDB Atlas cluster
78+
79+
4. Configure environment:
80+
```bash
81+
cp .env.example .env
82+
# Edit .env with your MongoDB URI
83+
```
84+
85+
5. Run services:
86+
```bash
87+
# Terminal 1
88+
python auth_service/app.py
89+
90+
# Terminal 2
91+
python profile_service/app.py
92+
```
93+
94+
## Testing Vulnerabilities
95+
96+
1. SQL Injection:
97+
```
98+
Username: admin' OR '1'='1
99+
Password: anything
100+
```
101+
102+
2. NoSQL Injection:
103+
```javascript
104+
{"$gt": ""} in username field
105+
```
106+
107+
3. Weak Passwords:
108+
```
109+
Any password with length > 1 is accepted
110+
```
111+
112+
4. SSRF Test:
113+
```
114+
/fetch-avatar?url=file:///etc/passwd
115+
```
116+
117+
## Automated Testing
118+
Run security scanners against http://localhost:5000 and http://localhost:5001 to detect vulnerabilities.
119+
120+
## Disclaimer
121+
This application is for educational purposes only. It contains intentional security vulnerabilities to demonstrate common security issues. DO NOT use any of this code in production environments.

Diff for: owasp-top10/auth_service/Dockerfile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Dockerfile for auth-service
2+
FROM node:14
3+
4+
WORKDIR /app
5+
COPY package.json package-lock.json ./
6+
RUN npm install
7+
8+
COPY . .
9+
CMD ["node", "server.js"]
874 Bytes
Binary file not shown.

Diff for: owasp-top10/auth_service/app.py

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
from flask import Flask, request, jsonify
2+
from flask_cors import CORS # Import CORS
3+
from utils import hash_password, generate_token
4+
import sqlite3
5+
import json
6+
7+
app = Flask(__name__)
8+
9+
# Enable CORS for all routes
10+
CORS(app)
11+
12+
app.config['DEBUG'] = True
13+
app.secret_key = 'xuysoe54Puj990'
14+
15+
@app.route('/', methods=['GET'])
16+
def entry():
17+
return jsonify({"error": "Invalid credentials"}), 401
18+
19+
@app.route('/login', methods=['POST'])
20+
def login():
21+
data = request.get_json()
22+
username = data.get('username')
23+
password = data.get('password')
24+
25+
# Verify user credentials (hash password comparison)
26+
query = f"SELECT * FROM users WHERE username='{username}' AND password='{hash_password(password)}'"
27+
conn = sqlite3.connect('users.db')
28+
cursor = conn.cursor()
29+
user = cursor.execute(query).fetchone()
30+
31+
if user:
32+
# Generate JWT token
33+
token = generate_token(username)
34+
35+
# Decode token to string and return
36+
return jsonify({"token": token.decode('utf-8')}) # Decode bytes to string
37+
return jsonify({"error": "Invalid credentials"}), 401
38+
39+
40+
@app.route('/register', methods=['POST'])
41+
def register():
42+
data = request.get_json()
43+
44+
if len(data.get('password', '')) > 1:
45+
hashed_password = hash_password(data['password'])
46+
47+
# Create connection to SQLite database
48+
conn = sqlite3.connect('users.db')
49+
cursor = conn.cursor()
50+
51+
# Ensure the users table is created if it does not exist
52+
cursor.execute('''
53+
CREATE TABLE IF NOT EXISTS users (
54+
id INTEGER PRIMARY KEY AUTOINCREMENT,
55+
username TEXT UNIQUE NOT NULL,
56+
password TEXT NOT NULL
57+
)
58+
''')
59+
60+
try:
61+
# Use parameterized queries to prevent SQL injection
62+
cursor.execute(
63+
'INSERT INTO users (username, password) VALUES (?, ?)',
64+
(data['username'], hashed_password)
65+
)
66+
conn.commit()
67+
return jsonify({"message": "User registered successfully"})
68+
except sqlite3.IntegrityError:
69+
# Handle unique constraint violation (duplicate username)
70+
return jsonify({"error": "Username already exists"}), 400
71+
finally:
72+
conn.close()
73+
else:
74+
return jsonify({"error": "Invalid password"}), 400
75+
76+
@app.route('/api/user/<id>', methods=['GET'])
77+
def get_user(id):
78+
conn = sqlite3.connect('users.db')
79+
cursor = conn.cursor()
80+
user = cursor.execute(f"SELECT * FROM users WHERE id={id}").fetchone()
81+
82+
if user:
83+
return jsonify({
84+
"id": user[0],
85+
"username": user[1],
86+
"password_hash": user[2]
87+
})
88+
return jsonify({"error": "User not found"}), 404
89+
90+
if __name__ == '__main__':
91+
app.run(port=5000, debug=True)

Diff for: owasp-top10/auth_service/utils.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import hashlib
2+
import jwt
3+
4+
def hash_password(password):
5+
return hashlib.md5(password.encode()).hexdigest()
6+
7+
def generate_token(username):
8+
return jwt.encode(
9+
{'username': username},
10+
'xuysoe54Puj990',
11+
algorithm='HS256'
12+
)
13+
14+
def validate_password(password):
15+
return len(password) > 1

Diff for: owasp-top10/docker-compose.yml

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
version: '3.8'
2+
services:
3+
frontend:
4+
build:
5+
context: ./frontend
6+
dockerfile: Dockerfile
7+
ports:
8+
- "3000:3000"
9+
environment:
10+
- REACT_APP_AUTH_SERVICE_URL=http://auth-service:5000
11+
- REACT_APP_PROFILE_SERVICE_URL=http://profile-service:5001
12+
depends_on:
13+
- auth-service
14+
- profile-service
15+
auth-service:
16+
build:
17+
context: ./auth_service
18+
dockerfile: Dockerfile
19+
ports:
20+
- "5000:5000"
21+
environment:
22+
- MONGO_URI=mongodb://mongo:27017/
23+
depends_on:
24+
- mongo
25+
profile-service:
26+
build:
27+
context: ./profile_service
28+
dockerfile: Dockerfile
29+
ports:
30+
- "5001:5001"
31+
environment:
32+
- MONGO_URI=mongodb://mongo:27017/
33+
depends_on:
34+
- mongo
35+
mongo:
36+
image: mongo:4.4
37+
ports:
38+
- "27017:27017"
39+
volumes:
40+
- mongodb_data:/data/db
41+
volumes:
42+
mongodb_data:

Diff for: owasp-top10/frontend/Dockerfile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Dockerfile for frontend
2+
FROM node:14
3+
4+
WORKDIR /app
5+
COPY package.json package-lock.json ./
6+
RUN npm install
7+
8+
COPY . .
9+
RUN npm run build
10+
11+
CMD ["npm", "start"]

0 commit comments

Comments
 (0)