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 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 ServeDOSTerm(channel ssh.Channel) { go ReadSSHIn(channel) MyID := randSeq(5) FBIN := make(chan []byte) FrameBufferSubscribers[MyID] = FBIN defer delete(FrameBufferSubscribers, MyID) // Unsubscribe when dead FB := make([]byte, 0) for { FB = <-FBIN if len(FB) != 4000 { continue } channel.Write([]byte("\x1B[0;0H")) // Reset the cursor to 0,0 outbound := "" ptr := 0 for ptr < len(FB) { outbound = outbound + VESAtoVT100(FB[ptr+1]) outbound = outbound + CorrectBadChars(FB[ptr]) ptr = ptr + 2 } _, err := channel.Write([]byte(outbound)) if err != nil { return } } }
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 ReadSSHIn(channel ssh.Channel) { buffer := make([]byte, 2) for { _, err := channel.Read(buffer) if err != nil { return } Keyin <- string(buffer[0]) time.Sleep(time.Millisecond * 10) UpdateScreenNow <- true time.Sleep(time.Millisecond * 200) } }
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 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() }
func (chi *ChanInfo) serveReq(ch ssh.Channel, req *ssh.Request) (err error) { err = chi.onReq(req) if err != nil { log.Error("%s", err.Error()) req.Reply(false, nil) return } r, err := ch.SendRequest(req.Type, req.WantReply, req.Payload) if err != nil { log.Error("%s", err.Error()) req.Reply(false, nil) return } log.Debug("send chan req ok: %s(result: %t)", req.Type, r) err = req.Reply(r, nil) if err != nil { return } log.Debug("reply chan req ok: %s(result: %t)", req.Type, r) return }
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) } }