调整portscan结构

This commit is contained in:
shadow1ng 2021-03-08 10:00:56 +08:00
parent 5b330bb12d
commit 3ca56ff222
3 changed files with 47 additions and 82 deletions

View File

@ -9,93 +9,58 @@ import (
"time" "time"
) )
func ProbeHosts(host string, ports <-chan int, respondingHosts chan<- string, done chan<- bool, adjustedTimeout int64) { type Addr struct {
for port := range ports { ip string
con, err := net.DialTimeout("tcp4", fmt.Sprintf("%s:%d", host, port), time.Duration(adjustedTimeout)*time.Second) port int
if err == nil {
con.Close()
address := host + ":" + strconv.Itoa(port)
result := fmt.Sprintf("%s open", address)
common.LogSuccess(result)
respondingHosts <- address
}
}
done <- true
} }
func ScanAllports(address string, probePorts []int, threads int, adjustedTimeout int64) ([]string, error) { func PortScan(hostslist []string, ports string, timeout int64) []string {
ports := make(chan int, 20)
results := make(chan string)
done := make(chan bool, threads)
for worker := 0; worker < threads; worker++ {
go ProbeHosts(address, ports, results, done, adjustedTimeout)
}
for _, port := range probePorts {
ports <- port
}
close(ports)
var responses = []string{}
for {
select {
case found := <-results:
responses = append(responses, found)
case <-done:
threads--
if threads == 0 {
return responses, nil
}
}
}
}
func TCPportScan(hostslist []string, ports string, timeout int64) []string {
var AliveAddress []string var AliveAddress []string
probePorts := common.ParsePort(ports) probePorts := common.ParsePort(ports)
lm := 20 workers := common.Threads
if len(hostslist) > 5 && len(hostslist) <= 50 { Addrs := make(chan Addr)
lm = 40 results := make(chan string)
} else if len(hostslist) > 50 && len(hostslist) <= 100 {
lm = 50
} else if len(hostslist) > 100 && len(hostslist) <= 150 {
lm = 60
} else if len(hostslist) > 150 && len(hostslist) <= 200 {
lm = 70
} else if len(hostslist) > 200 {
lm = 75
}
thread := 10
if len(probePorts) > 500 && len(probePorts) <= 4000 {
thread = len(probePorts) / 100
} else if len(probePorts) > 4000 && len(probePorts) <= 6000 {
thread = len(probePorts) / 200
} else if len(probePorts) > 6000 && len(probePorts) <= 10000 {
thread = len(probePorts) / 350
} else if len(probePorts) > 10000 && len(probePorts) < 50000 {
thread = len(probePorts) / 400
} else if len(probePorts) >= 50000 && len(probePorts) <= 65535 {
thread = len(probePorts) / 500
}
var wg sync.WaitGroup var wg sync.WaitGroup
mutex := &sync.Mutex{}
limiter := make(chan struct{}, lm) //接收结果
for _, host := range hostslist { go func() {
wg.Add(1) for found := range results {
limiter <- struct{}{} AliveAddress = append(AliveAddress, found)
go func(host string) { }
defer wg.Done() }()
if aliveAdd, err := ScanAllports(host, probePorts, thread, timeout); err == nil && len(aliveAdd) > 0 {
mutex.Lock() //多线程扫描
AliveAddress = append(AliveAddress, aliveAdd...) for i := 0; i < workers; i++ {
mutex.Unlock() go func() {
for addr := range Addrs {
PortConnect(addr, results, timeout)
wg.Done()
} }
<-limiter }()
}(host)
} }
//添加扫描目标
for _, host := range hostslist {
for _, port := range probePorts {
Addrs <- Addr{host, port}
wg.Add(1)
}
}
wg.Wait() wg.Wait()
close(Addrs)
close(results)
return AliveAddress return AliveAddress
} }
func PortConnect(addr Addr, respondingHosts chan<- string, adjustedTimeout int64) {
host, port := addr.ip, addr.port
con, err := net.DialTimeout("tcp4", fmt.Sprintf("%s:%d", host, port), time.Duration(adjustedTimeout)*time.Second)
if err == nil {
con.Close()
address := host + ":" + strconv.Itoa(port)
result := fmt.Sprintf("%s open", address)
common.LogSuccess(result)
respondingHosts <- address
}
}

View File

@ -25,7 +25,7 @@ func Scan(info common.HostInfo) {
if info.Scantype == "icmp" { if info.Scantype == "icmp" {
return return
} }
AlivePorts := TCPportScan(Hosts, info.Ports, info.Timeout) AlivePorts := PortScan(Hosts, info.Ports, info.Timeout)
if info.Scantype == "portscan" { if info.Scantype == "portscan" {
return return
} }

View File

@ -27,7 +27,7 @@ func Flag(Info *HostInfo) {
flag.StringVar(&Info.Password, "pwd", "", "password") flag.StringVar(&Info.Password, "pwd", "", "password")
flag.Int64Var(&Info.Timeout, "time", 3, "Set timeout") flag.Int64Var(&Info.Timeout, "time", 3, "Set timeout")
flag.StringVar(&Info.Scantype, "m", "all", "Select scan type ,as: -m ssh") flag.StringVar(&Info.Scantype, "m", "all", "Select scan type ,as: -m ssh")
flag.IntVar(&Threads, "t", 200, "Thread nums") flag.IntVar(&Threads, "t", 600, "Thread nums")
flag.StringVar(&HostFile, "hf", "", "host file, -hs ip.txt") flag.StringVar(&HostFile, "hf", "", "host file, -hs ip.txt")
flag.StringVar(&Userfile, "userf", "", "username file") flag.StringVar(&Userfile, "userf", "", "username file")
flag.StringVar(&Passfile, "pwdf", "", "password file") flag.StringVar(&Passfile, "pwdf", "", "password file")