-
Notifications
You must be signed in to change notification settings - Fork 307
/
input.go
154 lines (144 loc) · 3.88 KB
/
input.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package zgrab2
import (
"encoding/csv"
"fmt"
"io"
"net"
"strconv"
"strings"
log "github.com/sirupsen/logrus"
)
// ParseCSVTarget takes a record from a CSV-format input file and
// returns the specified ipnet, domain, tag and port or an error.
//
// ZGrab2 input files have four fields:
//
// IP, DOMAIN, TAG, PORT
//
// Each line specifies a target to scan by its IP address, domain
// name or both, as well as an optional tag used to determine which
// scanners will be invoked.
//
// Port number has been added to the end of the line for compatibility reasons.
// A CIDR block may be provided in the IP field, in which case the
// framework expands the record into targets for every address in the
// block.
//
// Trailing empty fields may be omitted.
// Comment lines begin with #, and empty lines are ignored.
func ParseCSVTarget(fields []string) (ipnet *net.IPNet, domain string, tag string, port string, err error) {
for i := range fields {
fields[i] = strings.TrimSpace(fields[i])
}
if len(fields) > 0 && fields[0] != "" {
if ip := net.ParseIP(fields[0]); ip != nil {
ipnet = &net.IPNet{IP: ip}
} else if _, cidr, er := net.ParseCIDR(fields[0]); er == nil {
ipnet = cidr
} else if len(fields) != 1 {
err = fmt.Errorf("can't parse %q as an IP address or CIDR block", fields[0])
return
}
}
if len(fields) > 1 {
domain = fields[1]
}
if len(fields) > 2 {
tag = fields[2]
}
// Use string for port to allow empty port
if len(fields) > 3 {
port = fields[3]
}
if len(fields) > 4 {
err = fmt.Errorf("too many fields: %q", fields)
return
}
// For legacy reasons, we also allow targets of the form:
// DOMAIN
if ipnet == nil && len(fields) == 1 {
domain = fields[0]
}
if ipnet == nil && domain == "" {
err = fmt.Errorf("record doesn't specify an address, network, or domain: %v", fields)
return
}
return
}
func incrementIP(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}
func duplicateIP(ip net.IP) net.IP {
dup := make(net.IP, len(ip))
copy(dup, ip)
return dup
}
// InputTargetsCSV is an InputTargetsFunc that calls GetTargetsCSV with
// the CSV file provided on the command line.
func InputTargetsCSV(ch chan<- ScanTarget) error {
return GetTargetsCSV(config.inputFile, ch)
}
// GetTargetsCSV reads targets from a CSV source, generates ScanTargets,
// and delivers them to the provided channel.
func GetTargetsCSV(source io.Reader, ch chan<- ScanTarget) error {
csvreader := csv.NewReader(source)
csvreader.Comment = '#'
csvreader.FieldsPerRecord = -1 // variable
for {
fields, err := csvreader.Read()
if err == io.EOF {
break
} else if err != nil {
return err
}
if len(fields) == 0 {
continue
}
ipnet, domain, tag, port, err := ParseCSVTarget(fields)
if err != nil {
log.Errorf("parse error, skipping: %v", err)
continue
}
var ip net.IP
var port_uint uint
if port != "" {
port_int, err := strconv.Atoi(port)
port_uint = uint(port_int)
if err != nil {
log.Errorf("parse error, skipping: %v", err)
continue
}
}
if ipnet != nil {
if ipnet.Mask != nil {
// expand CIDR block into one target for each IP
for ip = ipnet.IP.Mask(ipnet.Mask); ipnet.Contains(ip); incrementIP(ip) {
if port == "" {
ch <- ScanTarget{IP: duplicateIP(ip), Domain: domain, Tag: tag}
} else {
ch <- ScanTarget{IP: duplicateIP(ip), Domain: domain, Tag: tag, Port: &port_uint}
}
}
continue
} else {
ip = ipnet.IP
}
}
if port == "" {
ch <- ScanTarget{IP: ip, Domain: domain, Tag: tag}
} else {
ch <- ScanTarget{IP: ip, Domain: domain, Tag: tag, Port: &port_uint}
}
}
return nil
}
// InputTargetsFunc is a function type for target input functions.
//
// A function of this type generates ScanTargets on the provided
// channel. It returns nil if there are no further inputs or error.
type InputTargetsFunc func(ch chan<- ScanTarget) error