Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added ssh-agent support #6

Merged
merged 1 commit into from
Oct 8, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions session/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package session

import (
"fmt"
"golang.org/x/crypto/ssh"
"io/ioutil"
"net"
"os"
"sync"
"time"

"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"

"github.com/evilsocket/shellz/core"
"github.com/evilsocket/shellz/log"
)
Expand All @@ -29,18 +33,31 @@ func ctx2ClientConfig(ctx Context) (error, *ssh.ClientConfig) {
}

if ctx.KeyFile != "" {
if ctx.KeyFile, err = core.ExpandPath(ctx.KeyFile); err != nil {
return fmt.Errorf("error while expanding path '%s': %s", ctx.KeyFile, err), nil
}

log.Debug("loading ssh key from %s ...", ctx.KeyFile)

if key, err := ioutil.ReadFile(ctx.KeyFile); err != nil {
return fmt.Errorf("error while reading key file %s: %s", ctx.KeyFile, err), nil
} else if signer, err := ssh.ParsePrivateKey(key); err != nil {
return fmt.Errorf("error while parsing key file %s: %s", ctx.KeyFile, err), nil
if ctx.KeyFile == "agent" {
if socket := os.Getenv("SSH_AUTH_SOCK"); socket != "" {
if conn, err := net.Dial("unix", socket); err == nil {
agentClient := agent.NewClient(conn)
authMethods = append(authMethods, ssh.PublicKeysCallback(agentClient.Signers))
} else {
return fmt.Errorf("error while connection to ssh-agent '%s': %s", socket, err), nil
}
} else {
return fmt.Errorf("error while connecting to ssh-agent (cant find SSH_AUTH_SOCK variable)"), nil
}
} else {
authMethods = append(authMethods, ssh.PublicKeys(signer))
if ctx.KeyFile, err = core.ExpandPath(ctx.KeyFile); err != nil {
return fmt.Errorf("error while expanding path '%s': %s", ctx.KeyFile, err), nil
}

log.Debug("loading ssh key from %s ...", ctx.KeyFile)

if key, err := ioutil.ReadFile(ctx.KeyFile); err != nil {
return fmt.Errorf("error while reading key file %s: %s", ctx.KeyFile, err), nil
} else if signer, err := ssh.ParsePrivateKey(key); err != nil {
return fmt.Errorf("error while parsing key file %s: %s", ctx.KeyFile, err), nil
} else {
authMethods = append(authMethods, ssh.PublicKeys(signer))
}
}
}

Expand Down