● Notice //GitHub (@shehzade) is temporarily suspended pending an automated false-positive review. All tools are hosted on this Code Mirror and available on request (contact); GitHub links will return once reinstated.
PUBLIC MIRRORGo v1.0

Multi-protocol network egress assessment and DLP verification framework featuring automated L4 sweeps, corporate proxy traversal, and covert protocol smuggling across 20+ channels.

$ git clone https://shehzade.io/repos/contraband.git Copy
Source ZIP (1.93 MB)
Code 174 Commits 1
contraband / pkg / server / certgen / ssh.go
← Back to directory
59 lines · 1.6 KB
Raw
1package certgen
2
3import (
4 "crypto/ed25519"
5 "crypto/rand"
6 "encoding/pem"
7 "fmt"
8
9 "golang.org/x/crypto/ssh"
10)
11
12// SSHHostKey holds the generated ephemeral SSH host key and credentials.
13type SSHHostKey struct {
14 Signer ssh.Signer
15 PublicKey ssh.PublicKey
16 PrivPEM []byte
17}
18
19// GenerateSSHHostKey creates an ephemeral in-memory Ed25519 SSH host key for SFTP.
20func GenerateSSHHostKey() (*SSHHostKey, error) {
21 _, privKey, err := ed25519.GenerateKey(rand.Reader)
22 if err != nil {
23 return nil, fmt.Errorf("failed to generate ed25519 key: %w", err)
24 }
25
26 signer, err := ssh.NewSignerFromKey(privKey)
27 if err != nil {
28 return nil, fmt.Errorf("failed to create ssh signer from ed25519 key: %w", err)
29 }
30
31 privBlock, err := ssh.MarshalPrivateKey(privKey, "")
32 if err != nil {
33 return nil, fmt.Errorf("failed to marshal ssh private key: %w", err)
34 }
35 privPEM := pem.EncodeToMemory(privBlock)
36
37 return &SSHHostKey{
38 Signer: signer,
39 PublicKey: signer.PublicKey(),
40 PrivPEM: privPEM,
41 }, nil
42}
43
44// AuthorizedKeyString returns the OpenSSH authorized_keys format string.
45func (k *SSHHostKey) AuthorizedKeyString(comment string) string {
46 if comment == "" {
47 comment = "contraband-ephemeral-sftp"
48 }
49 return fmt.Sprintf("%s %s", string(ssh.MarshalAuthorizedKey(k.PublicKey)), comment)
50}
51
52// NewSSHServerConfig configures an SSH server for the SFTP listener with the host key.
53func NewSSHServerConfig(hostKey *SSHHostKey, authCallback func(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error)) *ssh.ServerConfig {
54 cfg := &ssh.ServerConfig{
55 PasswordCallback: authCallback,
56 }
57 cfg.AddHostKey(hostKey.Signer)
58 return cfg
59}