I am trying to create a keyboard shortcut to automate upgrading my shell to a fully interactive TTY for reverse shells.
Currently, I have a shortcut configured in Konsole to add this to my stdin: python3 -c "import pty;pty.spawn('/bin/bash');"
. I then need to press ctrl-z twice, once to suspend the running process and once more to execute the following shortcut (adapted from Jonathan Hodgson's blogpost):
## Upgrade shells with keyboard shortcut (also configured in Konsole settings)
function fg-bg() {
if [[ $#BUFFER -eq 0 ]]; then
local backgroundProgram="$(jobs | tail -n 1 | awk '{print $4}')"
case "$backgroundProgram" in
"nc"|"ncat"|"netcat")
# Make sure that /dev/tty is given to the stty command by doing </dev/tty
local columns=$(stty -a < /dev/tty | grep -oE 'columns [0-9]+' | cut -d' ' -f2)
local rows=$(stty -a < /dev/tty | grep -oE 'rows [0-9]+' | cut -d' ' -f2)
notify-send "Terminal dimensions" "Rows: $rows\nColumns: $columns\nstty command on clipboard"
stty raw -echo < /dev/tty; fg; zle -U "stty rows $rows cols $columns
export TERM=\"xterm-256color\""
;;
*)
fg
;;
esac
fi
}
zle -N fg-bg
bindkey '^Z' fg-bg
This works OK, but I'd like to make it better by removing the need to have three shortcuts pressed in quick succession. I thought it might be possible to change Konsole's shortcut to make the process suspend, for example by adding \r\n^Z\r\nzle fg-bg\r\n
to the python3 shortcut, but that just adds the text literally (except for carriage returns).