From simple reverse shell to remote tty and terminal resize
Connect a program's stdin/out/err to a socket and back:
socat TCP-LISTEN:1337,reuseaddr,fork EXEC:bash
socat - TCP:localhost:1337
^Cquits your shell, not an app on the other end (very frustrating!)
- it doesn't handle tty programs such as
Alright, we can do better:
socat TCP-LISTEN:1337,reuseaddr,fork EXEC:bash,pty,stderr,setsid,sigint,sane
socat FILE:`tty`,raw,echo=0 TCP:localhost:1337
- resizing the local terminal does not resize the remote terminal
- we can use
stty rows X cols Ybut on every window resize it is painful
TTY and terminal resize
SIGWINCH is delivered when the window size changes, and we
TIOCGWINSZ to know the rows/cols of the local terminal.
With that, we can send it over to the other end, and set the window size with
iotctl counterpart. However the socket is taken for relaying
the tty data, so we need to multiplex some sort of control channel
to send the window change event and window size information.
Add some TLS and you're not too far from SSH! We could even add port forwarding or file transfer, why not even inline using the control channel to handle commands and multiplex new streams on the fly...