hostsec

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: MIT Imports: 18 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 hostsec looks for signs that a NextDeploy VPS has been compromised: things an intruder leaves behind (new SSH keys, accounts, cron jobs, services, listeners, processes running from /tmp, modified app files) and gaps that make a break-in likely (password SSH, no firewall, no updates).

It is honest about its limits: a check that runs on the machine can be lied to by an attacker who already has root. A clean report means "none of the common signs", not proof of a clean host.

Index

Constants

View Source
const (
	// DefaultIntegrityDir is root-only: the app runs as `nextdeploy` and can
	// write its own release dir (ISR), so the fingerprints must live where
	// the app can't rewrite them to cover its tracks.
	DefaultIntegrityDir = "/var/lib/nextdeploy/integrity"
	// DefaultAppsDir is where NextDeploy keeps each app's releases.
	DefaultAppsDir = "/opt/nextdeploy/apps"
)
View Source
const DefaultBaselinePath = "/var/lib/nextdeploy/security/baseline.json"

DefaultBaselinePath is root-only state written by prepare / --accept.

Variables

This section is empty.

Functions

func Format

func Format(w io.Writer, r *Report, showOK bool)

Format renders the report for a terminal.

func ManifestPath

func ManifestPath(dir, app, release string) string

ManifestPath is where a release's manifest lives.

func SaveBaseline

func SaveBaseline(path string, b *Baseline) error

SaveBaseline writes it root-only and atomically.

func WriteReleaseManifest

func WriteReleaseManifest(integrityDir, appsDir, app, release, releaseDir string) error

WriteReleaseManifest fingerprints a freshly extracted release (before it runs) and prunes manifests of releases that no longer exist.

Types

type Baseline

type Baseline struct {
	CreatedAt time.Time           `json:"created_at"`
	Users     []string            `json:"users"`     // name:uid:shell for login-capable accounts
	SSHKeys   map[string][]string `json:"ssh_keys"`  // user -> "SHA256:… comment"
	Files     map[string]string   `json:"files"`     // sudoers + cron path -> sha256
	Services  []string            `json:"services"`  // enabled systemd services/timers
	Listeners []string            `json:"listeners"` // "proc/proto" listening on a public address
}

Baseline is the expected state of the things intruders change.

func CollectBaseline

func CollectBaseline(s System) Baseline

CollectBaseline snapshots the current state.

func LoadBaseline

func LoadBaseline(path string) (Baseline, bool, error)

LoadBaseline reads a baseline; ok=false when none exists yet.

type Drift

type Drift struct {
	Modified, Added, Removed []string
}

Drift is how a release differs from its manifest.

func VerifyRelease

func VerifyRelease(m *Manifest, dir string) (Drift, error)

VerifyRelease compares a release directory against its manifest.

type Finding

type Finding struct {
	Severity Severity `json:"severity"`
	Check    string   `json:"check"` // ssh, firewall, listeners, processes, accounts, ssh-keys, sudoers, cron, services, logins, updates, fail2ban, integrity
	Title    string   `json:"title"`
	Detail   []string `json:"detail,omitempty"`
	Fix      string   `json:"fix,omitempty"`
}

Finding is one result.

type Manifest

type Manifest struct {
	App       string            `json:"app"`
	Release   string            `json:"release"`
	CreatedAt time.Time         `json:"created_at"`
	Files     map[string]string `json:"files"` // rel path -> sha256 (or "link:<target>")
}

Manifest fingerprints every deployed file of one release.

func BuildManifest

func BuildManifest(app, release, dir string) (*Manifest, error)

BuildManifest hashes a release directory.

type Options

type Options struct {
	Baseline    Baseline
	HasBaseline bool
	// IntegrityDir holds release manifests (see WriteReleaseManifest).
	IntegrityDir string
	AppsDir      string
	LoginWindow  time.Duration
}

Options for a check run.

type Report

type Report struct {
	GeneratedAt      time.Time        `json:"generated_at"`
	Hostname         string           `json:"hostname,omitempty"`
	BaselineAt       time.Time        `json:"baseline_at,omitzero"`
	BaselineCreated  bool             `json:"baseline_created,omitempty"`
	BaselineAccepted bool             `json:"baseline_accepted,omitempty"`
	Verdict          string           `json:"verdict"`
	Counts           map[Severity]int `json:"counts"`
	Findings         []Finding        `json:"findings"`
}

Report is the JSON contract of `nextdeployd security-check --json` and `nextdeploy security check --json`. Add fields; don't rename them.

func Run

func Run(s System, o *Options) *Report

Run executes every check.

func (*Report) ExitCode

func (r *Report) ExitCode() int

ExitCode is 0 clean/medium, 2 high, 3 critical — so cron/CI can alert.

func (*Report) Finalize

func (r *Report) Finalize()

Finalize sorts findings and computes the verdict.

type Severity

type Severity string

Severity orders findings. Critical = likely compromise; High = a door is open or something changed that an attacker would change; Medium = weaker posture; Info = context; OK = a passed check.

const (
	Critical Severity = "critical"
	High     Severity = "high"
	Medium   Severity = "medium"
	Info     Severity = "info"
	OK       Severity = "ok"
)

Severity levels, most to least serious.

type System

type System struct {
	Root string
	Run  func(name string, args ...string) (string, error)
	// Country maps an IP to a country code for login reports ("" = unknown).
	Country func(ip string) string
	Now     func() time.Time
}

System is the machine being checked. Root prefixes every file path (tests point it at a temp dir); Run executes a command.

func LocalSystem

func LocalSystem() System

LocalSystem is the real host.

Jump to

Keyboard shortcuts

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