fscan/common/Parse.go

248 lines
4.8 KiB
Go
Raw Normal View History

2020-12-29 01:17:10 -08:00
package common
import (
"bufio"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
2020-12-29 01:17:10 -08:00
"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
}
if TmpOutputfile != "" {
2020-12-30 05:30:36 -08:00
if !strings.Contains(Outputfile, "/") && !strings.Contains(Outputfile, `\`) {
Outputfile = getpath() + TmpOutputfile
2020-12-30 05:30:36 -08:00
} else {
Outputfile = TmpOutputfile
2020-12-30 05:30:36 -08:00
}
2020-12-29 01:17:10 -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://") {
Socks5Proxy = "socks5://" + Socks5Proxy
2022-08-16 00:10:09 -07:00
NoPing = true
}
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 {
2022-06-12 19:27:23 -07:00
case "rdp":
Info.Ports = "3389"
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
func getpath() string {
file, _ := exec.LookPath(os.Args[0])
path1, _ := filepath.Abs(file)
filename := filepath.Dir(path1)
2020-12-30 05:30:36 -08:00
var path string
if strings.Contains(filename, "/") {
tmp := strings.Split(filename, `/`)
tmp[len(tmp)-1] = ``
path = strings.Join(tmp, `/`)
} else if strings.Contains(filename, `\`) {
tmp := strings.Split(filename, `\`)
tmp[len(tmp)-1] = ``
path = strings.Join(tmp, `\`)
}
return path
}
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)
}