sshlog/main.go

138 lines
3.2 KiB
Go
Raw Permalink Normal View History

2021-06-01 23:05:41 +02:00
package main
import (
2021-06-01 23:27:41 +02:00
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
2021-06-01 23:05:41 +02:00
"fmt"
"log"
"net"
"os"
"time"
2021-06-01 23:05:41 +02:00
flags "github.com/spf13/pflag"
"golang.org/x/crypto/ssh"
)
2021-06-03 00:12:34 +02:00
var (
address string
isHelp bool
keyPath string
isJson bool
2021-06-03 00:12:34 +02:00
)
func init() {
flags.StringVarP(&address, "address", "a", "0.0.0.0:22", "Address to listen for incoming connections.")
2021-06-01 23:27:41 +02:00
flags.StringVarP(&keyPath, "key", "k", "", "Path to the host key for the ssh server.\nIf absent it will automatically generate a new one for each run.")
2021-06-01 23:05:41 +02:00
flags.BoolVarP(&isHelp, "help", "h", false, "Prints this help message and exits.")
flags.BoolVar(&isJson, "json", false, "Log in JSON instead of plain text.")
2021-06-01 23:05:41 +02:00
flags.Parse()
2021-06-03 00:12:34 +02:00
}
func main() {
2021-06-01 23:05:41 +02:00
if isHelp {
printHelp()
os.Exit(0)
}
privateKey, err := getKey(keyPath)
if err != nil {
log.Fatalf("Failed to get keys: %v\n", err)
}
hostKey, err := ssh.ParsePrivateKey(privateKey)
if err != nil {
log.Fatalf("Failed to parse private key: %v\n", err)
}
2021-06-01 23:27:41 +02:00
serverConfig := ssh.ServerConfig{PasswordCallback: printConnectionData}
serverConfig.AddHostKey(hostKey)
log.Printf("Starting ssh logger on %s...\n", address)
listener, err := net.Listen("tcp", address)
2021-06-01 23:05:41 +02:00
if err != nil {
log.Fatalf("Failed to open listener on '%s': %v\n", address, err)
2021-06-01 23:05:41 +02:00
}
defer listener.Close()
2021-06-02 23:37:22 +02:00
2021-06-01 23:05:41 +02:00
for {
con, err := listener.Accept()
if err != nil {
log.Printf("Failed to accept incoming connection: %v\n", err)
2021-06-01 23:05:41 +02:00
}
go func(con net.Conn, serverConfig ssh.ServerConfig) {
err := connectionHandler(con, serverConfig)
if err != nil {
log.Printf("Failed to handle connection: %v\n", err)
}
}(con, serverConfig)
2021-06-01 23:27:41 +02:00
}
}
func getKey(path string) ([]byte, error) {
2021-06-01 23:27:41 +02:00
if path != "" {
privateBytes, err := os.ReadFile(path)
2021-06-01 23:27:41 +02:00
if err != nil {
return nil, fmt.Errorf("load private key '%s': %w", path, err)
2021-06-01 23:27:41 +02:00
}
return privateBytes, nil
2021-06-01 23:27:41 +02:00
}
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
2021-06-01 23:27:41 +02:00
if err != nil {
return nil, fmt.Errorf("generate RSA key: %w", err)
2021-06-01 23:05:41 +02:00
}
2021-06-01 23:27:41 +02:00
privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey)
privateKeyPem := pem.EncodeToMemory(
2021-06-01 23:27:41 +02:00
&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: privateKeyBytes,
2021-06-01 23:27:41 +02:00
},
)
return privateKeyPem, nil
2021-06-01 23:05:41 +02:00
}
func connectionHandler(con net.Conn, serverConfig ssh.ServerConfig) error {
_, _, _, err := ssh.NewServerConn(con, &serverConfig)
2021-06-01 23:05:41 +02:00
if err != nil {
return fmt.Errorf("connection handler: %w", err)
2021-06-01 23:05:41 +02:00
}
return nil
2021-06-01 23:05:41 +02:00
}
func printConnectionData(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error) {
responseErr := fmt.Errorf("password rejected for %s", conn.User())
address = conn.RemoteAddr().String()
ip, _, err := net.SplitHostPort(address)
if err != nil {
log.Printf("Could not split address '%s' in ip and port: %v\n", address, err)
return nil, responseErr
}
if isJson {
fmt.Printf(
"{\"date\": \"%s\", \"src\": \"%s\", \"username\": \"%s\", \"password\": \"%s\"}\n",
time.Now().Format(time.RFC3339),
ip,
conn.User(),
string(password),
)
} else {
log.Printf("SRC=%s USERNAME=%s PASSWORD=%s\n", ip, conn.User(), string(password))
}
return nil, responseErr
}
2021-06-01 23:05:41 +02:00
func printHelp() {
2021-06-03 00:19:56 +02:00
fmt.Println(`A small tool to log IPs, usernames and passwords from incoming ssh-auth requests.
2021-06-01 23:27:41 +02:00
2021-06-01 23:05:41 +02:00
USAGE:
sshlog [FLAGS]
FLAGS:`)
flags.PrintDefaults()
}