generating key automatically on the agent side. Should be done later at the rendez-vous server since there will be many agents running on different servers
This commit is contained in:
parent
e90c3ed57a
commit
c55af94857
@ -3,6 +3,10 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"cidebug/pkg/iowrappers"
|
"cidebug/pkg/iowrappers"
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/rsa"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/pem"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"io"
|
"io"
|
||||||
@ -50,7 +54,7 @@ func setWinsize(f *os.File, w, h int) {
|
|||||||
uintptr(unsafe.Pointer(&struct{ h, w, x, y uint16 }{uint16(h), uint16(w), 0, 0})))
|
uintptr(unsafe.Pointer(&struct{ h, w, x, y uint16 }{uint16(h), uint16(w), 0, 0})))
|
||||||
}
|
}
|
||||||
|
|
||||||
func sshServer() *ssh.Server {
|
func sshServer(hostKeyFile string) *ssh.Server {
|
||||||
ssh.Handle(func(s ssh.Session) {
|
ssh.Handle(func(s ssh.Session) {
|
||||||
cmd := exec.Command("bash")
|
cmd := exec.Command("bash")
|
||||||
ptyReq, winCh, isPty := s.Pty()
|
ptyReq, winCh, isPty := s.Pty()
|
||||||
@ -78,12 +82,18 @@ func sshServer() *ssh.Server {
|
|||||||
|
|
||||||
log.Println("starting ssh server")
|
log.Println("starting ssh server")
|
||||||
server := ssh.Server{
|
server := ssh.Server{
|
||||||
//Addr: ":2222",
|
|
||||||
PasswordHandler: passwordAuth,
|
PasswordHandler: passwordAuth,
|
||||||
SubsystemHandlers: map[string]ssh.SubsystemHandler{
|
SubsystemHandlers: map[string]ssh.SubsystemHandler{
|
||||||
"sftp": SftpHandler,
|
"sftp": SftpHandler,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
err := generateHostKey(hostKeyFile, 2048)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Could not create host key file '%s': %v", hostKeyFile, err)
|
||||||
|
}
|
||||||
|
option := ssh.HostKeyFile(hostKeyFile)
|
||||||
|
option(&server)
|
||||||
|
|
||||||
return &server
|
return &server
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,10 +126,41 @@ func (server ConnectionServer) Run(listener net.Listener) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
go echoServer(conn)
|
go server(conn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ReaderFunc func(p []byte) (n int, err error)
|
||||||
|
|
||||||
|
func (f ReaderFunc) Read(p []byte) (n int, err error) {
|
||||||
|
return f(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateHostKey(filename string, bitSize int) error {
|
||||||
|
if _, err := os.Stat(filename); !os.IsNotExist(err) {
|
||||||
|
log.Printf("Reusing key file '%s'", filename)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
privateKey, err := rsa.GenerateKey(rand.Reader, bitSize)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
privateKeyPEM := &pem.Block{
|
||||||
|
Type: "RSA PRIVATE KEY",
|
||||||
|
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
|
||||||
|
}
|
||||||
|
|
||||||
|
privateKeyFile, err := os.Create(filename)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer privateKeyFile.Close()
|
||||||
|
|
||||||
|
log.Printf("Generating key '%s'", filename)
|
||||||
|
return pem.Encode(privateKeyFile, privateKeyPEM)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
wsURL := os.Args[1]
|
wsURL := os.Args[1]
|
||||||
conn, _, err := websocket.DefaultDialer.Dial(wsURL, nil)
|
conn, _, err := websocket.DefaultDialer.Dial(wsURL, nil)
|
||||||
@ -138,7 +179,9 @@ func main() {
|
|||||||
log.Println("Connection established to rendez-vous server, waiting for debug sessions")
|
log.Println("Connection established to rendez-vous server, waiting for debug sessions")
|
||||||
|
|
||||||
var service AgentService
|
var service AgentService
|
||||||
service = ListenerServer(sshServer)
|
service = ListenerServer(func() *ssh.Server {
|
||||||
|
return sshServer("hostkey.pem")
|
||||||
|
})
|
||||||
//service = ConnectionServer(echoServer)
|
//service = ConnectionServer(echoServer)
|
||||||
//service := ConnectionServer(netCatServer)
|
//service := ConnectionServer(netCatServer)
|
||||||
service.Run(listener)
|
service.Run(listener)
|
||||||
|
Loading…
Reference in New Issue
Block a user