mirror of
https://github.com/shadow1ng/fscan.git
synced 2025-07-13 12:52:44 +08:00
fix: 修复#443
This commit is contained in:
parent
2c4e1d9c28
commit
e962b9171b
303
Plugins/SSH.go
303
Plugins/SSH.go
@ -8,131 +8,196 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SshScan(info *Common.HostInfo) (tmperr error) {
|
// SshCredential 表示一个SSH凭据
|
||||||
|
type SshCredential struct {
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
}
|
||||||
|
|
||||||
|
// SshScanResult 表示SSH扫描结果
|
||||||
|
type SshScanResult struct {
|
||||||
|
Success bool
|
||||||
|
Error error
|
||||||
|
Credential SshCredential
|
||||||
|
}
|
||||||
|
|
||||||
|
// SshScan 扫描SSH服务弱密码
|
||||||
|
func SshScan(info *Common.HostInfo) error {
|
||||||
if Common.DisableBrute {
|
if Common.DisableBrute {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
maxRetries := Common.MaxRetries
|
|
||||||
target := fmt.Sprintf("%v:%v", info.Host, info.Ports)
|
target := fmt.Sprintf("%v:%v", info.Host, info.Ports)
|
||||||
|
|
||||||
Common.LogDebug(fmt.Sprintf("开始扫描 %s", target))
|
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
|
credentials := generateCredentials(Common.Userdict["ssh"], Common.Passwords)
|
||||||
|
Common.LogDebug(fmt.Sprintf("开始尝试用户名密码组合 (总用户数: %d, 总密码数: %d, 总组合数: %d)",
|
||||||
|
len(Common.Userdict["ssh"]), len(Common.Passwords), len(credentials)))
|
||||||
|
|
||||||
// 遍历所有用户名密码组合
|
// 使用工作池并发扫描
|
||||||
for _, user := range Common.Userdict["ssh"] {
|
result := concurrentSshScan(info, credentials, Common.Timeout, Common.MaxRetries)
|
||||||
for _, pass := range Common.Passwords {
|
if result != nil {
|
||||||
tried++
|
// 记录成功结果
|
||||||
pass = strings.Replace(pass, "{user}", user, -1)
|
logAndSaveSuccess(info, target, result)
|
||||||
Common.LogDebug(fmt.Sprintf("[%d/%d] 尝试: %s:%s", tried, total, user, pass))
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// 重试循环
|
Common.LogDebug(fmt.Sprintf("扫描完成,共尝试 %d 个组合", len(credentials)))
|
||||||
for retryCount := 0; retryCount < maxRetries; retryCount++ {
|
return nil
|
||||||
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)
|
// generateCredentials 生成所有用户名密码组合
|
||||||
done := make(chan struct {
|
func generateCredentials(users, passwords []string) []SshCredential {
|
||||||
success bool
|
var credentials []SshCredential
|
||||||
err error
|
for _, user := range users {
|
||||||
}, 1)
|
for _, pass := range passwords {
|
||||||
|
actualPass := strings.Replace(pass, "{user}", user, -1)
|
||||||
|
credentials = append(credentials, SshCredential{
|
||||||
|
Username: user,
|
||||||
|
Password: actualPass,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return credentials
|
||||||
|
}
|
||||||
|
|
||||||
go func(user, pass string) {
|
// concurrentSshScan 并发扫描SSH服务
|
||||||
success, err := SshConn(info, user, pass)
|
func concurrentSshScan(info *Common.HostInfo, credentials []SshCredential, timeout int64, maxRetries int) *SshScanResult {
|
||||||
select {
|
// 限制并发数
|
||||||
case <-ctx.Done():
|
maxConcurrent := 10
|
||||||
case done <- struct {
|
if maxConcurrent > len(credentials) {
|
||||||
success bool
|
maxConcurrent = len(credentials)
|
||||||
err error
|
}
|
||||||
}{success, err}:
|
|
||||||
}
|
|
||||||
}(user, pass)
|
|
||||||
|
|
||||||
var err error
|
// 创建工作池
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
resultChan := make(chan *SshScanResult, 1)
|
||||||
|
workChan := make(chan SshCredential, maxConcurrent)
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
// 启动工作协程
|
||||||
|
for i := 0; i < maxConcurrent; i++ {
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
for credential := range workChan {
|
||||||
select {
|
select {
|
||||||
case result := <-done:
|
|
||||||
err = result.err
|
|
||||||
if result.success {
|
|
||||||
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
|
|
||||||
}
|
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
err = fmt.Errorf("连接超时")
|
return
|
||||||
}
|
default:
|
||||||
|
result := trySshCredential(info, credential, timeout, maxRetries)
|
||||||
cancel()
|
if result.Success {
|
||||||
|
select {
|
||||||
if err != nil {
|
case resultChan <- result:
|
||||||
errMsg := fmt.Sprintf("SSH认证失败 %s User:%v Pass:%v Err:%v",
|
cancel() // 找到有效凭据,取消其他工作
|
||||||
target, user, pass, err)
|
default:
|
||||||
Common.LogError(errMsg)
|
|
||||||
|
|
||||||
if retryErr := Common.CheckErrs(err); retryErr != nil {
|
|
||||||
if retryCount == maxRetries-1 {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
if Common.SshKeyPath != "" {
|
// 发送工作
|
||||||
Common.LogDebug("检测到SSH密钥路径,停止密码尝试")
|
go func() {
|
||||||
return err
|
for i, cred := range credentials {
|
||||||
}
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
break
|
break
|
||||||
|
default:
|
||||||
|
Common.LogDebug(fmt.Sprintf("[%d/%d] 尝试: %s:%s", i+1, len(credentials), cred.Username, cred.Password))
|
||||||
|
workChan <- cred
|
||||||
|
}
|
||||||
|
}
|
||||||
|
close(workChan)
|
||||||
|
}()
|
||||||
|
|
||||||
|
// 等待结果或完成
|
||||||
|
go func() {
|
||||||
|
wg.Wait()
|
||||||
|
close(resultChan)
|
||||||
|
}()
|
||||||
|
|
||||||
|
// 获取结果
|
||||||
|
result, ok := <-resultChan
|
||||||
|
if ok {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// trySshCredential 尝试单个SSH凭据
|
||||||
|
func trySshCredential(info *Common.HostInfo, credential SshCredential, timeout int64, maxRetries int) *SshScanResult {
|
||||||
|
var lastErr error
|
||||||
|
|
||||||
|
for retry := 0; retry < maxRetries; retry++ {
|
||||||
|
if retry > 0 {
|
||||||
|
Common.LogDebug(fmt.Sprintf("第%d次重试: %s:%s", retry+1, credential.Username, credential.Password))
|
||||||
|
time.Sleep(500 * time.Millisecond) // 重试前等待
|
||||||
|
}
|
||||||
|
|
||||||
|
success, err := attemptSshConnection(info, credential.Username, credential.Password, timeout)
|
||||||
|
if success {
|
||||||
|
return &SshScanResult{
|
||||||
|
Success: true,
|
||||||
|
Credential: credential,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lastErr = err
|
||||||
|
if err != nil {
|
||||||
|
// 检查是否需要重试
|
||||||
|
if retryErr := Common.CheckErrs(err); retryErr == nil {
|
||||||
|
break // 不需要重试的错误
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Common.LogDebug(fmt.Sprintf("扫描完成,共尝试 %d 个组合", tried))
|
return &SshScanResult{
|
||||||
return tmperr
|
Success: false,
|
||||||
|
Error: lastErr,
|
||||||
|
Credential: credential,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SshConn(info *Common.HostInfo, user string, pass string) (flag bool, err error) {
|
// attemptSshConnection 尝试SSH连接
|
||||||
|
func attemptSshConnection(info *Common.HostInfo, username, password string, timeoutSeconds int64) (bool, error) {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeoutSeconds)*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
connChan := make(chan struct {
|
||||||
|
success bool
|
||||||
|
err error
|
||||||
|
}, 1)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
success, err := sshConnect(info, username, password)
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
case connChan <- struct {
|
||||||
|
success bool
|
||||||
|
err error
|
||||||
|
}{success, err}:
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case result := <-connChan:
|
||||||
|
return result.success, result.err
|
||||||
|
case <-ctx.Done():
|
||||||
|
return false, fmt.Errorf("连接超时")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// sshConnect 建立SSH连接并验证
|
||||||
|
func sshConnect(info *Common.HostInfo, username, password string) (bool, 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)
|
||||||
@ -146,11 +211,11 @@ func SshConn(info *Common.HostInfo, user string, pass string) (flag bool, err er
|
|||||||
}
|
}
|
||||||
auth = []ssh.AuthMethod{ssh.PublicKeys(signer)}
|
auth = []ssh.AuthMethod{ssh.PublicKeys(signer)}
|
||||||
} else {
|
} else {
|
||||||
auth = []ssh.AuthMethod{ssh.Password(pass)}
|
auth = []ssh.AuthMethod{ssh.Password(password)}
|
||||||
}
|
}
|
||||||
|
|
||||||
config := &ssh.ClientConfig{
|
config := &ssh.ClientConfig{
|
||||||
User: user,
|
User: username,
|
||||||
Auth: auth,
|
Auth: auth,
|
||||||
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
|
||||||
@ -174,9 +239,45 @@ func SshConn(info *Common.HostInfo, user string, pass string) (flag bool, err er
|
|||||||
if Common.Command != "" {
|
if Common.Command != "" {
|
||||||
_, err := session.CombinedOutput(Common.Command)
|
_, err := session.CombinedOutput(Common.Command)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return true, err
|
return true, fmt.Errorf("命令执行失败: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// logAndSaveSuccess 记录并保存成功结果
|
||||||
|
func logAndSaveSuccess(info *Common.HostInfo, target string, result *SshScanResult) {
|
||||||
|
successMsg := fmt.Sprintf("SSH认证成功 %s User:%v Pass:%v",
|
||||||
|
target, result.Credential.Username, result.Credential.Password)
|
||||||
|
Common.LogSuccess(successMsg)
|
||||||
|
|
||||||
|
details := map[string]interface{}{
|
||||||
|
"port": info.Ports,
|
||||||
|
"service": "ssh",
|
||||||
|
"username": result.Credential.Username,
|
||||||
|
"password": result.Credential.Password,
|
||||||
|
"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)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user