fscan/Plugins/SSH.go

178 lines
3.8 KiB
Go
Raw Normal View History

2020-12-29 17:17:10 +08:00
package Plugins
import (
"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"
2024-12-20 20:57:27 +08:00
"sync"
2020-12-29 17:17:10 +08:00
"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-21 02:00:16 +08:00
threads := Common.BruteThreads // 使用 BruteThreads 来控制线程数
2024-12-20 20:57:27 +08:00
taskChan := make(chan struct {
user string
pass string
}, len(Common.Userdict["ssh"])*len(Common.Passwords))
// 创建结果通道
resultChan := make(chan error, threads)
// 生成所有任务
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-20 20:57:27 +08:00
taskChan <- struct {
user string
pass string
}{user, pass}
}
}
close(taskChan)
2024-12-18 15:19:53 +08:00
2024-12-20 20:57:27 +08:00
// 启动工作线程
var wg sync.WaitGroup
for i := 0; i < threads; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for task := range taskChan {
// 为每个任务创建结果通道
done := make(chan struct {
success bool
err error
})
2024-12-18 15:19:53 +08:00
2024-12-20 20:57:27 +08:00
// 执行SSH连接
go func(user, pass string) {
success, err := SshConn(info, user, pass)
done <- struct {
success bool
err error
}{success, err}
}(task.user, task.pass)
// 等待结果或超时
var err error
select {
case result := <-done:
err = result.err
if result.success {
resultChan <- nil
return
}
case <-time.After(time.Duration(Common.Timeout) * time.Second):
err = fmt.Errorf("连接超时")
2024-12-20 20:44:59 +08:00
}
2024-12-18 15:19:53 +08:00
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, task.user, task.pass, err)
Common.LogError(errlog)
if Common.CheckErrs(err) {
resultChan <- err
return
}
}
if Common.SshKeyPath != "" {
resultChan <- err
return
}
2024-12-18 15:19:53 +08:00
}
2024-12-20 20:57:27 +08:00
resultChan <- nil
}()
}
2024-12-18 15:19:53 +08:00
2024-12-20 20:57:27 +08:00
// 等待所有线程完成
go func() {
wg.Wait()
close(resultChan)
}()
// 检查结果
for err := range resultChan {
if err != nil {
tmperr = err
if Common.CheckErrs(err) {
2021-05-31 10:03:01 +08:00
return err
}
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()
flag = true
if Common.Command != "" {
2024-12-20 20:44:59 +08:00
output, err := session.CombinedOutput(Common.Command)
if err != nil {
return true, err
}
if Common.SshKeyPath != "" {
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)))
2020-12-29 17:17:10 +08:00
}
} else {
2024-12-20 03:46:09 +08:00
if Common.SshKeyPath != "" {
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))
}
2020-12-29 17:17:10 +08:00
}
return flag, nil
2020-12-29 17:17:10 +08:00
}