2020-12-29 01:17:10 -08:00
|
|
|
package Plugins
|
|
|
|
|
|
|
|
import (
|
2021-05-30 19:03:01 -07:00
|
|
|
"errors"
|
2020-12-29 01:17:10 -08:00
|
|
|
"fmt"
|
|
|
|
"github.com/shadow1ng/fscan/common"
|
|
|
|
"golang.org/x/crypto/ssh"
|
2021-05-30 19:03:01 -07:00
|
|
|
"io/ioutil"
|
2020-12-29 01:17:10 -08:00
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func SshScan(info *common.HostInfo) (tmperr error) {
|
2021-11-24 18:16:39 -08:00
|
|
|
if common.IsBrute {
|
|
|
|
return
|
|
|
|
}
|
2021-03-30 03:12:54 -07:00
|
|
|
starttime := time.Now().Unix()
|
2020-12-29 01:17:10 -08:00
|
|
|
for _, user := range common.Userdict["ssh"] {
|
|
|
|
for _, pass := range common.Passwords {
|
|
|
|
pass = strings.Replace(pass, "{user}", user, -1)
|
|
|
|
flag, err := SshConn(info, user, pass)
|
|
|
|
if flag == true && err == nil {
|
|
|
|
return err
|
|
|
|
} else {
|
2021-04-20 09:13:04 -07:00
|
|
|
errlog := fmt.Sprintf("[-] ssh %v:%v %v %v %v", info.Host, info.Ports, user, pass, err)
|
2021-02-04 22:43:07 -08:00
|
|
|
common.LogError(errlog)
|
2020-12-29 01:17:10 -08:00
|
|
|
tmperr = err
|
2021-03-30 03:12:54 -07:00
|
|
|
if common.CheckErrs(err) {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-03 08:41:39 -07:00
|
|
|
if time.Now().Unix()-starttime > (int64(len(common.Userdict["ssh"])*len(common.Passwords)) * common.Timeout) {
|
2021-03-30 03:12:54 -07:00
|
|
|
return err
|
|
|
|
}
|
2020-12-29 01:17:10 -08:00
|
|
|
}
|
2022-07-03 08:41:39 -07:00
|
|
|
if common.SshKey != "" {
|
2021-05-30 19:03:01 -07:00
|
|
|
return err
|
|
|
|
}
|
2020-12-29 01:17:10 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return tmperr
|
|
|
|
}
|
|
|
|
|
|
|
|
func SshConn(info *common.HostInfo, user string, pass string) (flag bool, err error) {
|
|
|
|
flag = false
|
2021-04-20 09:13:04 -07:00
|
|
|
Host, Port, Username, Password := info.Host, info.Ports, user, pass
|
2021-05-30 19:03:01 -07:00
|
|
|
Auth := []ssh.AuthMethod{}
|
2022-07-03 08:41:39 -07:00
|
|
|
if common.SshKey != "" {
|
|
|
|
pemBytes, err := ioutil.ReadFile(common.SshKey)
|
2021-05-30 19:03:01 -07:00
|
|
|
if err != nil {
|
|
|
|
return false, errors.New("read key failed" + err.Error())
|
|
|
|
}
|
|
|
|
signer, err := ssh.ParsePrivateKey(pemBytes)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.New("parse key failed" + err.Error())
|
|
|
|
}
|
|
|
|
Auth = []ssh.AuthMethod{ssh.PublicKeys(signer)}
|
|
|
|
} else {
|
|
|
|
Auth = []ssh.AuthMethod{ssh.Password(Password)}
|
|
|
|
}
|
|
|
|
|
2020-12-29 01:17:10 -08:00
|
|
|
config := &ssh.ClientConfig{
|
2021-05-30 19:03:01 -07:00
|
|
|
User: Username,
|
|
|
|
Auth: Auth,
|
2022-07-03 08:41:39 -07:00
|
|
|
Timeout: time.Duration(common.Timeout) * time.Second,
|
2020-12-29 01:17:10 -08:00
|
|
|
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := ssh.Dial("tcp", fmt.Sprintf("%v:%v", Host, Port), config)
|
|
|
|
if err == nil {
|
|
|
|
defer client.Close()
|
|
|
|
session, err := client.NewSession()
|
|
|
|
if err == nil {
|
|
|
|
defer session.Close()
|
|
|
|
flag = true
|
2021-05-30 19:03:01 -07:00
|
|
|
var result string
|
2022-07-03 08:41:39 -07:00
|
|
|
if common.Command != "" {
|
|
|
|
combo, _ := session.CombinedOutput(common.Command)
|
2021-05-30 19:03:01 -07:00
|
|
|
result = fmt.Sprintf("[+] SSH:%v:%v:%v %v \n %v", Host, Port, Username, Password, string(combo))
|
2022-07-03 08:41:39 -07:00
|
|
|
if common.SshKey != "" {
|
2021-05-30 19:03:01 -07:00
|
|
|
result = fmt.Sprintf("[+] SSH:%v:%v sshkey correct \n %v", Host, Port, string(combo))
|
|
|
|
}
|
2020-12-29 01:17:10 -08:00
|
|
|
common.LogSuccess(result)
|
|
|
|
} else {
|
2021-05-30 19:03:01 -07:00
|
|
|
result = fmt.Sprintf("[+] SSH:%v:%v:%v %v", Host, Port, Username, Password)
|
2022-07-03 08:41:39 -07:00
|
|
|
if common.SshKey != "" {
|
2021-05-30 19:03:01 -07:00
|
|
|
result = fmt.Sprintf("[+] SSH:%v:%v sshkey correct", Host, Port)
|
|
|
|
}
|
2020-12-29 01:17:10 -08:00
|
|
|
common.LogSuccess(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return flag, err
|
|
|
|
|
|
|
|
}
|