Repository navigation
Expand file tree
/
Copy pathsubenum.go
More file actions
966 lines (854 loc) · 33.2 KB
/
Copy pathsubenum.go
File metadata and controls
966 lines (854 loc) · 33.2 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
// Package subenum embeds subenum's subdomain enumeration engine in other Go
// programs. It is the same engine the command-line tool runs: a worker pool
// over a wordlist, wildcard fingerprinting and filtering, per-second query
// pacing, retries, a reliability guard and per-outcome accounting.
//
// Scan is the simplest entry point and returns everything a finished scan
// found. Run streams results, notices and progress as they happen.
//
// Only enumerate domains you own or have permission to test. Simulate
// produces synthetic results without sending any DNS queries, which is what
// examples and tests should use.
//
// This package follows semantic versioning from v1.0.0. Until then it may
// change between minor releases; the command-line interface is stable.
package subenum
import (
"context"
"errors"
"fmt"
"math/rand/v2"
"slices"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/TMHSDigital/subenum/data"
"github.com/TMHSDigital/subenum/internal/candidates"
"github.com/TMHSDigital/subenum/internal/dns"
"github.com/TMHSDigital/subenum/internal/libhook"
"github.com/TMHSDigital/subenum/internal/permute"
"github.com/TMHSDigital/subenum/internal/scan"
"github.com/TMHSDigital/subenum/internal/validate"
"github.com/TMHSDigital/subenum/internal/wordlist"
)
// Defaults applied to a Config's zero values. They are the command line's
// defaults too; TestDefaultsMatchTheCLI keeps them in step.
const (
DefaultResolver = "8.8.8.8:53"
DefaultConcurrency = 100
DefaultTimeout = time.Second
DefaultAttempts = 1
DefaultHitRate = 15
)
// Config describes one scan. Every zero value selects the default above, so
// Config{Domain: "example.com"} is a complete configuration.
type Config struct {
// Domain is the apex to enumerate under, for example "example.com". A
// URL, a trailing dot, a port or an internationalized name are
// normalized the way the command line normalizes them.
Domain string
// Words are the subdomain labels to try, one label per entry ("www"),
// or a dotted path to enumerate deeper ("api.eu"). Blank entries, "#"
// comments, duplicates and entries too long to form a legal name are
// dropped. Nil uses DefaultWords.
Words []string
// Resolver is the DNS server every lookup goes to: "ip:port",
// "tls://host[:853]" for DNS over TLS, or "https://host/path" for DNS
// over HTTPS. Empty means DefaultResolver.
Resolver string
// ResolverPool spreads candidate lookups over several resolvers ("ip",
// "ip:port", "tls://host[:port]" or "https://host/path", as Resolver
// takes them), which is faster but less trustworthy
// than one server. Every name a pool member reports is re-checked
// against Resolver, and only Resolver's answer is reported. Preflight
// and wildcard probes always use Resolver.
ResolverPool []string
// RatePerResolver caps the DNS queries per second sent to each
// ResolverPool member; Rate still caps the total, so the stricter limit
// wins. 0 is unlimited. Ignored without a ResolverPool.
RatePerResolver int
// Concurrency is the number of workers looking up names at once.
Concurrency int
// Timeout bounds one lookup of one record type.
Timeout time.Duration
// Attempts is the number of tries per name. 1 does not retry. Only
// failures (timeouts, SERVFAIL, REFUSED) are retried, never an answer
// saying the name does not exist.
Attempts int
// Rate caps DNS queries per second on the wire across all workers,
// counting every record type, retry and wildcard probe. 0 is
// unlimited. Pace scans of resolvers you do not own.
Rate int
// Types are the record types to look up: "A", "AAAA", "CNAME", "NS",
// "MX", "TXT", "SRV" and "HTTPS". Nil looks up A and AAAA. CNAME, NS,
// SRV and HTTPS (an AliasMode target) are what populate Result.Takeover.
Types []string
// Recursive enumerates Words again under every name that is found,
// down to Depth levels.
Recursive bool
// Depth is the deepest level a recursive scan reaches. 1, the default,
// does not recurse. Each level multiplies the candidate count by
// len(Words), or by len(RecursiveWords) when it is set, so set
// MaxQueries as well.
Depth int
// FollowRecords also looks up the names under the domain that a found
// name's CNAME, MX or NS records point at, once each, unless they are
// already candidates, within MaxQueries. Their results' Sources are
// "records" (#432).
FollowRecords bool
// RecursiveWords are the labels a recursive scan tries under each name
// it finds, at depth 2 and below, cleaned like Words. Nil uses Words;
// a short list makes deep recursion affordable (#403).
RecursiveWords []string
// MaxQueries caps how many candidate names are tested. 0 is unlimited.
MaxQueries int
// MaxDuration is a time budget, counted from when Run starts but not
// including the preflight and wildcard probes sent before the first
// candidate lookup, so Run can take longer than MaxDuration. Once it
// runs out no new name is looked up, lookups already under way finish,
// and the names left are counted in Stats.Skipped with Stats.Expired
// set. 0 is unlimited.
MaxDuration time.Duration
// AutoRate keeps a scan going when its resolver starts failing: when
// more than a fifth of the last 200 lookups failed, the query rate is
// halved (from the rate reached so far when Rate is 0), up to six times
// and down to 5 per second, and raised again once lookups succeed. A
// KindNotice event with NoticeRate reports each change. Only when it
// can slow down no further does the reliability guard apply.
AutoRate bool
// RetryInconclusive looks up once more, after every candidate has been
// tried, each name whose lookup timed out, was refused or failed. The
// retry goes to Resolver. A name that then resolves is reported like any
// other, but not expanded by Recursive. Stats.Retried and
// Stats.Recovered count the pass; Stats.Inconclusive lists the names
// still without an answer, with or without it.
RetryInconclusive bool
// Sources propose extra candidates before the scan, such as
// CertificateTransparency(), WaybackMachine(), CertSpotter() and
// ZoneTransfer(): their
// names are added after Words and looked up like any other, so a stale
// name is never reported. Each is asked once; one that fails sends a KindNotice event
// with NoticeSource and the scan goes on without it. Ignored when
// Simulate is set.
Sources []Source
// TakeoverProviders adds takeover-prone services to the built-in list
// that Result.Takeover's "provider:<name>" hints come from. An entry for
// a suffix already listed replaces it, and a Name of "-" removes the
// suffix. The longest matching suffix wins.
TakeoverProviders []TakeoverProvider
// TakeoverProvidersOnly uses TakeoverProviders alone, without the
// built-in list.
TakeoverProvidersOnly bool
// Exclude lists names that are out of scope and are never queried.
// "mail.example.com" excludes one name; "*.corp.example.com" excludes
// a whole branch.
Exclude []string
// Force scans a domain whose DNS answers every name (a wildcard zone)
// instead of refusing. Results matching the wildcard fingerprint are
// still dropped and counted in Stats.WildcardFiltered.
Force bool
// NoAbort keeps scanning when more than a fifth of lookups fail.
// Without it the scan stops early and Stats.Aborted is set, because a
// resolver that is failing cannot be distinguished from a domain whose
// names do not exist. With it, Run still sends the reliability
// warning as a KindError event, but the scan finishes and Scan
// returns no error for it.
NoAbort bool
// Simulate invents results instead of sending DNS queries, for demos,
// examples and tests. HitRate and Seed apply only to simulated scans.
Simulate bool
// HitRate is the percentage of names a simulated scan reports as
// resolved, 1 to 100.
HitRate int
// Seed makes a simulated scan reproducible: the same seed and words
// give the same results. 0 picks a random seed.
Seed uint64
// Verbose logs every lookup through Logf.
Verbose bool
// Logf receives verbose lines when Verbose is set. Nil writes them to
// standard error.
Logf func(format string, args ...any)
}
// TakeoverProvider is a takeover-prone service for Config.TakeoverProviders:
// CNAME targets under Suffix (".example-cdn.net", the leading dot optional)
// belong to Name ("example-cdn": lowercase letters, digits and hyphens).
type TakeoverProvider struct {
Suffix string
Name string
}
// Record is one DNS record behind a result.
type Record struct {
Type string // "A", "AAAA", "CNAME", "NS", "MX", "TXT", "SRV" or "HTTPS"
Value string // an address, a CNAME target, a name server, "<preference> <host>" (MX), a text (TXT), "<priority> <weight> <port> <target>" (SRV) or "<priority> <target> [alpn=<ids>] [port=<n>]" (HTTPS)
}
// Result is a subdomain that resolved.
type Result struct {
// Name is the full subdomain, such as "www.example.com".
Name string
// Records are the records it resolved to. A result found through a
// CNAME carries the alias and the addresses it leads to.
Records []Record
// Takeover is a hint that a CNAME (or an AliasMode HTTPS record)
// points somewhere unclaimed:
// "dangling" (the target does not resolve), "provider:<name>" (the
// target belongs to a known hosting provider) or "dangling:<name>"
// (both); or "dangling-ns" when the name is delegated to a name server
// that does not resolve, or "dangling-srv" when an SRV target does not
// resolve. It is derived only from DNS, so it is a lead to verify by
// hand, not a finding. Empty unless Types includes "CNAME", "NS", "SRV"
// or "HTTPS".
Takeover string
// Internal names the most notable non-public scope among the
// addresses: "loopback", "unspecified", "link-local", "private"
// (RFC 1918 or fc00::/7) or "cgnat" (100.64.0.0/10). A public name
// that answers with one leaks internal addressing. Empty when every
// address is public, and always empty in a simulated scan.
Internal string
// Partial is set when a lookup of one of Types failed (timeout,
// refusal or other error) on every attempt while another type
// answered, so Records may lack records of the failed type.
Partial bool
// Sources names what proposed the name (#273): "records" alone for a
// name FollowRecords took from another result's records (#432),
// otherwise "wordlist" for Words
// (and, with Recursive, an entry below a found name), and each of
// Config.Sources that returned it. A source is named by its Name()
// string method when it has one ("ct", "wayback" and "certspotter" for
// the built-in ones), else as "Sources[i]", its index in Config.Sources. Several
// sources are listed in the order of Config.Sources, after "wordlist".
Sources []string
}
// Stats accounts for every candidate name a finished scan looked up.
type Stats struct {
// Outcomes. Each candidate name lands in exactly one of these.
Found int64 // resolved and reported
NXDomain int64 // the resolver said the name does not exist
Timeout int64 // no answer in time
Refused int64 // the resolver refused to answer
Other int64 // any other failure, such as SERVFAIL
WildcardFiltered int64 // resolved, but matched the wildcard fingerprint
QueriesSent int64 // DNS queries sent, retries included; 0 when simulated
Skipped int64 // candidates left untested because MaxQueries or MaxDuration was reached
Excluded int64 // candidates left untested because Exclude covered them
Takeover int64 // results carrying a Takeover hint
Internal int64 // results with an Internal address scope
Partial int64 // results marked Partial
// ParentChecks counts the names that failed with SERVFAIL, with NS
// among Types, whose delegation was asked of the zone's own name
// servers, and ParentQueries the queries that took; QueriesSent
// includes them. A subzone whose every name server is gone is found
// this way, as a Result with a dangling-ns Takeover hint.
ParentChecks int64
ParentQueries int64
PoolHits int64 // names a ResolverPool member reported
Confirmed int64 // of those, the ones Resolver confirmed
Unconfirmed int64 // of those, the ones Resolver said do not exist
// Aborted reports that the reliability guard stopped the scan early,
// so the results are incomplete and the negatives mean nothing.
Aborted bool
// Expired reports that MaxDuration ran out before every candidate was
// tested; Skipped counts the ones left.
Expired bool
// Wildcard reports that the domain answers every name. Only Force
// lets such a scan run.
Wildcard bool
// WildcardFingerprint is the answer the wildcard gives, learned from
// random probes. Empty when there is no wildcard.
WildcardFingerprint []Record
// Retried counts the names Config.RetryInconclusive looked up again,
// and Recovered the ones that then got an answer; the outcome counters
// above already count those under their new outcome.
Retried int64
Recovered int64
// Inconclusive lists, sorted, the names whose lookup still timed out,
// was refused or failed when the scan ended: their existence is
// unknown. Feed them to another scan to try again.
Inconclusive []string
// RateBackoffs counts how often AutoRate halved the rate, and
// FinalRate is the rate in queries per second when the scan ended
// (0 = unlimited). Both are 0 without AutoRate.
RateBackoffs int64
FinalRate int64
}
// Total returns the number of candidate names that were looked up and
// classified.
func (s Stats) Total() int64 {
return s.Found + s.NXDomain + s.Timeout + s.Refused + s.Other + s.WildcardFiltered
}
// Kind says which fields of an Event are set.
type Kind int
// Event kinds.
const (
// KindResult reports one resolved subdomain in Event.Result.
KindResult Kind = iota
// KindProgress reports how far the scan has got, in Event.Processed,
// Event.Total and Event.Found.
KindProgress
// KindNotice reports something worth telling the user, in
// Event.Message, categorized by Event.Notice.
KindNotice
// KindError reports in Event.Message a problem serious enough to stop
// the scan: the domain is out of scope, the resolver failed a
// preflight check, the domain is a wildcard zone and Force is not
// set, or the reliability guard gave up. Few or no results follow.
// The exception is the reliability guard under Config.NoAbort: its
// warning arrives as a KindError, and the scan then runs to the end.
// Event.Fatal tells the two apart.
KindError
// KindDone is always the last event, and reports Event.Stats. It
// arrives whether the scan completed, never started, was stopped by
// the reliability guard or was cancelled through the context.
KindDone
)
// String names a kind.
func (k Kind) String() string {
switch k {
case KindResult:
return "result"
case KindProgress:
return "progress"
case KindNotice:
return "notice"
case KindError:
return "error"
case KindDone:
return "done"
}
return "unknown"
}
// Notice categorizes a KindNotice event, so a consumer can act on it without
// reading the message.
type Notice int
// Notice categories.
const (
// NoticeNone is the zero value, used by events that are not notices.
NoticeNone Notice = iota
// NoticeWildcard means the domain answers every name, or that the
// wildcard check itself failed under Force.
NoticeWildcard
// NoticeCap means MaxQueries or MaxDuration was reached and candidates
// were skipped.
NoticeCap
// NoticeCeiling means a recursive scan could generate a very large
// number of queries.
NoticeCeiling
// NoticeSkip means one recursive branch was not expanded.
NoticeSkip
// NoticeRate means AutoRate changed the query rate.
NoticeRate
// NoticeSource means a Config.Sources lookup failed; the scan went on
// without that source's names.
NoticeSource
)
// String names a notice category.
func (n Notice) String() string {
switch n {
case NoticeWildcard:
return "wildcard"
case NoticeCap:
return "cap"
case NoticeCeiling:
return "ceiling"
case NoticeSkip:
return "skip"
case NoticeRate:
return "rate"
case NoticeSource:
return "source"
}
return "none"
}
// Event is one update from a running scan.
type Event struct {
Kind Kind
// Result is the subdomain found, on KindResult.
Result Result
// Notice categorizes a KindNotice event.
Notice Notice
// Message is the human-readable text of a KindNotice or KindError
// event.
Message string
// Processed, Total and Found report progress on KindProgress. Total
// grows during a recursive scan as new branches are queued, so treat
// it as an estimate.
Processed int64
Total int64
Found int64
// Stats is the final accounting, on KindDone.
Stats Stats
// Stopped is set on KindDone when the scan ended before testing any
// candidate, after a KindError (the domain is out of scope, a wildcard
// zone without Force, a failed resolver check) or because ctx was
// cancelled during those checks. Stats then covers only the checks.
Stopped bool
// Fatal is set on a KindError that ends the scan. The one KindError
// without it is the reliability guard's warning under Config.NoAbort.
Fatal bool
// Failed is set on KindDone when the scan failed: a fatal KindError
// was sent, or the reliability guard stopped the scan. A scan cancelled
// through the context has not failed.
Failed bool
}
// DefaultWords returns the bundled wordlist: the most common subdomain labels,
// from SecLists' top-5000 list, already normalized and deduplicated.
// Config.Words defaults to it. The returned slice is a fresh copy, safe to
// append to or filter.
func DefaultWords() []string {
return slices.Clone(defaultWords())
}
// defaultWords parses the bundled list once; callers must not modify it.
var defaultWords = sync.OnceValue(func() []string {
lines, err := wordlist.ReadLinesFrom(strings.NewReader(data.Subdomains5k))
if err != nil {
// The list is compiled into the binary, so this cannot fail.
panic("subenum: reading the bundled wordlist: " + err.Error())
}
words, _, _ := wordlist.Build(lines, "")
return words
})
// Permutations returns second-pass candidates for names a scan already found,
// the same ones the CLI's -permute tries: api suggests api-dev, dev-api,
// dev.api, api2 and so on. Each seed is a name relative to the apex, such as
// "api" for api.example.com or "api.us" for api.us.example.com; its leftmost
// label is permuted and the rest kept. words are combined with each seed
// label; nil selects the CLI's built-in list of environment, version and role
// names. Candidates that are not valid DNS names are left out. The result is
// relative to the apex, like Config.Words, sorted and duplicate-free, so it
// can be passed as the Words of a follow-up scan. It sends no DNS queries.
func Permutations(seeds, words []string) []string {
if words == nil {
words = permute.Words
}
return permute.Generate(seeds, words, nil)
}
// doneGrace is how long Run's forwarder waits to hand over the final event
// after ctx is cancelled, when the caller may have stopped reading.
var doneGrace atomic.Int64 // a time.Duration; atomic so tests can change it while forwarders run
func init() { doneGrace.Store(int64(time.Second)) }
// ErrNoWords is returned when a Config's wordlist has no usable entries.
var ErrNoWords = errors.New("subenum: wordlist has no usable entries")
// Run starts a scan and streams its events. The channel ends with exactly one
// KindDone event and is then closed, so a range loop over it always sees the
// final Stats. Problems with cfg itself are reported before the scan starts,
// as an error and no channel.
//
// Read the channel until it closes, or cancel ctx: the usual
// `for ev := range events { ... break ... }` with a deferred cancel is safe.
// While ctx is live, an unread channel pauses the scan once its buffer
// fills. Once ctx is cancelled the scan stops and never waits for the
// caller: results nobody reads may be dropped, and a caller that keeps
// reading still gets KindDone with the counts so far. Every goroutine the
// scan started exits within about a second of cancellation, whether or not
// the channel is read (#112).
func Run(ctx context.Context, cfg Config) (<-chan Event, error) {
return run(ctx, cfg, nil)
}
// subenum mcp reaches run through internal/libhook, to change engine
// settings Config does not expose (#349, #367).
func init() { libhook.Register(run) }
// run is Run with tweak, if non-nil, applied to the engine's options.
func run(ctx context.Context, cfg Config, tweak func(*scan.Options)) (<-chan Event, error) {
internal, err := cfg.options()
if err != nil {
return nil, err
}
if tweak != nil {
tweak(&internal)
}
out := make(chan Event, 64)
go func() {
defer close(out)
var cands *candidates.Set // nil: every result is the wordlist's
// A Domain that Exclude rules out is refused by the scan before any
// query, so its sources are not asked either (#372).
if len(cfg.Sources) > 0 && !cfg.Simulate && !scan.TargetOutOfScope(cfg.Exclude, internal.Domain) {
cands = withSourceNames(ctx, out, cfg.Sources, internal.Domain, internal.Entries, scan.Excluder(cfg.Exclude))
// The names stay apart from Words: no copy of the list (#365).
internal.Entries, internal.Extra = cands.Lists()
}
in := make(chan scan.Event, 64)
go scan.Run(ctx, internal.Config, in)
// scan.Run always ends with exactly one EventDone (#99), so the
// channel ends with exactly one KindDone too.
for ev := range in {
e := translate(ev)
switch {
case e.Kind != KindResult:
case ev.Followed:
e.Result.Sources = candidates.FromRecords // FollowRecords (#432)
default:
e.Result.Sources = cands.SourcesOf(e.Result.Name)
}
forward(ctx, out, e)
}
}()
return out, nil
}
// forward hands ev to the caller. Before cancellation it waits for the
// caller to read. After it, it never blocks the engine, which must be
// drained to finish: other events are dropped when the buffer is full, and
// the final event waits at most doneGrace for a reader.
func forward(ctx context.Context, out chan<- Event, ev Event) {
if ctx.Err() == nil {
select {
case out <- ev:
return
case <-ctx.Done():
}
}
if ev.Kind != KindDone {
select {
case out <- ev:
default:
}
return
}
timer := time.NewTimer(time.Duration(doneGrace.Load()))
defer timer.Stop()
select {
case out <- ev:
case <-timer.C:
}
}
// Scan runs a scan to completion and returns everything it found, in the
// order the names resolved.
//
// An error means the results are incomplete or absent: the context was
// cancelled, the domain is a wildcard zone and Force is not set, the resolver
// failed its preflight check, or too many lookups were failing and the
// reliability guard stopped the scan. The results and Stats gathered up to
// that point come back alongside it, so a partial scan can still be reported.
func Scan(ctx context.Context, cfg Config) ([]Result, Stats, error) {
events, err := Run(ctx, cfg)
if err != nil {
return nil, Stats{}, err
}
var results []Result
var stats Stats
var failure string
for ev := range events {
switch ev.Kind {
case KindResult:
results = append(results, ev.Result)
case KindError:
// The reliability guard's warning under NoAbort is not fatal:
// the scan runs to completion and is a success.
if ev.Fatal && failure == "" {
failure = ev.Message
}
case KindDone:
stats = ev.Stats
}
}
switch {
case ctx.Err() != nil:
return results, stats, ctx.Err()
case failure != "":
// The engine's wording names the specific problem.
return results, stats, fmt.Errorf("subenum: scanning %s: %s", cfg.Domain, failure)
case stats.Aborted:
return results, stats, fmt.Errorf("subenum: scanning %s: stopped early, too many lookups were failing", cfg.Domain)
}
return results, stats, nil
}
// Validate reports the error Run would return for cfg, without starting a
// scan or sending a query: an invalid Domain, Types or Exclude pattern, an
// empty word list, and the other problems with the config itself.
func (c Config) Validate() error {
_, err := c.options()
return err
}
// options validates the config, applies defaults and builds the internal
// options. Messages name Config fields, since a library caller has no flags.
func (c Config) options() (scan.Options, error) {
domain, _, err := validate.NormalizeDomain(c.Domain)
if err != nil {
return scan.Options{}, fmt.Errorf("subenum: Domain: %w", err)
}
// Only the checks that differ from scan.Options.Validate live here: 0
// means "use the default" for these fields, so a negative value must be
// caught before the default replaces it. Every other range is checked
// once, by Validate below (#414).
switch {
case c.Concurrency < 0:
return scan.Options{}, fmt.Errorf("subenum: Concurrency must not be negative, got %d", c.Concurrency)
case c.Timeout < 0:
return scan.Options{}, fmt.Errorf("subenum: Timeout must not be negative, got %s", c.Timeout)
case c.Attempts < 0:
return scan.Options{}, fmt.Errorf("subenum: Attempts must not be negative, got %d", c.Attempts)
case c.Depth < 0:
return scan.Options{}, fmt.Errorf("subenum: Depth must not be negative, got %d", c.Depth)
}
resolver := orDefaultString(c.Resolver, DefaultResolver)
if normalized, err := validate.NormalizeDNSServer(resolver); err == nil {
resolver = normalized
}
if !c.Simulate {
if err := validate.DNSServer(resolver); err != nil {
return scan.Options{}, fmt.Errorf("subenum: Resolver %s: %w", dns.DisplayServer(resolver), err)
}
}
types, err := parseTypes(c.Types)
if err != nil {
return scan.Options{}, err
}
words := c.Words
if words == nil {
words = DefaultWords()
}
entries, _, _ := wordlist.Build(words, domain)
if len(entries) == 0 {
return scan.Options{}, ErrNoWords
}
var deep []string
if c.RecursiveWords != nil {
if deep, _, _ = wordlist.Build(c.RecursiveWords, domain); len(deep) == 0 {
return scan.Options{}, fmt.Errorf("%w: RecursiveWords", ErrNoWords)
}
}
seed := c.Seed
for c.Simulate && seed == 0 {
seed = rand.Uint64() // as documented, and as the CLI and TUI do (#154)
}
opts := scan.Options{
Config: scan.Config{
Domain: domain,
Entries: entries,
RecursiveEntries: deep,
Concurrency: orDefault(c.Concurrency, DefaultConcurrency),
// Whole milliseconds, rounded up, as the CLI's -timeout takes.
Timeout: time.Duration(ceilMillis(orDefaultDuration(c.Timeout, DefaultTimeout))) * time.Millisecond,
DNSServer: resolver,
Simulate: c.Simulate,
HitRate: orDefault(c.HitRate, DefaultHitRate),
Seed: seed,
Attempts: orDefault(c.Attempts, DefaultAttempts),
Force: c.Force,
Types: types,
Recursive: c.Recursive,
FollowRecords: c.FollowRecords,
Depth: orDefault(c.Depth, 1),
Rate: c.Rate,
AutoRate: c.AutoRate,
MaxQueries: c.MaxQueries,
Exclude: c.Exclude,
NoAbort: c.NoAbort,
Verbose: c.Verbose,
Logf: dns.Logf(c.Logf),
RetryInconclusive: c.RetryInconclusive,
ListInconclusive: true, // Stats.Inconclusive is part of the API
},
MaxDuration: c.MaxDuration,
RatePerResolver: c.RatePerResolver,
}
if c.MaxDuration > 0 {
opts.Deadline = time.Now().Add(c.MaxDuration)
}
if len(c.TakeoverProviders) > 0 || c.TakeoverProvidersOnly {
lines := make([]string, 0, len(c.TakeoverProviders))
for _, p := range c.TakeoverProviders {
if strings.ContainsAny(p.Suffix+p.Name, " \t\r\n") || p.Suffix == "" || p.Name == "" {
return scan.Options{}, fmt.Errorf("subenum: TakeoverProviders: invalid entry %+v", p)
}
lines = append(lines, p.Suffix+" "+p.Name)
}
providers, err := dns.ParseTakeoverProviders(lines, c.TakeoverProvidersOnly)
if err != nil {
return scan.Options{}, fmt.Errorf("subenum: TakeoverProviders: %w", err)
}
opts.TakeoverProviders = providers
}
if len(c.ResolverPool) > 0 {
if c.Simulate {
return scan.Options{}, errors.New("subenum: ResolverPool cannot be used with Simulate, which sends no DNS queries")
}
pool, err := dns.NewPoolFromLines(c.ResolverPool, opts.Timeout, c.RatePerResolver)
if errors.Is(err, dns.ErrNoResolvers) {
return scan.Options{}, errors.New("subenum: ResolverPool lists no resolvers")
}
if err != nil {
return scan.Options{}, fmt.Errorf("subenum: ResolverPool: %w", err)
}
opts.Pool = pool
}
if err := opts.Validate(); err != nil {
return scan.Options{}, fmt.Errorf("subenum: %s", fieldNames(err.Error()))
}
return opts, nil
}
// parseTypes checks the record types, defaulting to A and AAAA.
func parseTypes(types []string) ([]string, error) {
if len(types) == 0 {
return nil, nil
}
parsed, err := dns.ParseTypes(strings.Join(types, ","))
if err != nil {
return nil, fmt.Errorf("subenum: Types: %w", err)
}
return parsed, nil
}
// ceilMillis rounds a duration up to whole milliseconds, the engine's unit,
// so a positive Timeout below 1ms becomes 1ms instead of an invalid 0.
func ceilMillis(d time.Duration) int {
return int((d + time.Millisecond - 1) / time.Millisecond)
}
func orDefault(v, def int) int {
if v == 0 {
return def
}
return v
}
func orDefaultString(v, def string) string {
if v == "" {
return def
}
return v
}
func orDefaultDuration(v, def time.Duration) time.Duration {
if v == 0 {
return def
}
return v
}
// fieldNames renders the setting tokens in an engine message as Config
// fields (scan.FieldName). Engine messages refer to settings by token (#100),
// and a library caller has no flags: "Use {force} to scan anyway" reads
// "Use Force to scan anyway".
func fieldNames(msg string) string {
return scan.Render(msg, scan.FieldName)
}
// translate converts an internal event to the public one.
func translate(ev scan.Event) Event {
out := Event{
Message: fieldNames(ev.Message),
Processed: ev.Processed,
Total: ev.Total,
Found: ev.Found,
}
switch ev.Kind {
case scan.EventResult:
out.Kind = KindResult
out.Result = Result{Name: ev.Result.Subdomain, Records: records(ev.Result.Records), Takeover: ev.Result.Takeover, Internal: ev.Result.Internal, Partial: ev.Result.Partial}
case scan.EventProgress:
out.Kind = KindProgress
case scan.EventNotice:
out.Kind = KindNotice
out.Notice = notice(ev.Notice)
case scan.EventError:
out.Kind = KindError
out.Fatal = ev.Fatal
case scan.EventDone:
out.Kind = KindDone
out.Stats = stats(ev.Stats)
out.Stopped = ev.Stopped
out.Failed = ev.Failed
}
return out
}
func records(in []dns.Record) []Record {
if len(in) == 0 {
return nil
}
out := make([]Record, 0, len(in))
for _, r := range in {
out = append(out, Record{Type: r.Type, Value: r.Value})
}
return out
}
func notice(n scan.NoticeKind) Notice {
switch n {
case scan.NoticeWildcard:
return NoticeWildcard
case scan.NoticeCap:
return NoticeCap
case scan.NoticeCeiling:
return NoticeCeiling
case scan.NoticeSkip:
return NoticeSkip
case scan.NoticeRate:
return NoticeRate
}
return NoticeNone
}
// Verdicts a finished scan can get from Stats.Verdict, the same ones the
// CLI's run-quality report gives.
const (
VerdictComplete = scan.VerdictComplete // a name missing from the results most likely does not exist
VerdictDegraded = scan.VerdictDegraded // some names were not tested or got no answer
VerdictUnreliable = scan.VerdictUnreliable // too many lookups failed: a missing name means nothing
)
// Verdict says how far the results of a finished scan can be trusted, by
// the rules of the CLI's run-quality report: VerdictComplete,
// VerdictDegraded or VerdictUnreliable, with the reason in plain words.
// interrupted reports that the scan was cancelled before it ended. The
// reason names Config fields, such as MaxQueries.
func (s Stats) Verdict(interrupted bool) (verdict, reason string) {
v, r := engineStats(s).Verdict(interrupted)
return v, scan.Render(r, scan.FieldName)
}
// engineStats is the inverse of stats, for the engine's verdict rules.
func engineStats(s Stats) scan.Stats {
var fp []dns.Record
if len(s.WildcardFingerprint) > 0 {
fp = dnsRecords(s.WildcardFingerprint)
}
return scan.Stats{
Found: s.Found,
NXDomain: s.NXDomain,
Timeout: s.Timeout,
Refused: s.Refused,
Other: s.Other,
WildcardFiltered: s.WildcardFiltered,
QueriesSent: s.QueriesSent,
Skipped: s.Skipped,
Excluded: s.Excluded,
Takeover: s.Takeover,
Internal: s.Internal,
Partial: s.Partial,
ParentChecks: s.ParentChecks,
ParentQueries: s.ParentQueries,
PoolHits: s.PoolHits,
Confirmed: s.Confirmed,
Unconfirmed: s.Unconfirmed,
Aborted: s.Aborted,
Expired: s.Expired,
Wildcard: s.Wildcard,
Fingerprint: fp,
Retried: s.Retried,
Recovered: s.Recovered,
Inconclusive: s.Inconclusive,
RateBackoffs: s.RateBackoffs,
FinalRate: s.FinalRate,
}
}
func stats(s scan.Stats) Stats {
return Stats{
Found: s.Found,
NXDomain: s.NXDomain,
Timeout: s.Timeout,
Refused: s.Refused,
Other: s.Other,
WildcardFiltered: s.WildcardFiltered,
QueriesSent: s.QueriesSent,
Skipped: s.Skipped,
Excluded: s.Excluded,
Takeover: s.Takeover,
Internal: s.Internal,
Partial: s.Partial,
ParentChecks: s.ParentChecks,
ParentQueries: s.ParentQueries,
PoolHits: s.PoolHits,
Confirmed: s.Confirmed,
Unconfirmed: s.Unconfirmed,
Aborted: s.Aborted,
Expired: s.Expired,
Wildcard: s.Wildcard,
WildcardFingerprint: records(s.Fingerprint),
Retried: s.Retried,
Recovered: s.Recovered,
Inconclusive: s.Inconclusive,
RateBackoffs: s.RateBackoffs,
FinalRate: s.FinalRate,
}
}