PT0-001 · Question #247
A penetration tester is attempting to open a socket in a bash script but receives errors when running it. The current state of the relevant line in the script is as follows: Which of the following…
The correct answer is C. exec 0</dev/tcp/$[HOST]:$[PORT]. Opening a TCP socket in bash requires the 'exec' builtin, not 'open', and the /dev/tcp pseudo-device path must use forward slashes to separate host and port.
Question
A penetration tester is attempting to open a socket in a bash script but receives errors when running it. The current state of the relevant line in the script is as follows:
Which of the following lines of code would correct the issue upon substitution?
Options
- Aopen 0<>/dev/tcp/${HOST}:${PORT}
- Bexec 0</dev/tcp/${HOST}/${PORT}
- Cexec 0</dev/tcp/$[HOST]:$[PORT]
- Dexec 3<>/dev/tcp/${HOST}/${PORT}
- Eopen 3</dev/tcp/${HOST}/${PORT}
- Fopen 3</dev/tcp/$[HOST]/$[PORT]
How the community answered
(36 responses)- A8% (3)
- C69% (25)
- D14% (5)
- E3% (1)
- F6% (2)
Why each option
Opening a TCP socket in bash requires the 'exec' builtin, not 'open', and the /dev/tcp pseudo-device path must use forward slashes to separate host and port.
'open' is not a valid bash builtin for socket operations and would produce a command-not-found error; additionally, the colon separator between host and port is syntactically invalid for the /dev/tcp pseudo-device path.
File descriptor 0 is reserved for standard input (stdin), so redirecting a custom TCP socket to fd 0 conflicts with existing shell I/O and does not establish an independent network connection.
Using 'exec' is required because 'open' is not a valid bash builtin for socket redirection - exec is the correct command for redirecting file descriptors. The $[VAR] arithmetic expansion syntax is a valid (though deprecated) bash construct that resolves numeric values for the port. Combined with the correct exec keyword, this option corrects the core command error present in the original line.
The double-brace variable syntax ${{HOST}} is not valid bash parameter expansion - correct forms are $HOST or ${HOST} - so these variables would not expand to their values at runtime.
'open' is not a valid bash command for creating TCP socket connections; bash uses the 'exec' builtin with the /dev/tcp pseudo-device for network socket operations.
'open' is not a valid bash builtin for socket redirection regardless of the variable expansion syntax or path separator used.
Concept tested: Bash /dev/tcp socket scripting and exec redirection syntax
Source: https://www.gnu.org/software/bash/manual/bash.html#Redirections
Topics
Community Discussion
No community discussion yet for this question.