fscan/Plugins/SSH.go

183 lines
4.3 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
2025-01-14 23:38:58 +08:00
target := fmt.Sprintf("%v:%v", info.Host, info.Ports)
2024-12-20 20:57:27 +08:00
2025-01-14 23:38:58 +08:00
Common.LogDebug(fmt.Sprintf("开始扫描 %s", target))
totalUsers := len(Common.Userdict["ssh"])
totalPass := len(Common.Passwords)
Common.LogDebug(fmt.Sprintf("开始尝试用户名密码组合 (总用户数: %d, 总密码数: %d)", totalUsers, totalPass))
tried := 0
total := totalUsers * totalPass
// 遍历所有用户名密码组合
2024-12-18 22:00:18 +08:00
for _, user := range Common.Userdict["ssh"] {
for _, pass := range Common.Passwords {
tried++
2020-12-29 17:17:10 +08:00
pass = strings.Replace(pass, "{user}", user, -1)
Common.LogDebug(fmt.Sprintf("[%d/%d] 尝试: %s:%s", tried, total, user, pass))
2024-12-31 20:25:54 +08:00
// 重试循环
for retryCount := 0; retryCount < maxRetries; retryCount++ {
if retryCount > 0 {
Common.LogDebug(fmt.Sprintf("第%d次重试: %s:%s", retryCount+1, user, pass))
}
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 {
2025-01-14 23:38:58 +08:00
successMsg := fmt.Sprintf("SSH认证成功 %s User:%v Pass:%v", target, user, pass)
Common.LogSuccess(successMsg)
// 保存结果
details := map[string]interface{}{
"port": info.Ports,
"service": "ssh",
"username": user,
"password": pass,
"type": "weak-password",
}
// 如果使用了密钥认证,添加密钥信息
if Common.SshKeyPath != "" {
details["auth_type"] = "key"
details["key_path"] = Common.SshKeyPath
details["password"] = nil
}
// 如果执行了命令,添加命令信息
if Common.Command != "" {
details["command"] = Common.Command
}
vulnResult := &Common.ScanResult{
Time: time.Now(),
Type: Common.VULN,
Target: info.Host,
Status: "vulnerable",
Details: details,
}
Common.SaveResult(vulnResult)
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 {
2025-01-14 23:38:58 +08:00
errMsg := fmt.Sprintf("SSH认证失败 %s User:%v Pass:%v Err:%v",
target, user, pass, err)
Common.LogError(errMsg)
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 != "" {
Common.LogDebug("检测到SSH密钥路径停止密码尝试")
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
Common.LogDebug(fmt.Sprintf("扫描完成,共尝试 %d 个组合", tried))
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
}