Skip to content

Commit 279aa3d

Browse files
committed
Add option for client binary to specify private key, closes #200
1 parent f010356 commit 279aa3d

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

cmd/client/main.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"syscall"
1515

1616
"github.com/NHAS/reverse_ssh/internal/client"
17+
"github.com/NHAS/reverse_ssh/internal/client/keys"
1718
"github.com/NHAS/reverse_ssh/internal/terminal"
1819
"github.com/NHAS/reverse_ssh/pkg/logger"
1920
)
@@ -64,6 +65,8 @@ func printHelp() {
6465
fmt.Println("\t\t--sni\tWhen using TLS set the clients requested SNI to this value")
6566
fmt.Println("\t\t--log-level\tChange logging output levels, [INFO,WARNING,ERROR,FATAL,DISABLED]")
6667
fmt.Println("\t\t--version-string\tSSH version string to use, i.e SSH-VERSION, defaults to internal.Version-runtime.GOOS_runtime.GOARCH")
68+
fmt.Println("\t\t--private-key-path\tOptional path to unencrypted SSH key to use for connecting")
69+
6770
if runtime.GOOS == "windows" {
6871
fmt.Println("\t\t--host-kerberos\tUse kerberos authentication on proxy server (if proxy server specified)")
6972
}
@@ -146,6 +149,25 @@ func main() {
146149
}
147150
}
148151

152+
privateKeyPath, err := line.GetArgString("private-key-path")
153+
if err == nil {
154+
keyBytes, err := os.ReadFile(privateKeyPath)
155+
if err != nil {
156+
log.Fatalf("private key path was specified %q, but could not read: %s", privateKeyPath, err)
157+
}
158+
159+
if err = keys.SetPrivateKey(string(keyBytes)); err != nil {
160+
log.Fatalf("invalid private key %q: %s", privateKeyPath, err)
161+
}
162+
163+
authKeyLine, err := keys.AuthorisedKeysLine()
164+
if err != nil {
165+
log.Fatalf("failed to generate authorised key line from private key %q, %s", privateKeyPath, err)
166+
}
167+
168+
log.Printf("authorized_controllee_key line: %q", strings.TrimSpace(authKeyLine))
169+
}
170+
149171
userSpecifiedSNI, err := line.GetArgString("sni")
150172
if err == nil {
151173
settings.SNI = userSpecifiedSNI

internal/client/keys/embed.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ package keys
22

33
import (
44
_ "embed"
5+
"fmt"
56
"log"
67

78
"github.com/NHAS/reverse_ssh/internal"
89
"golang.org/x/crypto/ssh"
910
)
1011

1112
//go:embed private_key
12-
var privateKey []byte
13+
var privateKey string
1314

1415
func GetPrivateKey() (ssh.Signer, error) {
15-
sshPriv, err := ssh.ParsePrivateKey(privateKey)
16+
sshPriv, err := ssh.ParsePrivateKey([]byte(privateKey))
1617
if err != nil {
1718
log.Println("Unable to load embedded private key: ", err)
1819
bs, err := internal.GeneratePrivateKey()
@@ -28,3 +29,23 @@ func GetPrivateKey() (ssh.Signer, error) {
2829

2930
return sshPriv, nil
3031
}
32+
33+
func SetPrivateKey(key string) error {
34+
_, err := ssh.ParsePrivateKey([]byte(key))
35+
if err != nil {
36+
return fmt.Errorf("private key invalid: %w", err)
37+
}
38+
39+
privateKey = key
40+
return nil
41+
}
42+
43+
func AuthorisedKeysLine() (string, error) {
44+
priv, err := ssh.ParsePrivateKey([]byte(privateKey))
45+
if err != nil {
46+
return "", fmt.Errorf("private key invalid: %w", err)
47+
}
48+
49+
return string(ssh.MarshalAuthorizedKey(priv.PublicKey())), nil
50+
51+
}

internal/client/proxy_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func createType2Message() []byte {
8383
return challengeMessage
8484
}
8585

86-
func setupTestServer(t *testing.T) *httptest.Server {
86+
func setupTestServer() *httptest.Server {
8787
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
8888
w.WriteHeader(http.StatusOK)
8989
w.Write([]byte("Connected to target"))
@@ -140,7 +140,7 @@ func TestNTLMProxyAuth(t *testing.T) {
140140
},
141141
}
142142

143-
target := setupTestServer(t)
143+
target := setupTestServer()
144144
defer target.Close()
145145

146146
for _, tt := range tests {

0 commit comments

Comments
 (0)