From a38194a7fdf57073735ebe3df64f5354c1229e3b Mon Sep 17 00:00:00 2001 From: Jeff Cody Date: Wed, 21 Aug 2019 14:53:56 -0400 Subject: [PATCH] Add `port` to ScanTarget{} that overrides Config The port field is tied to the configuration of each instance of `Scanner` struct. However, applications using zgrab2 scan modules may want to specify specific ports to scan, without needing to initialize a whole new module. This patch adds a pointer to a uint describing a port to `ScanTarget{}`. If that is nil, the specified port will override the port in the Config. --- processing.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/processing.go b/processing.go index 64b6c40..30859fc 100644 --- a/processing.go +++ b/processing.go @@ -22,6 +22,7 @@ type ScanTarget struct { IP net.IP Domain string Tag string + Port *uint } func (target ScanTarget) String() string { @@ -56,7 +57,15 @@ func (target *ScanTarget) Host() string { // Open connects to the ScanTarget using the configured flags, and returns a net.Conn that uses the configured timeouts for Read/Write operations. func (target *ScanTarget) Open(flags *BaseFlags) (net.Conn, error) { - address := net.JoinHostPort(target.Host(), fmt.Sprintf("%d", flags.Port)) + var port uint + // If the port is supplied in ScanTarget, let that override the cmdline option + if target.Port != nil { + port = *target.Port + } else { + port = flags.Port + } + + address := net.JoinHostPort(target.Host(), fmt.Sprintf("%d", port)) return DialTimeoutConnection("tcp", address, flags.Timeout, flags.BytesReadLimit) } @@ -75,7 +84,14 @@ func (target *ScanTarget) OpenTLS(baseFlags *BaseFlags, tlsFlags *TLSFlags) (*TL // OpenUDP connects to the ScanTarget using the configured flags, and returns a net.Conn that uses the configured timeouts for Read/Write operations. // Note that the UDP "connection" does not have an associated timeout. func (target *ScanTarget) OpenUDP(flags *BaseFlags, udp *UDPFlags) (net.Conn, error) { - address := net.JoinHostPort(target.Host(), fmt.Sprintf("%d", flags.Port)) + var port uint + // If the port is supplied in ScanTarget, let that override the cmdline option + if target.Port != nil { + port = *target.Port + } else { + port = flags.Port + } + address := net.JoinHostPort(target.Host(), fmt.Sprintf("%d", port)) var local *net.UDPAddr if udp != nil && (udp.LocalAddress != "" || udp.LocalPort != 0) { local = &net.UDPAddr{}