T05 · Unauthorized Access and Privilege Escalation
- Location
tieba_spider.py:151- Finding
Unrestricted Image URL Retrieval Enables Server-Side Request Forgery and Local Resource Access
- Content
View full analysis
str: """下载图片到本地,返回本地路径""" os.makedirs(save_dir, exist_ok=True) ext = ".jpg" url_path = url.split("?")[0] if "." in url_path.split("/")[-1]: ext = "." + url_path.split("/")[-1].split(".")[-1] if len(ext) > 5: ext = ".jpg" local_filename = filename + ext local_path = os.path.join(save_dir, local_filename) if os.path.exists(local_path): return local_filename try: req = urllib.request.Request( url, headers={ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36", "Referer": "https://tieba.baidu.com/", }, ) with urllib.request.urlopen(req, timeout=20) as resp: with open(local_path, "wb") as f: f.write(resp.read()) log(f"已下载图片: {local_filename}") return local_filename except Exception as e: log(f"图片下载失败: {url[:60]}... 错误: {e}") return "" ``` ### Technical Analysis Image URLs are obtained from remotely supplied Tieba post data and passed directly to `urllib.request.urlopen()`. The implementation does not validate: - The URL scheme. - The destination hostname or port. - The resolved IP address. - Redirect destinations. - Whether the destination is a loopback, private, link-local, or otherwise reserved network address. - The response content type. - The response size. Consequently, a malicious or tampered API response can cause the crawler to request attacker-selected resources. Depending on the URL handlers available to `urllib`, this can include internal HTTP services or non-HTTP local resources. Redirects may also move an initially acceptable request to a prohi ...[truncated 1596 chars]- Remediation
View remediation
