mirror of
https://github.com/shadow1ng/fscan.git
synced 2025-07-14 05:12:36 +08:00
145 lines
3.5 KiB
Go
145 lines
3.5 KiB
Go
package Plugins
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
_ "github.com/go-sql-driver/mysql"
|
|
"github.com/shadow1ng/fscan/Common"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// MysqlScan 执行MySQL服务扫描
|
|
func MysqlScan(info *Common.HostInfo) (tmperr error) {
|
|
if Common.DisableBrute {
|
|
return
|
|
}
|
|
|
|
maxRetries := Common.MaxRetries
|
|
target := fmt.Sprintf("%v:%v", info.Host, info.Ports)
|
|
|
|
Common.LogDebug(fmt.Sprintf("开始扫描 %s", target))
|
|
totalUsers := len(Common.Userdict["mysql"])
|
|
totalPass := len(Common.Passwords)
|
|
Common.LogDebug(fmt.Sprintf("开始尝试用户名密码组合 (总用户数: %d, 总密码数: %d)", totalUsers, totalPass))
|
|
|
|
tried := 0
|
|
total := totalUsers * totalPass
|
|
|
|
// 遍历所有用户名密码组合
|
|
for _, user := range Common.Userdict["mysql"] {
|
|
for _, pass := range Common.Passwords {
|
|
tried++
|
|
pass = strings.Replace(pass, "{user}", user, -1)
|
|
Common.LogDebug(fmt.Sprintf("[%d/%d] 尝试: %s:%s", tried, total, user, pass))
|
|
|
|
// 重试循环
|
|
for retryCount := 0; retryCount < maxRetries; retryCount++ {
|
|
if retryCount > 0 {
|
|
Common.LogDebug(fmt.Sprintf("第%d次重试: %s:%s", retryCount+1, user, pass))
|
|
}
|
|
|
|
done := make(chan struct {
|
|
success bool
|
|
err error
|
|
}, 1)
|
|
|
|
go func(user, pass string) {
|
|
success, err := MysqlConn(info, user, pass)
|
|
select {
|
|
case done <- struct {
|
|
success bool
|
|
err error
|
|
}{success, err}:
|
|
default:
|
|
}
|
|
}(user, pass)
|
|
|
|
var err error
|
|
select {
|
|
case result := <-done:
|
|
err = result.err
|
|
if result.success && err == nil {
|
|
successMsg := fmt.Sprintf("MySQL %s %v %v", target, user, pass)
|
|
Common.LogSuccess(successMsg)
|
|
|
|
// 保存结果
|
|
vulnResult := &Common.ScanResult{
|
|
Time: time.Now(),
|
|
Type: Common.VULN,
|
|
Target: info.Host,
|
|
Status: "vulnerable",
|
|
Details: map[string]interface{}{
|
|
"port": info.Ports,
|
|
"service": "mysql",
|
|
"username": user,
|
|
"password": pass,
|
|
"type": "weak-password",
|
|
},
|
|
}
|
|
Common.SaveResult(vulnResult)
|
|
return nil
|
|
}
|
|
case <-time.After(time.Duration(Common.Timeout) * time.Second):
|
|
err = fmt.Errorf("连接超时")
|
|
}
|
|
|
|
if err != nil {
|
|
errMsg := fmt.Sprintf("MySQL %s %v %v %v", target, user, pass, err)
|
|
Common.LogError(errMsg)
|
|
|
|
if strings.Contains(err.Error(), "Access denied") {
|
|
break // 认证失败,尝试下一个密码
|
|
}
|
|
|
|
if retryErr := Common.CheckErrs(err); retryErr != nil {
|
|
if retryCount == maxRetries-1 {
|
|
tmperr = err
|
|
if !strings.Contains(err.Error(), "Access denied") {
|
|
continue
|
|
}
|
|
}
|
|
continue
|
|
}
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Common.LogDebug(fmt.Sprintf("扫描完成,共尝试 %d 个组合", tried))
|
|
return tmperr
|
|
}
|
|
|
|
// MysqlConn 尝试MySQL连接
|
|
func MysqlConn(info *Common.HostInfo, user string, pass string) (bool, error) {
|
|
host, port, username, password := info.Host, info.Ports, user, pass
|
|
timeout := time.Duration(Common.Timeout) * time.Second
|
|
|
|
// 构造连接字符串
|
|
connStr := fmt.Sprintf(
|
|
"%v:%v@tcp(%v:%v)/mysql?charset=utf8&timeout=%v",
|
|
username, password, host, port, timeout,
|
|
)
|
|
|
|
// 建立数据库连接
|
|
db, err := sql.Open("mysql", connStr)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer db.Close()
|
|
|
|
// 设置连接参数
|
|
db.SetConnMaxLifetime(timeout)
|
|
db.SetConnMaxIdleTime(timeout)
|
|
db.SetMaxIdleConns(0)
|
|
|
|
// 测试连接
|
|
if err = db.Ping(); err != nil {
|
|
return false, err
|
|
}
|
|
|
|
// 连接成功,只返回结果,不打印日志
|
|
return true, nil
|
|
}
|