gosshd

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package gosshd is an SSH server for WinPE guests.

Win32-OpenSSH cannot serve a session there. Per connection it spawns a pre-auth child as a virtual account — LsaManageSidNameMapping, then LogonUserExExW with LOGON32_PROVIDER_VIRTUAL — and authenticates the user with an S4U logon. WinPE supports no user logons at all, so both stages fail and the connection closes before authentication. Privilege separation has been mandatory upstream since 7.5, so there is no configuration that avoids it.

This server answers with a password callback in its own process and starts children with a plain CreateProcess, so no part of a session touches the Windows logon subsystem. The guest is a throwaway PE behind a host-only NAT reached over a per-run forwarded port, which is what makes a fixed password acceptable here.

Index

Constants

View Source
const (
	// DefaultAddr is the guest-side listen address. The host reaches it
	// through a per-run forwarded port, never directly.
	DefaultAddr = ":22"

	// DefaultUser and DefaultPassword are the credentials the guest is
	// built with. WinPE's Administrator has no password and no usable
	// account database, so these name nothing in Windows — they are only
	// this server's own shared secret with the harness.
	DefaultUser     = "admin"
	DefaultPassword = "admin"
)

Variables

This section is empty.

Functions

func ListenVsock added in v0.3.0

func ListenVsock(port uint32) (net.Listener, error)

ListenVsock returns an error on non-Windows platforms. The viosock.sys driver that provides AF_VSOCK is Windows-only (virtio-win).

func SessionCommand

func SessionCommand(request string) []string

SessionCommand is the argv for a session under cmd.exe. An empty request means the client asked for a shell rather than a single command.

cmd.exe rather than pwsh: WinPE always has it, and the harness stages pwsh at a path that is not on PATH.

func SessionCommandShell added in v0.3.0

func SessionCommandShell(request, shell string) []string

SessionCommandShell is the argv for a session under the named shell: "cmd" (default) or "powershell". Under powershell the request is passed as a single -Command argument, so the host can send a PowerShell one-liner verbatim with no cmd re-parsing — what the wsl provisioning path relies on.

Types

type Client added in v0.3.0

type Client struct {
	// contains filtered or unexported fields
}

Client connects to a gosshd server running inside a WinPE guest.

func Dial added in v0.3.0

func Dial(ctx context.Context, addr string) (*Client, error)

Dial connects to a gosshd server at addr using the default credentials. The context deadline controls the connection timeout.

func DialWith added in v0.3.0

func DialWith(ctx context.Context, addr, user, password string) (*Client, error)

DialWith connects with explicit credentials.

func (*Client) Close added in v0.3.0

func (c *Client) Close() error

Close closes the SSH connection.

func (*Client) Run added in v0.3.0

func (c *Client) Run(ctx context.Context, cmd string) (stdout, stderr []byte, exitCode int, err error)

Run executes a command and returns captured stdout, stderr, and exit code. Cancelling ctx closes the session and returns ctx.Err() instead of blocking on a guest that never answers.

func (*Client) RunInput added in v0.3.0

func (c *Client) RunInput(ctx context.Context, cmd string, input io.Reader, stdout, stderr io.Writer) (int, error)

RunInput executes a command with stdin fed from input and output streamed to the writers. The server pipes session stdin into the command, so this is the transport for pushing files into a guest that has no sftp. Cancelling ctx closes the session, like Run.

func (*Client) RunStream added in v0.3.0

func (c *Client) RunStream(ctx context.Context, cmd string, stdout, stderr io.Writer) (int, error)

RunStream executes a command and streams output to the provided writers. Cancelling ctx closes the session, like Run.

type Server

type Server struct {
	// Addr is the listen address, ":22" when empty.
	Addr string
	// User and Password are the only accepted credentials. Both empty
	// means no credentials are accepted at all.
	User, Password string
	// Log receives connection events. Discarded when nil.
	Log *log.Logger

	// Shell selects the interpreter sessions run under. "" or "cmd" uses
	// cmd.exe — the WinPE-safe default, because full Windows PowerShell is not
	// guaranteed on WinPE's PATH (pwsh is staged elsewhere). "powershell" uses
	// Windows PowerShell (System32\...\powershell.exe), which the wsl
	// provisioning server runs under so the host can send PowerShell directly.
	Shell string

	// Events receives one structured JSON line per session (command, exit
	// code, captured stdout/stderr, duration). Discarded when nil. In the
	// guest this is the virtio-serial structured port whose host end is
	// build.jsonl, giving a durable 1:1 record of everything run over SSH.
	Events io.Writer
	// contains filtered or unexported fields
}

Server serves one WinPE guest.

func (Server) Authenticate

func (s Server) Authenticate(user, password string) bool

Authenticate reports whether the offered credentials are the configured ones. A Server with no credentials configured accepts nothing, so a zero-value Server cannot be reached with an empty password.

func (Server) ListenAndServe

func (s Server) ListenAndServe() error

ListenAndServe serves until the listener fails.

func (Server) Serve

func (s Server) Serve(l net.Listener) error

Serve serves connections from l until it fails.

type VsockAddr added in v0.3.0

type VsockAddr uint32

VsockAddr is a vsock port address.

func (VsockAddr) Network added in v0.3.0

func (a VsockAddr) Network() string

func (VsockAddr) String added in v0.3.0

func (a VsockAddr) String() string

Directories

Path Synopsis
cmd
gosshd command
Command gosshd is the SSH server staged into a guest.
Command gosshd is the SSH server staged into a guest.

Jump to

Keyboard shortcuts

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