mirror of
https://github.com/shadow1ng/fscan.git
synced 2025-09-14 14:06:44 +08:00

- 统一所有服务插件的实现模式,移除i18n国际化依赖 - 删除硬编码的备份凭据列表,统一使用GenerateCredentials() - 移除过度工程化的Context取消检查 - 清理exploitation功能,专注于弱密码检测和服务识别 - 简化代码结构,移除冗余注释和说明文档 - 优化19个服务插件:activemq, cassandra, elasticsearch, ftp, kafka, ldap, memcached, mongodb, mssql, mysql, neo4j, oracle, postgresql, rabbitmq, rsync, smtp, snmp, telnet, vnc - 代码总量减少约40%,提升维护效率 此次重构确保插件架构的一致性和简洁性
150 lines
3.0 KiB
Go
150 lines
3.0 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"net"
|
|
"time"
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
"github.com/shadow1ng/fscan/common"
|
|
)
|
|
|
|
type MySQLPlugin struct {
|
|
name string
|
|
ports []int
|
|
}
|
|
|
|
func NewMySQLPlugin() *MySQLPlugin {
|
|
return &MySQLPlugin{
|
|
name: "mysql",
|
|
ports: []int{3306, 3307, 33060},
|
|
}
|
|
}
|
|
|
|
func (p *MySQLPlugin) GetName() string {
|
|
return p.name
|
|
}
|
|
|
|
func (p *MySQLPlugin) GetPorts() []int {
|
|
return p.ports
|
|
}
|
|
|
|
func (p *MySQLPlugin) Scan(ctx context.Context, info *common.HostInfo) *ScanResult {
|
|
target := fmt.Sprintf("%s:%s", info.Host, info.Ports)
|
|
|
|
if common.DisableBrute {
|
|
return p.identifyService(info)
|
|
}
|
|
|
|
credentials := GenerateCredentials("mysql")
|
|
if len(credentials) == 0 {
|
|
return &ScanResult{
|
|
Success: false,
|
|
Service: "mysql",
|
|
Error: fmt.Errorf("没有可用的测试凭据"),
|
|
}
|
|
}
|
|
|
|
for _, cred := range credentials {
|
|
if p.testCredential(ctx, info, cred) {
|
|
common.LogSuccess(fmt.Sprintf("MySQL %s %s:%s", target, cred.Username, cred.Password))
|
|
|
|
return &ScanResult{
|
|
Success: true,
|
|
Service: "mysql",
|
|
Username: cred.Username,
|
|
Password: cred.Password,
|
|
}
|
|
}
|
|
}
|
|
|
|
return &ScanResult{
|
|
Success: false,
|
|
Service: "mysql",
|
|
Error: fmt.Errorf("未发现弱密码"),
|
|
}
|
|
}
|
|
|
|
func (p *MySQLPlugin) testCredential(ctx context.Context, info *common.HostInfo, cred Credential) bool {
|
|
connStr := fmt.Sprintf("%s:%s@tcp(%s:%s)/mysql?charset=utf8&timeout=%ds",
|
|
cred.Username, cred.Password, info.Host, info.Ports, common.Timeout)
|
|
|
|
db, err := sql.Open("mysql", connStr)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
defer db.Close()
|
|
|
|
db.SetConnMaxLifetime(time.Duration(common.Timeout) * time.Second)
|
|
db.SetMaxOpenConns(1)
|
|
db.SetMaxIdleConns(0)
|
|
|
|
err = db.PingContext(ctx)
|
|
return err == nil
|
|
}
|
|
|
|
|
|
func (p *MySQLPlugin) identifyService(info *common.HostInfo) *ScanResult {
|
|
target := fmt.Sprintf("%s:%s", info.Host, info.Ports)
|
|
|
|
conn, err := common.WrapperTcpWithTimeout("tcp", target, time.Duration(common.Timeout)*time.Second)
|
|
if err != nil {
|
|
return &ScanResult{
|
|
Success: false,
|
|
Service: "mysql",
|
|
Error: err,
|
|
}
|
|
}
|
|
defer conn.Close()
|
|
|
|
if banner := p.readMySQLBanner(conn); banner != "" {
|
|
common.LogSuccess(fmt.Sprintf("MySQL %s %s", target, banner))
|
|
return &ScanResult{
|
|
Success: true,
|
|
Service: "mysql",
|
|
Banner: banner,
|
|
}
|
|
}
|
|
|
|
return &ScanResult{
|
|
Success: false,
|
|
Service: "mysql",
|
|
Error: fmt.Errorf("无法识别为MySQL服务"),
|
|
}
|
|
}
|
|
|
|
func (p *MySQLPlugin) readMySQLBanner(conn net.Conn) string {
|
|
conn.SetReadDeadline(time.Now().Add(time.Duration(common.Timeout) * time.Second))
|
|
|
|
handshake := make([]byte, 256)
|
|
n, err := conn.Read(handshake)
|
|
if err != nil || n < 10 {
|
|
return ""
|
|
}
|
|
|
|
if handshake[4] != 10 {
|
|
return ""
|
|
}
|
|
|
|
versionStart := 5
|
|
versionEnd := versionStart
|
|
for versionEnd < n && handshake[versionEnd] != 0 {
|
|
versionEnd++
|
|
}
|
|
|
|
if versionEnd <= versionStart {
|
|
return ""
|
|
}
|
|
|
|
versionStr := string(handshake[versionStart:versionEnd])
|
|
return fmt.Sprintf("MySQL %s", versionStr)
|
|
}
|
|
|
|
|
|
func init() {
|
|
RegisterPlugin("mysql", func() Plugin {
|
|
return NewMySQLPlugin()
|
|
})
|
|
} |