fscan/Plugins/POP3.go

188 lines
4.8 KiB
Go
Raw Normal View History

2024-12-23 02:21:17 +08:00
package Plugins
import (
"bufio"
"crypto/tls"
"fmt"
"github.com/shadow1ng/fscan/Common"
"net"
"strings"
"time"
)
func POP3Scan(info *Common.HostInfo) (tmperr error) {
if Common.DisableBrute {
return
}
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)
2025-01-14 23:38:58 +08:00
Common.LogDebug(fmt.Sprintf("开始扫描 %s", target))
totalUsers := len(Common.Userdict["pop3"])
totalPass := len(Common.Passwords)
Common.LogDebug(fmt.Sprintf("开始尝试用户名密码组合 (总用户数: %d, 总密码数: %d)", totalUsers, totalPass))
tried := 0
total := totalUsers * totalPass
2024-12-23 02:21:17 +08:00
// 遍历所有用户名密码组合
2024-12-23 02:21:17 +08:00
for _, user := range Common.Userdict["pop3"] {
for _, pass := range Common.Passwords {
tried++
2024-12-23 02:21:17 +08:00
pass = strings.Replace(pass, "{user}", user, -1)
Common.LogDebug(fmt.Sprintf("[%d/%d] 尝试: %s:%s", tried, total, user, pass))
2024-12-23 02:21:17 +08:00
// 重试循环
for retryCount := 0; retryCount < maxRetries; retryCount++ {
if retryCount > 0 {
Common.LogDebug(fmt.Sprintf("第%d次重试: %s:%s", retryCount+1, user, pass))
}
done := make(chan struct {
success bool
err error
2025-01-14 23:38:58 +08:00
isTLS bool
}, 1)
go func(user, pass string) {
2025-01-14 23:38:58 +08:00
success, isTLS, err := POP3Conn(info, user, pass)
select {
case done <- struct {
2024-12-31 20:25:54 +08:00
success bool
err error
2025-01-14 23:38:58 +08:00
isTLS bool
}{success, err, isTLS}:
default:
}
}(user, pass)
var err error
select {
case result := <-done:
err = result.err
if result.success && err == nil {
2025-01-14 23:38:58 +08:00
successMsg := fmt.Sprintf("POP3服务 %s 用户名: %v 密码: %v", target, user, pass)
if result.isTLS {
successMsg += " (TLS)"
}
Common.LogSuccess(successMsg)
// 保存结果
vulnResult := &Common.ScanResult{
Time: time.Now(),
Type: Common.VULN,
Target: info.Host,
Status: "vulnerable",
Details: map[string]interface{}{
"port": info.Ports,
"service": "pop3",
"username": user,
"password": pass,
"type": "weak-password",
"tls": result.isTLS,
},
}
Common.SaveResult(vulnResult)
return nil
2024-12-31 20:25:54 +08:00
}
case <-time.After(time.Duration(Common.Timeout) * time.Second):
err = fmt.Errorf("连接超时")
}
2024-12-31 20:25:54 +08:00
if err != nil {
2025-01-14 23:38:58 +08:00
errMsg := fmt.Sprintf("POP3服务 %s 尝试失败 用户名: %v 密码: %v 错误: %v",
target, user, pass, err)
Common.LogError(errMsg)
2024-12-31 20:25:54 +08:00
if retryErr := Common.CheckErrs(err); retryErr != nil {
if retryCount == maxRetries-1 {
continue
2024-12-31 20:25:54 +08:00
}
2025-01-14 23:38:58 +08:00
continue
2024-12-31 20:25:54 +08:00
}
}
2025-01-14 23:38:58 +08:00
break
2024-12-23 02:21:17 +08:00
}
}
}
2024-12-31 20:25:54 +08:00
Common.LogDebug(fmt.Sprintf("扫描完成,共尝试 %d 个组合", tried))
2024-12-23 02:21:17 +08:00
return tmperr
}
2025-01-14 23:38:58 +08:00
func POP3Conn(info *Common.HostInfo, user string, pass string) (success bool, isTLS bool, err error) {
2024-12-23 02:21:17 +08:00
timeout := time.Duration(Common.Timeout) * time.Second
2025-01-14 23:38:58 +08:00
addr := fmt.Sprintf("%s:%s", info.Host, info.Ports)
2024-12-23 02:21:17 +08:00
// 首先尝试普通连接
conn, err := net.DialTimeout("tcp", addr, timeout)
if err == nil {
2025-01-14 23:38:58 +08:00
if flag, err := tryPOP3Auth(conn, user, pass, timeout); err == nil {
return flag, false, nil
2024-12-23 02:21:17 +08:00
}
conn.Close()
}
// 如果普通连接失败尝试TLS连接
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
}
conn, err = tls.DialWithDialer(&net.Dialer{Timeout: timeout}, "tcp", addr, tlsConfig)
if err != nil {
2025-01-14 23:38:58 +08:00
return false, false, fmt.Errorf("连接失败: %v", err)
2024-12-23 02:21:17 +08:00
}
defer conn.Close()
2025-01-14 23:38:58 +08:00
success, err = tryPOP3Auth(conn, user, pass, timeout)
return success, true, err
2024-12-23 02:21:17 +08:00
}
2025-01-14 23:38:58 +08:00
func tryPOP3Auth(conn net.Conn, user string, pass string, timeout time.Duration) (bool, error) {
2024-12-23 02:21:17 +08:00
reader := bufio.NewReader(conn)
conn.SetDeadline(time.Now().Add(timeout))
// 读取欢迎信息
_, err := reader.ReadString('\n')
if err != nil {
return false, fmt.Errorf("读取欢迎消息失败: %v", err)
}
// 发送用户名
conn.SetDeadline(time.Now().Add(timeout))
_, err = conn.Write([]byte(fmt.Sprintf("USER %s\r\n", user)))
if err != nil {
return false, fmt.Errorf("发送用户名失败: %v", err)
}
// 读取用户名响应
conn.SetDeadline(time.Now().Add(timeout))
response, err := reader.ReadString('\n')
if err != nil {
return false, fmt.Errorf("读取用户名响应失败: %v", err)
}
if !strings.Contains(response, "+OK") {
return false, fmt.Errorf("用户名无效")
}
// 发送密码
conn.SetDeadline(time.Now().Add(timeout))
_, err = conn.Write([]byte(fmt.Sprintf("PASS %s\r\n", pass)))
if err != nil {
return false, fmt.Errorf("发送密码失败: %v", err)
}
// 读取密码响应
conn.SetDeadline(time.Now().Add(timeout))
response, err = reader.ReadString('\n')
if err != nil {
return false, fmt.Errorf("读取密码响应失败: %v", err)
}
if strings.Contains(response, "+OK") {
return true, nil
}
return false, fmt.Errorf("认证失败")
}