nuclei/pkg/js/libs/ssh/ssh.go

115 lines
3.0 KiB
Go
Raw Normal View History

javascript protocol for scripting (includes 15+ proto libs) (#4109) * rebase js-layer PR from @ice3man543 * package restructuring * working * fix duplicated event & matcher status * fix lint error * fix response field * add new functions * multiple minor improvements * fix incorrect stats in js protocol * sort output metadata in cli * remove temp files * remove dead code * add unit and integration test * fix lint error * add jsdoclint using llm * fix error in test * add js lint using llm * generate docs of libs * llm lint * remove duplicated docs * update generated docs * update prompt in doclint * update docs * temp disable version check test * fix unit test and add retry * fix panic in it * update and move jsdocs * updated jsdocs * update docs * update container platform in test * dir restructure and adding docs * add api_reference and remove markdown docs * fix imports * add javascript design and contribution docs * add js protocol documentation * update integration test and docs * update doc ext mdx->md * minor update to docs * new integration test and more * move go libs and add docs * gen new net docs and more * final docs update * add new devtool * use fastdialer * fix build fail * use fastdialer + network sandbox support * add reserved keyword 'Port' * update Port to new syntax * misc update * always enable templatectx in js protocol * move docs to 'js-proto-docs' repo * remove scrapefuncs binary --------- Co-authored-by: sandeep <8293321+ehsandeep@users.noreply.github.com>
2023-09-16 16:02:17 +05:30
package ssh
import (
"errors"
"fmt"
"strings"
"time"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/protocolstate"
javascript protocol for scripting (includes 15+ proto libs) (#4109) * rebase js-layer PR from @ice3man543 * package restructuring * working * fix duplicated event & matcher status * fix lint error * fix response field * add new functions * multiple minor improvements * fix incorrect stats in js protocol * sort output metadata in cli * remove temp files * remove dead code * add unit and integration test * fix lint error * add jsdoclint using llm * fix error in test * add js lint using llm * generate docs of libs * llm lint * remove duplicated docs * update generated docs * update prompt in doclint * update docs * temp disable version check test * fix unit test and add retry * fix panic in it * update and move jsdocs * updated jsdocs * update docs * update container platform in test * dir restructure and adding docs * add api_reference and remove markdown docs * fix imports * add javascript design and contribution docs * add js protocol documentation * update integration test and docs * update doc ext mdx->md * minor update to docs * new integration test and more * move go libs and add docs * gen new net docs and more * final docs update * add new devtool * use fastdialer * fix build fail * use fastdialer + network sandbox support * add reserved keyword 'Port' * update Port to new syntax * misc update * always enable templatectx in js protocol * move docs to 'js-proto-docs' repo * remove scrapefuncs binary --------- Co-authored-by: sandeep <8293321+ehsandeep@users.noreply.github.com>
2023-09-16 16:02:17 +05:30
"github.com/zmap/zgrab2/lib/ssh"
)
// SSHClient is a client for SSH servers.
//
// Internally client uses github.com/zmap/zgrab2/lib/ssh driver.
type SSHClient struct{}
// Connect tries to connect to provided host and port
// with provided username and password with ssh.
//
// Returns state of connection and error. If error is not nil,
// state will be false
func (c *SSHClient) Connect(host string, port int, username, password string) (bool, error) {
conn, err := connect(host, port, username, password, "")
if err != nil {
return false, err
}
defer conn.Close()
return true, nil
}
// ConnectWithKey tries to connect to provided host and port
// with provided username and private_key.
//
// Returns state of connection and error. If error is not nil,
// state will be false
func (c *SSHClient) ConnectWithKey(host string, port int, username, key string) (bool, error) {
conn, err := connect(host, port, username, "", key)
if err != nil {
return false, err
}
defer conn.Close()
return true, nil
}
// ConnectSSHInfoMode tries to connect to provided host and port
// with provided host and port
//
// Returns HandshakeLog and error. If error is not nil,
// state will be false
//
// HandshakeLog is a struct that contains information about the
// ssh connection
func (c *SSHClient) ConnectSSHInfoMode(host string, port int) (*ssh.HandshakeLog, error) {
return connectSSHInfoMode(host, port)
}
func connectSSHInfoMode(host string, port int) (*ssh.HandshakeLog, error) {
if !protocolstate.IsHostAllowed(host) {
// host is not valid according to network policy
return nil, protocolstate.ErrHostDenied.Msgf(host)
}
data := new(ssh.HandshakeLog)
sshConfig := ssh.MakeSSHConfig()
sshConfig.Timeout = 10 * time.Second
sshConfig.ConnLog = data
sshConfig.DontAuthenticate = true
sshConfig.BannerCallback = func(banner string) error {
data.Banner = strings.TrimSpace(banner)
return nil
}
rhost := fmt.Sprintf("%s:%d", host, port)
client, err := ssh.Dial("tcp", rhost, sshConfig)
if err != nil {
return nil, err
}
defer client.Close()
return data, nil
}
func connect(host string, port int, user, password, privateKey string) (*ssh.Client, error) {
if !protocolstate.IsHostAllowed(host) {
// host is not valid according to network policy
return nil, protocolstate.ErrHostDenied.Msgf(host)
}
if host == "" || port <= 0 {
return nil, errors.New("invalid host or port")
}
conf := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{},
}
if len(password) > 0 {
conf.Auth = append(conf.Auth, ssh.Password(password))
}
if len(privateKey) > 0 {
signer, err := ssh.ParsePrivateKey([]byte(privateKey))
if err != nil {
return nil, err
}
conf.Auth = append(conf.Auth, ssh.PublicKeys(signer))
}
client, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", host, port), conf)
if err != nil {
return nil, err
}
return client, nil
}