3 ms·
netcat is listening on your machine. the python script is connecting to your machine and giving you a shell. dup2 will take one file descriptor and change it
by simcop2387 13y ago
netcat is listening on your machine. the python script is connecting to your machine and giving you a shell.
dup2 will take one file descriptor and change it into another.
0 is STDIN's file descriptor by default in *nix (I believe this is standard POSIX, but i could be wrong).
1 is STDOUT,
2 is STDERR.
os.dup2 in this case is taking the file descriptor for the socket and placing it into STDIN, STDOUT, and STDERR.
At this point all the code that does anything with these will end up writing to the TCP socket instead.
When subprocess.call is used, it will inherit the parent's STDIN, STDOUT, and STDERR handles, in this case the TCP socket.
So when the shell tries to output anything it'll go to the TCP socket, when it tries to read it'll go to the TCP socket and get the information.
This is the same way that things like nohup work, by putting all the sockets to a file, the process doesn't get killed when the tty goes away, since it doesn't have one. It also grabs some of the signals from the child first also so the shell doesn't kill it directly.