2017-08-01 23:13:28 +00:00
|
|
|
package zgrab2
|
2017-07-14 20:44:36 +00:00
|
|
|
|
|
|
|
import (
|
2017-08-04 20:20:32 +00:00
|
|
|
"bufio"
|
2017-07-14 20:44:36 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2017-08-04 20:20:32 +00:00
|
|
|
"net"
|
2017-07-14 20:44:36 +00:00
|
|
|
"sync"
|
2017-08-03 19:55:15 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2017-07-14 20:44:36 +00:00
|
|
|
)
|
|
|
|
|
2017-07-17 21:28:10 +00:00
|
|
|
type Grab struct {
|
2017-08-30 19:39:25 +00:00
|
|
|
IP string `json:"ip,omitempty"`
|
2017-08-09 20:13:18 +00:00
|
|
|
Domain string `json:"domain,omitempty"`
|
2017-08-30 19:39:25 +00:00
|
|
|
Data map[string]ModuleResponse `json:"data,omitempty"`
|
2017-07-14 20:44:36 +00:00
|
|
|
}
|
|
|
|
|
2017-08-30 19:39:25 +00:00
|
|
|
type target struct {
|
2017-08-04 20:20:32 +00:00
|
|
|
IP net.IP
|
|
|
|
Domain string
|
|
|
|
}
|
|
|
|
|
2017-08-30 19:39:25 +00:00
|
|
|
type ModuleResponse struct {
|
2017-08-03 19:55:15 +00:00
|
|
|
Result interface{} `json:"result,omitempty"`
|
2017-08-26 20:01:11 +00:00
|
|
|
Time string `json:"time,omitempty"`
|
2017-08-03 19:55:15 +00:00
|
|
|
Error *error `json:"error,omitempty"`
|
|
|
|
ErrorComponent string `json:"error_component,omitempty"`
|
2017-07-14 20:44:36 +00:00
|
|
|
}
|
|
|
|
|
2017-08-30 19:39:25 +00:00
|
|
|
// grabTarget calls handler for each action
|
|
|
|
func grabTarget(input target, m *Monitor) []byte {
|
|
|
|
moduleResult := make(map[string]ModuleResponse)
|
2017-08-01 23:13:28 +00:00
|
|
|
|
2017-08-30 19:47:39 +00:00
|
|
|
for _, moduleName := range orderedModules {
|
2017-08-31 21:24:05 +00:00
|
|
|
module := modules[moduleName]
|
|
|
|
name, res := RunModule(*module, m, input.IP)
|
2017-08-09 20:13:18 +00:00
|
|
|
moduleResult[name] = res
|
2017-08-09 18:56:05 +00:00
|
|
|
if res.Error != nil && !config.Multiple.ContinueOnError {
|
2017-08-01 23:13:28 +00:00
|
|
|
break
|
2017-07-14 20:44:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-30 19:39:25 +00:00
|
|
|
var ipstr string
|
|
|
|
if input.IP == nil {
|
|
|
|
ipstr = ""
|
2017-08-04 20:20:32 +00:00
|
|
|
} else {
|
|
|
|
s := input.IP.String()
|
2017-08-30 19:39:25 +00:00
|
|
|
ipstr = s
|
2017-08-04 20:20:32 +00:00
|
|
|
}
|
|
|
|
|
2017-08-09 20:13:18 +00:00
|
|
|
a := Grab{IP: ipstr, Domain: input.Domain, Data: moduleResult}
|
2017-08-04 20:20:32 +00:00
|
|
|
result, err := json.Marshal(a)
|
|
|
|
if err != nil {
|
2017-08-30 19:39:25 +00:00
|
|
|
log.Fatalf("unable to marshal data: %s", err)
|
2017-08-04 20:20:32 +00:00
|
|
|
}
|
2017-07-14 20:44:36 +00:00
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process sets up an output encoder, input reader, and starts grab workers
|
2017-08-16 16:09:20 +00:00
|
|
|
func Process(mon *Monitor) {
|
2017-08-03 19:55:15 +00:00
|
|
|
workers := config.Senders
|
2017-08-30 19:39:25 +00:00
|
|
|
processQueue := make(chan target, workers*4)
|
2017-08-16 16:09:20 +00:00
|
|
|
outputQueue := make(chan []byte, workers*4)
|
2017-07-14 20:44:36 +00:00
|
|
|
|
|
|
|
//Create wait groups
|
|
|
|
var workerDone sync.WaitGroup
|
|
|
|
var outputDone sync.WaitGroup
|
|
|
|
workerDone.Add(int(workers))
|
|
|
|
outputDone.Add(1)
|
|
|
|
|
|
|
|
// Start the output encoder
|
|
|
|
go func() {
|
2017-08-14 18:13:18 +00:00
|
|
|
out := bufio.NewWriter(config.outputFile)
|
2017-08-31 21:24:05 +00:00
|
|
|
defer outputDone.Done()
|
2017-08-14 18:13:18 +00:00
|
|
|
defer out.Flush()
|
2017-07-14 20:44:36 +00:00
|
|
|
for result := range outputQueue {
|
|
|
|
if _, err := out.Write(result); err != nil {
|
2017-08-09 18:56:05 +00:00
|
|
|
log.Fatal(err)
|
2017-07-14 20:44:36 +00:00
|
|
|
}
|
2017-08-31 21:24:05 +00:00
|
|
|
if err := out.WriteByte('\n'); err != nil {
|
2017-08-09 18:56:05 +00:00
|
|
|
log.Fatal(err)
|
2017-07-14 20:44:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
//Start all the workers
|
2017-07-17 21:28:10 +00:00
|
|
|
for i := 0; i < workers; i++ {
|
2017-07-14 20:44:36 +00:00
|
|
|
go func() {
|
|
|
|
for obj := range processQueue {
|
2017-09-04 22:44:58 +00:00
|
|
|
for run := uint(0); run < config.ConnectionsPerHost; run++ {
|
|
|
|
result := grabTarget(obj, mon)
|
|
|
|
outputQueue <- result
|
|
|
|
}
|
2017-07-14 20:44:36 +00:00
|
|
|
}
|
|
|
|
workerDone.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the input, send to workers
|
2017-08-04 20:20:32 +00:00
|
|
|
input := bufio.NewReader(config.inputFile)
|
2017-07-14 20:44:36 +00:00
|
|
|
for {
|
2017-08-04 20:20:32 +00:00
|
|
|
obj, err := input.ReadBytes('\n')
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
st := string(obj)
|
|
|
|
ipnet, domain, err := ParseInput(st[:len(st)-1]) //remove newline
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2017-08-09 18:56:05 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-09-03 03:14:33 +00:00
|
|
|
var ip net.IP
|
2017-08-14 18:13:18 +00:00
|
|
|
if ipnet != nil {
|
2017-09-03 03:14:33 +00:00
|
|
|
if ipnet.Mask != nil {
|
|
|
|
for ip = ipnet.IP.Mask(ipnet.Mask); ipnet.Contains(ip); incrementIP(ip) {
|
|
|
|
processQueue <- target{IP: duplicateIP(ip), Domain: domain}
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
ip = ipnet.IP
|
2017-08-04 20:20:32 +00:00
|
|
|
}
|
2017-07-14 20:44:36 +00:00
|
|
|
}
|
2017-09-03 03:14:33 +00:00
|
|
|
processQueue <- target{IP: ip, Domain: domain}
|
2017-07-14 20:44:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close(processQueue)
|
|
|
|
workerDone.Wait()
|
|
|
|
close(outputQueue)
|
|
|
|
outputDone.Wait()
|
|
|
|
}
|