fscan/common/Parse.go

274 lines
5.4 KiB
Go
Raw Normal View History

2020-12-29 01:17:10 -08:00
package common
import (
"bufio"
"encoding/hex"
2020-12-29 01:17:10 -08:00
"flag"
"fmt"
"net/url"
2020-12-29 01:17:10 -08:00
"os"
"strconv"
"strings"
)
func Parse(Info *HostInfo) {
2022-07-03 08:41:39 -07:00
ParseUser()
2020-12-29 01:17:10 -08:00
ParsePass(Info)
ParseInput(Info)
2022-05-12 02:56:32 -07:00
ParseScantype(Info)
2020-12-29 01:17:10 -08:00
}
2022-07-03 08:41:39 -07:00
func ParseUser() {
if Username == "" && Userfile == "" {
2021-09-11 01:43:38 -07:00
return
}
2022-07-03 08:41:39 -07:00
var Usernames []string
if Username != "" {
Usernames = strings.Split(Username, ",")
2020-12-29 01:17:10 -08:00
}
2021-09-11 01:43:38 -07:00
if Userfile != "" {
2021-03-01 05:55:19 -08:00
users, err := Readfile(Userfile)
2020-12-29 01:17:10 -08:00
if err == nil {
2021-03-01 05:55:19 -08:00
for _, user := range users {
if user != "" {
2022-07-03 08:41:39 -07:00
Usernames = append(Usernames, user)
2020-12-29 01:17:10 -08:00
}
}
}
}
2022-07-03 08:41:39 -07:00
Usernames = RemoveDuplicate(Usernames)
2021-09-11 01:43:38 -07:00
for name := range Userdict {
2022-07-03 08:41:39 -07:00
Userdict[name] = Usernames
2021-09-11 01:43:38 -07:00
}
2020-12-29 01:17:10 -08:00
}
func ParsePass(Info *HostInfo) {
2022-07-03 08:41:39 -07:00
var PwdList []string
if Password != "" {
passs := strings.Split(Password, ",")
2020-12-29 01:17:10 -08:00
for _, pass := range passs {
if pass != "" {
2022-07-03 08:41:39 -07:00
PwdList = append(PwdList, pass)
2020-12-29 01:17:10 -08:00
}
}
2022-07-03 08:41:39 -07:00
Passwords = PwdList
2020-12-29 01:17:10 -08:00
}
if Passfile != "" {
passs, err := Readfile(Passfile)
2020-12-29 01:17:10 -08:00
if err == nil {
for _, pass := range passs {
if pass != "" {
2022-07-03 08:41:39 -07:00
PwdList = append(PwdList, pass)
2020-12-29 01:17:10 -08:00
}
}
2022-07-03 08:41:39 -07:00
Passwords = PwdList
}
}
if URL != "" {
urls := strings.Split(URL, ",")
TmpUrls := make(map[string]struct{})
for _, url := range urls {
if _, ok := TmpUrls[url]; !ok {
TmpUrls[url] = struct{}{}
if url != "" {
Urls = append(Urls, url)
}
}
}
}
if UrlFile != "" {
urls, err := Readfile(UrlFile)
if err == nil {
TmpUrls := make(map[string]struct{})
for _, url := range urls {
if _, ok := TmpUrls[url]; !ok {
TmpUrls[url] = struct{}{}
if url != "" {
Urls = append(Urls, url)
}
}
}
2020-12-29 01:17:10 -08:00
}
}
if PortFile != "" {
ports, err := Readfile(PortFile)
if err == nil {
newport := ""
for _, port := range ports {
if port != "" {
newport += port + ","
}
}
Info.Ports = newport
}
}
2020-12-29 01:17:10 -08:00
}
func Readfile(filename string) ([]string, error) {
file, err := os.Open(filename)
if err != nil {
2021-03-01 05:59:47 -08:00
fmt.Printf("Open %s error, %v\n", filename, err)
2020-12-29 01:17:10 -08:00
os.Exit(0)
}
defer file.Close()
var content []string
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
text := strings.TrimSpace(scanner.Text())
if text != "" {
content = append(content, scanner.Text())
}
}
return content, nil
}
func ParseInput(Info *HostInfo) {
if Info.Host == "" && HostFile == "" && URL == "" && UrlFile == "" {
2020-12-29 01:17:10 -08:00
fmt.Println("Host is none")
flag.Usage()
os.Exit(0)
}
2021-03-30 07:30:16 -07:00
2022-04-28 02:02:48 -07:00
if BruteThread <= 0 {
BruteThread = 1
}
2022-11-29 18:49:02 -08:00
if TmpSave == true {
2020-12-29 01:17:10 -08:00
IsSave = false
}
2021-04-20 09:13:04 -07:00
if Info.Ports == DefaultPorts {
2021-11-30 23:22:48 -08:00
Info.Ports += "," + Webport
2021-04-20 09:13:04 -07:00
}
if PortAdd != "" {
if strings.HasSuffix(Info.Ports, ",") {
Info.Ports += PortAdd
} else {
Info.Ports += "," + PortAdd
}
}
2022-01-06 18:51:36 -08:00
if UserAdd != "" {
user := strings.Split(UserAdd, ",")
2022-07-03 08:41:39 -07:00
for a := range Userdict {
2022-01-06 18:51:36 -08:00
Userdict[a] = append(Userdict[a], user...)
Userdict[a] = RemoveDuplicate(Userdict[a])
}
}
if PassAdd != "" {
pass := strings.Split(PassAdd, ",")
Passwords = append(Passwords, pass...)
Passwords = RemoveDuplicate(Passwords)
}
if Socks5Proxy != "" && !strings.HasPrefix(Socks5Proxy, "socks5://") {
if !strings.Contains(Socks5Proxy, ":") {
Socks5Proxy = "socks5://127.0.0.1" + Socks5Proxy
} else {
Socks5Proxy = "socks5://" + Socks5Proxy
}
}
if Socks5Proxy != "" {
fmt.Println("Socks5Proxy:", Socks5Proxy)
_, err := url.Parse(Socks5Proxy)
if err != nil {
fmt.Println("Socks5Proxy parse error:", err)
os.Exit(0)
}
2022-08-16 00:10:09 -07:00
NoPing = true
}
if Proxy != "" {
if Proxy == "1" {
Proxy = "http://127.0.0.1:8080"
} else if Proxy == "2" {
Proxy = "socks5://127.0.0.1:1080"
} else if !strings.Contains(Proxy, "://") {
Proxy = "http://127.0.0.1:" + Proxy
}
fmt.Println("Proxy:", Proxy)
if !strings.HasPrefix(Proxy, "socks") && !strings.HasPrefix(Proxy, "http") {
fmt.Println("no support this proxy")
os.Exit(0)
}
_, err := url.Parse(Proxy)
if err != nil {
fmt.Println("Proxy parse error:", err)
os.Exit(0)
}
}
if Hash != "" && len(Hash) != 32 {
fmt.Println("[-] Hash is error,len(hash) must be 32")
os.Exit(0)
} else {
var err error
HashBytes, err = hex.DecodeString(Hash)
if err != nil {
fmt.Println("[-] Hash is error,hex decode error")
os.Exit(0)
}
}
2020-12-29 01:17:10 -08:00
}
func ParseScantype(Info *HostInfo) {
2022-07-03 08:41:39 -07:00
_, ok := PORTList[Scantype]
2020-12-29 01:17:10 -08:00
if !ok {
2021-03-09 01:21:27 -08:00
showmode()
2020-12-29 01:17:10 -08:00
}
2022-07-03 08:41:39 -07:00
if Scantype != "all" && Info.Ports == DefaultPorts+","+Webport {
switch Scantype {
case "wmiexec":
Info.Ports = "135"
case "wmiinfo":
Info.Ports = "135"
case "smbinfo":
Info.Ports = "445"
case "hostname":
Info.Ports = "135,137,139,445"
case "smb2":
Info.Ports = "445"
2022-06-12 19:27:23 -07:00
case "web":
Info.Ports = Webport
case "webonly":
Info.Ports = Webport
case "ms17010":
Info.Ports = "445"
case "cve20200796":
Info.Ports = "445"
case "portscan":
Info.Ports = DefaultPorts + "," + Webport
case "main":
Info.Ports = DefaultPorts
default:
2022-07-03 08:41:39 -07:00
port, _ := PORTList[Scantype]
2022-06-12 19:27:23 -07:00
Info.Ports = strconv.Itoa(port)
2020-12-29 01:17:10 -08:00
}
2022-07-03 08:41:39 -07:00
fmt.Println("-m ", Scantype, " start scan the port:", Info.Ports)
2020-12-29 01:17:10 -08:00
}
}
func CheckErr(text string, err error, flag bool) {
2020-12-29 01:17:10 -08:00
if err != nil {
fmt.Println("Parse", text, "error: ", err.Error())
if flag {
if err != ParseIPErr {
fmt.Println(ParseIPErr)
}
os.Exit(0)
}
2020-12-29 01:17:10 -08:00
}
}
2020-12-30 05:30:36 -08:00
2021-03-09 01:21:27 -08:00
func showmode() {
fmt.Println("The specified scan type does not exist")
fmt.Println("-m")
for name := range PORTList {
fmt.Println(" [" + name + "]")
}
os.Exit(0)
}