func StartServer(ip string, keyFile string, credentialsChan CredentialsChan) error { // The PasswordCallback always returns false. The supplied credentials get // passed to the specified callback for further processing. config := &ssh.ServerConfig{ PasswordCallback: func(conn *ssh.ServerConn, user, pass string) bool { credentialsChan <- &Credentials{user, pass} return false }, } // Read the server's private key pemBytes, err := ioutil.ReadFile(keyFile) if err != nil { return err } if err = config.SetRSAPrivateKey(pemBytes); err != nil { return err } // set up listener listener, err := ssh.Listen("tcp", ip, config) if err != nil { return err } go processConnections(listener) return nil }
func main() { // An SSH server is represented by a ServerConfig, which holds // certificate details and handles authentication of ServerConns. config := &ssh.ServerConfig{ PasswordCallback: func(conn *ssh.ServerConn, user, pass string) bool { return user == "testuser" && pass == "tiger" }, } pemBytes, err := ioutil.ReadFile("id_rsa") if err != nil { panic("Failed to load private key") } if err = config.SetRSAPrivateKey(pemBytes); err != nil { panic("Failed to parse private key") } // Once a ServerConfig has been configured, connections can be // accepted. listener, err := ssh.Listen("tcp", "0.0.0.0:2022", config) if err != nil { panic("failed to listen for connection") } for { sConn, err := listener.Accept() if err != nil { panic("failed to accept incoming connection") } go handleUser(sConn) } }
func Open(config *ssh.ServerConfig) *ssh.Listener { conn, err := ssh.Listen("tcp", ":8765", config) if err != nil { log.Fatal("Failed to listen for connection") } return conn }
func newServer(c *gc.C) *sshServer { private, _, err := ssh.GenerateKey("test-server") c.Assert(err, gc.IsNil) key, err := cryptossh.ParsePrivateKey([]byte(private)) c.Assert(err, gc.IsNil) server := &sshServer{ cfg: &cryptossh.ServerConfig{}, } server.cfg.AddHostKey(key) server.Listener, err = cryptossh.Listen("tcp", "127.0.0.1:0", server.cfg) c.Assert(err, gc.IsNil) return server }
func main() { // Config parse file, err := os.Open("./conf.json") if err != nil { log.Fatal("Error opening config file") } decoder := json.NewDecoder(file) decoder.Decode(&conf) // An SSH server is represented by a ServerConfig, which holds // certificate details and handles authentication of ServerConns. config := &ssh.ServerConfig{ PasswordCallback: func(conn *ssh.ServerConn, user, pass string) bool { notify(newAttacker(conn, user, pass)) return false }, } pemBytes, err := ioutil.ReadFile("rsa.key") if err != nil { log.Fatal("Failed to load private key:", err) } if err = config.SetRSAPrivateKey(pemBytes); err != nil { log.Fatal("Failed to parse private key:", err) } // Once a ServerConfig has been configured, connections can be // accepted. conn, err := ssh.Listen("tcp", "0.0.0.0:2022", config) if err != nil { log.Fatal("failed to listen for connection") } for { // A ServerConn multiplexes several channels, which must // themselves be Accepted. log.Println("accept") sConn, err := conn.Accept() attacker := newAttacker(sConn, "", "") if err != nil { notify(attacker) log.Println("failed to accept incoming connection") continue } if err := sConn.Handshake(); err != nil { notify(attacker) log.Println("failed to handshake") continue } go handleServerConn(sConn) } }
func newMockLineServer(t *testing.T) string { l, err := ssh.Listen("tcp", "127.0.0.1:0", serverConfig) if err != nil { t.Fatalf("unable to newMockAuthServer: %s", err) } go func() { defer l.Close() c, err := l.Accept() if err != nil { t.Errorf("Unable to accept incoming connection: %v", err) return } if err := c.Handshake(); err != nil { // not Errorf because this is expected to // fail for some tests. t.Logf("Handshaking error: %v", err) return } t.Log("Accepted SSH connection") defer c.Close() channel, err := c.Accept() if err != nil { t.Errorf("Unable to accept a channel: %s", err) return } // Just go in a loop now accepting things... we need to // do this to handle packets for SSH. go func() { c.Accept() }() channel.Accept() t.Log("Accepted channel") defer channel.Close() }() return l.Addr().String() }
func configDial(t *testing.T, b *serverBehavior) net.Conn { l, err := ssh.Listen("tcp", "127.0.0.1:0", serverConfig) if err != nil { t.Fatal("unable to listen:", err) } go func() { defer l.Close() conn, err := l.Accept() if err != nil { t.Error("unable to accept:", err) return } defer conn.Close() if err := conn.Handshake(); err != nil { t.Error("unable to handshake:", err) return } for { time.Sleep(b.sessionDelay) ch, err := conn.Accept() if err == io.EOF { return } if err != nil { t.Error("unable to accept:", err) return } ch.Accept() ch.Close() } }() c, err := net.Dial("tcp", l.Addr().String()) if err != nil { t.Fatal("unable to dial test server:", err) } return c }
func main() { // An SSH server is represented by a ServerConfig, which holds // certificate details and handles authentication of ServerConns. config := &ssh.ServerConfig{ PasswordCallback: func(conn *ssh.ServerConn, user, pass string) bool { return user == "testuser" && pass == "tiger" }, } privateBytes, err := ioutil.ReadFile("id_rsa") if err != nil { panic("Failed to load private key") } private, err := ssh.ParsePrivateKey(privateBytes) if err != nil { panic("Failed to parse private key") } config.AddHostKey(private) // Once a ServerConfig has been configured, connections can be // accepted. listener, err := ssh.Listen("tcp", "0.0.0.0:2022", config) if err != nil { panic("failed to listen for connection") } sConn, err := listener.Accept() if err != nil { panic("failed to accept incoming connection") } if err := sConn.Handshake(); err != nil { panic("failed to handshake") } // A ServerConn multiplexes several channels, which must // themselves be Accepted. for { // Accept reads from the connection, demultiplexes packets // to their corresponding channels and returns when a new // channel request is seen. Some goroutine must always be // calling Accept; otherwise no messages will be forwarded // to the channels. channel, err := sConn.Accept() if err != nil { panic("error from Accept") } // Channels have a type, depending on the application level // protocol intended. In the case of a shell, the type is // "session" and ServerShell may be used to present a simple // terminal interface. if channel.ChannelType() != "session" { channel.Reject(ssh.UnknownChannelType, "unknown channel type") continue } channel.Accept() term := terminal.NewTerminal(channel, "> ") serverTerm := &ssh.ServerTerminal{ Term: term, Channel: channel, } go func() { defer channel.Close() for { line, err := serverTerm.ReadLine() if err != nil { break } fmt.Println(line) } }() } }