Skip to content

Commit c5c7895

Browse files
ghggnyuhan6665
authored andcommitted
log useful IPs to results.txt
hide failed logs by default add param to set handshake timeout (default 10)
1 parent a29e0f7 commit c5c7895

File tree

1 file changed

+45
-22
lines changed

1 file changed

+45
-22
lines changed

main.go

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"math/big"
88
"net"
9+
"os"
910
"sync"
1011
"time"
1112
)
@@ -14,21 +15,35 @@ func main() {
1415
addrPtr := flag.String("addr", "127.0.0.1", "Destination to start scan")
1516
portPtr := flag.String("port", "443", "Port to scan")
1617
threadPtr := flag.Int("thread", 2, "Number of threads to scan in parallel")
18+
outPutFile := flag.Bool("o", true, "Number of threads to scan in parallel")
19+
timeOutPtr := flag.Int("timeOut", 10, "Time out of a scan")
20+
showFailPtr := flag.Bool("showFail", false, "Is Show fail logs")
1721
flag.Parse()
1822
fmt.Println("Reality TLS Scanner running: ", *addrPtr, ":", *portPtr)
19-
s := Scanner {
20-
addr: *addrPtr,
21-
port: *portPtr,
22-
timeout: 10 * time.Second,
23+
s := Scanner{
24+
addr: *addrPtr,
25+
port: *portPtr,
26+
showFail: *showFailPtr,
27+
output: *outPutFile,
28+
timeout: time.Duration(*timeOutPtr) * time.Second,
2329
numberOfThread: *threadPtr,
24-
mu: new(sync.Mutex),
30+
mu: new(sync.Mutex),
2531
}
32+
if *outPutFile {
33+
s.logFile, _ = os.OpenFile("results.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
34+
}
35+
location, _ := time.LoadLocation("Asia/Shanghai")
36+
s.logFile.WriteString("start scan at : " + time.Now().In(location).String() + "\n")
37+
defer s.logFile.Close()
2638
s.Run()
2739
}
2840

2941
type Scanner struct {
3042
addr string
3143
port string
44+
output bool
45+
showFail bool
46+
logFile *os.File
3247
timeout time.Duration
3348
numberOfThread int
3449
mu *sync.Mutex
@@ -39,6 +54,10 @@ type Scanner struct {
3954
func (s *Scanner) Run() {
4055
str := s.addr
4156
addr := net.ParseIP(s.addr)
57+
if addr == nil {
58+
fmt.Println("Invalid address format")
59+
return
60+
}
4261
if addr != nil && addr.To4() == nil {
4362
str = "[" + addr.String() + "]"
4463
}
@@ -49,9 +68,9 @@ func (s *Scanner) Run() {
4968
addr = conn.RemoteAddr().(*net.TCPAddr).IP
5069
line := "" + conn.RemoteAddr().String() + " \t"
5170
conn.SetDeadline(time.Now().Add(s.timeout))
52-
c := tls.Client(conn, &tls.Config {
71+
c := tls.Client(conn, &tls.Config{
5372
InsecureSkipVerify: true,
54-
NextProtos: []string{"h2", "http/1.1"},
73+
NextProtos: []string{"h2", "http/1.1"},
5574
})
5675
err = c.Handshake()
5776
if err != nil {
@@ -67,16 +86,12 @@ func (s *Scanner) Run() {
6786
}
6887
}
6988

70-
if addr == nil {
71-
fmt.Println("Invalid address format")
72-
return
73-
}
7489
s.mu.Lock()
7590
s.high = addr
7691
s.low = addr
7792
s.mu.Unlock()
7893
for i := 0; i < s.numberOfThread; i++ {
79-
go s.Scan(i % 2 == 0)
94+
go s.Scan(i%2 == 0)
8095
}
8196
for {
8297
// now the scans are performed in goroutines
@@ -104,39 +119,47 @@ func (s *Scanner) Scan(increment bool) {
104119
} else {
105120
line := "" + conn.RemoteAddr().String() + " \t"
106121
conn.SetDeadline(time.Now().Add(s.timeout))
107-
c := tls.Client(conn, &tls.Config {
122+
c := tls.Client(conn, &tls.Config{
108123
InsecureSkipVerify: true,
109-
NextProtos: []string{"h2", "http/1.1"},
124+
NextProtos: []string{"h2", "http/1.1"},
110125
})
111126
err = c.Handshake()
112127
if err != nil {
113-
fmt.Println("", line, "TLS handshake failed: ", err)
128+
if s.showFail {
129+
fmt.Println("", line, "TLS handshake failed: ", err)
130+
}
114131
} else {
115132
defer c.Close()
116133
state := c.ConnectionState()
117134
alpn := state.NegotiatedProtocol
118135
if alpn == "" {
119136
alpn = " "
120137
}
121-
fmt.Println("", line, "----- Found TLS v", TlsDic[state.Version], "\tALPN", alpn, "\t", state.PeerCertificates[0].Subject)
138+
outStr := fmt.Sprint("", line, "----- Found TLS v", TlsDic[state.Version], "\tALPN ", alpn, "\t", state.PeerCertificates[0].Subject)
139+
if state.Version == 0x0304 && alpn == "h2" {
140+
if s.output {
141+
s.logFile.WriteString(outStr + "\n")
142+
}
143+
}
144+
fmt.Println(outStr)
122145
}
123146
}
124147
go s.Scan(increment)
125148
}
126149

127150
func nextIP(ip net.IP, increment bool) net.IP {
128-
// Convert to big.Int and increment
129-
ipb := big.NewInt(0).SetBytes([]byte(ip))
151+
// Convert to big.Int and increment
152+
ipb := big.NewInt(0).SetBytes([]byte(ip))
130153
if increment {
131154
ipb.Add(ipb, big.NewInt(1))
132155
} else {
133156
ipb.Sub(ipb, big.NewInt(1))
134157
}
135158

136-
// Add leading zeros
137-
b := ipb.Bytes()
138-
b = append(make([]byte, len(ip)-len(b)), b...)
139-
return net.IP(b)
159+
// Add leading zeros
160+
b := ipb.Bytes()
161+
b = append(make([]byte, len(ip)-len(b)), b...)
162+
return net.IP(b)
140163
}
141164

142165
var TlsDic = map[uint16]string{

0 commit comments

Comments
 (0)