v8rs
v8rs

Reputation: 307

Using Go to spawn a shell with a TTY from "nc -e /bin/bash"

I want to escape a restricted shell spawning a bash shell via Go. In other words, I want to do this but using Go:

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

I am totally new to Go. I have tried this (following the answer in this question Go: How to spawn a bash shell) but nothing happens:

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()
}

Also if I add the fmt.Println("hello") line at the end of the main function nothing is printed

UPDATE

Maybe I did not expalined well. What I am trying to achieve it's to spawn a shell gotten a restricted shell. This is what I did: Listener:

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

Connects to listener: And I exec the code here

nc.traditional localhost 8080 -v

Upvotes: 1

Views: 2040

Answers (1)

Nick Craig-Wood
Nick Craig-Wood

Reputation: 54079

Your program works fine for me. I put some error checking in and an extra print statement which should make what is happening clearer. You are getting an interactive shell, it just looks exactly like your previous shell.

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

Here is the revised program

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")
}

Upvotes: 2

Related Questions