3
main file descriptors (often abbreviated fd) for each application:stdin
stdout
stderr
stderr
to /dev/null
, which is the same principle.socat
to redirect stdin
and stdout
(and sometimes stderr
) to the user when they connect. These are super simple and often require no more than a replacement ofstdin
and stdout
may not be shown back to the user.3
. Once we connect, we set up another fd, fd 4
(neither the 3
nor the 4
is certain, but statistically likely).0
, 1
and 2
for its I/O.stdin
and stderr
to fd 4
, and glibc provides a simple way to do so.dup
syscall (and C function) duplicates the fd and uses the lowest-numbered free fd. However, we need to ensure it's fd 4
that's used, so we can use dup2()
. dup2
takes in two parameters: a newfd
and an oldfd
. Descriptor oldfd
is duplicated to newfd
, allowing us to interact with stdin
and stdout
and actually use any shell we may have popped.newfd
is in use it is silently closed, which is exactly what we wish.