This repository has been archived by the owner on Jun 14, 2022. It is now read-only.
/
main.go
332 lines (268 loc) · 6.58 KB
/
main.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
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
package main
import (
"flag"
"fmt"
"net"
"net/url"
"os"
"strconv"
"strings"
"sync"
"time"
"golang.org/x/net/proxy"
log "github.com/sirupsen/logrus"
"github.com/RumbleDiscovery/jarm-go"
"github.com/RumbleDiscovery/rumble-tools/pkg/rnd"
)
// Version is set by the goreleaser build
var Version = "dev"
var defaultPorts = flag.String("p", "443", "default ports")
var workerCount = flag.Int("w", 256, "worker count")
var quietMode = flag.Bool("q", false, "quiet mode")
var retries = flag.Int("r", 0, "number of times to retry dialing")
// ValidPort determines if a port number is valid
func ValidPort(pnum int) bool {
if pnum < 1 || pnum > 65535 {
return false
}
return true
}
// CrackPortsWithDefaults turns a comma-delimited port list into an array, handling defaults
func CrackPortsWithDefaults(pspec string, defaults []uint16) ([]int, error) {
results := []int{}
// Use a map to dedup and shuffle ports
ports := make(map[int]bool)
bits := strings.Split(pspec, ",")
for _, bit := range bits {
// Support the magic strings "default" and "defaults"
if bit == "default" || bit == "defaults" {
for _, pnum := range defaults {
ports[int(pnum)] = true
}
continue
}
// Split based on dash
prange := strings.Split(bit, "-")
// Scan all ports if the specifier is a single dash
if bit == "-" {
prange = []string{"1", "65535"}
}
// No port range
if len(prange) == 1 {
pnum, err := strconv.Atoi(bit)
if err != nil || !ValidPort(pnum) {
return results, fmt.Errorf("invalid port %s", bit)
}
// Record the valid port
ports[pnum] = true
continue
}
if len(prange) != 2 {
return results, fmt.Errorf("invalid port range %s (%d)", prange, len(prange))
}
pstart, err := strconv.Atoi(prange[0])
if err != nil || !ValidPort(pstart) {
return results, fmt.Errorf("invalid start port %d", pstart)
}
pstop, err := strconv.Atoi(prange[1])
if err != nil || !ValidPort(pstop) {
return results, fmt.Errorf("invalid stop port %d", pstop)
}
if pstart > pstop {
return results, fmt.Errorf("invalid port range %d-%d", pstart, pstop)
}
for pnum := pstart; pnum <= pstop; pnum++ {
ports[pnum] = true
}
}
// Create the results from the map
for port := range ports {
results = append(results, port)
}
return results, nil
}
// Fingerprint probes a single host/port
func Fingerprint(t target, och chan result) {
results := []string{}
for _, probe := range jarm.GetProbes(t.Host, t.Port) {
dialer := proxy.FromEnvironmentUsing(&net.Dialer{Timeout: time.Second * 2})
addr := net.JoinHostPort(t.Host, fmt.Sprintf("%d", t.Port))
c := net.Conn(nil)
n := 0
for c == nil && n <= t.Retries {
// Ignoring error since error message was already being dropped.
// Also, if theres an error, c == nil.
if c, _ = dialer.Dial("tcp", addr); c != nil || t.Retries == 0 {
break
}
bo := t.Backoff
if bo == nil {
bo = DefualtBackoff
}
time.Sleep(bo(n, t.Retries))
n++
}
if c == nil {
return
}
data := jarm.BuildProbe(probe)
c.SetWriteDeadline(time.Now().Add(time.Second * 5))
_, err := c.Write(data)
if err != nil {
results = append(results, "")
c.Close()
continue
}
c.SetReadDeadline(time.Now().Add(time.Second * 5))
buff := make([]byte, 1484)
c.Read(buff)
c.Close()
ans, err := jarm.ParseServerHello(buff, probe)
if err != nil {
results = append(results, "")
continue
}
results = append(results, ans)
}
och <- result{
Target: t,
Hash: jarm.RawHashToFuzzyHash(strings.Join(results, ",")),
}
}
var DefualtBackoff = func(r, m int) time.Duration {
return time.Second
}
type target struct {
Host string
Port int
Retries int
Backoff func(r, m int) time.Duration
}
type result struct {
Target target
Hash string
Error error
}
func main() {
flag.Parse()
if len(os.Args) < 2 {
log.Fatalf("usage: ./jarm -p <ports> [host] <host:8443> <https://host:port> <host,port>...")
}
if *workerCount < 1 {
log.Fatalf("invalid worker count: %d", *workerCount)
}
if *quietMode {
dn, _ := os.Create(os.DevNull)
log.SetOutput(dn)
}
defaultPorts, err := CrackPortsWithDefaults(*defaultPorts, []uint16{})
if err != nil {
log.Fatalf("invalid ports: %s", err)
}
tch := make(chan target, 1)
och := make(chan result, 1)
wgo := sync.WaitGroup{}
wgt := sync.WaitGroup{}
for x := 0; x <= *workerCount; x++ {
wgt.Add(1)
go func() {
defer wgt.Done()
for t := range tch {
Fingerprint(t, och)
}
}()
}
// Output consolidator
wgo.Add(1)
go func() {
defer wgo.Done()
for o := range och {
if o.Error != nil {
log.Printf("failed to scan %s:%d: %s", o.Target.Host, o.Target.Port, o.Error)
continue
}
if len(o.Target.Host) > 24 {
fmt.Printf("JARM\t%s:%d\t%s\n", o.Target.Host, o.Target.Port, o.Hash)
} else {
fmt.Printf("JARM\t%24s:%d\t%s\n", o.Target.Host, o.Target.Port, o.Hash)
}
}
}()
// Process targets
for _, s := range flag.Args() {
t := target{}
// Try parsing as a URL first
if u, err := url.Parse(s); err == nil {
t.Host = u.Hostname()
port, _ := strconv.Atoi(u.Port())
t.Port = port
}
// Next try parsing as an address:port pair
if t.Host == "" {
host, portStr, _ := net.SplitHostPort(s)
port, _ := strconv.Atoi(portStr)
t.Host = host
t.Port = port
}
// Next try parsing as a host,port pair
if t.Host == "" {
bits := strings.SplitN(s, ",", 2)
if len(bits) == 2 && bits[0] != "" {
t.Host = bits[0]
port, _ := strconv.Atoi(bits[1])
t.Port = port
}
}
// Finally try parsing as a host:port pair
if t.Host == "" {
bits := strings.SplitN(s, ":", 2)
if bits[0] != "" {
t.Host = bits[0]
}
if len(bits) == 2 && bits[0] != "" {
port, _ := strconv.Atoi(bits[1])
t.Port = port
}
}
hosts := []string{t.Host}
for _, host := range hosts {
// Support CIDR networks as targets
hch := make(chan string, 1)
qch := make(chan int, 1)
hwg := sync.WaitGroup{}
hwg.Add(1)
go func() {
defer hwg.Done()
for thost := range hch {
ports := defaultPorts
if t.Port != 0 {
ports = []int{t.Port}
}
for _, port := range ports {
tch <- target{
Host: thost,
Port: port,
Retries: *retries,
}
}
}
}()
// Try to iterate the host as a CIDR range
herr := rnd.AddressesFromCIDR(host, hch, qch)
// Not a parseable range, handle it as a bare host instead
if herr != nil {
hch <- host
}
// Wrap up and wait
close(hch)
hwg.Wait()
close(qch)
}
}
// Wait for scans to complete
close(tch)
wgt.Wait()
// Wait for output to finish
close(och)
wgo.Wait()
}