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-18 23:38:49 +08:00
|
|
|
// FtpScan 执行FTP服务扫描
|
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
|
|
|
|
2024-12-31 20:25:54 +08:00
|
|
|
// 先尝试匿名登录
|
|
|
|
for retryCount := 0; retryCount < maxRetries; retryCount++ {
|
|
|
|
flag, err := FtpConn(info, "anonymous", "")
|
|
|
|
if flag && err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2025-01-01 07:18:36 +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)
|
|
|
|
|
2025-01-01 07:18:36 +08:00
|
|
|
if retryErr := Common.CheckErrs(err); retryErr != nil {
|
|
|
|
if retryCount == maxRetries-1 {
|
|
|
|
return err
|
2024-12-31 20:25:54 +08:00
|
|
|
}
|
2025-01-01 07:18:36 +08:00
|
|
|
continue
|
2024-12-31 20:25:54 +08:00
|
|
|
}
|
|
|
|
break
|
2021-03-30 18:12:54 +08:00
|
|
|
}
|
|
|
|
|
2025-01-01 07:18:36 +08:00
|
|
|
starttime := time.Now().Unix()
|
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 {
|
|
|
|
pass = strings.Replace(pass, "{user}", user, -1)
|
2025-01-01 05:24:49 +08:00
|
|
|
|
2025-01-01 07:18:36 +08:00
|
|
|
// 检查是否超时
|
|
|
|
if time.Now().Unix()-starttime > int64(Common.Timeout) {
|
|
|
|
return fmt.Errorf("扫描超时")
|
|
|
|
}
|
2024-12-31 20:42:08 +08:00
|
|
|
|
2025-01-01 07:18:36 +08:00
|
|
|
// 重试循环
|
|
|
|
for retryCount := 0; retryCount < maxRetries; retryCount++ {
|
|
|
|
// 执行FTP连接
|
|
|
|
done := make(chan struct {
|
|
|
|
success bool
|
|
|
|
err error
|
|
|
|
})
|
|
|
|
|
|
|
|
go func(user, pass string) {
|
|
|
|
success, err := FtpConn(info, user, pass)
|
|
|
|
done <- struct {
|
2024-12-31 20:25:54 +08:00
|
|
|
success bool
|
|
|
|
err error
|
2025-01-01 07:18:36 +08:00
|
|
|
}{success, err}
|
|
|
|
}(user, pass)
|
2024-12-31 20:25:54 +08:00
|
|
|
|
2025-01-01 07:18:36 +08:00
|
|
|
// 等待结果或超时
|
|
|
|
var err error
|
|
|
|
select {
|
|
|
|
case result := <-done:
|
|
|
|
err = result.err
|
|
|
|
if result.success && err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case <-time.After(time.Duration(Common.Timeout) * time.Second):
|
|
|
|
err = fmt.Errorf("连接超时")
|
|
|
|
}
|
2024-12-31 20:42:08 +08:00
|
|
|
|
2025-01-01 07:18:36 +08:00
|
|
|
// 处理错误情况
|
|
|
|
if err != nil {
|
|
|
|
errlog := fmt.Sprintf("[-] ftp %v:%v %v %v %v",
|
|
|
|
info.Host, info.Ports, user, pass, err)
|
|
|
|
Common.LogError(errlog)
|
2024-12-31 20:42:08 +08:00
|
|
|
|
2025-01-01 07:18:36 +08:00
|
|
|
// 检查是否需要重试
|
|
|
|
if retryErr := Common.CheckErrs(err); retryErr != nil {
|
|
|
|
if retryCount == maxRetries-1 {
|
|
|
|
return err
|
2024-12-31 20:25:54 +08:00
|
|
|
}
|
2025-01-01 07:18:36 +08:00
|
|
|
continue // 继续重试
|
2024-12-31 20:25:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-01 07:18:36 +08:00
|
|
|
break // 如果不需要重试,跳出重试循环
|
2020-12-29 17:17:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-12-31 20:25:54 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 尝试登录
|
|
|
|
if err = conn.Login(Username, Password); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 登录成功,获取目录信息
|
2025-01-01 07:18:36 +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
|
|
|
|
2025-01-01 07:18:36 +08:00
|
|
|
Common.LogSuccess(result)
|
2024-12-18 23:38:49 +08:00
|
|
|
return true, nil
|
2020-12-29 17:17:10 +08:00
|
|
|
}
|