Dieser Artikel vermittelt Ihnen hauptsächlich die Implementierung des Port-Scanners. Ich hoffe, dass er Ihnen weiter unten hilft.
Verwenden Sie GO, um Server-Ports stapelweise zu scannen
package mainimport ( "fmt" "net" "time" "unsafe")func main() { tcpScan("127.0.0.1", 1, 65535)}func tcpScan(ip string, portStart int, portEnd int) { start := time.Now() // 参数校验 isok := verifyParam(ip, portStart, portEnd) if isok == false { fmt.Printf("[Exit]\n") } for i := portStart; i <= portEnd; i++ { address := fmt.Sprintf("%s:%d", ip, i) conn, err := net.Dial("tcp", address) if err != nil { fmt.Printf("[info] %s Close \n", address) continue } conn.Close() fmt.Printf("[info] %s Open \n", address) } cost := time.Since(start) fmt.Printf("[tcpScan] cost %s second \n", cost)}func verifyParam(ip string, portStart int, portEnd int) bool { netip := net.ParseIP(ip) if netip == nil { fmt.Println("[Error] ip type is must net.ip") return false } fmt.Printf("[Info] ip=%s | ip type is: %T | ip size is: %d \n", netip, netip, unsafe.Sizeof(netip)) if portStart < 1 || portEnd > 65535 { fmt.Println("[Error] port is must in the range of 1~65535") return false } fmt.Printf("[Info] port start:%d end:%d \n", portStart, portEnd) return true}
package mainimport ( "fmt" "net" "sync" "time" "unsafe")func main() { tcpScanByGoroutine("127.0.0.1", 1, 65535)}func tcpScanByGoroutine(ip string, portStart int, portEnd int) { start := time.Now() // 参数校验 isok := verifyParam(ip, portStart, portEnd) if isok == false { fmt.Printf("[Exit]\n") } var wg sync.WaitGroup for i := portStart; i <= portEnd; i++ { wg.Add(1) go func(j int) { defer wg.Done() address := fmt.Sprintf("%s:%d", ip, j) conn, err := net.Dial("tcp", address) if err != nil { fmt.Printf("[info] %s Close \n", address) return } conn.Close() fmt.Printf("[info] %s Open \n", address) }(i) } wg.Wait() cost := time.Since(start) fmt.Printf("[tcpScanByGoroutine] cost %s second \n", cost)}func verifyParam(ip string, portStart int, portEnd int) bool { netip := net.ParseIP(ip) if netip == nil { fmt.Println("[Error] ip type is must net.ip") return false } fmt.Printf("[Info] ip=%s | ip type is: %T | ip size is: %d \n", netip, netip, unsafe.Sizeof(netip)) if portStart < 1 || portEnd > 65535 { fmt.Println("[Error] port is must in the range of 1~65535") return false } fmt.Printf("[Info] port start:%d end:%d \n", portStart, portEnd) return true}
Das obige ist der detaillierte Inhalt vonEin Artikel, der ausführlich erklärt, wie man einen Port-Scanner in Go implementiert. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!