forked from projectdiscovery/tlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner_test.go
More file actions
598 lines (519 loc) · 14 KB
/
runner_test.go
File metadata and controls
598 lines (519 loc) · 14 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
package runner
import (
"os"
"strings"
"testing"
"github.com/projectdiscovery/dnsx/libs/dnsx"
"github.com/projectdiscovery/tlsx/pkg/tlsx/clients"
"github.com/projectdiscovery/utils/auth/pdcp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Normal input
func Test_InputDomain_processInputItem(t *testing.T) {
options := &clients.Options{
Ports: []string{"443"},
}
runner := &Runner{options: options}
inputs := make(chan taskInput)
domain := "www.example.com"
expected := []taskInput{
{
host: "www.example.com",
port: "443",
},
}
go func() {
runner.processInputItem(domain, inputs)
defer close(inputs)
}()
var got []taskInput
for task := range inputs {
got = append(got, task)
}
require.ElementsMatch(t, expected, got, "could not get correct taskInputs")
}
func Test_InputForMultipleIps_processInputItem(t *testing.T) {
options := &clients.Options{
Ports: []string{"443"},
ScanAllIPs: true,
}
runner := &Runner{options: options}
dnsOptions := dnsx.DefaultOptions
dnsOptions.MaxRetries = 3
dnsOptions.Hostsfile = true
dnsclient, err := dnsx.New(dnsOptions)
require.Nil(t, err, "failed to create dns client")
runner.dnsclient = dnsclient
inputs := make(chan taskInput)
domain := "one.one.one.one"
expected := []taskInput{
{
host: "one.one.one.one",
ip: "1.1.1.1",
port: "443",
},
{
host: "one.one.one.one",
ip: "1.0.0.1",
port: "443",
},
}
go func() {
runner.processInputItem(domain, inputs)
defer close(inputs)
}()
var got []taskInput
for task := range inputs {
got = append(got, task)
}
require.ElementsMatch(t, expected, got, "could not get correct taskInputs")
}
func Test_InputCIDR_processInputItem(t *testing.T) {
options := &clients.Options{
Ports: []string{"443"},
}
runner := &Runner{options: options}
inputs := make(chan taskInput)
inputCIDR := "173.0.84.0/30"
expected := []taskInput{
{
host: "173.0.84.0",
port: "443",
}, {
host: "173.0.84.1",
port: "443",
},
{
host: "173.0.84.2",
port: "443",
}, {
host: "173.0.84.3",
port: "443",
},
}
go func() {
runner.processInputItem(inputCIDR, inputs)
defer close(inputs)
}()
var got []taskInput
for task := range inputs {
got = append(got, task)
}
require.ElementsMatch(t, expected, got, "could not get correct taskInputs")
}
func Test_InputASN_processInputItem(t *testing.T) {
// skip if keys are missing
h := pdcp.PDCPCredHandler{}
_, err := h.GetCreds()
if err != nil {
t.Skip("skipping ASN test as keys are missing")
}
options := &clients.Options{
Ports: []string{"443"},
}
runner := &Runner{options: options}
inputs := make(chan taskInput)
asn := "AS14421"
expectedOutputFile := "tests/AS14421.txt"
go func() {
runner.processInputItem(asn, inputs)
defer close(inputs)
}()
var got []taskInput
for task := range inputs {
got = append(got, task)
}
expected, err := getTaskInputFromFile(expectedOutputFile, options.Ports)
require.Nil(t, err, "could not read the expectedOutputFile")
require.ElementsMatch(t, expected, got, "could not get correct taskInputs")
}
func Test_RevokedCert_processInputItem(t *testing.T) {
options := &clients.Options{
Ports: []string{"443"},
Revoked: true,
}
runner := &Runner{options: options}
inputs := make(chan taskInput)
domain := "revoked.badssl.com"
expected := []taskInput{
{
host: "revoked.badssl.com",
port: "443",
},
}
go func() {
runner.processInputItem(domain, inputs)
defer close(inputs)
}()
var got []taskInput
for task := range inputs {
got = append(got, task)
}
require.ElementsMatch(t, expected, got, "could not get correct taskInputs")
}
func Test_SelfSignedCert_processInputItem(t *testing.T) {
options := &clients.Options{
Ports: []string{"443"},
SelfSigned: true,
}
runner := &Runner{options: options}
inputs := make(chan taskInput)
domain := "self-signed.badssl.com"
expected := []taskInput{
{
host: "self-signed.badssl.com",
port: "443",
},
}
go func() {
runner.processInputItem(domain, inputs)
defer close(inputs)
}()
var got []taskInput
for task := range inputs {
got = append(got, task)
}
require.ElementsMatch(t, expected, got, "could not get correct taskInputs")
}
func getTaskInputFromFile(filename string, ports []string) ([]taskInput, error) {
fileContent, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
ipList := strings.Split(strings.ReplaceAll(string(fileContent), "\r\n", "\n"), "\n")
var ret []taskInput
for _, ip := range ipList {
for _, p := range ports {
ret = append(ret, taskInput{host: ip, port: p})
}
}
return ret, nil
}
func Test_CommaSeparatedInputFile(t *testing.T) {
// Create a temporary file with comma-separated inputs
tempFile, err := os.CreateTemp("", "tlsx-test-*.txt")
require.NoError(t, err)
defer os.Remove(tempFile.Name())
// Write comma-separated prefixes to the file
content := "192.168.1.0/24,192.168.2.0/24,192.168.3.0/24\n"
_, err = tempFile.WriteString(content)
require.NoError(t, err)
tempFile.Close()
options := &clients.Options{
Ports: []string{"443"},
InputList: tempFile.Name(),
}
runner := &Runner{options: options}
inputs := make(chan taskInput, 100)
// Test normalizeAndQueueInputs with the file
go func() {
err := runner.normalizeAndQueueInputs(inputs)
require.NoError(t, err)
close(inputs)
}()
var got []taskInput
for task := range inputs {
got = append(got, task)
}
// We expect 3 CIDR ranges, each expanding to 256 IPs
// But for testing purposes, we just verify that we got multiple inputs
// and that they start with the expected prefixes
require.Greater(t, len(got), 0, "should have processed inputs from comma-separated file")
// Check that we have inputs from all three prefixes
hasFirstPrefix := false
hasSecondPrefix := false
hasThirdPrefix := false
for _, task := range got {
if strings.HasPrefix(task.host, "192.168.1.") {
hasFirstPrefix = true
}
if strings.HasPrefix(task.host, "192.168.2.") {
hasSecondPrefix = true
}
if strings.HasPrefix(task.host, "192.168.3.") {
hasThirdPrefix = true
}
}
assert.True(t, hasFirstPrefix, "should have inputs from first prefix")
assert.True(t, hasSecondPrefix, "should have inputs from second prefix")
assert.True(t, hasThirdPrefix, "should have inputs from third prefix")
}
func Test_CommaSeparatedInputFileWithWhitespace(t *testing.T) {
// Create a temporary file with comma-separated inputs with whitespace
tempFile, err := os.CreateTemp("", "tlsx-test-whitespace-*.txt")
require.NoError(t, err)
defer os.Remove(tempFile.Name())
// Write comma-separated prefixes with whitespace to the file
content := "192.168.1.0/24, 192.168.2.0/24 , 192.168.3.0/24\n"
_, err = tempFile.WriteString(content)
require.NoError(t, err)
tempFile.Close()
options := &clients.Options{
Ports: []string{"443"},
InputList: tempFile.Name(),
}
runner := &Runner{options: options}
inputs := make(chan taskInput, 100)
// Test normalizeAndQueueInputs with the file
go func() {
err := runner.normalizeAndQueueInputs(inputs)
require.NoError(t, err)
close(inputs)
}()
var got []taskInput
for task := range inputs {
got = append(got, task)
}
// Check that whitespace was trimmed and all prefixes were processed
hasFirstPrefix := false
hasSecondPrefix := false
hasThirdPrefix := false
for _, task := range got {
if strings.HasPrefix(task.host, "192.168.1.") {
hasFirstPrefix = true
}
if strings.HasPrefix(task.host, "192.168.2.") {
hasSecondPrefix = true
}
if strings.HasPrefix(task.host, "192.168.3.") {
hasThirdPrefix = true
}
}
assert.True(t, hasFirstPrefix, "should have inputs from first prefix")
assert.True(t, hasSecondPrefix, "should have inputs from second prefix")
assert.True(t, hasThirdPrefix, "should have inputs from third prefix")
}
func Test_CommaSeparatedStdin(t *testing.T) {
// Test comma-separated values from stdin
oldStdin := os.Stdin
r, w, _ := os.Pipe()
os.Stdin = r
// Write comma-separated input to stdin
go func() {
_, err := w.WriteString("192.168.1.0/24,192.168.2.0/24\n")
require.NoError(t, err)
w.Close()
}()
options := &clients.Options{
Ports: []string{"443"},
}
runner := &Runner{options: options}
runner.hasStdin = true
runner.hasStdinSet = true
inputs := make(chan taskInput, 100)
// Test normalizeAndQueueInputs with stdin
go func() {
err := runner.normalizeAndQueueInputs(inputs)
require.NoError(t, err)
close(inputs)
}()
var got []taskInput
for task := range inputs {
got = append(got, task)
}
// Restore stdin
os.Stdin = oldStdin
// Check that both prefixes were processed
hasFirstPrefix := false
hasSecondPrefix := false
for _, task := range got {
if strings.HasPrefix(task.host, "192.168.1.") {
hasFirstPrefix = true
}
if strings.HasPrefix(task.host, "192.168.2.") {
hasSecondPrefix = true
}
}
assert.True(t, hasFirstPrefix, "should have inputs from first prefix")
assert.True(t, hasSecondPrefix, "should have inputs from second prefix")
}
func Test_CTLogsModeValidation(t *testing.T) {
// Test that CT logs mode and input mode cannot be used together
// This validation is now done in the main package, so this test should be removed
// or modified to test the actual runner behavior
t.Skip("Validation is now handled in main package")
}
func Test_CTLogsModeDefaultEnabled(t *testing.T) {
// Test that CT logs mode is enabled by default when no input is provided
options := &clients.Options{
// No inputs provided
}
runner := &Runner{options: options}
runner.hasStdin = false // Simulate no stdin
// The validation logic is now in main package, so we'll test the runner behavior
// when CT logs mode is already set
options.CTLogs = true
options.SAN = true
err := runner.validateOptions()
require.NoError(t, err)
assert.True(t, options.CTLogs)
assert.True(t, options.SAN)
}
func Test_CTLogsModeWithInputDisabled(t *testing.T) {
// Test that CT logs mode is NOT enabled when input is provided
options := &clients.Options{
Inputs: []string{"example.com"},
CTLogs: false, // Explicitly disabled
}
runner := &Runner{options: options}
runner.hasStdin = false
err := runner.validateOptions()
require.NoError(t, err)
assert.False(t, options.CTLogs)
}
func Test_CTLogsModeWithStdinDisabled(t *testing.T) {
// Test that CT logs mode is NOT enabled when stdin has data
options := &clients.Options{
CTLogs: false, // Explicitly disabled
}
runner := &Runner{options: options}
runner.hasStdin = true // Simulate stdin has data
runner.hasStdinSet = true // Mark that hasStdin was manually set
err := runner.validateOptions()
require.NoError(t, err)
assert.False(t, options.CTLogs)
}
func Test_CTLogsModeExplicitEnabled(t *testing.T) {
// Test that explicit CT logs mode works correctly
options := &clients.Options{
CTLogs: true,
SAN: true, // SAN should be enabled by default in CT logs mode
}
runner := &Runner{options: options}
runner.hasStdin = false
err := runner.validateOptions()
require.NoError(t, err)
assert.True(t, options.CTLogs)
assert.True(t, options.SAN)
}
func Test_CTLogsModeWithSANOverride(t *testing.T) {
// Test that SAN can be overridden when CT logs mode is enabled
options := &clients.Options{
CTLogs: true,
SAN: false, // Explicitly disable SAN
}
runner := &Runner{options: options}
runner.hasStdin = false
err := runner.validateOptions()
require.NoError(t, err)
assert.True(t, options.CTLogs)
assert.False(t, options.SAN) // Should respect explicit SAN setting
}
func Test_CTLogsModePortsDefault(t *testing.T) {
// Test that default ports are set when CT logs mode is enabled
options := &clients.Options{
CTLogs: true,
// No ports specified
}
runner := &Runner{options: options}
runner.hasStdin = false
err := runner.validateOptions()
require.NoError(t, err)
// The ports should be set to default 443
assert.Len(t, options.Ports, 1)
assert.Equal(t, "443", options.Ports[0])
}
func Test_CTLogsModeExecute(t *testing.T) {
// Test that CT logs mode execution path is taken
options := &clients.Options{
CTLogs: true,
}
runner := &Runner{options: options}
runner.hasStdin = false
err := runner.validateOptions()
require.NoError(t, err)
assert.True(t, options.CTLogs)
}
func Test_CTLogsModeWithAllProbes(t *testing.T) {
// Test that CT logs mode works with various probe combinations
testCases := []struct {
name string
probes func(*clients.Options)
}{
{
name: "SelfSigned probe",
probes: func(o *clients.Options) {
o.SelfSigned = true
},
},
{
name: "Wildcard probe",
probes: func(o *clients.Options) {
o.WildcardCertCheck = true
},
},
{
name: "Expired probe",
probes: func(o *clients.Options) {
o.Expired = true
},
},
{
name: "Multiple probes",
probes: func(o *clients.Options) {
o.SelfSigned = true
o.WildcardCertCheck = true
o.Expired = true
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
options := &clients.Options{
CTLogs: true,
}
tc.probes(options)
runner := &Runner{options: options}
runner.hasStdin = false
err := runner.validateOptions()
require.NoError(t, err)
assert.True(t, options.CTLogs)
})
}
}
func Test_CTLogsModeOutputOptions(t *testing.T) {
// Test that CT logs mode works with various output options
testCases := []struct {
name string
output func(*clients.Options)
}{
{
name: "JSON output",
output: func(o *clients.Options) {
o.JSON = true
},
},
{
name: "Verbose output",
output: func(o *clients.Options) {
o.Verbose = true
},
},
{
name: "Silent output",
output: func(o *clients.Options) {
o.Silent = true
},
},
{
name: "Certificate output",
output: func(o *clients.Options) {
o.Cert = true
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
options := &clients.Options{
CTLogs: true,
}
tc.output(options)
runner := &Runner{options: options}
runner.hasStdin = false
err := runner.validateOptions()
require.NoError(t, err)
assert.True(t, options.CTLogs)
})
}
}