zgrab2/processing.go
justinbastress a244ec15e6 TLS scan prototype + Travis integration (#25)
* Fix typo

* Actually call init per sender for each goroutine

* split out TLS handling

* Rename tls_handshake; update docs

* fix comments

* format fixes

* merge updates

* fix path

* refactor heartbleed logging (now the same as original zgrab)

* add ScanStatus, update modules to return it

* fix threaded for loop; fix styling of dict literal

* fix compile errors, note un-bubbled handshake error

* initial schema commit

* fix comment

* schema cleanup

* comments

* fix TODOs

* first attempt at docker integration in travis; also add schema validation

* add integration_tests.sh

* need sudo?

* try pip install --user

* revert regression

* add docker service again

* chmod +x integration_tests.sh

* fix path of binary

* Dump output file to stdout

* travis work

* use jp's build-self-contained script

* use go get/go build to get jp

* fix jp path

* switch from bogus regex to wildcard

* do all mysql versions; fix version comparison

* re-enable notifications; fix successful version check log message; comment TryGetScanStatus

* move to conf.d layout for integration tests

* update README

* add missing scripts

* add ./ to path
2017-12-15 09:25:17 -05:00

134 lines
2.9 KiB
Go

package zgrab2
import (
"bufio"
"encoding/json"
"io"
"net"
"strings"
"sync"
log "github.com/sirupsen/logrus"
)
// Grab contains all scan responses for a single host
type Grab struct {
IP string `json:"ip,omitempty"`
Domain string `json:"domain,omitempty"`
Data map[string]ScanResponse `json:"data,omitempty"`
}
// ScanTarget is the host that will be scanned
type ScanTarget struct {
IP net.IP
Domain string
}
// grabTarget calls handler for each action
func grabTarget(input ScanTarget, m *Monitor) []byte {
moduleResult := make(map[string]ScanResponse)
for _, scannerName := range orderedScanners {
scanner := scanners[scannerName]
name, res := RunScanner(*scanner, m, input)
moduleResult[name] = res
if res.Error != nil && !config.Multiple.ContinueOnError {
break
}
}
var ipstr string
if input.IP == nil {
ipstr = ""
} else {
s := input.IP.String()
ipstr = s
}
a := Grab{IP: ipstr, Domain: input.Domain, Data: moduleResult}
result, err := json.Marshal(a)
if err != nil {
log.Fatalf("unable to marshal data: %s", err)
}
return result
}
// Process sets up an output encoder, input reader, and starts grab workers
func Process(mon *Monitor) {
workers := config.Senders
processQueue := make(chan ScanTarget, workers*4)
outputQueue := make(chan []byte, workers*4)
//Create wait groups
var workerDone sync.WaitGroup
var outputDone sync.WaitGroup
workerDone.Add(int(workers))
outputDone.Add(1)
// Start the output encoder
go func() {
out := bufio.NewWriter(config.outputFile)
defer outputDone.Done()
defer out.Flush()
for result := range outputQueue {
if _, err := out.Write(result); err != nil {
log.Fatal(err)
}
if err := out.WriteByte('\n'); err != nil {
log.Fatal(err)
}
}
}()
//Start all the workers
for i := 0; i < workers; i++ {
go func(i int) {
for _, scannerName := range orderedScanners {
scanner := *scanners[scannerName]
scanner.InitPerSender(i)
}
for obj := range processQueue {
for run := uint(0); run < uint(config.ConnectionsPerHost); run++ {
result := grabTarget(obj, mon)
outputQueue <- result
}
}
workerDone.Done()
}(i)
}
// Read the input, send to workers
input := bufio.NewReader(config.inputFile)
for {
obj, err := input.ReadBytes('\n')
if err == io.EOF {
break
} else if err != nil {
log.Error(err)
}
st := strings.TrimSpace(string(obj))
ipnet, domain, err := ParseTarget(st)
if err != nil {
log.Error(err)
continue
}
var ip net.IP
if ipnet != nil {
if ipnet.Mask != nil {
for ip = ipnet.IP.Mask(ipnet.Mask); ipnet.Contains(ip); incrementIP(ip) {
processQueue <- ScanTarget{IP: duplicateIP(ip), Domain: domain}
}
continue
} else {
ip = ipnet.IP
}
}
processQueue <- ScanTarget{IP: ip, Domain: domain}
}
close(processQueue)
workerDone.Wait()
close(outputQueue)
outputDone.Wait()
}