forked from XTLS/RealiTLScanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
147 lines (138 loc) · 3.27 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
package main
import (
"crypto/tls"
"flag"
"fmt"
"math/big"
"net"
"sync"
"time"
)
func main() {
addrPtr := flag.String("addr", "127.0.0.1", "Destination to start scan")
portPtr := flag.String("port", "443", "Port to scan")
threadPtr := flag.Int("thread", 2, "Number of threads to scan in parallel")
flag.Parse()
fmt.Println("Reality TLS Scanner running: ", *addrPtr, ":", *portPtr)
s := Scanner {
addr: *addrPtr,
port: *portPtr,
timeout: 10 * time.Second,
numberOfThread: *threadPtr,
mu: new(sync.Mutex),
}
s.Run()
}
type Scanner struct {
addr string
port string
timeout time.Duration
numberOfThread int
mu *sync.Mutex
high net.IP
low net.IP
}
func (s *Scanner) Run() {
str := s.addr
addr := net.ParseIP(s.addr)
if addr != nil && addr.To4() == nil {
str = "[" + addr.String() + "]"
}
conn, err := net.DialTimeout("tcp", str+":"+s.port, s.timeout)
if err != nil {
fmt.Println("Dial failed: ", err)
} else {
addr = conn.RemoteAddr().(*net.TCPAddr).IP
line := "" + conn.RemoteAddr().String() + " \t"
conn.SetDeadline(time.Now().Add(s.timeout))
c := tls.Client(conn, &tls.Config {
InsecureSkipVerify: true,
NextProtos: []string{"h2", "http/1.1"},
})
err = c.Handshake()
if err != nil {
fmt.Println("", line, "TLS handshake failed: ", err)
} else {
state := c.ConnectionState()
alpn := state.NegotiatedProtocol
if alpn == "" {
alpn = " "
}
fmt.Println("", line, "----- Found TLS v", TlsDic[state.Version], "\tALPN", alpn, "\t", state.PeerCertificates[0].Subject)
c.Close()
}
}
if addr == nil {
fmt.Println("Invalid address format")
return
}
s.mu.Lock()
s.high = addr
s.low = addr
s.mu.Unlock()
for i := 0; i < s.numberOfThread; i++ {
go s.Scan(i % 2 == 0)
}
for {
// now the scans are performed in goroutines
}
}
func (s *Scanner) Scan(increment bool) {
var addr net.IP
s.mu.Lock()
if increment {
s.high = nextIP(s.high, increment)
addr = s.high
} else {
s.low = nextIP(s.low, increment)
addr = s.low
}
s.mu.Unlock()
str := addr.String()
if addr.To4() == nil {
str = "[" + str + "]"
}
conn, err := net.DialTimeout("tcp", str+":"+s.port, s.timeout)
if err != nil {
fmt.Println("Dial failed: ", err)
} else {
line := "" + conn.RemoteAddr().String() + " \t"
conn.SetDeadline(time.Now().Add(s.timeout))
c := tls.Client(conn, &tls.Config {
InsecureSkipVerify: true,
NextProtos: []string{"h2", "http/1.1"},
})
err = c.Handshake()
if err != nil {
fmt.Println("", line, "TLS handshake failed: ", err)
} else {
defer c.Close()
state := c.ConnectionState()
alpn := state.NegotiatedProtocol
if alpn == "" {
alpn = " "
}
fmt.Println("", line, "----- Found TLS v", TlsDic[state.Version], "\tALPN", alpn, "\t", state.PeerCertificates[0].Subject)
}
}
go s.Scan(increment)
}
func nextIP(ip net.IP, increment bool) net.IP {
// Convert to big.Int and increment
ipb := big.NewInt(0).SetBytes([]byte(ip))
if increment {
ipb.Add(ipb, big.NewInt(1))
} else {
ipb.Sub(ipb, big.NewInt(1))
}
// Add leading zeros
b := ipb.Bytes()
b = append(make([]byte, len(ip)-len(b)), b...)
return net.IP(b)
}
var TlsDic = map[uint16]string{
0x0301: "1.0",
0x0302: "1.1",
0x0303: "1.2",
0x0304: "1.3",
}