2020-12-29 17:17:10 +08:00
|
|
|
|
package Plugins
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2024-12-18 21:56:08 +08:00
|
|
|
|
"github.com/shadow1ng/fscan/Config"
|
2023-11-13 11:27:01 +08:00
|
|
|
|
"github.com/shadow1ng/fscan/common"
|
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
|
"io/ioutil"
|
2020-12-29 17:17:10 +08:00
|
|
|
|
"net"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2024-12-18 15:19:53 +08:00
|
|
|
|
// SshScan 执行SSH服务的认证扫描
|
2024-12-18 21:56:08 +08:00
|
|
|
|
func SshScan(info *Config.HostInfo) (tmperr error) {
|
2023-11-13 11:27:01 +08:00
|
|
|
|
if common.IsBrute {
|
2021-11-25 10:16:39 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
2024-12-18 15:19:53 +08:00
|
|
|
|
|
|
|
|
|
startTime := time.Now().Unix()
|
|
|
|
|
|
|
|
|
|
// 遍历用户名和密码字典进行认证尝试
|
2020-12-29 17:17:10 +08:00
|
|
|
|
for _, user := range common.Userdict["ssh"] {
|
|
|
|
|
for _, pass := range common.Passwords {
|
2024-12-18 15:19:53 +08:00
|
|
|
|
// 替换密码中的用户名占位符
|
2020-12-29 17:17:10 +08:00
|
|
|
|
pass = strings.Replace(pass, "{user}", user, -1)
|
2024-12-18 15:19:53 +08:00
|
|
|
|
|
|
|
|
|
success, err := SshConn(info, user, pass)
|
|
|
|
|
if success && err == nil {
|
2020-12-29 17:17:10 +08:00
|
|
|
|
return err
|
|
|
|
|
}
|
2024-12-18 15:19:53 +08:00
|
|
|
|
|
|
|
|
|
// 记录失败信息
|
|
|
|
|
errlog := fmt.Sprintf("[x] SSH认证失败 %v:%v User:%v Pass:%v Err:%v",
|
|
|
|
|
info.Host, info.Ports, user, pass, err)
|
|
|
|
|
common.LogError(errlog)
|
|
|
|
|
tmperr = err
|
|
|
|
|
|
|
|
|
|
// 检查是否需要中断扫描
|
|
|
|
|
if common.CheckErrs(err) {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查是否超时
|
|
|
|
|
timeoutLimit := int64(len(common.Userdict["ssh"])*len(common.Passwords)) * common.Timeout
|
|
|
|
|
if time.Now().Unix()-startTime > timeoutLimit {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果指定了SSH密钥,则不进行密码尝试
|
2023-11-13 11:27:01 +08:00
|
|
|
|
if common.SshKey != "" {
|
2021-05-31 10:03:01 +08:00
|
|
|
|
return err
|
|
|
|
|
}
|
2020-12-29 17:17:10 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return tmperr
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-18 15:19:53 +08:00
|
|
|
|
// SshConn 尝试建立SSH连接并进行认证
|
2024-12-18 21:56:08 +08:00
|
|
|
|
func SshConn(info *Config.HostInfo, user string, pass string) (flag bool, err error) {
|
2024-12-18 15:19:53 +08:00
|
|
|
|
// 准备认证方法
|
|
|
|
|
var auth []ssh.AuthMethod
|
2023-11-13 11:27:01 +08:00
|
|
|
|
if common.SshKey != "" {
|
2024-12-18 15:19:53 +08:00
|
|
|
|
// 使用SSH密钥认证
|
2023-11-13 11:27:01 +08:00
|
|
|
|
pemBytes, err := ioutil.ReadFile(common.SshKey)
|
2021-05-31 10:03:01 +08:00
|
|
|
|
if err != nil {
|
2024-12-18 15:19:53 +08:00
|
|
|
|
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 {
|
2024-12-18 15:19:53 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2024-12-18 15:19:53 +08:00
|
|
|
|
// 配置SSH客户端
|
2020-12-29 17:17:10 +08:00
|
|
|
|
config := &ssh.ClientConfig{
|
2024-12-18 15:19:53 +08:00
|
|
|
|
User: user,
|
|
|
|
|
Auth: auth,
|
2023-11-13 11:27:01 +08:00
|
|
|
|
Timeout: time.Duration(common.Timeout) * time.Second,
|
2020-12-29 17:17:10 +08:00
|
|
|
|
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-18 15:19:53 +08:00
|
|
|
|
// 建立SSH连接
|
|
|
|
|
client, err := ssh.Dial("tcp", fmt.Sprintf("%v:%v", info.Host, info.Ports), config)
|
2020-12-29 17:17:10 +08:00
|
|
|
|
if err == nil {
|
|
|
|
|
defer client.Close()
|
|
|
|
|
session, err := client.NewSession()
|
|
|
|
|
if err == nil {
|
|
|
|
|
defer session.Close()
|
|
|
|
|
flag = true
|
2024-12-18 15:19:53 +08:00
|
|
|
|
|
|
|
|
|
// 处理认证成功的情况
|
2023-11-13 11:27:01 +08:00
|
|
|
|
if common.Command != "" {
|
2024-12-18 15:19:53 +08:00
|
|
|
|
// 执行指定命令
|
|
|
|
|
output, _ := session.CombinedOutput(common.Command)
|
2023-11-13 11:27:01 +08:00
|
|
|
|
if common.SshKey != "" {
|
2024-12-18 15:19:53 +08:00
|
|
|
|
common.LogSuccess(fmt.Sprintf("[✓] SSH密钥认证成功 %v:%v\n命令输出:\n%v",
|
|
|
|
|
info.Host, info.Ports, string(output)))
|
|
|
|
|
} else {
|
|
|
|
|
common.LogSuccess(fmt.Sprintf("[✓] SSH认证成功 %v:%v User:%v Pass:%v\n命令输出:\n%v",
|
|
|
|
|
info.Host, info.Ports, user, pass, string(output)))
|
2021-05-31 10:03:01 +08:00
|
|
|
|
}
|
2020-12-29 17:17:10 +08:00
|
|
|
|
} else {
|
2024-12-18 15:19:53 +08:00
|
|
|
|
// 仅记录认证成功
|
2023-11-13 11:27:01 +08:00
|
|
|
|
if common.SshKey != "" {
|
2024-12-18 15:19:53 +08:00
|
|
|
|
common.LogSuccess(fmt.Sprintf("[✓] SSH密钥认证成功 %v:%v",
|
|
|
|
|
info.Host, info.Ports))
|
|
|
|
|
} else {
|
|
|
|
|
common.LogSuccess(fmt.Sprintf("[✓] SSH认证成功 %v:%v User:%v Pass:%v",
|
|
|
|
|
info.Host, info.Ports, user, pass))
|
2021-05-31 10:03:01 +08:00
|
|
|
|
}
|
2020-12-29 17:17:10 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return flag, err
|
|
|
|
|
}
|