# ldap_server_sim.py - Simulated LDAP authentication server
from flask import Flask, request, jsonify
app = Flask(__name__)
# Fake user database
USERS = {
'admin': {'password': 'secret', 'role': 'admin'},
'user': {'password': 'pass', 'role': 'user'}
}
def ldap_search(username, password):
# Simulate an LDAP filter injection: user-controlled username goes directly into filter
# Real LDAP query: (&(uid={username})(userPassword={password}))
# Here we just simulate: if the username contains wildcard, it may bypass.
if '*' in username:
# Vulnerability: filter becomes (uid=*) which matches any user
# We'll return the first matching user (admin)
return USERS.get('admin')
return USERS.get(username)
@app.route('/login', methods=['POST'])
def login():
username = request.form.get('username')
password = request.form.get('password')
user = ldap_search(username, password)
if user and user['password'] == password:
return jsonify({"message": "Authenticated", "role": user['role']})
return jsonify({"message": "Invalid"}), 401
if __name__ == '__main__':
app.run(port=5000)
认证系统通过直接拼接用户输入来构建 LDAP 搜索过滤器,且未对特殊字符进行转义。攻击者可以注入通配符(*)来绕过认证或枚举用户。
*、(、) 等字符修改 LDAP 过滤器逻辑。pip install flask
python ldap_server_sim.py
python exploit_ldap_injection.py