-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
171 lines (144 loc) · 3.76 KB
/
main.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package parser
/*
* https://www.DIVD.nl
* released under the Apache 2.0 license
* https://www.apache.org/licenses/LICENSE-2.0
*/
import (
"bufio"
"encoding/json"
"fmt"
"github.com/DIVD-NL/nuclei-parse-enrich/pkg/enricher"
"github.com/DIVD-NL/nuclei-parse-enrich/pkg/types"
"io"
"log"
"net"
"os"
"sync"
"github.com/sirupsen/logrus"
)
type Parser struct {
*json.Decoder
*os.File
Enrichment []types.EnrichInfo
SimpleIPs []types.SimpleIPRecord
ScanRecords []types.NucleiJsonRecord
MergeResults []types.MergeResult
}
func (p *Parser) NewSimpleParser(file *os.File) *Parser {
return &Parser{
File: file,
}
}
func (p *Parser) NewParser(file *os.File) *Parser {
return &Parser{
Decoder: json.NewDecoder(file),
File: file,
}
}
func (p *Parser) ProcessSimpleScan() {
scanner := bufio.NewScanner(p.File)
for scanner.Scan() {
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
var record types.NucleiJsonRecord
record.Ip = scanner.Text()
p.ScanRecords = append(p.ScanRecords, record)
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
}
func (p *Parser) ProcessNucleiScan() {
logrus.Debug("parser: ProcessNucleiScan - started parsing: ", p.File.Name())
for {
var record types.NucleiJsonRecord
err := p.Decode(&record)
if err != nil {
if err == io.EOF {
break
}
logrus.Debug(err)
}
p.ScanRecords = append(p.ScanRecords, record)
}
logrus.Debug("parser: ProcessNucleiScan - ended parsing ", len(p.ScanRecords), " records")
}
func (p *Parser) EnrichScanRecords() {
uniqueIPAddresses := make(map[string]struct{})
for _, record := range p.ScanRecords {
ip := net.ParseIP(record.Ip)
if ip == nil {
logrus.Debug("parser: EnrichScanRecords - skipping invalid IP address: ", record.Ip)
continue
}
if !ip.IsGlobalUnicast() {
logrus.Debug("parser: EnrichScanRecords - skipping non-global IP address: ", record.Ip)
continue
}
if ip.IsPrivate() {
logrus.Debug("parser: EnrichScanRecords - skipping private IP address: ", record.Ip)
continue
}
uniqueIPAddresses[record.Ip] = struct{}{}
}
nucleiEnricher := enricher.NewEnricher()
limitCh := make(chan bool, 8)
resultCh := make(chan types.EnrichInfo, 3)
var wg sync.WaitGroup
go func() {
for enrichResult := range resultCh {
p.Enrichment = append(p.Enrichment, enrichResult)
wg.Done()
}
}()
for ipAddr := range uniqueIPAddresses {
logrus.Debug("enriching IP: ", ipAddr)
wg.Add(1)
ipAddr := ipAddr
limitCh <- true
go func() {
wg.Add(1) // gets marked as Done in resultCh loop
resultCh <- nucleiEnricher.EnrichIP(ipAddr)
<-limitCh
wg.Done()
}()
}
wg.Wait()
close(resultCh)
close(limitCh)
}
func (p *Parser) MergeScanEnrichment() {
logrus.Debug("parser: MergeScanEnrichment - start")
var mergeResult = types.MergeResult{}
if len(p.Enrichment) < 1 {
logrus.Debug("Length of ips in scan is ", len(p.ScanRecords))
logrus.Fatal("No enrichment info to merge")
}
for _, record := range p.ScanRecords {
for _, enrichment := range p.Enrichment {
if record.Ip == enrichment.Ip {
mergeResult.EnrichInfo = enrichment
mergeResult.NucleiJsonRecord = record
p.MergeResults = append(p.MergeResults, mergeResult)
}
}
}
logrus.Debug("parser: MergeScanEnrichment - merged ", len(p.MergeResults), " records")
}
func (p *Parser) WriteOutput(outputFile *os.File) error {
// TODO: don't drop JSON index
mergeResultsMap := make(map[string]types.MergeResult)
for _, mergeResult := range p.MergeResults {
mergeResultsMap[mergeResult.NucleiJsonRecord.Ip] = mergeResult
}
encoder := json.NewEncoder(outputFile)
encoder.SetIndent("", " ")
err := encoder.Encode(mergeResultsMap)
if err != nil {
return fmt.Errorf("error writing output: %v", err)
}
logrus.Debug("parser: WriteOutput - ended")
return nil
}