sshlog

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 3 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package sshlog extracts the client address and outcome from OpenSSH server log lines.

sshd logs the user name a client sends verbatim, so a client can put text that looks like an address into it:

Invalid user x from 192.0.2.7 port 1 from 198.51.100.10 port 36465

Every pattern here therefore takes the address from a part of the line the client does not control: the fixed end of the line, or a field sshd fills from its own state. The exported patterns use RE2 syntax, so a database that extracts with RE2 (ClickHouse's extract, for example) can use them unchanged.

This holds only for messages sshd sends to syslog (the journal included), where it escapes control characters. With -e it writes to standard error without escaping, so a user name with a newline in it can forge a whole line. Only count lines that sshd itself logged through syslog.

Index

Constants

View Source
const (
	InvalidUserPattern = `^Invalid user .* from (\S+) port \d+$`
	NotAllowedPattern  = `^User \S+ from (\S+) not allowed because `
	DropPattern        = `^drop connection #\d+ from \[([^\]]+)\]:\d+ on `
	AuthClosedPattern  = `^Connection closed by authenticating user \S+ (\S+) port \d+ \[preauth\]$`
	// AcceptedPattern's groups are the user, the address and the key's
	// SHA256 fingerprint; for a certificate, the certified key's, followed by
	// the CA-signed ID, serial and CA key.
	AcceptedPattern = `^Accepted publickey for (\S+) from (\S+) port \d+ ssh2: \S+ (SHA256:\S+)(?: ID .*)?$`
)

RE2 patterns, each with the address as its first group.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event struct {
	Kind        Kind
	Address     netip.Addr
	User        string // Accepted only
	Fingerprint string // Accepted only
}

Event is what one line records.

func Parse

func Parse(line string) (Event, bool)

Parse reads one message, without the syslog prefix. It reports false for lines it does not recognise and for addresses that do not parse, such as a host name when sshd runs with UseDNS.

type Kind

type Kind string

Kind is the outcome a line records.

const (
	// InvalidUser means the client named a user that does not exist.
	InvalidUser Kind = "invalid_user"
	// NotAllowed means the user exists but AllowUsers, AllowGroups, DenyUsers or
	// DenyGroups refuse it. sshd prints the account's name from the password
	// database here, not the client's string.
	NotAllowed Kind = "not_allowed"
	// PenaltyDrop means sshd refused a connection because its source is serving a
	// PerSourcePenalties penalty.
	PenaltyDrop Kind = "penalty_drop"
	// MaxStartupsDrop means sshd refused a connection because too many were
	// unauthenticated. It hits whoever is unlucky, not only the cause.
	MaxStartupsDrop Kind = "maxstartups_drop"
	// AuthClosed means a client for a real account disconnected before
	// authenticating, such as with a wrong key.
	AuthClosed Kind = "auth_closed"
	// Accepted means a public key login succeeded.
	Accepted Kind = "accepted_publickey"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL