Skip to content

Commit

Permalink
simple hack to accept multiple comma separated tags
Browse files Browse the repository at this point in the history
  • Loading branch information
RicYaben committed Jul 23, 2024
1 parent ab1c011 commit fd887a6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 38 deletions.
8 changes: 4 additions & 4 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ func GetTargetsCSV(source io.Reader, ch chan<- ScanTarget) error {
// 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}
ch <- ScanTarget{IP: duplicateIP(ip), Domain: domain, Tags: tag}
} else {
ch <- ScanTarget{IP: duplicateIP(ip), Domain: domain, Tag: tag, Port: &port_uint}
ch <- ScanTarget{IP: duplicateIP(ip), Domain: domain, Tags: tag, Port: &port_uint}
}
}
continue
Expand All @@ -139,9 +139,9 @@ func GetTargetsCSV(source io.Reader, ch chan<- ScanTarget) error {
}
}
if port == "" {
ch <- ScanTarget{IP: ip, Domain: domain, Tag: tag}
ch <- ScanTarget{IP: ip, Domain: domain, Tags: tag}
} else {
ch <- ScanTarget{IP: ip, Domain: domain, Tag: tag, Port: &port_uint}
ch <- ScanTarget{IP: ip, Domain: domain, Tags: tag, Port: &port_uint}
}
}
return nil
Expand Down
14 changes: 7 additions & 7 deletions input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,16 @@ example.com
`
port := uint(443)
expected := []ScanTarget{
ScanTarget{IP: net.ParseIP("10.0.0.1"), Domain: "example.com", Tag: "tag"},
ScanTarget{IP: net.ParseIP("10.0.0.1"), Domain: "example.com", Tags: "tag"},
ScanTarget{IP: net.ParseIP("10.0.0.1"), Domain: "example.com"},
ScanTarget{IP: net.ParseIP("10.0.0.1")},
ScanTarget{Domain: "example.com"},
ScanTarget{Domain: "example.com"},
ScanTarget{IP: net.ParseIP("2.2.2.0"), Tag: "tag"},
ScanTarget{IP: net.ParseIP("2.2.2.1"), Tag: "tag"},
ScanTarget{IP: net.ParseIP("2.2.2.2"), Tag: "tag"},
ScanTarget{IP: net.ParseIP("2.2.2.3"), Tag: "tag"},
ScanTarget{IP: net.ParseIP("10.0.0.1"), Domain: "example.com", Tag: "tag", Port: &port},
ScanTarget{IP: net.ParseIP("2.2.2.0"), Tags: "tag"},
ScanTarget{IP: net.ParseIP("2.2.2.1"), Tags: "tag"},
ScanTarget{IP: net.ParseIP("2.2.2.2"), Tags: "tag"},
ScanTarget{IP: net.ParseIP("2.2.2.3"), Tags: "tag"},
ScanTarget{IP: net.ParseIP("10.0.0.1"), Domain: "example.com", Tags: "tag", Port: &port},
ScanTarget{IP: net.ParseIP("10.0.0.1"), Port: &port},
}

Expand All @@ -208,7 +208,7 @@ example.com
for i := range expected {
if res[i].IP.String() != expected[i].IP.String() ||
res[i].Domain != expected[i].Domain ||
res[i].Tag != expected[i].Tag {
res[i].Tags != expected[i].Tags {
t.Errorf("wrong data in ScanTarget %d (got %v; expected %v)", i, res[i], expected[i])
}
}
Expand Down
1 change: 0 additions & 1 deletion output.json

This file was deleted.

65 changes: 39 additions & 26 deletions processing.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package zgrab2

import (
"context"
"encoding/json"
"fmt"
"net"
"slices"
"strings"
"sync"

log "github.com/sirupsen/logrus"
Expand All @@ -22,7 +25,7 @@ type Grab struct {
type ScanTarget struct {
IP net.IP
Domain string
Tag string
Tags string
Port *uint
}

Expand All @@ -38,8 +41,8 @@ func (target ScanTarget) String() string {
} else {
res = target.Domain
}
if target.Tag != "" {
res += " tag:" + target.Tag
if target.Tags != "" {
res += " tag:" + target.Tags
}
return res
}
Expand Down Expand Up @@ -85,13 +88,8 @@ 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) {
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
}
var port uint = *target.Port | flags.Port

address := net.JoinHostPort(target.Host(), fmt.Sprintf("%d", port))
var local *net.UDPAddr
if udp != nil && (udp.LocalAddress != "" || udp.LocalPort != 0) {
Expand All @@ -103,6 +101,7 @@ func (target *ScanTarget) OpenUDP(flags *BaseFlags, udp *UDPFlags) (net.Conn, er
local.Port = int(udp.LocalPort)
}
}

remote, err := net.ResolveUDPAddr("udp", address)
if err != nil {
return nil, err
Expand All @@ -111,7 +110,7 @@ func (target *ScanTarget) OpenUDP(flags *BaseFlags, udp *UDPFlags) (net.Conn, er
if err != nil {
return nil, err
}
return NewTimeoutConnection(nil, conn, flags.Timeout, 0, 0, flags.BytesReadLimit), nil
return NewTimeoutConnection(context.Background(), conn, flags.Timeout, 0, 0, flags.BytesReadLimit), nil
}

// BuildGrabFromInputResponse constructs a Grab object for a target, given the
Expand All @@ -135,34 +134,48 @@ func BuildGrabFromInputResponse(t *ScanTarget, responses map[string]ScanResponse

// EncodeGrab serializes a Grab to JSON, handling the debug fields if necessary.
func EncodeGrab(raw *Grab, includeDebug bool) ([]byte, error) {
var outputData interface{}
if includeDebug {
outputData = raw
} else {
// If the caller doesn't explicitly request debug data, strip it out.
// TODO: Migrate this to the ZMap fork of sheriff, once it's more
// stable.
processor := output.Processor{Verbose: false}
stripped, err := processor.Process(raw)
if err != nil {
log.Debugf("Error processing results: %v", err)
stripped = raw
}
outputData = stripped
return json.Marshal(raw)
}
return json.Marshal(outputData)
// If the caller doesn't explicitly request debug data, strip it out.
// TODO: Migrate this to the ZMap fork of sheriff, once it's more
// stable.
processor := output.Processor{Verbose: false}
stripped, err := processor.Process(raw)
if err != nil {
log.Debugf("Error processing results: %v", err)
stripped = raw
}
return json.Marshal(stripped)
}

func parseTags(rawTags string, delimiter string) []string {
if len(rawTags) == 0 {
return []string{}
}

if len(delimiter) == 0 {
delimiter = string(rune(','))
}

return strings.Split(rawTags, delimiter)
}

// grabTarget calls handler for each action
func grabTarget(input ScanTarget, m *Monitor) []byte {
moduleResult := make(map[string]ScanResponse)

// TODO: we are choosing the delimiter here. This should be a flag
tags := parseTags(input.Tags, ",")

for _, scannerName := range orderedScanners {
scanner := scanners[scannerName]
trigger := (*scanner).GetTrigger()
if input.Tag != trigger {

if !slices.Contains(tags, trigger) {
continue
}

defer func(name string) {
if e := recover(); e != nil {
log.Errorf("Panic on scanner %s when scanning target %s: %#v", scannerName, input.String(), e)
Expand Down

0 comments on commit fd887a6

Please sign in to comment.