2020-12-29 17:17:10 +08:00
|
|
|
package Plugins
|
|
|
|
|
|
|
|
import (
|
2025-01-01 05:24:49 +08:00
|
|
|
"context"
|
2020-12-29 17:17:10 +08:00
|
|
|
"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"
|
2024-12-31 20:25:54 +08:00
|
|
|
"sync"
|
2023-11-13 10:43:11 +08:00
|
|
|
"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
|
|
|
|
threads := Common.BruteThreads
|
2025-01-01 05:24:49 +08:00
|
|
|
|
|
|
|
// 创建带取消功能的context
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
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 05:24:49 +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)
|
|
|
|
|
2024-12-31 20:42:08 +08:00
|
|
|
if err != nil && !strings.Contains(err.Error(), "Login incorrect") {
|
|
|
|
if retryErr := Common.CheckErrs(err); retryErr != nil {
|
|
|
|
if retryCount == maxRetries-1 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
2024-12-31 20:25:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
2021-03-30 18:12:54 +08:00
|
|
|
}
|
|
|
|
|
2024-12-31 20:25:54 +08:00
|
|
|
taskChan := make(chan struct {
|
|
|
|
user string
|
|
|
|
pass string
|
|
|
|
}, len(Common.Userdict["ftp"])*len(Common.Passwords))
|
|
|
|
|
2025-01-01 05:24:49 +08:00
|
|
|
// 任务分发goroutine
|
|
|
|
go func() {
|
|
|
|
defer close(taskChan)
|
|
|
|
for _, user := range Common.Userdict["ftp"] {
|
|
|
|
for _, pass := range Common.Passwords {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
pass = strings.Replace(pass, "{user}", user, -1)
|
|
|
|
taskChan <- struct {
|
|
|
|
user string
|
|
|
|
pass string
|
|
|
|
}{user, pass}
|
|
|
|
}
|
|
|
|
}
|
2024-12-31 20:25:54 +08:00
|
|
|
}
|
2025-01-01 05:24:49 +08:00
|
|
|
}()
|
2024-12-18 23:38:49 +08:00
|
|
|
|
2024-12-31 20:25:54 +08:00
|
|
|
var wg sync.WaitGroup
|
2025-01-01 05:24:49 +08:00
|
|
|
resultChan := make(chan error, threads)
|
|
|
|
|
|
|
|
// 启动工作线程
|
2024-12-31 20:25:54 +08:00
|
|
|
for i := 0; i < threads; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for task := range taskChan {
|
2024-12-31 20:42:08 +08:00
|
|
|
select {
|
2025-01-01 05:24:49 +08:00
|
|
|
case <-ctx.Done():
|
2024-12-31 20:42:08 +08:00
|
|
|
resultChan <- nil
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2024-12-31 20:25:54 +08:00
|
|
|
// 重试循环
|
|
|
|
for retryCount := 0; retryCount < maxRetries; retryCount++ {
|
|
|
|
done := make(chan struct {
|
|
|
|
success bool
|
|
|
|
err error
|
2025-01-01 05:24:49 +08:00
|
|
|
}, 1)
|
|
|
|
|
|
|
|
connCtx, connCancel := context.WithTimeout(ctx, time.Duration(Common.Timeout)*time.Second)
|
2024-12-31 20:25:54 +08:00
|
|
|
|
|
|
|
go func(user, pass string) {
|
|
|
|
success, err := FtpConn(info, user, pass)
|
2025-01-01 05:24:49 +08:00
|
|
|
select {
|
|
|
|
case <-connCtx.Done():
|
|
|
|
case done <- struct {
|
2024-12-31 20:25:54 +08:00
|
|
|
success bool
|
|
|
|
err error
|
2025-01-01 05:24:49 +08:00
|
|
|
}{success, err}:
|
|
|
|
}
|
2024-12-31 20:25:54 +08:00
|
|
|
}(task.user, task.pass)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
select {
|
2025-01-01 05:24:49 +08:00
|
|
|
case <-ctx.Done():
|
|
|
|
connCancel()
|
|
|
|
resultChan <- nil
|
|
|
|
return
|
2024-12-31 20:25:54 +08:00
|
|
|
case result := <-done:
|
|
|
|
err = result.err
|
|
|
|
if result.success && err == nil {
|
2025-01-01 05:24:49 +08:00
|
|
|
successLog := fmt.Sprintf("FTP %v:%v %v %v",
|
|
|
|
info.Host, info.Ports, task.user, task.pass)
|
|
|
|
Common.LogSuccess(successLog)
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
cancel() // 取消所有操作
|
2024-12-31 20:25:54 +08:00
|
|
|
resultChan <- nil
|
|
|
|
return
|
|
|
|
}
|
2025-01-01 05:24:49 +08:00
|
|
|
case <-connCtx.Done():
|
2024-12-31 20:25:54 +08:00
|
|
|
err = fmt.Errorf("连接超时")
|
|
|
|
}
|
|
|
|
|
2025-01-01 05:24:49 +08:00
|
|
|
connCancel()
|
|
|
|
|
2024-12-31 20:25:54 +08:00
|
|
|
if err != nil {
|
2025-01-01 05:24:49 +08:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
resultChan <- nil
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
errlog := fmt.Sprintf("ftp %v:%v %v %v %v",
|
2024-12-31 20:25:54 +08:00
|
|
|
info.Host, info.Ports, task.user, task.pass, err)
|
|
|
|
Common.LogError(errlog)
|
|
|
|
|
2024-12-31 20:42:08 +08:00
|
|
|
if strings.Contains(err.Error(), "Login incorrect") {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(err.Error(), "too many connections") {
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
if retryCount < maxRetries-1 {
|
2025-01-01 05:24:49 +08:00
|
|
|
continue
|
2024-12-31 20:42:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-31 20:25:54 +08:00
|
|
|
if retryErr := Common.CheckErrs(err); retryErr != nil {
|
|
|
|
if retryCount == maxRetries-1 {
|
2025-01-01 05:24:49 +08:00
|
|
|
continue
|
2024-12-31 20:25:54 +08:00
|
|
|
}
|
2025-01-01 05:24:49 +08:00
|
|
|
continue
|
2024-12-31 20:25:54 +08:00
|
|
|
}
|
|
|
|
}
|
2025-01-01 05:24:49 +08:00
|
|
|
break
|
2024-12-31 20:25:54 +08:00
|
|
|
}
|
2024-12-18 23:38:49 +08:00
|
|
|
}
|
2024-12-31 20:25:54 +08:00
|
|
|
resultChan <- nil
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(resultChan)
|
|
|
|
}()
|
2024-12-18 23:38:49 +08:00
|
|
|
|
2024-12-31 20:25:54 +08:00
|
|
|
for err := range resultChan {
|
|
|
|
if err != nil {
|
|
|
|
tmperr = err
|
2024-12-31 20:42:08 +08:00
|
|
|
if !strings.Contains(err.Error(), "扫描超时") {
|
|
|
|
if retryErr := Common.CheckErrs(err); retryErr != nil {
|
2025-01-01 05:24:49 +08:00
|
|
|
continue
|
2024-12-31 20:42:08 +08:00
|
|
|
}
|
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
|
|
|
|
}
|
2024-12-31 20:42:08 +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-01 05:24:49 +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
|
|
|
}
|