GPEN practice question 14 of 35

You run the following bash script in Linux: for i in 'cat hostlist.txt' ;do nc -q 2 -v $i 80 < request.txt done where, hostlist.txt file contains the list of IP addresses and request.txt is the output file. Which of the following tasks do you want to perform by running this script?

Full set: 385 questions
Answer options
A: You want to perform port scanning to the hosts given in the IP address list.
B: You want to transfer file hostlist.txt to the hosts given in the IP address list.
C: You want to perform banner grabbing to the hosts given in the IP address list. Correct answer
D: You want to put nmap in the listen mode to the hosts given in the IP address list.
Correct Answer:
C. You want to perform banner grabbing to the hosts given in the IP address list.
Explanation

The script iterates through a list of IP addresses stored in hostlist.txt. For each IP address ($i), it uses Netcat (nc) to establish a connection to port 80. The < request.txt part redirects the content of request.txt (presumably a valid HTTP request like GET / HTTP/1.0) to the server. The -v (verbose) flag ensures that the response from the server is printed to standard output. This response from a web server on port 80 includes HTTP headers containing version and server type information, a process known as banner grabbing. The -q 2 option ensures the connection closes 2 seconds after the request is sent.

Why Incorrect

A. This is not a port scan because the script only connects to a single, predefined port (80) for each host.
B. The script sends the contents of request.txt to the remote hosts, not the hostlist.txt file itself.
D. The script uses Netcat (nc), not nmap, and it is actively connecting to hosts, not listening for incoming connections.

References

1. OpenBSD. (n.d.). nc(1) - arbitrary TCP and UDP connections and listens. OpenBSD manual pages. Retrieved from https://man.openbsd.org/nc.1. This manual describes the fundamental operation of Netcat (nc) as a client to connect to a specified host and port, reading from standard input and sending the data to the remote host. The script uses this exact client functionality.

2. Shields, C. (2015). Lecture 3: Scanning. EN.600.412/612 Hacking and the Art of Exploitation, Johns Hopkins University, Department of Computer Science. Slide 23, "Banner Grabbing," demonstrates using Netcat for this purpose with the command echo "HEAD / HTTP/1.0\r\n\r\n" | nc -v -w 1 80. This directly parallels the technique used in the question's script, confirming its use for banner grabbing in an academic context.

3. GNU. (n.d.). GNU Netcat: The TCP/IP Swiss Army Knife. GNU Netcat Manual. Section 2.1, "Basic Usage," outlines the client mode syntax netcat [options] host port, which is the mode used in the script to connect to the target hosts. The manual confirms that data from standard input is sent over the established connection.

Scroll to Top