Files
sresarehumantoo 2f1dbda6e1 wsmancer: initial public release (0.1.0)
An interactive WinRM shell for offensive Windows work, speaking the
PowerShell Remoting Protocol end to end. Command results come back as typed
objects (structured JSON on demand), and it authenticates against stock,
locked-down listeners: NTLM and Kerberos with WinRM message sealing that is
integrity-checked in both directions, pass-the-hash, and certificate auth.

Highlights:
  - -o json structured output; -x one-shot and -x - batch/session mode over
    one connection, built for automation as well as a prompt
  - context-aware Tab completion (local/remote paths, the target's own cmdlet
    names, and parameters/values via the target's TabExpansion2), ghost text
  - a session that recovers a dropped connection, and detects a shell the
    target has forgotten
  - the full evil-winrm post-ex set: -s scripts + menu, services,
    invoke-binary, donut-loader, bypass-4msi
  - SOCKS5 pivoting, TLS verification (--ca-cert / --accept-fingerprint /
    --insecure), client certs / mutual TLS

Every claim is live-verified against a real WinRM target; see
docs/verification.md. A Go port of Hackplayers/evil-winrm (LGPL-3.0), with all
20 of its flags and every post-ex command; PSRP transport builds on a patched
smnsjas/go-psrp (MIT), vendored under go-psrp-fork/.
2026-08-12 20:39:57 -04:00

127 lines
3.1 KiB
Go

// Package transcript writes a timestamped record of a session to a file, the
// -l/--log feature. It mirrors the original evil-winrm: a per-session, per-host
// transcript under ~/.wsmancer/logs (override with $WSMANCER_LOG_DIR), so an
// operator keeps a paper trail of exactly what was run and what came back.
//
// Every method is safe to call on a nil *Log, so a caller can hold a
// possibly-nil handle and log unconditionally without guarding each call.
package transcript
import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
const stamp = "2006-01-02 15:04:05"
type Log struct {
mu sync.Mutex
f *os.File
path string
}
// Open creates a new transcript file for host and returns it. The path is
// <root>/<host>/<YYYYMMDD-HHMMSS>.log, where root is $WSMANCER_LOG_DIR or
// ~/.wsmancer/logs. The directory tree is created if missing.
func Open(host string) (*Log, error) {
root, err := logRoot()
if err != nil {
return nil, err
}
dir := filepath.Join(root, sanitizeHost(host))
if err := os.MkdirAll(dir, 0o700); err != nil {
return nil, err
}
path := filepath.Join(dir, time.Now().Format("20060102-150405")+".log")
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600)
if err != nil {
return nil, err
}
return &Log{f: f, path: path}, nil
}
// Path is the file being written, for an "Info: session log: ..." message.
func (l *Log) Path() string {
if l == nil {
return ""
}
return l.path
}
// Event writes one timestamped line, a command, a transfer, an error.
func (l *Log) Event(format string, args ...any) {
if l == nil {
return
}
l.write(time.Now().Format(stamp) + " " + fmt.Sprintf(format, args...))
}
// Output writes a raw block, such as a command's output, with no per-line
// timestamp so it reads exactly as it did on screen. Trailing blank lines and
// empty blocks are dropped.
func (l *Log) Output(text string) {
if l == nil {
return
}
text = strings.TrimRight(text, "\n")
if text == "" {
return
}
l.write(text)
}
// Close writes an end marker and closes the file.
func (l *Log) Close() error {
if l == nil {
return nil
}
l.Event("=== session end ===")
l.mu.Lock()
defer l.mu.Unlock()
return l.f.Close()
}
func (l *Log) write(s string) {
l.mu.Lock()
defer l.mu.Unlock()
fmt.Fprintln(l.f, s)
}
func logRoot() (string, error) {
if d := os.Getenv("WSMANCER_LOG_DIR"); d != "" {
return d, nil
}
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, ".wsmancer", "logs"), nil
}
// sanitizeHost keeps a host usable as a single path element: a hostile or odd
// target name must not be able to introduce path separators or traversal.
func sanitizeHost(host string) string {
if host == "" {
return "unknown"
}
clean := strings.Map(func(r rune) rune {
switch {
case r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z', r >= '0' && r <= '9':
return r
case r == '.' || r == '-' || r == '_':
return r
default:
return '_'
}
}, host)
// Guard against a name that reduces to dots ("." / "..").
if strings.Trim(clean, ".") == "" {
return "unknown"
}
return clean
}