-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
266 lines (230 loc) · 6.25 KB
/
main.go
File metadata and controls
266 lines (230 loc) · 6.25 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package main
import (
"flag"
"fmt"
"net"
"os"
"strconv"
"sync"
"sync/atomic"
"time"
"golang.org/x/crypto/ssh"
)
// ANSI Color Codes
const (
ColorReset = "\033[0m"
ColorRed = "\033[31m"
ColorGreen = "\033[32m"
ColorYellow = "\033[33m"
ColorBlue = "\033[34m"
ColorCyan = "\033[36m"
)
type Config struct {
User string
Password string
Workers int
Timeout time.Duration
Port int
OutputFile string
CIDR string
}
func parseConfig() *Config {
cfg := &Config{}
flag.StringVar(&cfg.User, "u", "test", "SSH username")
flag.StringVar(&cfg.Password, "p", "123456", "SSH password")
flag.IntVar(&cfg.Workers, "w", 100, "Number of concurrent workers")
flag.DurationVar(&cfg.Timeout, "t", 3*time.Second, "SSH connection timeout")
flag.IntVar(&cfg.Port, "P", 22, "SSH port")
flag.StringVar(&cfg.OutputFile, "o", "", "Output file for successful IPs")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] <cidr> [user] [password]\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\nOptions:\n")
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nExamples:\n")
fmt.Fprintf(os.Stderr, " %s 192.168.1.0/24\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s -u admin -p password -w 200 192.168.1.0/24\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s 3 root 123456 (Equivalent to: -u root -p 123456 192.168.3.0/24)\n", os.Args[0])
}
flag.Parse()
switch flag.NArg() {
case 1:
cfg.CIDR = flag.Arg(0)
case 3:
cfg.CIDR = flag.Arg(0)
cfg.User = flag.Arg(1)
cfg.Password = flag.Arg(2)
default:
flag.Usage()
os.Exit(1)
}
return cfg
}
func main() {
cfg := parseConfig()
ip, ipNet, err := parseInput(cfg.CIDR)
if err != nil {
fmt.Printf("%sInvalid CIDR or IP: %v%s\n", ColorRed, err, ColorReset)
os.Exit(1)
}
// Calculate total IPs
ones, bits := ipNet.Mask.Size()
totalIPs := uint64(1) << (bits - ones)
if bits == 0 { // Handle non-standard masks or issues gracefully
totalIPs = 0
}
// Use a channel for IPs to save memory on large ranges
ips := generateIPs(ip, ipNet, cfg.Workers)
fmt.Printf("%sScanning %s (%d IPs) with %d workers on port %d...%s\n",
ColorCyan, cfg.CIDR, totalIPs, cfg.Workers, cfg.Port, ColorReset)
scan(ips, cfg, totalIPs)
}
func parseInput(input string) (net.IP, *net.IPNet, error) {
// Check if input is a single integer (backward compatibility)
// e.g. "3" -> "192.168.3.0/24"
if _, err := strconv.Atoi(input); err == nil {
input = fmt.Sprintf("192.168.%s.0/24", input)
}
ip, ipNet, err := net.ParseCIDR(input)
if err != nil {
// Try to parse as single IP
if ip := net.ParseIP(input); ip != nil {
input = input + "/32"
_, ipNet, err = net.ParseCIDR(input)
return ip, ipNet, err
}
return nil, nil, err
}
return ip, ipNet, nil
}
func generateIPs(ip net.IP, ipNet *net.IPNet, workers int) chan string {
out := make(chan string, workers*2) // Buffer slightly to keep workers busy
go func() {
defer close(out)
// Ensure we are working with 4-byte IP for IPv4 to avoid confusion
if ip4 := ip.To4(); ip4 != nil {
ip = ip4
}
// ip.Mask(ipNet.Mask) gives the network address.
// We clone it because we modify it in the loop.
currentIP := make(net.IP, len(ip))
copy(currentIP, ip.Mask(ipNet.Mask))
// Iterate through the range
for ; ipNet.Contains(currentIP); inc(currentIP) {
out <- currentIP.String()
}
}()
return out
}
func inc(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}
func scan(ips chan string, cfg *Config, totalIPs uint64) {
var (
wg sync.WaitGroup
failNum, okNum atomic.Int32
processedNum atomic.Int32
sem = make(chan struct{}, cfg.Workers)
outputMutex sync.Mutex
printMutex sync.Mutex // Synchronize stdout
f *os.File
err error
)
if cfg.OutputFile != "" {
f, err = os.Create(cfg.OutputFile)
if err != nil {
fmt.Printf("%sFailed to create output file: %v%s\n", ColorRed, err, ColorReset)
return
}
defer f.Close()
}
startTime := time.Now()
// Helper to print progress bar
printProgress := func() {
current := processedNum.Load()
percent := 0.0
if totalIPs > 0 {
percent = float64(current) / float64(totalIPs) * 100
}
fmt.Printf("\rProgress: %d/%d (%.1f%%) | Found: %s%d%s",
current, totalIPs, percent, ColorGreen, okNum.Load(), ColorReset)
}
// Progress updater
done := make(chan struct{})
go func() {
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ticker.C:
printMutex.Lock()
printProgress()
printMutex.Unlock()
case <-done:
return
}
}
}()
for ip := range ips {
wg.Add(1)
sem <- struct{}{} // Acquire token
go func(targetIP string) {
defer wg.Done()
defer func() { <-sem }() // Release token
addr := fmt.Sprintf("%s:%d", targetIP, cfg.Port)
if err := tryConnectSSH(addr, cfg.User, cfg.Password, cfg.Timeout); err == nil {
okNum.Add(1)
// Critical section for printing
printMutex.Lock()
// Clear line to avoid messing up progress bar
fmt.Printf("\r\033[K")
fmt.Printf("%s[+] %s%s\n", ColorGreen, targetIP, ColorReset)
// Immediately reprint progress bar to avoid flashing
printProgress()
printMutex.Unlock()
if f != nil {
outputMutex.Lock()
f.WriteString(targetIP + "\n")
outputMutex.Unlock()
}
} else {
failNum.Add(1)
}
processedNum.Add(1)
}(ip)
}
wg.Wait()
close(done)
duration := time.Since(startTime)
rate := float64(processedNum.Load()) / duration.Seconds()
// Final clear and summary
printMutex.Lock()
fmt.Printf("\r\033[K")
fmt.Println("--------------------")
fmt.Printf("Scan Complete in %s%v%s\n", ColorCyan, duration.Round(time.Millisecond), ColorReset)
fmt.Printf("Rate: %s%.2f IPs/s%s\n", ColorCyan, rate, ColorReset)
fmt.Printf("Results: %s%d Success%s, %s%d Failed%s\n",
ColorGreen, okNum.Load(), ColorReset,
ColorRed, failNum.Load(), ColorReset)
printMutex.Unlock()
}
func tryConnectSSH(addr, user, password string, timeout time.Duration) error {
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: timeout,
}
client, err := ssh.Dial("tcp", addr, config)
if err != nil {
return err
}
client.Close()
return nil
}