-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
251 lines (225 loc) · 5.77 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package main
import (
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"os/signal"
"strings"
"time"
"github.com/hashicorp/mdns"
"golang.org/x/crypto/ssh"
)
func main() {
log.SetOutput(io.Discard)
// Make a channel for results and start listening
entriesCh := make(chan *mdns.ServiceEntry, 4)
doneCh := make(chan struct{})
go func() {
printEntries(entriesCh)
close(doneCh)
}()
// Make a new mDNS lookup parameters structure
params := &mdns.QueryParam{
Service: "_services._dns-sd._udp",
Domain: "local",
Timeout: time.Second * 5,
Entries: entriesCh,
}
// Start the lookup
lookupErr := mdns.Query(params)
if lookupErr != nil && !strings.Contains(lookupErr.Error(), "Failed to query instance") {
fmt.Println("Error from mDNS query:", lookupErr)
return
}
// Close the entries channel after the query is done
close(entriesCh)
// Wait for the printEntries goroutine to finish
<-doneCh
// Wait for CTRL+C
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
<-sig
fmt.Println("\nLeaving...")
}
func printEntries(entriesCh chan *mdns.ServiceEntry) {
totalDevices := 0
totalOpenPorts := 0
serviceCounts := make(map[string]int)
for entry := range entriesCh {
totalDevices++
fmt.Println("Found entry:")
fmt.Println("Name: ", entry.Name)
fmt.Println("Host: ", entry.Host)
fmt.Println("AddrV4: ", entry.AddrV4)
fmt.Println("AddrV6: ", entry.AddrV6)
fmt.Println("Port: ", entry.Port)
if len(entry.InfoFields) > 0 {
fmt.Println("Info:")
for key, value := range entry.InfoFields {
fmt.Printf(" %d: %s\n", key, value)
}
}
fmt.Println("-------------------")
fmt.Println("Scanning ports...")
for port := 1; port <= 10000; port++ {
fmt.Printf("Scanning port %d/10000...\r", port)
address := fmt.Sprintf("%s:%d", entry.AddrV4, port)
conn, err := net.DialTimeout("tcp", address, 3*time.Second)
if err != nil {
continue
}
conn.Close()
totalOpenPorts++
fmt.Printf("\nPort %d is open\n", port)
service := identifyService(port)
serviceMsg := probeService(address)
if serviceMsg != "" {
fmt.Printf("Service message: %s\n", serviceMsg)
}
if service != "" {
fmt.Printf("Port %d is likely associated with %s\n", port, service)
serviceCounts[service]++
if service == "HTTP" {
interactWithHTTP(entry.AddrV4.String(), port)
} else if service == "SSH" {
interactWithSSH(entry.AddrV4.String(), port)
}
}
}
}
fmt.Println("Total devices:", totalDevices)
fmt.Println("Total open ports:", totalOpenPorts)
for service, count := range serviceCounts {
fmt.Printf("Service %s found %d times\n", service, count)
}
}
func identifyService(port int) string {
switch port {
case 22:
return "SSH"
case 80, 443:
return "HTTP"
case 554:
return "RTSP"
case 5353:
return "mDNS"
case 8008, 8009:
return "Google Cast"
case 9000:
return "DLNA"
case 123:
return "NTP"
case 1900:
return "SSDP"
case 2869:
return "UPnP"
case 5350, 5351:
return "Bonjour Sleep Proxy"
case 9090:
return "AirPlay"
case 9091:
return "AirTunes"
case 1901:
return "PlayStation"
case 32400:
return "Plex Media Server"
case 3689:
return "iTunes"
case 5357:
return "Web Services for Devices"
case 10243:
return "Windows Remote Management"
case 5000:
return "Synology DiskStation Manager"
case 5431:
return "UPnP IGD"
case 32469:
return "Roku Media Server"
default:
return ""
}
}
func interactWithHTTP(ip string, port int) {
url := fmt.Sprintf("http://%s:%d", ip, port)
resp, err := http.Get(url)
if err != nil {
fmt.Printf("Error making HTTP request to %s: %v\n", url, err)
return
}
defer resp.Body.Close()
fmt.Printf("Got HTTP response from %s: %s\n", url, resp.Status)
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Error reading HTTP response body from %s: %v\n", url, err)
return
}
bodyStr := string(bodyBytes)
if len(bodyStr) > 100 {
bodyStr = bodyStr[:100] + "..."
}
fmt.Printf("Beginning of response body from %s: %s\n", url, bodyStr)
}
func interactWithSSH(ip string, port int) {
passwords := []string{"password", "admin", "administrator", "root", ""}
for _, password := range passwords {
sshConfig := &ssh.ClientConfig{
User: "root",
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
address := fmt.Sprintf("%s:%d", ip, port)
client, err := ssh.Dial("tcp", address, sshConfig)
if err != nil {
fmt.Printf("Error connecting to SSH server at %s: %v\n", address, err)
continue
}
defer client.Close()
session, err := client.NewSession()
if err != nil {
fmt.Printf("Error creating SSH session: %v\n", err)
continue
}
defer session.Close()
// Attempt to execute a command to check if the connection responds to root
cmd := "whoami"
output, err := session.Output(cmd)
if err != nil {
fmt.Printf("Failed to execute command: %v\n", err)
continue
}
// Check if the output contains "root"
if strings.TrimSpace(string(output)) == "root" {
fmt.Printf("SSH server responds to root user with password: %s\n", password)
} else {
fmt.Printf("SSH server does not respond to root user with password: %s\n", password)
}
fmt.Printf("Connected to SSH server at %s\n", address)
}
}
func probeService(address string) string {
conn, err := net.DialTimeout("tcp", address, 3*time.Second)
if err != nil {
return ""
}
defer conn.Close()
// Set a deadline for the read operation
deadline := time.Now().Add(2 * time.Second)
err = conn.SetReadDeadline(deadline)
if err != nil {
fmt.Println("Failed to set read deadline:", err)
return ""
}
// Read the first 1024 bytes from the connection
buf := make([]byte, 1024)
n, err := conn.Read(buf)
if err != nil && err != io.EOF {
return ""
}
// Convert to a string and return
return string(buf[:n])
}