【问题标题】:Can ZSH's ZLE input into a child process?ZSH的ZLE可以输入子进程吗?
【发布时间】:2022-09-28 13:25:20
【问题描述】:

我正在尝试创建一个键盘快捷键,以自动将我的 shell 升级为用于反向 shell 的完全交互式 TTY。

目前,我在 Konsole 中配置了一个快捷方式来将其添加到我的标准输入:python3 -c \"import pty;pty.spawn(\'/bin/bash\');\"。然后我需要按 ctrl-z 两次,一次暂停正在运行的进程,再一次执行以下快捷方式(改编自 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

这工作正常,但我希望通过消除快速连续按下三个快捷方式的需要来使其变得更好。我认为可能可以更改 Konsole 的快捷方式以使进程暂停,例如通过将 \\r\\n^Z\\r\\nzle fg-bg\\r\\n 添加到 python3 快捷方式,但这只是按字面意思添加文本(回车除外)。

    标签: zsh penetration-testing konsole reverse-shell zsh-zle


    【解决方案1】:

    在运行前台作业时(在这种情况下,在您提交命令行之后),ZLE 不再处于活动状态,因此无法处理您的输入。它仅在编辑命令行时处于活动状态。

    当您在前台作业期间按 ^Z 时,这会导致终端驱动程序(而不是 ZLE)向其发送 TSTP 信号。这会导致它被暂停。

    如果您希望您的 ^Z 以不同方式处理前台作业,您最好的选择可能是配置您的终端^Z 时发送^Z^Z。但是,它也会在 ZLE 处于活动状态时发送此信息。您最好自己按两次^Z

    至于您在 Konsole 中配置的快捷方式:尝试从命令行运行 zle fg-bg 是徒劳的,因为再次执行命令行时,ZLE 不再处于活动状态。

    【讨论】:

      猜你喜欢
      • 2011-07-21
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多