ExamGecko
Question list
Search
Search

List of questions

Search

Related questions











Question 18 - PT0-003 discussion

Report
Export

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?

A.
The condition on line 6
Answers
A.
The condition on line 6
B.
The method on line 5
Answers
B.
The method on line 5
C.
The import on line 1
Answers
C.
The import on line 1
D.
The delimiter in line 3
Answers
D.
The delimiter in line 3
Suggested answer: A

Explanation:

Script Analysis:

Line 1: import requests - Imports the requests library to handle HTTP requests.

Line 2: import pathlib - Imports the pathlib library to handle file paths.

Line 4: for url in pathlib.Path('urls.txt').read_text().split('\n'): - Reads the urls.txt file, splits its contents by newline, and iterates over each URL.

Line 5: response = requests.get(url) - Sends a GET request to the URL and stores the response.

Line 6: if response.status == 401: - Checks if the response status code is 401 (Unauthorized).

Line 7: print('URL accessible') - Prints a message indicating the URL is accessible.

Error Identification:

The condition if response.status == 401: is incorrect for determining if a URL is publicly accessible. A 401 status code indicates that the resource requires authentication.

Correct Condition:

The correct condition should check for a 200 status code, which indicates that the request was successful and the resource is accessible.

Corrected Script:

Replace if response.status == 401: with if response.status_code == 200: to correctly identify publicly accessible URLs.

Pentest

Reference:

In penetration testing, checking the accessibility of multiple URLs is a common task, often part of reconnaissance. Identifying publicly accessible resources can reveal potential entry points for further testing.

The requests library in Python is widely used for making HTTP requests and handling responses. Understanding HTTP status codes is crucial for correctly interpreting the results of these requests.

By changing the condition to check for a 200 status code, the script will correctly identify and print URLs that are publicly accessible.

asked 02/10/2024
Ash Eller
37 questions
User
Your answer:
0 comments
Sorted by

Leave a comment first