mirror of
https://github.com/shadow1ng/fscan.git
synced 2025-09-14 14:06:44 +08:00
fix: SSH连接超时问题
This commit is contained in:
parent
e7d9354284
commit
1313916081
@ -18,7 +18,6 @@ var (
|
|||||||
|
|
||||||
// RedisScan 执行Redis服务扫描
|
// RedisScan 执行Redis服务扫描
|
||||||
func RedisScan(info *Common.HostInfo) (tmperr error) {
|
func RedisScan(info *Common.HostInfo) (tmperr error) {
|
||||||
fmt.Println("[+] Redis扫描模块开始...")
|
|
||||||
starttime := time.Now().Unix()
|
starttime := time.Now().Unix()
|
||||||
|
|
||||||
// 尝试无密码连接
|
// 尝试无密码连接
|
||||||
|
158
Plugins/SSH.go
158
Plugins/SSH.go
@ -1,7 +1,6 @@
|
|||||||
package Plugins
|
package Plugins
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/shadow1ng/fscan/Common"
|
"github.com/shadow1ng/fscan/Common"
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
@ -16,83 +15,35 @@ func SshScan(info *Common.HostInfo) (tmperr error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 增加全局扫描超时
|
|
||||||
scanCtx, scanCancel := context.WithTimeout(context.Background(), time.Duration(Common.Timeout*2)*time.Second)
|
|
||||||
defer scanCancel()
|
|
||||||
|
|
||||||
for _, user := range Common.Userdict["ssh"] {
|
for _, user := range Common.Userdict["ssh"] {
|
||||||
for _, pass := range Common.Passwords {
|
for _, pass := range Common.Passwords {
|
||||||
// 使用全局 context 创建子 context
|
|
||||||
ctx, cancel := context.WithTimeout(scanCtx, time.Duration(Common.Timeout)*time.Second)
|
|
||||||
|
|
||||||
// 替换密码中的用户名占位符
|
|
||||||
pass = strings.Replace(pass, "{user}", user, -1)
|
pass = strings.Replace(pass, "{user}", user, -1)
|
||||||
currentUser := user
|
success, err := SshConn(info, user, pass)
|
||||||
currentPass := pass
|
|
||||||
|
|
||||||
// 创建结果通道
|
|
||||||
done := make(chan struct {
|
|
||||||
success bool
|
|
||||||
err error
|
|
||||||
}, 1)
|
|
||||||
|
|
||||||
// 在 goroutine 中执行单次连接尝试
|
|
||||||
go func() {
|
|
||||||
success, err := SshConn(ctx, info, currentUser, currentPass)
|
|
||||||
select {
|
|
||||||
case done <- struct {
|
|
||||||
success bool
|
|
||||||
err error
|
|
||||||
}{success, err}:
|
|
||||||
case <-ctx.Done():
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// 等待连接结果或超时
|
|
||||||
var err error
|
|
||||||
select {
|
|
||||||
case result := <-done:
|
|
||||||
err = result.err
|
|
||||||
if result.success {
|
|
||||||
cancel()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
case <-ctx.Done():
|
|
||||||
err = fmt.Errorf("[-] 连接超时: %v", ctx.Err())
|
|
||||||
}
|
|
||||||
|
|
||||||
cancel()
|
|
||||||
|
|
||||||
// 记录失败信息
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errlog := fmt.Sprintf("[-] SSH认证失败 %v:%v User:%v Pass:%v Err:%v",
|
errlog := fmt.Sprintf("[-] SSH认证失败 %v:%v User:%v Pass:%v Err:%v",
|
||||||
info.Host, info.Ports, currentUser, currentPass, err)
|
info.Host, info.Ports, user, pass, err)
|
||||||
Common.LogError(errlog)
|
Common.LogError(errlog)
|
||||||
tmperr = err
|
tmperr = err
|
||||||
|
|
||||||
|
if Common.CheckErrs(err) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否需要中断扫描
|
if success {
|
||||||
if Common.CheckErrs(err) {
|
return nil
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查全局超时
|
|
||||||
if scanCtx.Err() != nil {
|
|
||||||
return fmt.Errorf("扫描总时间超时: %v", scanCtx.Err())
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果指定了SSH密钥,则不进行密码尝试
|
|
||||||
if Common.SshKeyPath != "" {
|
if Common.SshKeyPath != "" {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return tmperr
|
return tmperr
|
||||||
}
|
}
|
||||||
|
|
||||||
func SshConn(ctx context.Context, info *Common.HostInfo, user string, pass string) (flag bool, err error) {
|
func SshConn(info *Common.HostInfo, user string, pass string) (flag bool, err error) {
|
||||||
// 准备认证方法
|
|
||||||
var auth []ssh.AuthMethod
|
var auth []ssh.AuthMethod
|
||||||
if Common.SshKeyPath != "" {
|
if Common.SshKeyPath != "" {
|
||||||
pemBytes, err := ioutil.ReadFile(Common.SshKeyPath)
|
pemBytes, err := ioutil.ReadFile(Common.SshKeyPath)
|
||||||
@ -115,61 +66,15 @@ func SshConn(ctx context.Context, info *Common.HostInfo, user string, pass strin
|
|||||||
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
|
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Timeout: time.Duration(Common.Timeout) * time.Second,
|
Timeout: time.Duration(Common.Timeout),
|
||||||
}
|
}
|
||||||
|
|
||||||
// 使用带超时的 Dial
|
client, err := ssh.Dial("tcp", fmt.Sprintf("%v:%v", info.Host, info.Ports), config)
|
||||||
conn, err := (&net.Dialer{Timeout: time.Duration(Common.Timeout) * time.Second}).DialContext(ctx, "tcp", fmt.Sprintf("%v:%v", info.Host, info.Ports))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
// 设置连接超时
|
|
||||||
if deadline, ok := ctx.Deadline(); ok {
|
|
||||||
conn.SetDeadline(deadline)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建一个新的 context 用于 SSH 握手
|
|
||||||
sshCtx, sshCancel := context.WithTimeout(ctx, time.Duration(Common.Timeout)*time.Second)
|
|
||||||
defer sshCancel()
|
|
||||||
|
|
||||||
// 使用 channel 来控制 SSH 握手的超时
|
|
||||||
sshDone := make(chan struct {
|
|
||||||
client *ssh.Client
|
|
||||||
err error
|
|
||||||
}, 1)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
sshConn, chans, reqs, err := ssh.NewClientConn(conn, fmt.Sprintf("%v:%v", info.Host, info.Ports), config)
|
|
||||||
if err != nil {
|
|
||||||
sshDone <- struct {
|
|
||||||
client *ssh.Client
|
|
||||||
err error
|
|
||||||
}{nil, err}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
client := ssh.NewClient(sshConn, chans, reqs)
|
|
||||||
sshDone <- struct {
|
|
||||||
client *ssh.Client
|
|
||||||
err error
|
|
||||||
}{client, nil}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// 等待 SSH 握手完成或超时
|
|
||||||
var client *ssh.Client
|
|
||||||
select {
|
|
||||||
case result := <-sshDone:
|
|
||||||
if result.err != nil {
|
|
||||||
return false, result.err
|
|
||||||
}
|
|
||||||
client = result.client
|
|
||||||
case <-sshCtx.Done():
|
|
||||||
return false, fmt.Errorf("SSH握手超时: %v", sshCtx.Err())
|
|
||||||
}
|
|
||||||
defer client.Close()
|
defer client.Close()
|
||||||
|
|
||||||
// 创建会话
|
|
||||||
session, err := client.NewSession()
|
session, err := client.NewSession()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
@ -179,37 +84,16 @@ func SshConn(ctx context.Context, info *Common.HostInfo, user string, pass strin
|
|||||||
flag = true
|
flag = true
|
||||||
|
|
||||||
if Common.Command != "" {
|
if Common.Command != "" {
|
||||||
// 执行命令的通道
|
output, err := session.CombinedOutput(Common.Command)
|
||||||
cmdDone := make(chan struct {
|
if err != nil {
|
||||||
output []byte
|
return true, err
|
||||||
err error
|
}
|
||||||
}, 1)
|
if Common.SshKeyPath != "" {
|
||||||
|
Common.LogSuccess(fmt.Sprintf("[+] SSH密钥认证成功 %v:%v\n命令输出:\n%v",
|
||||||
go func() {
|
info.Host, info.Ports, string(output)))
|
||||||
output, err := session.CombinedOutput(Common.Command)
|
} else {
|
||||||
select {
|
Common.LogSuccess(fmt.Sprintf("[+] SSH认证成功 %v:%v User:%v Pass:%v\n命令输出:\n%v",
|
||||||
case cmdDone <- struct {
|
info.Host, info.Ports, user, pass, string(output)))
|
||||||
output []byte
|
|
||||||
err error
|
|
||||||
}{output, err}:
|
|
||||||
case <-ctx.Done():
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return true, fmt.Errorf("命令执行超时: %v", ctx.Err())
|
|
||||||
case result := <-cmdDone:
|
|
||||||
if result.err != nil {
|
|
||||||
return true, result.err
|
|
||||||
}
|
|
||||||
if Common.SshKeyPath != "" {
|
|
||||||
Common.LogSuccess(fmt.Sprintf("[+] SSH密钥认证成功 %v:%v\n命令输出:\n%v",
|
|
||||||
info.Host, info.Ports, string(result.output)))
|
|
||||||
} else {
|
|
||||||
Common.LogSuccess(fmt.Sprintf("[+] SSH认证成功 %v:%v User:%v Pass:%v\n命令输出:\n%v",
|
|
||||||
info.Host, info.Ports, user, pass, string(result.output)))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if Common.SshKeyPath != "" {
|
if Common.SshKeyPath != "" {
|
||||||
|
Loading…
Reference in New Issue
Block a user