diff --git a/Plugins/icmp.go b/Plugins/icmp.go index c92c5d2..eab30a1 100644 --- a/Plugins/icmp.go +++ b/Plugins/icmp.go @@ -3,7 +3,6 @@ package Plugins import ( "bytes" "encoding/binary" - "fmt" "net" "os" "os/exec" @@ -12,6 +11,8 @@ import ( "strings" "sync" "time" + + "github.com/shadow1ng/fscan/common" ) var icmp ICMP @@ -91,7 +92,7 @@ func isping(ip string) bool { conn.SetReadDeadline(time.Time{}) if string(recvBuf[0:num]) != "" { - fmt.Printf("(ICMP) Target '%s' is alive\n", ip) + common.LogPrint("(ICMP) Target '", ip, "' is alive") return true } return false @@ -175,7 +176,7 @@ func PingCMDcheck(hostslist []string, bsenv string) { defer wg.Done() if ExecCommandPing(host, bsenv) { mutex.Lock() - fmt.Printf("(Ping) Target '%s' is alive\n", host) + common.LogPrint("(Ping) Target '", host, "' is alive") AliveHosts = append(AliveHosts, host) mutex.Unlock() } @@ -200,8 +201,8 @@ func ICMPRun(hostslist []string, IcmpThreads int, Ping bool) []string { PingCMDcheck(hostslist, "/bin/bash") } } else { - fmt.Println("The current user permissions unable to send icmp packets") - fmt.Println("start ping") + common.LogPrint("The current user permissions unable to send icmp packets") + common.LogPrint("start ping") PingCMDcheck(hostslist, "/bin/bash") } } else if SysInfo.OS == "darwin" { @@ -212,8 +213,8 @@ func ICMPRun(hostslist []string, IcmpThreads int, Ping bool) []string { PingCMDcheck(hostslist, "/bin/bash") } } else { - fmt.Println("The current user permissions unable to send icmp packets") - fmt.Println("start ping") + common.LogPrint("The current user permissions unable to send icmp packets") + common.LogPrint("start ping") PingCMDcheck(hostslist, "/bin/bash") } } diff --git a/Plugins/scanner.go b/Plugins/scanner.go index 49e3b28..e27761e 100644 --- a/Plugins/scanner.go +++ b/Plugins/scanner.go @@ -34,11 +34,11 @@ func IsContain(items []string, item string) bool { } func Scan(info common.HostInfo) { - fmt.Println("scan start") + common.LogPrint("scan task ", info, " start ") Hosts, _ := common.ParseIP(info.Host, info.HostFile) if info.Isping == false { Hosts = ICMPRun(Hosts, info.IcmpThreads, info.Ping) - fmt.Println("icmp alive hosts len is:", len(Hosts)) + common.LogPrint("icmp alive hosts len is:", len(Hosts)) } _, AlivePorts := TCPportScan(Hosts, info.Ports, "icmp", 3) //return AliveHosts,AlivePorts var severports []string //severports := []string{"21","22","135"."445","1433","3306","5432","6379","9200","11211","27017"...} diff --git a/common/Parse.go b/common/Parse.go index a9a6431..0e19d11 100644 --- a/common/Parse.go +++ b/common/Parse.go @@ -9,29 +9,27 @@ import ( "strings" ) -func Parse(Info *HostInfo){ +func Parse(Info *HostInfo) { ParseUser(Info) ParsePass(Info) ParseInput(Info) ParseScantype(Info) } - - -func ParseUser(Info *HostInfo){ - if Info.Username!=""{ +func ParseUser(Info *HostInfo) { + if Info.Username != "" { uesrs := strings.Split(Info.Username, ",") - for _,uesr := range uesrs{ - if uesr !=""{ - Info.Usernames = append(Info.Usernames,uesr) + for _, uesr := range uesrs { + if uesr != "" { + Info.Usernames = append(Info.Usernames, uesr) } } - for name := range Userdict{ + for name := range Userdict { Userdict[name] = Info.Usernames } } - if Info.Userfile!=""{ - uesrs,err := Readfile(Info.Userfile) + if Info.Userfile != "" { + uesrs, err := Readfile(Info.Userfile) if err == nil { for _, uesr := range uesrs { if uesr != "" { @@ -46,22 +44,22 @@ func ParseUser(Info *HostInfo){ } -func ParsePass(Info *HostInfo){ - if Info.Password!=""{ +func ParsePass(Info *HostInfo) { + if Info.Password != "" { passs := strings.Split(Info.Password, ",") - for _,pass := range passs{ - if pass !=""{ - Info.Passwords = append(Info.Passwords,pass) + for _, pass := range passs { + if pass != "" { + Info.Passwords = append(Info.Passwords, pass) } } Passwords = Info.Passwords } - if Info.Passfile!=""{ - passs,err := Readfile(Info.Passfile) - if err == nil{ - for _,pass := range passs{ - if pass !=""{ - Info.Passwords = append(Info.Passwords,pass) + if Info.Passfile != "" { + passs, err := Readfile(Info.Passfile) + if err == nil { + for _, pass := range passs { + if pass != "" { + Info.Passwords = append(Info.Passwords, pass) } } Passwords = Info.Passwords @@ -70,10 +68,10 @@ func ParsePass(Info *HostInfo){ } } -func Readfile(filename string)([]string,error){ +func Readfile(filename string) ([]string, error) { file, err := os.Open(filename) - if err!=nil{ - fmt.Println("Open %s error, %v", filename,err) + if err != nil { + fmt.Println("Open %s error, %v", filename, err) os.Exit(0) } defer file.Close() @@ -83,52 +81,49 @@ func Readfile(filename string)([]string,error){ for scanner.Scan() { text := strings.TrimSpace(scanner.Text()) if text != "" { - content=append(content,scanner.Text()) + content = append(content, scanner.Text()) } } - return content,nil + return content, nil } - - -func ParseInput(Info *HostInfo){ - if Info.Host=="" && Info.HostFile ==""{ +func ParseInput(Info *HostInfo) { + if Info.Host == "" && Info.HostFile == "" { fmt.Println("Host is none") flag.Usage() os.Exit(0) } - if Info.Outputfile != ""{ + if Info.Outputfile != "" { Outputfile = Info.Outputfile } - if Info.IsSave == true{ + if Info.IsSave == true { IsSave = false } } -func ParseScantype(Info *HostInfo){ - _,ok:=PORTList[Info.Scantype] - if !ok{ +func ParseScantype(Info *HostInfo) { + _, ok := PORTList[Info.Scantype] + if !ok { fmt.Println("The specified scan type does not exist") fmt.Println("-m") - for name,_:=range PORTList{ - fmt.Println(" ["+name+"]") + for name, _ := range PORTList { + fmt.Println(" [" + name + "]") } os.Exit(0) } - if Info.Scantype != "all" && Info.Ports != DefaultPorts{ + if Info.Scantype != "all" && Info.Ports != DefaultPorts { ScanPort := ParsePort(Info.Ports)[0] Info.Ports = strconv.Itoa(ScanPort) - fmt.Println("if -m and -p only scan the first port:",Info.Ports) - for name,_:=range PORTList{ + fmt.Println("if -m and -p only scan the first port:", Info.Ports) + for name, _ := range PORTList { PORTList[name] = ScanPort } } } - -func CheckErr(text string,err error){ - if err!=nil{ - fmt.Println(text,err.Error()) +func CheckErr(text string, err error) { + if err != nil { + fmt.Println(text, err.Error()) os.Exit(0) } -} \ No newline at end of file +} diff --git a/common/log.go b/common/log.go index 8033a7b..9f0b8db 100644 --- a/common/log.go +++ b/common/log.go @@ -1,33 +1,45 @@ package common import ( - "fmt" "os" "sync" + + log "github.com/sirupsen/logrus" ) -func LogSuccess(result string){ +func init() { + log.SetFormatter(&log.TextFormatter{ + DisableColors: false, + FullTimestamp: true}) +} + +func LogPrint(args ...interface{}) { + log.Println(args...) +} + +func LogSuccess(result string) { mutex := &sync.Mutex{} mutex.Lock() - fmt.Println(result) + log.Println(result) if IsSave { - WriteFile(result,Outputfile) + WriteFile(result, Outputfile) } mutex.Unlock() } -func WriteFile(result string,filename string) { - var text = []byte(result+"\n") +func WriteFile(result string, filename string) { + var text = []byte(result + "\n") fl, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0777) if err != nil { - fmt.Println(err) + log.Println("open file error : ", err) return } defer fl.Close() _, err = fl.Write(text) - if err!= nil{ - fmt.Println(err) + if err != nil { + log.Println("write file error ", err) } } + //err := ioutil.WriteFile(filename, text, 0666) //if err!= nil{ // fmt.Println(err) @@ -47,4 +59,4 @@ func WriteFile(result string,filename string) { // exist = false // } // return exist -//} \ No newline at end of file +//} diff --git a/go.mod b/go.mod index 26b5daf..48cf240 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/go-sql-driver/mysql v1.5.0 github.com/jlaffaye/ftp v0.0.0-20201112195030-9aae4d151126 github.com/lib/pq v1.8.0 + github.com/sirupsen/logrus v1.7.0 github.com/stacktitan/smb v0.0.0-20190531122847-da9a425dceb8 golang.org/x/crypto v0.0.0-20201116153603-4be66e5b6582 ) diff --git a/go.sum b/go.sum index 970e6d6..c502dc4 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/denisenkom/go-mssqldb v0.9.0 h1:RSohk2RsiZqLZ0zCjtfn3S4Gp4exhpBWHyQ7D0yGjAk= github.com/denisenkom/go-mssqldb v0.9.0/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= @@ -9,10 +11,15 @@ github.com/jlaffaye/ftp v0.0.0-20201112195030-9aae4d151126 h1:ly2C51IMpCCV8RpTDR github.com/jlaffaye/ftp v0.0.0-20201112195030-9aae4d151126/go.mod h1:2lmrmq866uF2tnje75wQHzmPXhmSWUt7Gyx2vgK1RCU= github.com/lib/pq v1.8.0 h1:9xohqzkUwzR4Ga4ivdTcawVS89YSDVxXMa3xJX3cGzg= github.com/lib/pq v1.8.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM= +github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/stacktitan/smb v0.0.0-20190531122847-da9a425dceb8 h1:GVFkBBJAEO3CpzIYcDDBdpUObzKwVW9okNWcLYL/nnU= github.com/stacktitan/smb v0.0.0-20190531122847-da9a425dceb8/go.mod h1:phLSETqH/UJsBtwDVBxSfJKwwkbJcGyy2Q/h4k+bmww= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= @@ -20,8 +27,10 @@ golang.org/x/crypto v0.0.0-20201116153603-4be66e5b6582 h1:0WDrJ1E7UolDk1KhTXxxw3 golang.org/x/crypto v0.0.0-20201116153603-4be66e5b6582/go.mod h1:tCqSYrHVcf3i63Co2FzBkTCo2gdF6Zak62921dSfraU= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.0.0-20201113234701-d7a72108b828/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 9576d1a..f286422 100644 --- a/main.go +++ b/main.go @@ -1,20 +1,14 @@ package main import ( - "github.com/shadow1ng/fscan/common" "github.com/shadow1ng/fscan/Plugins" - "fmt" + "github.com/shadow1ng/fscan/common" ) - func main() { var Info common.HostInfo - common.Flag(&Info) //fmt.Println(Info.Host,Info.Ports) + common.Flag(&Info) //fmt.Println(Info.Host,Info.Ports) common.Parse(&Info) Plugins.Scan(Info) - fmt.Println("scan end") + common.LogPrint("scan end .") } - - - -