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
|
2024-12-18 23:38:49 +08:00
|
|
|
|
|
2025-01-03 16:29:54 +08:00
|
|
|
|
Common.LogDebug(fmt.Sprintf("开始扫描 %v:%v", info.Host, info.Ports))
|
|
|
|
|
|
Common.LogDebug("尝试匿名登录...")
|
|
|
|
|
|
|
2024-12-31 20:25:54 +08:00
|
|
|
|
// 先尝试匿名登录
|
|
|
|
|
|
for retryCount := 0; retryCount < maxRetries; retryCount++ {
|
|
|
|
|
|
flag, err := FtpConn(info, "anonymous", "")
|
|
|
|
|
|
if flag && err == nil {
|
2025-01-03 16:29:54 +08:00
|
|
|
|
Common.LogSuccess("匿名登录成功!")
|
2024-12-31 20:25:54 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2025-01-03 16:29:54 +08:00
|
|
|
|
errlog := fmt.Sprintf("ftp %v:%v %v %v", info.Host, info.Ports, "anonymous", 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-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
|
|
|
|
// 执行FTP连接
|
|
|
|
|
|
done := make(chan struct {
|
|
|
|
|
|
success bool
|
|
|
|
|
|
err error
|
2025-01-03 16:29:54 +08:00
|
|
|
|
}, 1)
|
2025-01-01 07:18:36 +08:00
|
|
|
|
|
|
|
|
|
|
go func(user, pass string) {
|
|
|
|
|
|
success, 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
|
|
|
|
|
|
err error
|
2025-01-03 16:29:54 +08:00
|
|
|
|
}{success, err}:
|
|
|
|
|
|
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 {
|
|
|
|
|
|
successLog := fmt.Sprintf("FTP %v:%v %v %v",
|
|
|
|
|
|
info.Host, info.Ports, user, pass)
|
|
|
|
|
|
Common.LogSuccess(successLog)
|
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-01 07:18:36 +08:00
|
|
|
|
// 处理错误情况
|
2025-01-03 16:29:54 +08:00
|
|
|
|
if lastErr != nil {
|
|
|
|
|
|
errlog := fmt.Sprintf("ftp %v:%v %v %v %v",
|
|
|
|
|
|
info.Host, info.Ports, 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连接并尝试登录
|
2024-12-19 16:15:53 +08:00
|
|
|
|
func FtpConn(info *Common.HostInfo, user string, pass string) (flag bool, err error) {
|
2021-04-21 00:13:04 +08:00
|
|
|
|
Host, Port, Username, Password := info.Host, info.Ports, user, pass
|
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 {
|
|
|
|
|
|
return false, err
|
|
|
|
|
|
}
|
2025-01-03 16:29:54 +08:00
|
|
|
|
// 确保连接被关闭
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
if conn != nil {
|
|
|
|
|
|
conn.Quit() // 发送QUIT命令关闭连接
|
|
|
|
|
|
}
|
|
|
|
|
|
}()
|
2024-12-18 23:38:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 尝试登录
|
|
|
|
|
|
if err = conn.Login(Username, Password); err != nil {
|
|
|
|
|
|
return false, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 登录成功,获取目录信息
|
2025-01-03 16:29:54 +08:00
|
|
|
|
result := fmt.Sprintf("ftp %v:%v:%v %v", Host, Port, Username, Password)
|
2024-12-18 23:38:49 +08:00
|
|
|
|
dirs, err := conn.List("")
|
|
|
|
|
|
if err == nil && len(dirs) > 0 {
|
|
|
|
|
|
// 最多显示前6个目录
|
|
|
|
|
|
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
|
|
|
|
}
|
2024-12-18 23:38:49 +08:00
|
|
|
|
result += "\n [->]" + name
|
2020-12-29 17:17:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-12-18 23:38:49 +08:00
|
|
|
|
|
|
|
|
|
|
return true, nil
|
2020-12-29 17:17:10 +08:00
|
|
|
|
}
|