PT0-003 practice question 19 of 30

[Tools and Code Analysis] A penetration tester needs to test a very large number of URLs for public access. Given the following code snippet: 1…

Full set: 351 questions

[Tools and Code Analysis] A penetration tester needs to test a very large number of URLs for public access. Given the following code snippet:

1 import requests 2 import pathlib 3 4 for url in pathlib.Path("urls.txt").read_text().split("\n"): 5 response = requests.get(url) 6 if response.status == 401: 7 print("URL accessible")

Which of the following changes is required?

Answer options
A: The condition on line 6 Correct answer
B: The method on line 5
C: The import on line 1
D: The delimiter in line 3
Correct Answer:
A. The condition on line 6
Explanation

The script's objective is to identify URLs with public access. The condition on line 6, if response.status == 401:, incorrectly checks for an HTTP 401 Unauthorized status code. A 401 status indicates that the request requires authentication and is therefore not publicly accessible. To correctly identify publicly accessible resources, the script should check for a 200 OK status code, which signifies a successful request. The logic in the conditional statement is the direct opposite of the stated goal, making this change a requirement for the script to function correctly.

Why Incorrect

B. The method on line 5: The requests.get() method is appropriate for retrieving a resource and checking its accessibility. While a HEAD request could be more efficient, GET is not incorrect for this task.
C. The import on line 1: The requests library is a standard and necessary module in Python for making HTTP requests. This import is correct and required for the script to run.
D. The delimiter in line 3: The code on line 4 uses .split("\n") to read a list of URLs from a file, assuming one URL per line. This is a standard and correct method for parsing such a file.

References

1. Fielding

R.

et al. (2022). HTTP Semantics. Internet Engineering Task Force (IETF). RFC 9110.

Section 15.3.1

"200 OK": "The 200 (OK) status code indicates that the request has succeeded."

Section 15.5.2

"401 Unauthorized": "The 401 (Unauthorized) status code indicates that the request has not been applied because it lacks valid authentication credentials for the target resource."

2. MIT OpenCourseWare. (2016). 6.033 Computer System Engineering

Spring 2018. Massachusetts Institute of Technology.

Lecture 15: The World Wide Web: This lecture material covers the fundamentals of the HTTP protocol

including the meaning of common status codes like 200 (success) and 4xx (client errors)

which is foundational knowledge for this type of analysis.

3. Python Software Foundation. (n.d.). Requests: Quickstart. The Requests library documentation.

Section: Response Status Codes: The official documentation for the requests library shows that response.statuscode is the correct attribute for accessing the integer status code of an HTTP response

confirming the syntax is correct but the logical value being checked (401) is wrong for the stated purpose.

Scroll to Top