breach/breacher/ssh.go

95 lines
2.0 KiB
Go

package breacher
import (
"log"
"net"
"strconv"
"strings"
"github.com/spf13/cobra"
)
var (
sPasswd string
sKey string
)
var sshCmd = &cobra.Command{
Use: "tunnel [from address] [to address] [user@host:port]",
Short: "ssh tunneling to access remote services",
Long: `ssh tunneling to access remote services
ex.
breacher forward :8080 kumoly.io:5080
breacher forward :8080 :8000
breacher forward --udp :8080 192.168.51.211:53
`,
Args: cobra.ExactArgs(3),
Run: func(cmd *cobra.Command, args []string) {
localHost, localPortStr, err := net.SplitHostPort(args[0])
if err != nil {
log.Fatalln(err)
}
remoteHost, remotePortStr, err := net.SplitHostPort(args[1])
if err != nil {
log.Fatalln(err)
}
localPort, err := strconv.Atoi(localPortStr)
if err != nil {
log.Fatalln(err)
}
remotePort, err := strconv.Atoi(remotePortStr)
if err != nil {
log.Fatalln(err)
}
if localHost == "" {
localHost = "localhost"
}
if remoteHost == "" {
remoteHost = "localhost"
}
st := NewSSHTunnel(localHost, localPort, remoteHost, remotePort)
split := strings.Split(args[2], "@")
if len(split) != 2 {
log.Fatalln("ssh host name not valid")
}
usr := split[0]
sshHost := "localhost"
sshPort := 22
if !strings.Contains(split[1], ":") {
sshHost = split[1]
} else {
sshPortStr := ""
sshHost, sshPortStr, err = net.SplitHostPort(split[1])
if err != nil {
log.Fatalln(err)
}
if sshHost == "" {
log.Fatalln("no ssh host")
}
if sshPortStr != "" {
sshPort, err = strconv.Atoi(sshPortStr)
if err != nil {
log.Fatalln(err)
}
}
}
st.server.Host = sshHost
st.SetPort(sshPort)
st.SetUser(usr)
st.SetPassword("ubuntu")
st.SetDebug(true)
st.SetConnState(func(tun *SSHTun, state ConnState) {
switch state {
case StateStarting:
log.Printf("STATE is Starting")
case StateStarted:
log.Printf("STATE is Started")
case StateStopped:
log.Printf("STATE is Stopped")
}
})
log.Fatalln(st.Start())
},
}