fscan/Plugins/portscan.go

67 lines
1.3 KiB
Go
Raw Normal View History

2020-12-29 01:17:10 -08:00
package Plugins
import (
"fmt"
"github.com/shadow1ng/fscan/common"
"net"
"strconv"
"sync"
"time"
)
2021-03-07 18:00:56 -08:00
type Addr struct {
ip string
port int
2020-12-29 01:17:10 -08:00
}
2021-03-07 18:00:56 -08:00
func PortScan(hostslist []string, ports string, timeout int64) []string {
var AliveAddress []string
probePorts := common.ParsePort(ports)
workers := common.Threads
Addrs := make(chan Addr)
2020-12-29 01:17:10 -08:00
results := make(chan string)
2021-03-07 18:00:56 -08:00
var wg sync.WaitGroup
2020-12-29 01:17:10 -08:00
2021-03-07 18:00:56 -08:00
//接收结果
go func() {
for found := range results {
AliveAddress = append(AliveAddress, found)
2020-12-29 01:17:10 -08:00
}
2021-03-07 18:00:56 -08:00
}()
2020-12-29 01:17:10 -08:00
2021-03-07 18:00:56 -08:00
//多线程扫描
for i := 0; i < workers; i++ {
go func() {
for addr := range Addrs {
PortConnect(addr, results, timeout)
wg.Done()
}
}()
2020-12-29 01:17:10 -08:00
}
2021-03-07 18:00:56 -08:00
//添加扫描目标
2020-12-29 01:17:10 -08:00
for _, host := range hostslist {
2021-03-07 18:00:56 -08:00
for _, port := range probePorts {
Addrs <- Addr{host, port}
wg.Add(1)
}
2020-12-29 01:17:10 -08:00
}
2021-03-07 18:00:56 -08:00
2020-12-29 01:17:10 -08:00
wg.Wait()
2021-03-07 18:00:56 -08:00
close(Addrs)
close(results)
2020-12-29 01:17:10 -08:00
return AliveAddress
}
2021-03-07 18:00:56 -08:00
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
}
}