-
Notifications
You must be signed in to change notification settings - Fork 3
/
scanner.go
135 lines (129 loc) · 3.81 KB
/
scanner.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
package l9l4gfuzz
import (
"bufio"
"encoding/json"
"fmt"
"github.com/gboddin/goccm"
"github.com/schollz/progressbar/v3"
"io"
"log"
"os"
"os/signal"
"time"
)
type Scanner struct {
ListenAddress string `kong:"short='l',required,help='Listen address (ip:port)'"`
InputFile *os.File `kong:"short='i',required,help='Input file, - for STDIN'"`
OutputFile *os.File `kong:"short='o',help='Output file'"`
MaxConnections int `kong:"short='m',help='Max connections',default='100'"`
Psk string `kong:"short='k',help='Payload sign key',default='no-payload-check'"`
RequestTemplate string `kong:"short='r',help='Uses a custom request template'"`
PayloadTemplate string `kong:"short='p',help='Uses a custom payload'"`
Timeout int `kong:"short='t',default='2',help='Timeout (LDAP,http,tcp...)'"`
Wait int `kong:"short='w',default='60',help='Wait for ping after scan is done, < 0 waits forever'"`
JSON bool `kong:"short='j',default='false',help='Output results as JSON'"`
Quiet bool `kong:"short='q',help='No progress bar',default='false'"`
LDAPDebug *os.File `kong:"short='L',help='LDAP server debug log file'"`
Debug bool `kong:"short='d',help='Debug',default='false'"`
ccm *goccm.ConcurrencyManager
outputChannel FuzzerChannel
bar *progressbar.ProgressBar
}
func (cmd *Scanner) displayResults() {
var writer io.Writer
if cmd.OutputFile != nil {
writer = cmd.OutputFile
} else {
writer = os.Stdout
}
var jsonEncoder *json.Encoder
if cmd.JSON {
jsonEncoder = json.NewEncoder(writer)
}
for {
result, open := <-cmd.outputChannel
if !open {
return
}
if jsonEncoder != nil {
if err := jsonEncoder.Encode(result); err != nil {
log.Fatalln(err)
}
} else {
if _, err := writer.Write(
[]byte(fmt.Sprintf("[%s-reply] From: %s:%s | Source: %s | Vector: %s | Delay: %s\n",
result.Protocol,
result.Ip,
result.Port,
result.Token.SourceUrl,
result.Token.PayloadSource,
time.Since(result.Token.IssueDate)))); err != nil && cmd.Debug {
log.Fatalln(err)
}
}
}
}
func (cmd *Scanner) Run() (err error) {
cmd.outputChannel = make(FuzzerChannel)
go cmd.displayResults()
cmdFuzzer, err := NewFuzzer(
WithListenAddress(cmd.ListenAddress),
WithTimeout(cmd.Timeout),
WithLDAPLogOutput(cmd.LDAPDebug),
WithOutputChannel(cmd.outputChannel),
WithTokenTranslator(&TokenTranslator{Secret: []byte(cmd.Psk)}),
WithRequestTemplate(cmd.RequestTemplate),
WithPayloadTemplate(cmd.PayloadTemplate),
)
if err != nil {
return err
}
if !cmd.Quiet {
cmd.bar = progressbar.NewOptions(-1,
progressbar.OptionSetWriter(os.Stderr),
progressbar.OptionEnableColorCodes(true),
progressbar.OptionShowIts(),
progressbar.OptionSetWidth(40),
progressbar.OptionSetDescription("[cyan][1/2][reset] Scanning..."),
progressbar.OptionSetTheme(progressbar.Theme{
Saucer: "[green]=[reset]",
SaucerHead: "[green]>[reset]",
SaucerPadding: " ",
BarStart: "[",
BarEnd: "]",
}))
}
cmd.ccm = goccm.New(cmd.MaxConnections)
lineReader := bufio.NewScanner(cmd.InputFile)
for lineReader.Scan() {
line := lineReader.Text()
cmd.ccm.Wait()
go func() {
defer cmd.ccm.Done()
if err := cmdFuzzer.FuzzUrl(line); err != nil && cmd.Debug {
log.Println(err)
}
if cmd.bar != nil {
// #nosec , progress bar can fail
cmd.bar.Add(1)
}
}()
}
if cmd.bar != nil {
cmd.bar.Describe("[cyan][2/2][reset] Waiting...")
}
cmd.ccm.WaitAllDone()
if cmd.Wait < 0 {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
//wait for interrupt
<-c
} else {
time.Sleep(time.Duration(cmd.Wait) * time.Second)
}
close(cmd.outputChannel)
if cmd.OutputFile != nil {
return cmd.OutputFile.Close()
}
return nil
}