Reconnaissance and Enumeration
A tester's Python port checker reports every port as open: `if s.connect_ex((host, port)): print(port)`. What is the cause?
Show the answer
Correct answerC. connect_ex returns 0 on success, so any nonzero error code is evaluated as true
AWhy not this: connect() raises exceptions; connect_ex deliberately returns an error code instead.
BWhy not this: -eq is PowerShell syntax and is not valid in Python; == is the comparison operator.
CWhy this is right: 0 means success, so a bare if is true for failures; test connect_ex(...) == 0.
DWhy not this: SOCK_DGRAM creates UDP sockets; the default SOCK_STREAM is the correct type for TCP checks.
socket.connect_ex returns an error number instead of raising, with 0 meaning success. A bare if treats the nonzero failure codes as true, so closed ports print; compare the result with == 0. Raising an exception is connect() behavior.