fscan/Plugins/SSH.go

136 lines
2.9 KiB
Go
Raw Normal View History

2020-12-29 17:17:10 +08:00
package Plugins
import (
"context"
2020-12-29 17:17:10 +08:00
"fmt"
2024-12-18 22:00:18 +08:00
"github.com/shadow1ng/fscan/Common"
2023-11-13 11:27:01 +08:00
"golang.org/x/crypto/ssh"
"io/ioutil"
2020-12-29 17:17:10 +08:00
"net"
"strings"
"time"
)
2024-12-19 16:15:53 +08:00
func SshScan(info *Common.HostInfo) (tmperr error) {
2024-12-20 03:46:09 +08:00
if Common.DisableBrute {
2021-11-25 10:16:39 +08:00
return
}
2024-12-18 15:19:53 +08:00
2024-12-31 20:25:54 +08:00
maxRetries := Common.MaxRetries
2024-12-20 20:57:27 +08:00
// 遍历所有用户名密码组合
2024-12-18 22:00:18 +08:00
for _, user := range Common.Userdict["ssh"] {
for _, pass := range Common.Passwords {
2020-12-29 17:17:10 +08:00
pass = strings.Replace(pass, "{user}", user, -1)
2024-12-31 20:25:54 +08:00
// 重试循环
for retryCount := 0; retryCount < maxRetries; retryCount++ {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(Common.Timeout)*time.Second)
done := make(chan struct {
success bool
err error
}, 1)
2024-12-31 20:25:54 +08:00
go func(user, pass string) {
success, err := SshConn(info, user, pass)
2024-12-31 20:25:54 +08:00
select {
case <-ctx.Done():
case done <- struct {
success bool
err error
}{success, err}:
2024-12-20 20:57:27 +08:00
}
}(user, pass)
var err error
select {
case result := <-done:
err = result.err
if result.success {
successLog := fmt.Sprintf("SSH认证成功 %v:%v User:%v Pass:%v",
info.Host, info.Ports, user, pass)
Common.LogSuccess(successLog)
time.Sleep(100 * time.Millisecond)
cancel()
return nil
2024-12-31 20:25:54 +08:00
}
case <-ctx.Done():
err = fmt.Errorf("连接超时")
}
2024-12-20 20:57:27 +08:00
cancel()
2024-12-20 20:57:27 +08:00
if err != nil {
errlog := fmt.Sprintf("SSH认证失败 %v:%v User:%v Pass:%v Err:%v",
info.Host, info.Ports, user, pass, err)
Common.LogError(errlog)
if retryErr := Common.CheckErrs(err); retryErr != nil {
if retryCount == maxRetries-1 {
return err
}
continue
}
2024-12-20 20:57:27 +08:00
}
2024-12-18 15:19:53 +08:00
if Common.SshKeyPath != "" {
return err
}
2024-12-20 20:57:27 +08:00
break
2021-05-31 10:03:01 +08:00
}
2020-12-29 17:17:10 +08:00
}
}
2024-12-20 20:57:27 +08:00
2020-12-29 17:17:10 +08:00
return tmperr
}
2024-12-20 20:44:59 +08:00
func SshConn(info *Common.HostInfo, user string, pass string) (flag bool, err error) {
2024-12-18 15:19:53 +08:00
var auth []ssh.AuthMethod
2024-12-20 03:46:09 +08:00
if Common.SshKeyPath != "" {
pemBytes, err := ioutil.ReadFile(Common.SshKeyPath)
2021-05-31 10:03:01 +08:00
if err != nil {
return false, fmt.Errorf("读取密钥失败: %v", err)
2021-05-31 10:03:01 +08:00
}
2024-12-18 15:19:53 +08:00
2021-05-31 10:03:01 +08:00
signer, err := ssh.ParsePrivateKey(pemBytes)
if err != nil {
return false, fmt.Errorf("解析密钥失败: %v", err)
2021-05-31 10:03:01 +08:00
}
2024-12-18 15:19:53 +08:00
auth = []ssh.AuthMethod{ssh.PublicKeys(signer)}
2021-05-31 10:03:01 +08:00
} else {
2024-12-18 15:19:53 +08:00
auth = []ssh.AuthMethod{ssh.Password(pass)}
2021-05-31 10:03:01 +08:00
}
2020-12-29 17:17:10 +08:00
config := &ssh.ClientConfig{
User: user,
Auth: auth,
2020-12-29 17:17:10 +08:00
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
2024-12-20 20:57:27 +08:00
Timeout: time.Duration(Common.Timeout) * time.Millisecond,
2020-12-29 17:17:10 +08:00
}
2024-12-20 20:44:59 +08:00
client, err := ssh.Dial("tcp", fmt.Sprintf("%v:%v", info.Host, info.Ports), config)
if err != nil {
return false, err
}
defer client.Close()
session, err := client.NewSession()
if err != nil {
return false, err
}
defer session.Close()
// 如果需要执行命令
if Common.Command != "" {
_, err := session.CombinedOutput(Common.Command)
2024-12-20 20:44:59 +08:00
if err != nil {
return true, err
}
2020-12-29 17:17:10 +08:00
}
return true, nil
2020-12-29 17:17:10 +08:00
}