Linux

Bash: easy health check for TCP connections

Useful for "living off the land" techniques or in scripts:

timeout 1 bash -c "cat /dev/null > /dev/tcp/google.com/80"
echo $? # 0 = OK

TLDR: run the command "cat /dev/null > /dev/tcp/google.com/80" which uses linux's TCP device files and wait for 1 second. If the TCP connection is successful, exit code will be 0

Uwrapping the above command from the end:

  • /dev/tcp/google.com/80 : we're using linux's inbuilt TCP/IP dev files
  • cat /dev/null > /dev/tcp/google.com/80 : sending "null" to this endpoint (google.com, port 80)
  • Sending "null" will obviously not return any valid data, but it will attempt to open a TCP connection to our target. In order not to wait around forever, we use the timeout command, and wait for one second for our cat command to complete
  • Because we use a redirect in the bash command we feed into timeout, it's easier to wrap the command with inverted commas (to avoid redirecting the output of "timeout" itself by mistake), and feed it into bash -c which accepts string commands and feeds them to bash
  • echo $? outputs the previous command's exit code. In this case, 0 will mean TCP connection was successful, and otherwise a non-zero exit code is returned

Linux
InfoSec
0
Linux: Mount bind

Linux "mount" command takes the "bind" argument:

mount --bind /source/dir /dest/dir

this essentially creates a shortcut (similar to a symlink created with "ln"), however rather than a "link" the shortcut can be viewed more as a "gateway" and is hence stronger than a symlink

It's useful in situation where operations in a symlink target throw errors referencing the original location (e.g. some compilation pipelines, or SNAP packages). In this case, replace the symlink with a bind mount

Linux
0