Exam PT0-002 All QuestionsBrowse all questions from this exam
Question 237

A penetration tester created the following script to use in an engagement:

However, the tester is receiving the following error when trying to run the script:

Which of the following is the reason for the error?

    Correct Answer: C

    The error occurred because the sys module was not imported. In Python, the sys module provides access to some variables used or maintained by the interpreter and to functions interacting strongly with the interpreter. Specifically, sys.argv is used to get command-line arguments, and the script attempts to use this without importing the sys module, resulting in a NameError.

Discussion
dhillerOption: C

sys module not imported

cy_analystOption: C

The script uses the sys module to get the command line arguments. The error message indicates that the interpreter does not recognize the sys name, which suggests that the module was not imported.

cy_analyst

#!/usr/bin/python import socket ports = [21,22,23,25,80,139,443,445,3306,3389] if len(sys.argv) == 2: target = socket.gethostbyname(sys.argv[1]) else: print("Few arguments.") print("Syntax: python {} <>".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 Keyboardlnterrupt: print("Exiting...") sys.exit()

kenechiOption: C

This error occurs when the sys module is used without importing it first.

kenechi

https://bobbyhadz.com/blog/python-nameerror-name-sys-is-not-defined

KingIT_ENGOption: C

C is the answer

Etc_Shadow28000Option: C

The reason for the error is: C. The sys module was not imported. Explanation: In Python, the sys module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. The script attempts to use sys.argv to get the command-line arguments but encounters a NameError because the sys module has not been imported.

Etc_Shadow28000

Corrected Script: To fix the error, you need to import the sys module at the beginning of the script. Here is the corrected script: #!/usr/bin/python import socket import sys # Import the sys module ports = [21, 22, 23, 25, 80, 139, 443, 445, 3306, 3389] if len(sys.argv) == 2: target = socket.gethostbyname(sys.argv[1]) else: print("Few arguments.") print("Syntax: python {} <target>".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)) s.close() except KeyboardInterrupt: print("Exiting...") sys.exit()

0b18240Option: C

#!/usr/bin/python import socket import sys # Import the sys module ports = [21, 22, 23, 25, 80, 139, 443, 445, 3306, 3389] if len(sys.argv) == 2: target = socket.gethostbyname(sys.argv[1]) print("Few arguments.") print("Syntax: python {} <target>".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 open".format(port)) except KeyboardInterrupt: print("Exiting ... ") sys.exit()

[Removed]Option: C

The error message "NameError: name 'sys' is not defined" suggests that the script is trying to use the sys module, but it hasn't been properly imported or defined. Therefore, option C - "The sys module was not imported" - is the correct answer.