@@ -4,38 +4,49 @@ import (
44 "crypto/tls"
55 "flag"
66 "fmt"
7+ "math/big"
78 "net"
9+ "sync"
810 "time"
911)
1012
1113func main () {
1214 addrPtr := flag .String ("addr" , "127.0.0.1" , "Destination to start scan" )
1315 portPtr := flag .String ("port" , "443" , "Port to scan" )
16+ threadPtr := flag .Int ("thread" , 2 , "Number of threads to scan in parallel" )
1417 flag .Parse ()
15- fmt .Println ("Reality Tls Scanner running: " , * addrPtr , ":" , * portPtr )
18+ fmt .Println ("Reality TLS Scanner running: " , * addrPtr , ":" , * portPtr )
1619 s := Scanner {
1720 addr : * addrPtr ,
1821 port : * portPtr ,
1922 timeout : 10 * time .Second ,
20- numberOfThread : 1 ,
23+ numberOfThread : * threadPtr ,
24+ mu : new (sync.Mutex ),
2125 }
2226 s .Run ()
2327}
2428
2529type Scanner struct {
26- addr string
27- port string
28- timeout time.Duration
30+ addr string
31+ port string
32+ timeout time.Duration
2933 numberOfThread int
34+ mu * sync.Mutex
35+ high net.IP
36+ low net.IP
3037}
3138
32- func (s Scanner ) Run () {
39+ func (s * Scanner ) Run () {
3340 conn , err := net .DialTimeout ("tcp" , s .addr + ":" + s .port , s .timeout )
3441 if err != nil {
3542 fmt .Println ("Dial failed: " , err )
3643 return
3744 }
3845 line := "" + conn .RemoteAddr ().String () + " \t ----- "
46+ s .mu .Lock ()
47+ s .high = conn .RemoteAddr ().(* net.TCPAddr ).IP
48+ s .low = conn .RemoteAddr ().(* net.TCPAddr ).IP
49+ s .mu .Unlock ()
3950 conn .SetDeadline (time .Now ().Add (s .timeout ))
4051 c := tls .Client (conn , & tls.Config {
4152 InsecureSkipVerify : true ,
@@ -44,11 +55,65 @@ func (s Scanner) Run() {
4455 err = c .Handshake ()
4556 if err != nil {
4657 fmt .Println ("" , line , "TLS handshake failed: " , err )
47- return
58+ } else {
59+ defer c .Close ()
60+ state := c .ConnectionState ()
61+ fmt .Println ("" , line , "Found TLS v" , TlsDic [state .Version ], "\t ALPN" , state .NegotiatedProtocol , "\t " , state .PeerCertificates [0 ].Subject )
62+ }
63+ for i := 0 ; i < s .numberOfThread ; i ++ {
64+ go s .Scan (i % 2 == 0 )
4865 }
49- defer c .Close ()
50- state := c .ConnectionState ()
51- fmt .Println ("" , line , "Found TLS v" , TlsDic [state .Version ], "\t ALPN" , state .NegotiatedProtocol , "\t " , state .PeerCertificates [0 ].Subject )
66+ for {
67+ // now the scans are performed in goroutines
68+ }
69+ }
70+
71+ func (s * Scanner ) Scan (increment bool ) {
72+ var addr string
73+ s .mu .Lock ()
74+ if increment {
75+ s .high = nextIP (s .high , increment )
76+ addr = s .high .String ()
77+ } else {
78+ s .low = nextIP (s .low , increment )
79+ addr = s .low .String ()
80+ }
81+ s .mu .Unlock ()
82+ conn , err := net .DialTimeout ("tcp" , addr + ":" + s .port , s .timeout )
83+ if err != nil {
84+ fmt .Println ("Dial failed: " , err )
85+ } else {
86+ line := "" + conn .RemoteAddr ().String () + " \t ----- "
87+ conn .SetDeadline (time .Now ().Add (s .timeout ))
88+ c := tls .Client (conn , & tls.Config {
89+ InsecureSkipVerify : true ,
90+ NextProtos : []string {"h2" , "http/1.1" },
91+ })
92+ err = c .Handshake ()
93+ if err != nil {
94+ fmt .Println ("" , line , "TLS handshake failed: " , err )
95+ } else {
96+ defer c .Close ()
97+ state := c .ConnectionState ()
98+ fmt .Println ("" , line , "Found TLS v" , TlsDic [state .Version ], "\t ALPN" , state .NegotiatedProtocol , "\t " , state .PeerCertificates [0 ].Subject )
99+ }
100+ }
101+ go s .Scan (increment )
102+ }
103+
104+ func nextIP (ip net.IP , increment bool ) net.IP {
105+ // Convert to big.Int and increment
106+ ipb := big .NewInt (0 ).SetBytes ([]byte (ip ))
107+ if increment {
108+ ipb .Add (ipb , big .NewInt (1 ))
109+ } else {
110+ ipb .Sub (ipb , big .NewInt (1 ))
111+ }
112+
113+ // Add leading zeros
114+ b := ipb .Bytes ()
115+ b = append (make ([]byte , len (ip )- len (b )), b ... )
116+ return net .IP (b )
52117}
53118
54119var TlsDic = map [uint16 ]string {
0 commit comments