-
Notifications
You must be signed in to change notification settings - Fork 32
/
jarm.go
64 lines (56 loc) · 1.55 KB
/
jarm.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
package jarm
import (
"context"
"fmt"
"net"
"strings"
"time"
gojarm "github.com/hdm/jarm-go"
connpool "github.com/projectdiscovery/utils/conn/connpool"
)
// PoolCount defines how many connection are kept in the pool
var PoolCount = 3
// fingerprint probes a single host/port
func HashWithDialer(dialer connpool.Dialer, host string, port int, duration int) (string, error) {
var results []string
addr := net.JoinHostPort(host, fmt.Sprintf("%d", port))
timeout := time.Duration(duration) * time.Second
ctx, cancel := context.WithTimeout(context.Background(), (time.Duration(duration*PoolCount) * time.Second))
defer cancel()
// using connection pool as we need multiple probes
pool, err := connpool.NewOneTimePool(ctx, addr, PoolCount)
if err != nil {
return "", err
}
pool.Dialer = dialer
defer func() { _ = pool.Close() }()
go func() { _ = pool.Run() }()
for _, probe := range gojarm.GetProbes(host, port) {
conn, err := pool.Acquire(ctx)
if err != nil {
continue
}
if conn == nil {
continue
}
_ = conn.SetWriteDeadline(time.Now().Add(timeout))
_, err = conn.Write(gojarm.BuildProbe(probe))
if err != nil {
results = append(results, "")
_ = conn.Close()
continue
}
_ = conn.SetReadDeadline(time.Now().Add(timeout))
buff := make([]byte, 1484)
_, _ = conn.Read(buff)
_ = conn.Close()
ans, err := gojarm.ParseServerHello(buff, probe)
if err != nil {
results = append(results, "")
continue
}
results = append(results, ans)
}
hash := gojarm.RawHashToFuzzyHash(strings.Join(results, ","))
return hash, nil
}