ExamGecko
Question list
Search
Search

List of questions

Search

Related questions











Question 320 - PT0-002 discussion

Report
Export

A penetration tester developed the following script to be used during an engagement:

#!/usr/bin/python

import socket, sys

ports = [21, 22, 23, 25, 80, 139, 443, 445, 3306, 3389]

if len(sys.argv) > 1:

target = socket.gethostbyname (sys. argv [0])

else:

print ('Few arguments.')

print ('Syntax: python {} <target ip>'. format (sys. argv [0]))

sys.exit ()

try:

for port in ports:

s = socket. socket (socket. AF_INET, socket. SOCK_STREAM)

s.settimeout (2)

result = s.connect_ex ((target, port) )

if result == 0:

print ('Port {} is opened'. format (port) )

except KeyboardInterrupt:

print ('\nExiting ... ')

sys.exit ()

However, when the penetration tester ran the script, the tester received the following message:

socket.gaierror: [Errno -2] Name or service not known

Which of the following changes should the penetration tester implement to fix the script?

A.
From: target = socket.gethostbyname (sys. argv [0]) To: target = socket.gethostbyname (sys.argv[1])
Answers
A.
From: target = socket.gethostbyname (sys. argv [0]) To: target = socket.gethostbyname (sys.argv[1])
B.
From: s = socket. socket (socket. AF_INET, socket. SOCK_STREAM) To: s = socket.socket (socket.AF_INET, socket. SOCK_DGRAM)
Answers
B.
From: s = socket. socket (socket. AF_INET, socket. SOCK_STREAM) To: s = socket.socket (socket.AF_INET, socket. SOCK_DGRAM)
C.
From: import socket, sys To: import socket import sys
Answers
C.
From: import socket, sys To: import socket import sys
D.
From: result = s.connect_ex ((target, port) ) To: result = s.connect ( (target, port) )
Answers
D.
From: result = s.connect_ex ((target, port) ) To: result = s.connect ( (target, port) )
Suggested answer: A

Explanation:

The socket.gaierror: [Errno -2] Name or service not known is an error that occurs when the socket module cannot resolve the hostname or IP address given as an argument. In this case, the script is using sys.argv[0] as the argument for socket.gethostbyname, which is the name of the script itself, not the target IP address. The target IP address should be the first command-line argument after the script name, which is sys.argv1. Therefore, changing the script to use sys.argv1 as the argument for socket.gethostbyname will fix the error and allow the script to scan the ports of the target IP address.

Reference:

* The Official CompTIA PenTest+ Study Guide (Exam PT0-002), Chapter 5: Attacks and Exploits, page 262-263.

* socket.gaierror: [Errno -2] Name or service not known | Python1

* How do I fix the error socket.gaierror: [Errno -2] Name or service not known on debian/testing?2

asked 02/10/2024
muhammad ikram
32 questions
User
Your answer:
0 comments
Sorted by

Leave a comment first