2020-12-29 17:17:10 +08:00
|
|
|
|
package Plugins
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"fmt"
|
2023-06-09 08:13:56 -04:00
|
|
|
|
"github.com/jlaffaye/ftp"
|
2024-12-18 22:00:18 +08:00
|
|
|
|
"github.com/shadow1ng/fscan/Common"
|
2023-11-13 10:43:11 +08:00
|
|
|
|
"strings"
|
|
|
|
|
|
"time"
|
2020-12-29 17:17:10 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2024-12-19 16:15:53 +08:00
|
|
|
|
func FtpScan(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 23:38:49 +08:00
|
|
|
|
|
2024-12-31 20:25:54 +08:00
|
|
|
|
maxRetries := Common.MaxRetries
|
2025-01-14 23:38:58 +08:00
|
|
|
|
target := fmt.Sprintf("%v:%v", info.Host, info.Ports)
|
2024-12-18 23:38:49 +08:00
|
|
|
|
|
2025-01-14 23:38:58 +08:00
|
|
|
|
Common.LogDebug(fmt.Sprintf("开始扫描 %s", target))
|
2025-01-03 16:29:54 +08:00
|
|
|
|
Common.LogDebug("尝试匿名登录...")
|
|
|
|
|
|
|
2025-01-14 23:38:58 +08:00
|
|
|
|
// 尝试匿名登录
|
2024-12-31 20:25:54 +08:00
|
|
|
|
for retryCount := 0; retryCount < maxRetries; retryCount++ {
|
2025-01-14 23:38:58 +08:00
|
|
|
|
success, dirs, err := FtpConn(info, "anonymous", "")
|
|
|
|
|
|
if success && err == nil {
|
2025-01-03 16:29:54 +08:00
|
|
|
|
Common.LogSuccess("匿名登录成功!")
|
2025-01-14 23:38:58 +08:00
|
|
|
|
|
|
|
|
|
|
// 保存匿名登录结果
|
|
|
|
|
|
result := &Common.ScanResult{
|
|
|
|
|
|
Time: time.Now(),
|
|
|
|
|
|
Type: Common.VULN,
|
|
|
|
|
|
Target: info.Host,
|
|
|
|
|
|
Status: "vulnerable",
|
|
|
|
|
|
Details: map[string]interface{}{
|
|
|
|
|
|
"port": info.Ports,
|
|
|
|
|
|
"service": "ftp",
|
|
|
|
|
|
"username": "anonymous",
|
|
|
|
|
|
"password": "",
|
|
|
|
|
|
"type": "anonymous-login",
|
|
|
|
|
|
"directories": dirs,
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
Common.SaveResult(result)
|
2024-12-31 20:25:54 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2025-01-14 23:38:58 +08:00
|
|
|
|
errlog := fmt.Sprintf("ftp %s %v", target, err)
|
2024-12-31 20:25:54 +08:00
|
|
|
|
Common.LogError(errlog)
|
|
|
|
|
|
break
|
2021-03-30 18:12:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-03 16:29:54 +08:00
|
|
|
|
totalUsers := len(Common.Userdict["ftp"])
|
|
|
|
|
|
totalPass := len(Common.Passwords)
|
|
|
|
|
|
Common.LogDebug(fmt.Sprintf("开始尝试用户名密码组合 (总用户数: %d, 总密码数: %d)", totalUsers, totalPass))
|
|
|
|
|
|
|
|
|
|
|
|
tried := 0
|
|
|
|
|
|
total := totalUsers * totalPass
|
2024-12-18 23:38:49 +08:00
|
|
|
|
|
2025-01-14 23:38:58 +08:00
|
|
|
|
// 遍历用户名密码组合
|
2025-01-01 07:18:36 +08:00
|
|
|
|
for _, user := range Common.Userdict["ftp"] {
|
|
|
|
|
|
for _, pass := range Common.Passwords {
|
2025-01-03 16:29:54 +08:00
|
|
|
|
tried++
|
2025-01-01 07:18:36 +08:00
|
|
|
|
pass = strings.Replace(pass, "{user}", user, -1)
|
2025-01-03 16:29:54 +08:00
|
|
|
|
Common.LogDebug(fmt.Sprintf("[%d/%d] 尝试: %s:%s", tried, total, user, pass))
|
2025-01-01 05:24:49 +08:00
|
|
|
|
|
2025-01-03 16:29:54 +08:00
|
|
|
|
var lastErr error
|
2024-12-31 20:42:08 +08:00
|
|
|
|
|
2025-01-01 07:18:36 +08:00
|
|
|
|
// 重试循环
|
|
|
|
|
|
for retryCount := 0; retryCount < maxRetries; retryCount++ {
|
2025-01-03 16:29:54 +08:00
|
|
|
|
if retryCount > 0 {
|
|
|
|
|
|
Common.LogDebug(fmt.Sprintf("第%d次重试: %s:%s", retryCount+1, user, pass))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-01 07:18:36 +08:00
|
|
|
|
done := make(chan struct {
|
|
|
|
|
|
success bool
|
2025-01-14 23:38:58 +08:00
|
|
|
|
dirs []string
|
2025-01-01 07:18:36 +08:00
|
|
|
|
err error
|
2025-01-03 16:29:54 +08:00
|
|
|
|
}, 1)
|
2025-01-01 07:18:36 +08:00
|
|
|
|
|
|
|
|
|
|
go func(user, pass string) {
|
2025-01-14 23:38:58 +08:00
|
|
|
|
success, dirs, err := FtpConn(info, user, pass)
|
2025-01-03 16:29:54 +08:00
|
|
|
|
select {
|
|
|
|
|
|
case done <- struct {
|
2024-12-31 20:25:54 +08:00
|
|
|
|
success bool
|
2025-01-14 23:38:58 +08:00
|
|
|
|
dirs []string
|
2024-12-31 20:25:54 +08:00
|
|
|
|
err error
|
2025-01-14 23:38:58 +08:00
|
|
|
|
}{success, dirs, err}:
|
2025-01-03 16:29:54 +08:00
|
|
|
|
default:
|
|
|
|
|
|
}
|
2025-01-01 07:18:36 +08:00
|
|
|
|
}(user, pass)
|
2024-12-31 20:25:54 +08:00
|
|
|
|
|
2025-01-01 07:18:36 +08:00
|
|
|
|
select {
|
|
|
|
|
|
case result := <-done:
|
2025-01-03 16:29:54 +08:00
|
|
|
|
if result.success && result.err == nil {
|
2025-01-14 23:38:58 +08:00
|
|
|
|
successLog := fmt.Sprintf("FTP服务 %s 成功爆破 用户名: %v 密码: %v", target, user, pass)
|
2025-01-03 16:29:54 +08:00
|
|
|
|
Common.LogSuccess(successLog)
|
2025-01-14 23:38:58 +08:00
|
|
|
|
|
|
|
|
|
|
// 保存爆破成功结果
|
|
|
|
|
|
vulnResult := &Common.ScanResult{
|
|
|
|
|
|
Time: time.Now(),
|
|
|
|
|
|
Type: Common.VULN,
|
|
|
|
|
|
Target: info.Host,
|
|
|
|
|
|
Status: "vulnerable",
|
|
|
|
|
|
Details: map[string]interface{}{
|
|
|
|
|
|
"port": info.Ports,
|
|
|
|
|
|
"service": "ftp",
|
|
|
|
|
|
"username": user,
|
|
|
|
|
|
"password": pass,
|
|
|
|
|
|
"type": "weak-password",
|
|
|
|
|
|
"directories": result.dirs,
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
Common.SaveResult(vulnResult)
|
2025-01-01 07:18:36 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2025-01-03 16:29:54 +08:00
|
|
|
|
lastErr = result.err
|
2025-01-01 07:18:36 +08:00
|
|
|
|
case <-time.After(time.Duration(Common.Timeout) * time.Second):
|
2025-01-03 16:29:54 +08:00
|
|
|
|
lastErr = fmt.Errorf("连接超时")
|
2025-01-01 07:18:36 +08:00
|
|
|
|
}
|
2024-12-31 20:42:08 +08:00
|
|
|
|
|
2025-01-14 23:38:58 +08:00
|
|
|
|
// 错误处理
|
2025-01-03 16:29:54 +08:00
|
|
|
|
if lastErr != nil {
|
2025-01-14 23:38:58 +08:00
|
|
|
|
errlog := fmt.Sprintf("FTP服务 %s 尝试失败 用户名: %v 密码: %v 错误: %v",
|
|
|
|
|
|
target, user, pass, lastErr)
|
2025-01-01 07:18:36 +08:00
|
|
|
|
Common.LogError(errlog)
|
2024-12-31 20:42:08 +08:00
|
|
|
|
|
2025-01-03 16:29:54 +08:00
|
|
|
|
if strings.Contains(lastErr.Error(), "Login incorrect") {
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if strings.Contains(lastErr.Error(), "too many connections") {
|
|
|
|
|
|
Common.LogDebug("连接数过多,等待5秒...")
|
|
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
|
if retryCount < maxRetries-1 {
|
|
|
|
|
|
continue
|
2024-12-31 20:25:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-12-29 17:17:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-12-31 20:25:54 +08:00
|
|
|
|
|
2025-01-03 16:29:54 +08:00
|
|
|
|
Common.LogDebug(fmt.Sprintf("扫描完成,共尝试 %d 个组合", tried))
|
2020-12-29 17:17:10 +08:00
|
|
|
|
return tmperr
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-18 23:38:49 +08:00
|
|
|
|
// FtpConn 建立FTP连接并尝试登录
|
2025-01-14 23:38:58 +08:00
|
|
|
|
func FtpConn(info *Common.HostInfo, user string, pass string) (success bool, directories []string, err error) {
|
|
|
|
|
|
Host, Port := info.Host, info.Ports
|
2024-12-18 23:38:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 建立FTP连接
|
2024-12-18 22:00:18 +08:00
|
|
|
|
conn, err := ftp.DialTimeout(fmt.Sprintf("%v:%v", Host, Port), time.Duration(Common.Timeout)*time.Second)
|
2024-12-18 23:38:49 +08:00
|
|
|
|
if err != nil {
|
2025-01-14 23:38:58 +08:00
|
|
|
|
return false, nil, err
|
2024-12-18 23:38:49 +08:00
|
|
|
|
}
|
2025-01-03 16:29:54 +08:00
|
|
|
|
defer func() {
|
|
|
|
|
|
if conn != nil {
|
2025-01-14 23:38:58 +08:00
|
|
|
|
conn.Quit()
|
2025-01-03 16:29:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
}()
|
2024-12-18 23:38:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 尝试登录
|
2025-01-14 23:38:58 +08:00
|
|
|
|
if err = conn.Login(user, pass); err != nil {
|
|
|
|
|
|
return false, nil, err
|
2024-12-18 23:38:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-14 23:38:58 +08:00
|
|
|
|
// 获取目录信息
|
2024-12-18 23:38:49 +08:00
|
|
|
|
dirs, err := conn.List("")
|
|
|
|
|
|
if err == nil && len(dirs) > 0 {
|
2025-01-14 23:38:58 +08:00
|
|
|
|
directories = make([]string, 0, min(6, len(dirs)))
|
2024-12-18 23:38:49 +08:00
|
|
|
|
for i := 0; i < len(dirs) && i < 6; i++ {
|
|
|
|
|
|
name := dirs[i].Name
|
|
|
|
|
|
if len(name) > 50 {
|
|
|
|
|
|
name = name[:50]
|
2020-12-30 21:30:36 +08:00
|
|
|
|
}
|
2025-01-14 23:38:58 +08:00
|
|
|
|
directories = append(directories, name)
|
2020-12-29 17:17:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-12-18 23:38:49 +08:00
|
|
|
|
|
2025-01-14 23:38:58 +08:00
|
|
|
|
return true, directories, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// min 返回两个整数中的较小值
|
|
|
|
|
|
func min(a, b int) int {
|
|
|
|
|
|
if a < b {
|
|
|
|
|
|
return a
|
|
|
|
|
|
}
|
|
|
|
|
|
return b
|
2020-12-29 17:17:10 +08:00
|
|
|
|
}
|