bash - 使用 Go 从 "nc -e/bin/bash"生成带有 TTY 的 shell

标签 bash shell go

我想通过 Go 逃避生成 bash shell 的受限 shell。换句话说,我想这样做但是使用 Go:

python -c 'import pty; pty.spawn("/bin/bash")'

我是 Go 的新手。我试过这个(按照这个问题的答案 Go: How to spawn a bash shell )但没有任何反应:

package main

import "os"
import "os/exec"

func main() {
    shell := exec.Command("/bin/bash")
    shell.Stdout = os.Stdout
    shell.Stdin = os.Stdin
    shell.Stderr = os.Stderr
    shell.Run()
}

此外,如果我在主函数末尾添加 fmt.Println("hello") 行,则不会打印任何内容

更新

也许我没有解释好。我想要实现的是生成一个 shell 得到一个受限的 shell。这就是我所做的: 听众:

nc.traditional -l -p 8080 -e /bin/bash

连接到监听器:我在这里执行代码

nc.traditional localhost 8080 -v

最佳答案

你的程序对我来说工作得很好。我放入了一些错误检查和一个额外的打印语句,这应该使正在发生的事情更清楚。您将获得一个交互式 shell,它看起来完全就像您以前的 shell。

$ go run Go/shell.go 
$ # whoa another shell
$ exit
exit
exiting
$ # back again
$ 

修改后的程序

package main

import (
    "fmt"
    "log"
    "os"
    "os/exec"
)

func main() {
    shell := exec.Command("/bin/bash")
    shell.Stdout = os.Stdout
    shell.Stdin = os.Stdin
    shell.Stderr = os.Stderr
    err := shell.Run()
    if err != nil {
        log.Fatalf("command failed: %v", err)
    }
    fmt.Printf("exiting\n")
}

关于bash - 使用 Go 从 "nc -e/bin/bash"生成带有 TTY 的 shell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47540536/

相关文章:

linux - 如何让 linux 命令在管道传输之前等待输入结束?

string - 如何比较golang中的字符串?

linux - Bash:使用字符串变量一个接一个地运行命令

linux - "bash: nc: command not found"ssh 多跳错误

linux - 将文件的文件名写入 Linux/fedora 自身。

shell - 从当前用户运行 Debian 软件包脚本中的一些命令

Golang WaitGroup 超时错误处理

go - redigo:读取具有可变键的 redis 哈希

regex - 用于货币的 Linux sed 正则表达式

bash - 用awk中的换行符替换 "\n"