/
provider.go
282 lines (232 loc) · 5.26 KB
/
provider.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package kraaler
import (
"bufio"
"compress/gzip"
"encoding/json"
"fmt"
"net"
"net/http"
"net/url"
"os"
"sort"
"strconv"
"strings"
"sync"
"time"
"go.uber.org/zap"
)
type URLProvider interface {
UrlsC() <-chan *url.URL
}
type URLChanProvider struct {
C <-chan *url.URL
}
func (ucp URLChanProvider) UrlsC() <-chan *url.URL {
return ucp.C
}
type DomainFileProvider struct {
path string
c DomainFileProviderConfig
urls chan *url.URL
stop chan struct{}
once sync.Once
}
type DomainFileProviderConfig struct {
Logger *zap.Logger
Timeout time.Duration
Targets map[int]func(string) string
}
func NewDomainFileProvider(path string, conf *DomainFileProviderConfig) (*DomainFileProvider, error) {
var c DomainFileProviderConfig
if conf != nil {
c = *conf
}
if _, err := os.Stat(path); err != nil {
return nil, err
}
if c.Logger == nil {
c.Logger = zap.L()
}
if c.Timeout == 0 {
c.Timeout = 5 * time.Second
}
if c.Targets == nil {
c.Targets = map[int]func(string) string{
80: func(s string) string { return fmt.Sprintf("http://%s", s) },
443: func(s string) string { return fmt.Sprintf("https://%s", s) },
}
}
return &DomainFileProvider{
path: path,
c: c,
urls: make(chan *url.URL),
stop: make(chan struct{}),
}, nil
}
func (dfp *DomainFileProvider) UrlsC() <-chan *url.URL {
dfp.once.Do(func() {
openport := func(addr string, p int) bool {
endpoint := fmt.Sprintf("%s:%d", addr, p)
conn, err := net.DialTimeout("tcp", endpoint, dfp.c.Timeout)
if err != nil {
return false
}
conn.Close()
return true
}
go func() {
formatTxt := func(s string) string {
return strings.ToLower(strings.TrimSpace(s))
}
file, err := os.Open(dfp.path)
if err != nil {
return
}
defer file.Close()
defer close(dfp.urls)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
addr := formatTxt(scanner.Text())
for port, fmter := range dfp.c.Targets {
if openport(addr, port) {
foundUrl := fmter(addr)
u, _ := url.Parse(foundUrl)
dfp.c.Logger.Info("found_web_server",
zap.String("url", foundUrl),
)
select {
case dfp.urls <- u:
case <-dfp.stop:
return
}
}
}
}
if err := scanner.Err(); err != nil {
return
}
}()
})
return dfp.urls
}
func (dfp *DomainFileProvider) Close() {
close(dfp.stop)
}
type phishTankEntry struct {
ID int
RawID string `json:"phish_id"`
Url string `json:"url"`
SubmissionTime time.Time `json:"submission_time"`
VerificationTime time.Time `json:"verification_time"`
Online string `json:"online"`
Target string `json:"target"`
}
type phishTankEntryList []phishTankEntry
func (el phishTankEntryList) Len() int { return len(el) }
func (el phishTankEntryList) Swap(i, j int) { el[i], el[j] = el[j], el[i] }
func (el phishTankEntryList) Less(i, j int) bool { return el[i].ID < el[j].ID }
type PhishTankProvider struct {
conf PhishTankProviderConfig
once sync.Once
etag string
stop chan struct{}
urls chan *url.URL
}
type PhishTankProviderConfig struct {
Endpoint string
APIKey string
TickDuration time.Duration
}
func NewPhishTankProviderWithConfig(conf PhishTankProviderConfig) *PhishTankProvider {
if conf.Endpoint == "" {
conf.Endpoint = "http://data.phishtank.com/data/online-valid.json.gz"
}
if conf.APIKey != "" {
conf.Endpoint = fmt.Sprintf("http://data.phishtank.com/data/%s/online-valid.json.gz", conf.APIKey)
}
if conf.TickDuration == 0 {
conf.TickDuration = 20 * time.Minute
}
return &PhishTankProvider{
conf: conf,
stop: make(chan struct{}),
urls: make(chan *url.URL),
}
}
func NewPhishTankProvider() *PhishTankProvider {
return NewPhishTankProviderWithConfig(PhishTankProviderConfig{})
}
func (ptr *PhishTankProvider) getEntries() ([]phishTankEntry, error) {
resp, err := http.Head(ptr.conf.Endpoint)
if err != nil {
return nil, err
}
resp.Body.Close()
etag := resp.Header.Get("Etag")
if etag == ptr.etag {
return nil, nil
}
ptr.etag = etag
resp, err = http.Get(ptr.conf.Endpoint)
if err != nil {
return nil, err
}
defer resp.Body.Close()
uncompressed, err := gzip.NewReader(resp.Body)
if err != nil {
return nil, err
}
defer uncompressed.Close()
var entries phishTankEntryList
err = json.NewDecoder(uncompressed).Decode(&entries)
if err != nil {
return nil, err
}
for i, _ := range entries {
e := entries[i]
e.ID, _ = strconv.Atoi(e.RawID)
entries[i] = e
}
sort.Sort(entries)
return entries, nil
}
func (ptr *PhishTankProvider) UrlsC() <-chan *url.URL {
ptr.once.Do(func() {
go func() {
ticker := time.NewTicker(ptr.conf.TickDuration)
defer ticker.Stop()
defer close(ptr.urls)
var newestId int
for {
entries, err := ptr.getEntries()
if err != nil {
fmt.Println(err)
}
for _, e := range entries {
if e.ID <= newestId {
continue
}
u, err := url.Parse(e.Url)
if err != nil {
continue
}
select {
case ptr.urls <- u:
newestId = e.ID
case <-ptr.stop:
return
}
}
select {
case <-ticker.C:
case <-ptr.stop:
return
}
}
}()
})
return ptr.urls
}
func (ptr *PhishTankProvider) Close() {
close(ptr.stop)
}