Skip to content

Commit 54e3e84

Browse files
committed
changed to using lxml instead of ETree
Allows for XPath injection because ETree doesn't really allow for XPath logic.
1 parent 8f62419 commit 54e3e84

File tree

1 file changed

+15
-16
lines changed
  • injection/teacher-login-7

1 file changed

+15
-16
lines changed

injection/teacher-login-7/server

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,29 @@
22

33
import os, flask, xml.etree.ElementTree as ET
44
from markupsafe import escape
5+
from lxml import etree
56

67
app = flask.Flask(__name__)
78

89
@app.route("/login", methods=["POST"])
910
def login():
11+
username = flask.request.form.get("user", "")
1012
try:
11-
tree = ET.parse("users.xml")
13+
tree = etree.parse("users.xml")
1214
root = tree.getroot()
13-
except Exception:
14-
return "Error reading users.xml"
15+
except Exception as e:
16+
return f"Error reading XML: {e}"
1517

16-
username = flask.request.form.get("user", "")
17-
18-
# XPATH INJECTION VULNERABILITY HERE ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
19-
user = root.find(f".//user[username='{username}']") # UNSAFE!
20-
21-
if user is not None:
22-
teacher = user.findtext("is_teacher", default="no")
23-
if teacher in ("yes", "true", "1"):
24-
return f"<p>Logged in as a teacher!<br/>{open('/flag').read()}</p>"
25-
else:
26-
return "Logged in … but not as a teacher. <a href='/'>Try again?</a>"
27-
28-
return "No such user… <a href='/'>Try again?</a>"
18+
query = f"//user[username/text()='{username}' and is_teacher/text()='yes']"
19+
try:
20+
results = root.xpath(query)
21+
except Exception as e:
22+
return f"XPath error: {e}"
23+
24+
if results:
25+
return f"<p>Logged in as a teacher!<br/>{open('/flag').read()}</p>"
26+
else:
27+
return "Login failed. <a href='/'>Try again?</a>"
2928

3029
@app.route("/add", methods=["POST"])
3130
def add():

0 commit comments

Comments
 (0)