Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I do Password Authentication? #189

Closed
OfflinePing opened this issue Oct 30, 2022 · 2 comments
Closed

How can I do Password Authentication? #189

OfflinePing opened this issue Oct 30, 2022 · 2 comments

Comments

@OfflinePing
Copy link

Hey,
I want to know how I can use Password Authentication. I found no examples what so ever. Please help me

@NoHaxxJustLags
Copy link

Generate a new key pair
ssh-keygen -t rsa -b 2048 -N "" -f key.rsa

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"time"

	"github.com/gliderlabs/ssh"
	ssh2 "golang.org/x/crypto/ssh"
)

func ReadPrivateKeyFromFile(path string) (ssh.Signer, error) {
	keyBytes, err := ioutil.ReadFile(path)
	if err != nil {
		return nil, err
	}

	key, err := ssh2.ParsePrivateKey(keyBytes)
	if err != nil {
		return nil, err
	}

	return key, nil
}

func main() {

	// Create a new server instance
	s := &ssh.Server{
		Addr:            "127.0.0.1:2222",
		Handler:         handleSession,
		PasswordHandler: handleAuthentication,
		IdleTimeout:     60 * time.Second,
	}

	key, err := ReadPrivateKeyFromFile("key.rsa")
	if err != nil {
		panic(err)
	}
	s.AddHostKey(key)

	log.Printf("[+]Starting SSH server on address: %v\n", s.Addr)

	log.Fatal(s.ListenAndServe())

}

// Called if a new ssh session was created
func handleSession(s ssh.Session) {
	s.Write([]byte("Hello World!"))
	s.Close()
}

// Return true to accept password and false to deny
func handleAuthentication(ctx ssh.Context, passwd string) bool {

	if ctx.User() != "root" || passwd != "PA$$W0RD" {
                // Deny
		return false
	}

	fmt.Printf("User: %s,Password: %s, Address: %s", ctx.User(), passwd, ctx.RemoteAddr().String())

        // Accept
	return true

}

@gustavosbarreto
Copy link
Collaborator

@OfflinePing

You can simply define a hardcoded password in the password callback, or have the callback have its own business rule. But if you want to use the same password as the operating system user, you must authenticate via PAM module or Linux shadow. We did this in the @shellhub-io agent using Linux shadow, and you can see how it was done here:

  1. https://github.com/shellhub-io/shellhub/tree/master/agent/pkg/osauth
  2. https://github.com/shellhub-io/shellhub/tree/master/agent/pkg/yescrypt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants