fscan/Common/Parse.go

293 lines
6.5 KiB
Go
Raw Normal View History

2024-12-18 22:00:18 +08:00
package Common
2020-12-29 17:17:10 +08:00
import (
"bufio"
"encoding/hex"
2020-12-29 17:17:10 +08:00
"flag"
"fmt"
"net/url"
2020-12-29 17:17:10 +08:00
"os"
"strings"
)
2024-12-22 02:31:29 +08:00
func Parse(Info *HostInfo) error {
2022-07-03 23:41:39 +08:00
ParseUser()
2020-12-29 17:17:10 +08:00
ParsePass(Info)
2024-12-22 02:31:29 +08:00
if err := ParseInput(Info); err != nil {
return err
}
return nil
2020-12-29 17:17:10 +08:00
}
// ParseUser 解析用户名配置
func ParseUser() error {
// 如果未指定用户名和用户名文件,直接返回
2024-12-20 03:46:09 +08:00
if Username == "" && UsersFile == "" {
return nil
2021-09-11 16:43:38 +08:00
}
var usernames []string
// 处理直接指定的用户名列表
2022-07-03 23:41:39 +08:00
if Username != "" {
usernames = strings.Split(Username, ",")
LogInfo(fmt.Sprintf("加载用户名: %d 个", len(usernames)))
2020-12-29 17:17:10 +08:00
}
2021-09-11 16:43:38 +08:00
// 从文件加载用户名列表
2024-12-20 03:46:09 +08:00
if UsersFile != "" {
users, err := Readfile(UsersFile)
if err != nil {
return fmt.Errorf("读取用户名文件失败: %v", err)
}
// 过滤空用户名
for _, user := range users {
if user != "" {
usernames = append(usernames, user)
2020-12-29 17:17:10 +08:00
}
}
LogInfo(fmt.Sprintf("从文件加载用户名: %d 个", len(users)))
2020-12-29 17:17:10 +08:00
}
// 去重处理
usernames = RemoveDuplicate(usernames)
LogInfo(fmt.Sprintf("用户名总数: %d 个", len(usernames)))
// 更新用户字典
2021-09-11 16:43:38 +08:00
for name := range Userdict {
Userdict[name] = usernames
2021-09-11 16:43:38 +08:00
}
return nil
2020-12-29 17:17:10 +08:00
}
// ParsePass 解析密码、哈希值、URL和端口配置
2024-12-19 16:15:53 +08:00
func ParsePass(Info *HostInfo) error {
// 处理直接指定的密码列表
var pwdList []string
2022-07-03 23:41:39 +08:00
if Password != "" {
passes := strings.Split(Password, ",")
for _, pass := range passes {
2020-12-29 17:17:10 +08:00
if pass != "" {
pwdList = append(pwdList, pass)
2020-12-29 17:17:10 +08:00
}
}
Passwords = pwdList
LogInfo(fmt.Sprintf("加载密码: %d 个", len(pwdList)))
2020-12-29 17:17:10 +08:00
}
// 从文件加载密码列表
2024-12-20 03:46:09 +08:00
if PasswordsFile != "" {
passes, err := Readfile(PasswordsFile)
if err != nil {
return fmt.Errorf("读取密码文件失败: %v", err)
}
for _, pass := range passes {
if pass != "" {
pwdList = append(pwdList, pass)
2020-12-29 17:17:10 +08:00
}
}
Passwords = pwdList
LogInfo(fmt.Sprintf("从文件加载密码: %d 个", len(passes)))
}
// 处理哈希文件
2024-12-20 03:46:09 +08:00
if HashFile != "" {
hashes, err := Readfile(HashFile)
if err != nil {
return fmt.Errorf("读取哈希文件失败: %v", err)
}
validCount := 0
for _, line := range hashes {
if line == "" {
continue
}
if len(line) == 32 {
2024-12-20 03:46:09 +08:00
HashValues = append(HashValues, line)
validCount++
} else {
LogError(fmt.Sprintf("无效的哈希值: %s (长度!=32)", line))
2024-08-29 15:12:30 +08:00
}
}
LogInfo(fmt.Sprintf("加载有效哈希值: %d 个", validCount))
2024-08-29 15:12:30 +08:00
}
// 从文件加载端口列表
2024-12-20 03:46:09 +08:00
if PortsFile != "" {
ports, err := Readfile(PortsFile)
if err != nil {
return fmt.Errorf("读取端口文件失败: %v", err)
}
var newport strings.Builder
for _, port := range ports {
if port != "" {
newport.WriteString(port)
newport.WriteString(",")
}
}
Ports = newport.String()
LogInfo("从文件加载端口配置")
}
return nil
2020-12-29 17:17:10 +08:00
}
// Readfile 读取文件内容并返回非空行的切片
2020-12-29 17:17:10 +08:00
func Readfile(filename string) ([]string, error) {
// 打开文件
2020-12-29 17:17:10 +08:00
file, err := os.Open(filename)
if err != nil {
LogError(fmt.Sprintf("打开文件失败 %s: %v", filename, err))
return nil, err
2020-12-29 17:17:10 +08:00
}
defer file.Close()
2020-12-29 17:17:10 +08:00
var content []string
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
// 逐行读取文件内容
lineCount := 0
2020-12-29 17:17:10 +08:00
for scanner.Scan() {
text := strings.TrimSpace(scanner.Text())
if text != "" {
content = append(content, text)
lineCount++
2020-12-29 17:17:10 +08:00
}
}
// 检查扫描过程中是否有错误
if err := scanner.Err(); err != nil {
LogError(fmt.Sprintf("读取文件错误 %s: %v", filename, err))
return nil, err
}
LogInfo(fmt.Sprintf("读取文件成功 %s: %d 行", filename, lineCount))
2020-12-29 17:17:10 +08:00
return content, nil
}
// ParseInput 解析和验证输入参数配置
2024-12-19 16:15:53 +08:00
func ParseInput(Info *HostInfo) error {
// 检查必要的目标参数
if Info.Host == "" && HostsFile == "" {
LogError("未指定扫描目标")
2020-12-29 17:17:10 +08:00
flag.Usage()
return fmt.Errorf("必须指定扫描目标")
2020-12-29 17:17:10 +08:00
}
2021-03-30 22:30:16 +08:00
2024-12-21 18:26:44 +08:00
// 如果是本地扫描模式,输出提示
if LocalScan {
LogInfo("已启用本地扫描模式")
2024-12-21 18:26:44 +08:00
}
// 配置基本参数
2024-12-20 03:46:09 +08:00
if BruteThreads <= 0 {
BruteThreads = 1
LogInfo(fmt.Sprintf("暴力破解线程数: %d", BruteThreads))
2022-04-28 17:02:48 +08:00
}
2022-11-30 10:49:02 +08:00
//if DisableSave {
// IsSave = false
// LogInfo("已启用临时保存模式")
//}
// 处理端口配置
2024-12-20 03:46:09 +08:00
if Ports == MainPorts {
Ports += "," + WebPorts
2021-04-21 00:13:04 +08:00
}
2024-12-20 03:46:09 +08:00
if AddPorts != "" {
2023-11-13 16:23:19 +08:00
if strings.HasSuffix(Ports, ",") {
2024-12-20 03:46:09 +08:00
Ports += AddPorts
} else {
2024-12-20 03:46:09 +08:00
Ports += "," + AddPorts
}
LogInfo(fmt.Sprintf("额外端口: %s", AddPorts))
}
2022-01-07 10:51:36 +08:00
// 处理用户名配置
2024-12-20 03:46:09 +08:00
if AddUsers != "" {
users := strings.Split(AddUsers, ",")
for dict := range Userdict {
Userdict[dict] = append(Userdict[dict], users...)
Userdict[dict] = RemoveDuplicate(Userdict[dict])
2022-01-07 10:51:36 +08:00
}
LogInfo(fmt.Sprintf("额外用户名: %s", AddUsers))
2022-01-07 10:51:36 +08:00
}
// 处理密码配置
2024-12-20 03:46:09 +08:00
if AddPasswords != "" {
passes := strings.Split(AddPasswords, ",")
Passwords = append(Passwords, passes...)
2022-01-07 10:51:36 +08:00
Passwords = RemoveDuplicate(Passwords)
LogInfo(fmt.Sprintf("额外密码: %s", AddPasswords))
2022-01-07 10:51:36 +08:00
}
// 处理Socks5代理配置
if Socks5Proxy != "" {
if !strings.HasPrefix(Socks5Proxy, "socks5://") {
if !strings.Contains(Socks5Proxy, ":") {
Socks5Proxy = "socks5://127.0.0.1" + Socks5Proxy
} else {
Socks5Proxy = "socks5://" + Socks5Proxy
}
}
_, err := url.Parse(Socks5Proxy)
if err != nil {
return fmt.Errorf("Socks5代理格式错误: %v", err)
}
2024-12-20 03:46:09 +08:00
DisablePing = true
LogInfo(fmt.Sprintf("Socks5代理: %s", Socks5Proxy))
}
// 处理HTTP代理配置
2024-12-20 03:46:09 +08:00
if HttpProxy != "" {
switch HttpProxy {
case "1":
2024-12-20 03:46:09 +08:00
HttpProxy = "http://127.0.0.1:8080"
case "2":
2024-12-20 03:46:09 +08:00
HttpProxy = "socks5://127.0.0.1:1080"
default:
2024-12-20 03:46:09 +08:00
if !strings.Contains(HttpProxy, "://") {
HttpProxy = "http://127.0.0.1:" + HttpProxy
}
}
2024-12-20 03:46:09 +08:00
if !strings.HasPrefix(HttpProxy, "socks") && !strings.HasPrefix(HttpProxy, "http") {
return fmt.Errorf("不支持的代理类型")
}
2024-12-20 03:46:09 +08:00
_, err := url.Parse(HttpProxy)
if err != nil {
return fmt.Errorf("代理格式错误: %v", err)
}
LogInfo(fmt.Sprintf("HTTP代理: %s", HttpProxy))
}
// 处理Hash配置
2024-12-20 03:46:09 +08:00
if HashValue != "" {
if len(HashValue) != 32 {
return fmt.Errorf("Hash长度必须为32位")
}
2024-12-20 03:46:09 +08:00
HashValues = append(HashValues, HashValue)
2024-08-29 15:12:30 +08:00
}
// 处理Hash列表
2024-12-20 03:46:09 +08:00
HashValues = RemoveDuplicate(HashValues)
for _, hash := range HashValues {
hashByte, err := hex.DecodeString(hash)
if err != nil {
LogError(fmt.Sprintf("Hash解码失败: %s", hash))
2024-08-29 15:12:30 +08:00
continue
}
HashBytes = append(HashBytes, hashByte)
}
2024-12-20 03:46:09 +08:00
HashValues = []string{}
return nil
2020-12-29 17:17:10 +08:00
}