-
Notifications
You must be signed in to change notification settings - Fork 0
/
owasp.py
133 lines (120 loc) · 4 KB
/
owasp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from flask import Flask, request, jsonify, render_template_string, session
import sqlite3
import hashlib
import os
import pickle
app = Flask(__name__)
app.secret_key = 'supersecretkey' # Used for session management
# Database setup
def init_db():
conn = sqlite3.connect('vulnerable_app.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, username TEXT, password TEXT)''')
c.execute('''INSERT OR IGNORE INTO users (username, password) VALUES ('admin', 'admin')''')
conn.commit()
conn.close()
# Injection Vulnerability
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
conn = sqlite3.connect('vulnerable_app.db')
c = conn.cursor()
# Vulnerable to SQL Injection
c.execute(f"SELECT * FROM users WHERE username = '{username}' AND password = '{password}'")
user = c.fetchone()
conn.close()
if user:
session['username'] = username
return 'Logged in as ' + username
else:
return 'Invalid credentials'
return '''
<form method="post">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
'''
# Broken Authentication
@app.route('/admin')
def admin():
if session.get('username') == 'admin':
return 'Welcome to the admin page'
else:
return 'Access denied'
# Sensitive Data Exposure
@app.route('/hash_password', methods=['POST'])
def hash_password():
password = request.form['password']
# Weak hash algorithm (MD5)
hashed_password = hashlib.md5(password.encode()).hexdigest()
return jsonify({'hashed_password': hashed_password})
# XML External Entities (XXE)
@app.route('/parse_xml', methods=['POST'])
def parse_xml():
xml_data = request.data
try:
# Vulnerable to XXE
from lxml import etree
doc = etree.fromstring(xml_data)
return etree.tostring(doc)
except Exception as e:
return str(e)
# Broken Access Control
@app.route('/user/<int:user_id>')
def get_user(user_id):
conn = sqlite3.connect('vulnerable_app.db')
c = conn.cursor()
# Direct object reference without proper authorization check
c.execute("SELECT * FROM users WHERE id=?", (user_id,))
user = c.fetchone()
conn.close()
if user:
return jsonify({'id': user[0], 'username': user[1]})
return 'User not found'
# Security Misconfiguration
@app.route('/debug')
def debug():
# Exposing debug information
return str(app.config)
# Cross-Site Scripting (XSS)
@app.route('/greet', methods=['GET', 'POST'])
def greet():
if request.method == 'POST':
name = request.form['name']
# Vulnerable to XSS
return render_template_string('<h1>Hello, {}!</h1>'.format(name))
return '''
<form method="post">
Name: <input type="text" name="name"><br>
<input type="submit" value="Greet">
</form>
'''
# Insecure Deserialization
@app.route('/load_object', methods=['POST'])
def load_object():
serialized_object = request.data
# Vulnerable to insecure deserialization
obj = pickle.loads(serialized_object)
return str(obj)
# Using Components with Known Vulnerabilities
@app.route('/vulnerable_component')
def vulnerable_component():
# Using an old version of a library
import urllib3
http = urllib3.PoolManager()
r = http.request('GET', 'http://httpbin.org/robots.txt')
return r.data
# Insufficient Logging & Monitoring
@app.route('/transfer_money', methods=['POST'])
def transfer_money():
sender = request.form['sender']
recipient = request.form['recipient']
amount = request.form['amount']
# Insufficient logging
return f'{amount} transferred from {sender} to {recipient}'
if __name__ == '__main__':
init_db()
app.run(debug=True)