func HandleTerminalReading(channel ssh.Channel, term *terminal.Terminal) { defer channel.Close() for { line, err := term.ReadLine() if err != nil { break } cmd_log := Command{Cmd: string(line)} if strings.Contains(string(line), "exit") { logfile.Println("[exit requested]") channel.Close() } if line == "passwd" { line, _ := term.ReadPassword("Enter new UNIX password: "******"[password changed]: " + line) line, _ = term.ReadPassword("Retype new UNIX password: "******"[password changed confirmation]: " + line) term.Write([]byte("passwd: password updated successfully\r\n")) cmd_log.Cmd += " " + line } else { term.Write(RunCommand(line)) } cmd_log.Save() logfile.Println(line) } }
func HandleTcpReading(channel ssh.Channel, term *terminal.Terminal, http map[string]string) { defer channel.Close() for { line, err := term.ReadLine() if err != nil { break } logfile.Println(line) if line == "" { channel.Close() return } if strings.Contains(line, ":") { kv := strings.SplitAfterN(line, ":", 2) http[kv[0]] = strings.TrimSpace(kv[1]) } else { kv := strings.Fields(line) if kv[0] == "POST" || kv[0] == "GET" { http["Method"] = kv[0] http["URI"] = kv[1] } else { http[kv[0]] = kv[1] } } } }
func (ci *ChanInfo) serveReqs(ch ssh.Channel, reqs <-chan *ssh.Request) { defer ch.Close() log.Debug("chan reqs begin.") for req := range reqs { log.Debug("new chan req: %s(reply: %t, payload: %d).", req.Type, req.WantReply, len(req.Payload)) err := ci.serveReq(ch, req) if err != nil { log.Error("%s", err.Error()) } } log.Debug("chan reqs end.") }
func handleChannel(ch ssh.Channel) { term := terminal.NewTerminal(ch, "> ") serverTerm := &ssh.ServerTerminal{ Term: term, Channel: ch, } ch.Accept() defer ch.Close() for { line, err := serverTerm.ReadLine() if err == io.EOF { return } if err != nil { log.Println("handleChannel readLine err:", err) continue } fmt.Println(line) } }
func (s *Session) HandleChannelRequest(channel ssh.Channel, in <-chan *ssh.Request) { for req := range in { switch req.Type { case "shell": if len(req.Payload) == 0 { if err := req.Reply(true, nil); err != nil { log.Printf("Unable to reply to channel request (%s)", err) continue } go s.HandleNewTerminal(channel) } case "pty-req": termLen := req.Payload[3] w, h := ParseTerminalDims(req.Payload[termLen+4:]) s.WinH = uint16(h) s.WinW = uint16(w) if err := req.Reply(true, nil); err != nil { log.Printf("Unable to reply to channel request (%s)", err) } case "window-change": w := &win.Winsize{ Height: uint16(binary.BigEndian.Uint32(req.Payload[4:])), Width: uint16(binary.BigEndian.Uint32(req.Payload)), } if err := win.SetWinsize(s.pty.Fd(), w); err != nil { log.Printf("Unable to set window-change (%s)", err) } case "close": if err := channel.Close(); err != nil { log.Printf("Unable to close channel (%s)", err) } case "default": log.Printf("Invalid Request Type (%s)", req.Type) } } }
func HandleSshRequests(channel ssh.Channel, in <-chan *ssh.Request, term *terminal.Terminal) { for req := range in { ok := false logfile.Println("[request " + req.Type + "]: " + string(req.Payload)) switch req.Type { case "shell": // hacky way to get around presenting the correct prompt channel.Write([]byte("root@web1:/root# ")) term.SetPrompt("root@web1:/root# ") case "exec": term.SetPrompt("") fmt.Println(req) channel.Write(RunCommand(string(req.Payload[4:]))) // close after executing their one off command channel.Close() } /* this condition set and reply is needed to allow a PTY */ ok = true req.Reply(ok, nil) } }
func forwardUnixSocket(channel ssh.Channel, addr string) { conn, err := net.Dial("unix", addr) if err != nil { return } var wg sync.WaitGroup wg.Add(2) go func() { io.Copy(conn, channel) conn.(*net.UnixConn).CloseWrite() wg.Done() }() go func() { io.Copy(channel, conn) channel.CloseWrite() wg.Done() }() wg.Wait() conn.Close() channel.Close() }