Skip to content

Commit 8fcd291

Browse files
committed
feat: add support for custom DNS server
resolves zmap#419
1 parent baa27dc commit 8fcd291

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

config.go

+9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Config struct {
2424
ConnectionsPerHost int `long:"connections-per-host" default:"1" description:"Number of times to connect to each host (results in more output)"`
2525
ReadLimitPerHost int `long:"read-limit-per-host" default:"96" description:"Maximum total kilobytes to read for a single host (default 96kb)"`
2626
Prometheus string `long:"prometheus" description:"Address to use for Prometheus server (e.g. localhost:8080). If empty, Prometheus is disabled."`
27+
CustomDNS string `long:"dns" description:"Address of a custom DNS server for lookups. Default port is 53."`
2728
Multiple MultipleCommand `command:"multiple" description:"Multiple module actions"`
2829
inputFile *os.File
2930
outputFile *os.File
@@ -128,6 +129,14 @@ func validateFrameworkConfiguration() {
128129
if config.ReadLimitPerHost > 0 {
129130
DefaultBytesReadLimit = config.ReadLimitPerHost * 1024
130131
}
132+
133+
// Validate custom DNS
134+
if config.CustomDNS != "" {
135+
var err error
136+
if config.CustomDNS, err = AddDefaultPortToDNSServerName(config.CustomDNS); err != nil {
137+
log.Fatalf("invalid DNS server address: %s", err)
138+
}
139+
}
131140
}
132141

133142
// GetMetaFile returns the file to which metadata should be output

conn.go

+10
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,16 @@ func (d *Dialer) SetDefaults() *Dialer {
341341
KeepAlive: d.Timeout,
342342
DualStack: true,
343343
}
344+
345+
// Use custom DNS as default if set
346+
if config.CustomDNS != "" {
347+
d.Dialer.Resolver = &net.Resolver{
348+
PreferGo: true,
349+
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
350+
return net.Dial(network, config.CustomDNS)
351+
},
352+
}
353+
}
344354
}
345355
return d
346356
}

utility.go

+29-4
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@ package zgrab2
22

33
import (
44
"errors"
5+
"fmt"
56
"net"
67
"regexp"
78
"strconv"
89
"strings"
910

1011
"time"
1112

12-
"github.com/zmap/zflags"
13-
"github.com/sirupsen/logrus"
1413
"runtime/debug"
14+
15+
"github.com/sirupsen/logrus"
16+
flags "github.com/zmap/zflags"
1517
)
1618

1719
var parser *flags.Parser
@@ -214,8 +216,9 @@ func IsTimeoutError(err error) bool {
214216
// doing anything. Otherwise, it logs the stacktrace, the panic error, and the provided message
215217
// before re-raising the original panic.
216218
// Example:
217-
// defer zgrab2.LogPanic("Error decoding body '%x'", body)
218-
func LogPanic(format string, args...interface{}) {
219+
//
220+
// defer zgrab2.LogPanic("Error decoding body '%x'", body)
221+
func LogPanic(format string, args ...interface{}) {
219222
err := recover()
220223
if err == nil {
221224
return
@@ -224,3 +227,25 @@ func LogPanic(format string, args...interface{}) {
224227
logrus.Errorf(format, args...)
225228
panic(err)
226229
}
230+
231+
func AddDefaultPortToDNSServerName(inAddr string) (string, error) {
232+
// Try to split host and port to see if the port is already specified.
233+
host, port, err := net.SplitHostPort(inAddr)
234+
if err != nil {
235+
// might mean there's no port specified
236+
host = inAddr
237+
}
238+
239+
// Validate the host part as an IP address.
240+
ip := net.ParseIP(host)
241+
if ip == nil {
242+
return "", fmt.Errorf("invalid IP address")
243+
}
244+
245+
// If the original input does not have a port, specify port 53
246+
if port == "" {
247+
port = "53"
248+
}
249+
250+
return net.JoinHostPort(ip.String(), port), nil
251+
}

0 commit comments

Comments
 (0)