Challenge
修正一个 PHP 页面里同时带有 bug 和漏洞的那一行,提交修正后的整行。
关卡页给出的是一段数据录入代码,要求:There is only one line that has a vuln, correct it. The output does not have to be valid XHTML and assume that a mysql connection has been made already. There is a bug as well as a vuln. You MUST fix both.
<?php if(!empty($_POST['data'])) { $data = mysql_real_escape_string($_POST['data']); mysql_query("INSERT INTO tbl_data (data) VALUES ('$data')"); }?><form name="grezvahfvfnjuvavatovgpu" action="<?=$_SERVER['PHP_SELF']?>" method="get"> <input type="text" name="data" /> <input type="submit" /></form>Solution
#!/usr/bin/env python3"""HackThisSite Extended Basic 7 (playit) live solver.
The level prints a tiny PHP page and asks for the one line that carries both abug and a vulnerability:
<form name="grezvahfvfnjuvavatovgpu" action="<?=$_SERVER['PHP_SELF']?>" method="get">
* vuln: ``$_SERVER['PHP_SELF']`` is echoed raw into the ``action`` attribute, so a path like ``/x.php/"><script>alert(1)</script>`` is reflected as markup and becomes XSS. ``htmlspecialchars()`` fixes it.* bug: the form submits with ``method="get"`` while the handler only reads ``$_POST['data']``, so the INSERT never runs. ``method="post"`` fixes it.
The corrected line is posted to ``/missions/extbasic/template.php`` together withthe per-load ``formkey`` and ``lvl``. A ``Referer`` pointing at the level page ismandatory, otherwise the endpoint answers ``Invalid Referer`` and the attempt doesnot count. The session cookie comes from ``HTS_COOKIE`` and is never persisted."""import os27 collapsed lines
import reimport urllib.parseimport urllib.request
UA = ("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) " "Chrome/131.0.0.0 Safari/537.36")BASE = "https://www.hackthissite.org"LEVEL = BASE + "/missions/playit/extbasic/7/"TEMPLATE = BASE + "/missions/extbasic/template.php"CK = os.environ["HTS_COOKIE"]
ANSWER = ( '<form name="grezvahfvfnjuvavatovgpu" ' 'action="<?=htmlspecialchars($_SERVER[\'PHP_SELF\'])?>" method="post">')
def get(url): req = urllib.request.Request(url, headers={"Cookie": CK, "User-Agent": UA}) return urllib.request.urlopen(req, timeout=30).read().decode("utf-8", "replace")
def main(): page = get(LEVEL) formkey = re.search(r'name="formkey" value="([^"]+)"', page).group(1) data = urllib.parse.urlencode( {"formkey": formkey, "lvl": "7", "pass": ANSWER}).encode() req = urllib.request.Request( TEMPLATE, data=data, headers={"Cookie": CK, "User-Agent": UA, "Content-Type": "application/x-www-form-urlencoded", "Referer": LEVEL}) resp = urllib.request.urlopen(req, timeout=30).read().decode("utf-8", "replace") print("go-on link to level 8:", "/missions/playit/extbasic/8" in resp)
if __name__ == "__main__": main()