forked from projectdiscovery/tlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.go
More file actions
607 lines (545 loc) · 16.8 KB
/
runner.go
File metadata and controls
607 lines (545 loc) · 16.8 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
package runner
import (
"bufio"
"context"
"encoding/json"
"net"
"net/url"
"os"
"strconv"
"strings"
"sync"
"time"
"crypto/x509"
"github.com/miekg/dns"
"github.com/projectdiscovery/dnsx/libs/dnsx"
"github.com/projectdiscovery/fastdialer/fastdialer"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/gologger/formatter"
"github.com/projectdiscovery/gologger/levels"
"github.com/projectdiscovery/mapcidr"
"github.com/projectdiscovery/mapcidr/asn"
"github.com/projectdiscovery/tlsx/internal/pdcp"
"github.com/projectdiscovery/tlsx/pkg/ctlogs"
"github.com/projectdiscovery/tlsx/pkg/output"
"github.com/projectdiscovery/tlsx/pkg/output/stats"
"github.com/projectdiscovery/tlsx/pkg/tlsx"
"github.com/projectdiscovery/tlsx/pkg/tlsx/clients"
"github.com/projectdiscovery/tlsx/pkg/tlsx/openssl"
pdcpauth "github.com/projectdiscovery/utils/auth/pdcp"
"github.com/projectdiscovery/utils/errkit" //nolint
iputil "github.com/projectdiscovery/utils/ip"
sliceutil "github.com/projectdiscovery/utils/slice"
updateutils "github.com/projectdiscovery/utils/update"
"golang.org/x/net/proxy"
)
// Runner is a client for running the enumeration process
type Runner struct {
hasStdin bool
hasStdinSet bool // Track if hasStdin was manually set (for tests)
outputWriter output.Writer
fastDialer *fastdialer.Dialer
options *clients.Options
dnsclient *dnsx.DNSX
pdcpWriter *pdcp.UploadWriter
}
// New creates a new runner from provided configuration options
func New(options *clients.Options) (*Runner, error) {
// Disable coloring of log output if asked by user
if options.NoColor {
gologger.DefaultLogger.SetFormatter(formatter.NewCLI(true))
}
if options.Silent {
gologger.DefaultLogger.SetMaxLevel(levels.LevelSilent)
}
if options.OpenSSLBinary != "" {
openssl.UseOpenSSLBinary(options.OpenSSLBinary)
}
if options.TlsCiphersEnum {
// cipher enumeration requires tls versions
options.TlsVersionsEnum = true
}
showBanner()
if options.Version {
gologger.Info().Msgf("Current version: %s", version)
return nil, nil
}
if !options.DisableUpdateCheck {
latestVersion, err := updateutils.GetToolVersionCallback("tlsx", version)()
if err != nil {
if options.Verbose {
gologger.Error().Msgf("tlsx version check failed: %v", err.Error())
}
} else {
gologger.Info().Msgf("Current tlsx version %v %v", version, updateutils.GetVersionDescription(version, latestVersion))
}
}
runner := &Runner{options: options}
if err := runner.validateOptions(); err != nil {
return nil, errkit.Wrap(err, "could not validate options")
}
dialerTimeout := time.Duration(options.Timeout) * time.Second
var proxyDialer *proxy.Dialer
if options.Proxy != "" {
proxyURL, err := url.Parse(options.Proxy)
if err != nil {
return nil, errkit.Wrap(err, "could not parse proxy")
}
dialer, err := proxy.FromURL(proxyURL, &net.Dialer{
Timeout: dialerTimeout,
DualStack: true,
})
if err != nil {
return nil, errkit.Wrap(err, "could not create proxy dialer")
}
proxyDialer = &dialer
}
dialerOpts := fastdialer.DefaultOptions
dialerOpts.WithDialerHistory = true
dialerOpts.MaxRetries = 3
dialerOpts.DialerTimeout = dialerTimeout
if proxyDialer != nil {
dialerOpts.ProxyDialer = proxyDialer
}
if len(options.Resolvers) > 0 {
dialerOpts.BaseResolvers = options.Resolvers
}
fastDialer, err := fastdialer.NewDialer(dialerOpts)
if err != nil {
return nil, errkit.Wrap(err, "could not create dialer")
}
runner.fastDialer = fastDialer
runner.options.Fastdialer = fastDialer
dnsOptions := dnsx.DefaultOptions
dnsOptions.MaxRetries = runner.options.Retries
dnsOptions.Hostsfile = true
if sliceutil.Contains(options.IPVersion, "6") {
dnsOptions.QuestionTypes = append(dnsOptions.QuestionTypes, dns.TypeAAAA)
}
dnsclient, err := dnsx.New(dnsOptions)
if err != nil {
return nil, err
}
runner.dnsclient = dnsclient
outputWriter, err := output.New(options)
if err != nil {
return nil, errkit.Wrap(err, "could not create output writer")
}
runner.outputWriter = outputWriter
// Initialize PDCP upload writer if dashboard is enabled
if options.Dashboard {
handler := pdcpauth.PDCPCredHandler{}
creds, err := handler.GetCreds()
if err != nil {
if options.Verbose {
gologger.Warning().Msgf("Could not get PDCP credentials: %s", err)
}
} else {
ctx := context.Background()
pdcpWriter, err := pdcp.NewUploadWriterCallback(ctx, creds)
if err != nil {
if options.Verbose {
gologger.Warning().Msgf("Could not initialize PDCP upload writer: %s", err)
}
} else {
if options.PDCPAssetID != "" {
if err := pdcpWriter.SetAssetID(options.PDCPAssetID); err != nil {
gologger.Warning().Msgf("Invalid asset ID: %s", err)
}
}
if options.PDCPAssetName != "" {
pdcpWriter.SetAssetGroupName(options.PDCPAssetName)
}
if options.PDCPTeamID != "" {
pdcpWriter.SetTeamID(options.PDCPTeamID)
}
runner.pdcpWriter = pdcpWriter
}
}
}
if options.TlsCiphersEnum && !options.Silent {
gologger.Info().Msgf("Enumerating TLS Ciphers in %s mode", options.ScanMode)
}
return runner, nil
}
// Close closes the runner releasing resources
func (r *Runner) Close() error {
_ = r.outputWriter.Close()
// Close PDCP writer if enabled
if r.pdcpWriter != nil {
r.pdcpWriter.Close()
}
// Handle dashboard-upload flag for file uploads
if r.options.DashboardUpload != "" {
handler := pdcpauth.PDCPCredHandler{}
creds, err := handler.GetCreds()
if err != nil {
gologger.Warning().Msgf("Could not get PDCP credentials for file upload: %s", err)
} else {
ctx := context.Background()
pdcpWriter, err := pdcp.NewUploadWriterCallback(ctx, creds)
if err != nil {
gologger.Warning().Msgf("Could not initialize PDCP upload writer for file: %s", err)
} else {
if r.options.PDCPAssetID != "" {
if err := pdcpWriter.SetAssetID(r.options.PDCPAssetID); err != nil {
gologger.Warning().Msgf("Invalid asset ID: %s", err)
}
}
if r.options.PDCPAssetName != "" {
pdcpWriter.SetAssetGroupName(r.options.PDCPAssetName)
}
if r.options.PDCPTeamID != "" {
pdcpWriter.SetTeamID(r.options.PDCPTeamID)
}
// Read file and upload line by line
file, err := os.Open(r.options.DashboardUpload)
if err != nil {
gologger.Warning().Msgf("Could not open file for upload: %s", err)
} else {
defer file.Close()
scanner := bufio.NewScanner(file)
callback := pdcpWriter.GetWriterCallback()
for scanner.Scan() {
line := scanner.Bytes()
var resp clients.Response
if err := json.Unmarshal(line, &resp); err == nil {
callback(&resp)
}
}
pdcpWriter.Close()
}
}
}
}
r.fastDialer.Close()
return nil
}
type taskInput struct {
host string
ip string
port string
sni string
}
func (t taskInput) Address() string {
return net.JoinHostPort(t.host, t.port)
}
// Execute executes the main data collection loop
func (r *Runner) Execute() error {
// Handle CT logs streaming mode
if r.options.CTLogs {
return r.executeCTLogsMode()
}
// Create the worker goroutines for processing
inputs := make(chan taskInput, r.options.Concurrency)
wg := &sync.WaitGroup{}
for i := 0; i < r.options.Concurrency; i++ {
wg.Add(1)
go r.processInputElementWorker(inputs, wg)
}
// Queue inputs
if err := r.normalizeAndQueueInputs(inputs); err != nil {
gologger.Error().Msgf("Could not normalize queue inputs: %s", err)
}
close(inputs)
wg.Wait()
// Print the stats if auto fallback mode is used
if r.options.ScanMode == "auto" {
gologger.Info().Msgf("Connections made using crypto/tls: %d, zcrypto/tls: %d, openssl: %d", stats.LoadCryptoTLSConnections(), stats.LoadZcryptoTLSConnections(), stats.LoadOpensslTLSConnections())
}
return nil
}
// executeCTLogsMode executes CT logs streaming mode
func (r *Runner) executeCTLogsMode() error {
gologger.Info().Msg("Starting Certificate Transparency logs streaming mode…")
// Build functional options for ctlogs service
var svcOpts []ctlogs.ServiceOption
// Verbosity & certificate inclusion follow existing flags
svcOpts = append(svcOpts, ctlogs.WithVerbose(r.options.Verbose))
if r.options.Cert {
svcOpts = append(svcOpts, ctlogs.WithCert(true))
}
// Start mode handling
if r.options.CTLBeginning {
svcOpts = append(svcOpts, ctlogs.WithStartBeginning())
} else if len(r.options.CTLIndex) > 0 {
custom := make(map[string]uint64)
for _, item := range r.options.CTLIndex {
parts := strings.SplitN(item, "=", 2)
if len(parts) != 2 {
gologger.Warning().Msgf("invalid --ctl-index entry %q (expected <sourceID>=<index>, e.g. google_xenon2025h2=12345)", item)
continue
}
idx, err := strconv.ParseUint(parts[1], 10, 64)
if err != nil {
gologger.Warning().Msgf("invalid index in --ctl-index entry %q: %v", item, err)
continue
}
key := strings.ToLower(parts[0])
custom[key] = idx
}
if len(custom) > 0 {
svcOpts = append(svcOpts, ctlogs.WithCustomStartIndices(custom))
}
}
// Callback adapter converts ctlogs.EntryMeta + raw cert into tlsx Response
callback := func(meta ctlogs.EntryMeta, raw []byte, duplicate bool) {
// Skip duplicates to preserve historical CLI behaviour
if duplicate {
return
}
cert, err := x509.ParseCertificate(raw)
if err != nil {
if r.options.Verbose {
gologger.Warning().Msgf("failed to parse certificate: %v", err)
}
return
}
// Display CT log progress information in verbose mode
if r.options.Verbose {
progressPercent := float64(meta.Index) / float64(meta.TreeSize) * 100
gologger.Info().Msgf("[CT] %s: Index %d/%d (%.1f%%), Lag: %d, URL: %s",
meta.SourceDesc, meta.Index, meta.TreeSize, progressPercent, meta.Lag, meta.LogURL)
}
resp := ctlogs.ConvertCertificateToResponseWithMeta(cert, meta.SourceDesc, r.options.Cert, &meta)
if resp == nil {
return
}
// Enhance response with CT log metadata
if resp.CTLogSource == "" {
resp.CTLogSource = meta.SourceID
}
if err := r.outputWriter.Write(resp); err != nil {
gologger.Warning().Msgf("Could not write CT log output: %s", err)
}
// Send to PDCP if enabled
if r.pdcpWriter != nil {
callback := r.pdcpWriter.GetWriterCallback()
callback(resp)
}
}
svcOpts = append(svcOpts, ctlogs.WithCallback(callback))
ctService, err := ctlogs.New(svcOpts...)
if err != nil {
return errkit.Wrap(err, "could not create CT logs service")
}
// Start streaming
ctService.Start()
defer ctService.Stop()
// Block indefinitely (until SIGINT/SIGTERM) as streaming is async.
select {}
}
// processInputElementWorker processes an element from input
func (r *Runner) processInputElementWorker(inputs chan taskInput, wg *sync.WaitGroup) {
defer wg.Done()
tlsxService, err := tlsx.New(r.options)
if err != nil {
gologger.Fatal().Msgf("could not create tlsx client: %s", err)
return
}
for task := range inputs {
if r.options.Delay != "" {
duration, err := time.ParseDuration(r.options.Delay)
if err != nil {
gologger.Error().Msgf("error parsing delay %s: %s", r.options.Delay, err)
}
time.Sleep(duration)
}
if r.options.Verbose {
gologger.Info().Msgf("Processing input %s:%s", task.host, task.port)
}
response, err := tlsxService.ConnectWithOptions(task.host, task.ip, task.port, clients.ConnectOptions{SNI: task.sni})
if err != nil {
gologger.Warning().Msgf("Could not connect input %s: %s", task.Address(), err)
}
if response == nil {
continue
}
if err := r.outputWriter.Write(response); err != nil {
gologger.Warning().Msgf("Could not write output %s: %s", task.Address(), err)
continue
}
// Send to PDCP if enabled
if r.pdcpWriter != nil {
callback := r.pdcpWriter.GetWriterCallback()
callback(response)
}
}
}
// normalizeAndQueueInputs normalizes the inputs and queues them for execution
func (r *Runner) normalizeAndQueueInputs(inputs chan taskInput) error {
// Process Normal Inputs
for _, text := range r.options.Inputs {
r.processInputItem(text, inputs)
}
if r.options.InputList != "" {
file, err := os.Open(r.options.InputList)
if err != nil {
return errkit.Wrap(err, "could not open input file")
}
defer func() {
if err := file.Close(); err != nil {
gologger.Warning().Msgf("Failed to close input file: %v", err)
}
}()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
text := scanner.Text()
if text != "" {
// Split comma-separated values to match -u flag behavior
items := strings.Split(text, ",")
for _, item := range items {
item = strings.TrimSpace(item)
if item != "" {
r.processInputItem(item, inputs)
}
}
}
}
}
if r.hasStdin {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
text := scanner.Text()
if text != "" {
// Split comma-separated values to match -u flag behavior
items := strings.Split(text, ",")
for _, item := range items {
item = strings.TrimSpace(item)
if item != "" {
r.processInputItem(item, inputs)
}
}
}
}
}
return nil
}
// resolveFQDN resolves a FQDN and returns the IP addresses
func (r *Runner) resolveFQDN(target string) ([]string, error) {
// If the host is a Domain, then perform resolution and discover all IP
// addresses for a given host. Else use that host
var hostIPs []string
if !iputil.IsIP(target) {
dnsData, err := r.dnsclient.QueryMultiple(target)
if err != nil || dnsData == nil {
gologger.Warning().Msgf("Could not get IP for host: %s\n", target)
return nil, err
}
if len(r.options.IPVersion) > 0 {
if sliceutil.Contains(r.options.IPVersion, "4") {
hostIPs = append(hostIPs, dnsData.A...)
}
if sliceutil.Contains(r.options.IPVersion, "6") {
hostIPs = append(hostIPs, dnsData.AAAA...)
}
} else {
hostIPs = append(hostIPs, dnsData.A...)
}
} else {
hostIPs = append(hostIPs, target)
}
return hostIPs, nil
}
// processInputItem processes a single input item
func (r *Runner) processInputItem(input string, inputs chan taskInput) {
// AS Input
if asn.IsASN(input) {
r.processInputASN(input, inputs)
return
}
// CIDR input
if _, ipRange, _ := net.ParseCIDR(input); ipRange != nil {
r.processInputCIDR(input, inputs)
return
}
if r.options.ScanAllIPs || len(r.options.IPVersion) > 0 {
r.processInputForMultipleIPs(input, inputs)
return
}
// Normal input
host, customPort := r.getHostPortFromInput(input)
if customPort == "" {
for _, port := range r.options.Ports {
r.processInputItemWithSni(taskInput{host: host, port: port}, inputs)
}
} else {
r.processInputItemWithSni(taskInput{host: host, port: customPort}, inputs)
}
}
func (r *Runner) processInputItemWithSni(task taskInput, inputs chan taskInput) {
if len(r.options.ServerName) > 0 {
for _, serverName := range r.options.ServerName {
task.sni = serverName
inputs <- task
}
} else {
inputs <- task
}
}
// getHostPortFromInput returns host and optionally port from input.
// If no ports are found, port field is left blank and user specified ports
// are used.
func (r *Runner) getHostPortFromInput(input string) (string, string) {
host := input
if strings.Contains(input, "://") {
if parsed, err := url.Parse(input); err != nil {
return "", ""
} else {
host = parsed.Host
}
}
if strings.Contains(host, ":") {
if host, port, err := net.SplitHostPort(host); err != nil {
return "", ""
} else {
return host, port
}
}
return host, ""
}
// processInputASN processes a single ASN input
func (r *Runner) processInputASN(input string, inputs chan taskInput) {
ips, err := asn.GetIPAddressesAsStream(input)
if err != nil {
gologger.Error().Msgf("Could not get IP addresses for %s: %s", input, err)
return
}
for ip := range ips {
for _, port := range r.options.Ports {
r.processInputItemWithSni(taskInput{host: ip, port: port}, inputs)
}
}
}
// processInputCIDR processes a single ASN input
func (r *Runner) processInputCIDR(input string, inputs chan taskInput) {
cidrInputs, err := mapcidr.IPAddressesAsStream(input)
if err != nil {
gologger.Error().Msgf("Could not parse cidr %s: %s", input, err)
return
}
for cidr := range cidrInputs {
for _, port := range r.options.Ports {
r.processInputItemWithSni(taskInput{host: cidr, port: port}, inputs)
}
}
}
// processInputForMultipleIPs processes single input if scanall and IPVersion flag is passed
func (r *Runner) processInputForMultipleIPs(input string, inputs chan taskInput) {
host, customPort := r.getHostPortFromInput(input)
// If the host is a Domain, then perform resolution and discover all IP's
ipList, err := r.resolveFQDN(host)
if err != nil {
gologger.Warning().Msgf("Could not resolve %s: %s", host, err)
return
}
for _, ip := range ipList {
if customPort == "" {
for _, port := range r.options.Ports {
r.processInputItemWithSni(taskInput{host: host, ip: ip, port: port}, inputs)
}
} else {
r.processInputItemWithSni(taskInput{host: host, ip: ip, port: customPort}, inputs)
}
}
}