-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan.go
More file actions
237 lines (205 loc) · 6.52 KB
/
scan.go
File metadata and controls
237 lines (205 loc) · 6.52 KB
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
package main
import (
"bufio"
"fmt"
"math/rand"
"net"
"os/exec"
"regexp"
"sort"
"strconv"
"sync"
"time"
)
type PingResult struct {
IP string
RTT time.Duration
}
type EndpointResult struct {
Endpoint string
Latency time.Duration
Protocol string
}
func generateIPv4Addresses() []string {
var ips []string
subnets := []string{
"162.159.192.", "162.159.193.", "162.159.195.",
"188.114.96.", "188.114.97.", "188.114.98.", "188.114.99.",
}
for _, subnet := range subnets {
for i := 0; i < 5; i++ {
ips = append(ips, fmt.Sprintf("%s%d", subnet, rand.Intn(256)))
}
}
return ips
}
func generateIPv6Addresses() []string {
var ips []string
prefixes := []string{"2606:4700:d0::", "2606:4700:d1::"}
for _, prefix := range prefixes {
for i := 0; i < 5; i++ {
ip := fmt.Sprintf("%s%x:%x:%x:%x",
prefix, rand.Intn(0xffff), rand.Intn(0xffff), rand.Intn(0xffff), rand.Intn(0xffff))
ips = append(ips, ip)
}
}
return ips
}
func pingWithTermux(ipAddr string) (time.Duration, error) {
cmd := exec.Command("ping", "-c", "3", "-W", "2", ipAddr)
stdout, err := cmd.StdoutPipe()
if err != nil {
return 0, err
}
if err := cmd.Start(); err != nil {
return 0, err
}
scanner := bufio.NewScanner(stdout)
var avgRtt time.Duration
rttRegex := regexp.MustCompile(`rtt min/avg/max/mdev = [\d.]+/([\d.]+)/[\d.]+/[\d.]+ ms`)
for scanner.Scan() {
line := scanner.Text()
matches := rttRegex.FindStringSubmatch(line)
if len(matches) > 1 {
avg, err := strconv.ParseFloat(matches[1], 64)
if err == nil {
avgRtt = time.Duration(avg * float64(time.Millisecond))
}
}
}
if err := cmd.Wait(); err != nil {
return 0, fmt.Errorf("no response from host")
}
if avgRtt == 0 {
return 0, fmt.Errorf("could not parse RTT")
}
return avgRtt, nil
}
func scanPort(ip string, port int, protocol string, timeout time.Duration, resultsChan chan<- EndpointResult, wg *sync.WaitGroup) {
defer wg.Done()
address := net.JoinHostPort(ip, strconv.Itoa(port))
start := time.Now()
conn, err := net.DialTimeout(protocol, address, timeout)
latency := time.Since(start)
if err == nil {
conn.Close()
resultsChan <- EndpointResult{Endpoint: address, Latency: latency, Protocol: protocol}
}
}
func main() {
tcpTimeout := 5 * time.Second
udpTimeout := 5 * time.Second
rand.Seed(time.Now().UnixNano())
fmt.Println("Step 1: Finding best IPs with ping...")
allIPs := append(generateIPv4Addresses(), generateIPv6Addresses()...)
var pingWg sync.WaitGroup
pingResultsChan := make(chan PingResult, len(allIPs))
for _, ip := range allIPs {
pingWg.Add(1)
go func(ipAddr string) {
defer pingWg.Done()
rtt, err := pingWithTermux(ipAddr)
if err == nil {
pingResultsChan <- PingResult{IP: ipAddr, RTT: rtt}
}
}(ip)
}
pingWg.Wait()
close(pingResultsChan)
var bestIPs []PingResult
for result := range pingResultsChan {
bestIPs = append(bestIPs, result)
}
if len(bestIPs) == 0 {
fmt.Println("No responsive IPs found in Step 1. Exiting.")
return
}
sort.Slice(bestIPs, func(i, j int) bool {
return bestIPs[i].RTT < bestIPs[j].RTT
})
ipToPing := make(map[string]time.Duration)
for _, ipResult := range bestIPs {
ipToPing[ipResult.IP] = ipResult.RTT
}
fmt.Println("Step 1 Complete. Best IPs found.")
fmt.Println("\nStep 2: Scanning specific TCP and UDP ports on all found IPs...")
tcpPorts := []int{443, 8886, 908, 8854, 4198, 955, 988, 3854, 894, 7156, 1074, 939, 864, 854, 1070, 3476, 1387, 7559, 890, 1018}
udpPorts := []int{500, 1701, 4500, 2408, 878, 2371}
var portWg sync.WaitGroup
endpointResultsChan := make(chan EndpointResult, len(bestIPs)*(len(tcpPorts)+len(udpPorts)))
for _, ipResult := range bestIPs {
for _, port := range tcpPorts {
portWg.Add(1)
go scanPort(ipResult.IP, port, "tcp", tcpTimeout, endpointResultsChan, &portWg)
}
}
for _, ipResult := range bestIPs {
for _, port := range udpPorts {
portWg.Add(1)
go scanPort(ipResult.IP, port, "udp", udpTimeout, endpointResultsChan, &portWg)
}
}
portWg.Wait()
close(endpointResultsChan)
var tcpResults []EndpointResult
var udpResults []EndpointResult
for result := range endpointResultsChan {
if result.Protocol == "tcp" {
tcpResults = append(tcpResults, result)
} else {
udpResults = append(udpResults, result)
}
}
if len(tcpResults) == 0 && len(udpResults) == 0 {
fmt.Println("\n-------------------------------------------------------------")
fmt.Println("CRITICAL: Could not find any open TCP or UDP ports.")
fmt.Println("This may be due to heavy network restrictions.")
fmt.Println("-------------------------------------------------------------")
return
}
fmt.Println("\n--- TCP Results ---")
if len(tcpResults) > 0 {
sort.Slice(tcpResults, func(i, j int) bool {
return tcpResults[i].Latency < tcpResults[j].Latency
})
bestEndpoint := tcpResults[0]
host, _, _ := net.SplitHostPort(bestEndpoint.Endpoint)
realPing := ipToPing[host]
fmt.Printf("🏆 Best TCP Endpoint: %s\n", bestEndpoint.Endpoint)
fmt.Printf(" Latency: %.2f ms (Real Ping: %.2f ms)\n\n", float64(bestEndpoint.Latency.Nanoseconds())/1e6, float64(realPing.Nanoseconds())/1e6)
fmt.Println("--- Top 6 TCP Endpoints ---")
for i, result := range tcpResults {
if i >= 6 {
break
}
host, _, _ := net.SplitHostPort(result.Endpoint)
realPing := ipToPing[host]
fmt.Printf("%d. Endpoint: %s (Latency: %.2f ms, Real Ping: %.2f ms)\n", i+1, result.Endpoint, float64(result.Latency.Nanoseconds())/1e6, float64(realPing.Nanoseconds())/1e6)
}
} else {
fmt.Println("No open TCP Endpoints were found.")
}
fmt.Println("\n--- UDP Results ---")
if len(udpResults) > 0 {
sort.Slice(udpResults, func(i, j int) bool {
return udpResults[i].Latency < udpResults[j].Latency
})
bestEndpoint := udpResults[0]
host, _, _ := net.SplitHostPort(bestEndpoint.Endpoint)
realPing := ipToPing[host]
fmt.Printf("🏆 Best UDP Endpoint: %s\n", bestEndpoint.Endpoint)
fmt.Printf(" Latency: %.2f ms (Real Ping: %.2f ms)\n\n", float64(bestEndpoint.Latency.Nanoseconds())/1e6, float64(realPing.Nanoseconds())/1e6)
fmt.Println("--- Top 6 UDP Endpoints ---")
for i, result := range udpResults {
if i >= 6 {
break
}
host, _, _ := net.SplitHostPort(result.Endpoint)
realPing := ipToPing[host]
fmt.Printf("%d. Endpoint: %s (Latency: %.2f ms, Real Ping: %.2f ms)\n", i+1, result.Endpoint, float64(result.Latency.Nanoseconds())/1e6, float64(realPing.Nanoseconds())/1e6)
}
} else {
fmt.Println("No open UDP Endpoints were found.")
}
fmt.Println("\n(Latency is the connection time to the port. Real Ping is the ICMP echo time to the IP.)")
}