-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
227 lines (198 loc) · 5.47 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
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
package main
import (
"bufio"
"flag"
"fmt"
"net"
"os"
"strings"
"sync"
"time"
)
func usage() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] target1 target2 ... \n", os.Args[0])
fmt.Fprintf(os.Stderr, "Options:\n")
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nExamples:\n")
fmt.Fprintf(os.Stderr, " %s 192.168.1.1\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s --list ips.txt\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s --list ips.txt --port 2222\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s 192.168.1.0/24\n", os.Args[0])
}
func printBanner() {
banner := `
___ ___ _ _ _
_ _ ___ __ _ _ _ ___/ __/ __| || (_)___ _ _
| '_/ -_) _ | '_/ -_)__ \__ \ __ | / _ ' \
|_| \___\__, |_|_\___|___/___/_||_|_\___/_||_|
__| |_ |___/__| |_____ _ _
/ _| ' \/ -_) _| / / -_) '_|
\__|_||_\___\__|_\_\___|_|
version 0.1.0 | github.com/xonoxitron/regreSSHion-checker
`
fmt.Println(banner)
}
// Function to check if SSH port is open and retrieve banner
func getSSHBanner(ip string, port int, timeout time.Duration) (string, error) {
target := fmt.Sprintf("%s:%d", ip, port)
conn, err := net.DialTimeout("tcp", target, timeout)
if err != nil {
return "", err
}
defer conn.Close()
conn.SetReadDeadline(time.Now().Add(timeout))
reader := bufio.NewReader(conn)
banner, err := reader.ReadString('\n')
if err != nil {
return "", err
}
return strings.TrimSpace(banner), nil
}
// Function to check vulnerability based on SSH banner
func checkVulnerability(ip string, port int, timeout time.Duration, wg *sync.WaitGroup, resultChan chan<- string) {
defer wg.Done()
banner, err := getSSHBanner(ip, port, timeout)
if err != nil {
resultChan <- fmt.Sprintf("%s:%d closed: %v", ip, port, err)
return
}
vulnerableVersions := []string{
"SSH-2.0-OpenSSH_8.5",
"SSH-2.0-OpenSSH_8.6",
"SSH-2.0-OpenSSH_8.7",
"SSH-2.0-OpenSSH_8.8",
"SSH-2.0-OpenSSH_8.9",
"SSH-2.0-OpenSSH_9.0",
"SSH-2.0-OpenSSH_9.1",
"SSH-2.0-OpenSSH_9.2",
"SSH-2.0-OpenSSH_9.3",
"SSH-2.0-OpenSSH_9.4",
"SSH-2.0-OpenSSH_9.5",
"SSH-2.0-OpenSSH_9.6",
"SSH-2.0-OpenSSH_9.7",
}
excludedVersions := []string{
"SSH-2.0-OpenSSH_8.9p1 Ubuntu-3ubuntu0.10",
"SSH-2.0-OpenSSH_9.3p1 Ubuntu-3ubuntu3.6",
"SSH-2.0-OpenSSH_9.6p1 Ubuntu-3ubuntu13.3",
"SSH-2.0-OpenSSH_9.3p1 Ubuntu-1ubuntu3.6",
"SSH-2.0-OpenSSH_9.2p1 Debian-2+deb12u3",
"SSH-2.0-OpenSSH_8.4p1 Debian-5+deb11u3",
}
isVulnerable := false
for _, version := range vulnerableVersions {
if strings.Contains(banner, version) {
isVulnerable = true
break
}
}
if isVulnerable {
excluded := false
for _, excludedVersion := range excludedVersions {
if banner == excludedVersion {
excluded = true
break
}
}
if !excluded {
resultChan <- fmt.Sprintf("%s:%d vulnerable (running %s)", ip, port, banner)
return
}
}
resultChan <- fmt.Sprintf("%s:%d not vulnerable (running %s)", ip, port, banner)
}
func main() {
printBanner()
var targets []string
var ipListFile string
var port int
var timeout time.Duration
flag.IntVar(&port, "port", 22, "Port number to check (default: 22)")
flag.DurationVar(&timeout, "timeout", 1*time.Second, "Connection timeout in seconds (default: 1 second)")
flag.StringVar(&ipListFile, "list", "", "File containing a list of IP addresses to check")
flag.Usage = usage
flag.Parse()
if flag.NArg() == 0 && ipListFile == "" {
flag.Usage()
os.Exit(1)
}
targets = flag.Args()
if ipListFile != "" {
file, err := os.Open(ipListFile)
if err != nil {
fmt.Printf("❌ [-] Could not read file: %s\n", ipListFile)
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
targets = append(targets, scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Printf("❌ [-] Error reading file: %v\n", err)
return
}
}
var wg sync.WaitGroup
resultChan := make(chan string)
for _, target := range targets {
if strings.Contains(target, "/") {
ips, err := getIPsFromCIDR(target)
if err != nil {
fmt.Printf("❌ [-] Invalid CIDR notation: %s\n", target)
continue
}
for _, ip := range ips {
wg.Add(1)
go checkVulnerability(ip.String(), port, timeout, &wg, resultChan)
}
} else {
wg.Add(1)
go checkVulnerability(target, port, timeout, &wg, resultChan)
}
}
go func() {
wg.Wait()
close(resultChan)
}()
notVulnerableCount := 0
vulnerableCount := 0
closedPortsCount := 0
for result := range resultChan {
if strings.Contains(result, "closed") {
closedPortsCount++
} else if strings.Contains(result, "vulnerable") {
vulnerableCount++
fmt.Printf("🚨 %s\n", result)
} else if strings.Contains(result, "not vulnerable") {
notVulnerableCount++
fmt.Printf("🛡️ %s\n", result)
} else {
fmt.Printf("⚠️ [!] %s\n", result)
}
}
fmt.Printf("\n🔒 Servers with port %d closed: %d\n", port, closedPortsCount)
fmt.Printf("\n📊 Total scanned targets: %d\n", len(targets))
}
// Function to get IPs from CIDR notation
func getIPsFromCIDR(cidr string) ([]net.IP, error) {
ip, ipNet, err := net.ParseCIDR(cidr)
if err != nil {
return nil, err
}
var ips []net.IP
for ip := ip.Mask(ipNet.Mask); ipNet.Contains(ip); incIP(ip) {
ips = append(ips, net.IP{ip[0], ip[1], ip[2], ip[3]})
}
// Remove network address and broadcast address
return ips[1 : len(ips)-1], nil
}
// Function to increment IP address
func incIP(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}