-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmain.go
executable file
·189 lines (167 loc) · 7.64 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
package main
import (
"context"
"fmt"
"net/http"
"net/http/httptrace"
"crypto/tls"
"strconv"
"strings"
"sync"
"syscall/js"
"time"
"golang.org/x/sync/semaphore"
)
type PortScanner struct {
ip string
lock *semaphore.Weighted
portsMapping map[int]bool
}
func ScanPort(ip string, port int, timeout time.Duration, portsMapping map[int]bool) {
base := fmt.Sprintf("%s:%d", ip, port)
placeHolder := js.Global().Get("document").Call("getElementById", "counter")
placeHolder.Set("innerText", "Scanning "+ base + " / 10,000 ports")
if port == 10000 {
placeHolder.Set("innerText", "Scanned "+base+"; Waiting for the remaining responses...")
}
// TODO: WASI - add supprt for TCP/UDP session. only HTTP/HTTPS is supported in WASM JS today.
//conn, err := net.DialTimeout("tcp", target, timeout)
// HTTP session - supported from browsers API
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
client := http.Client{
Transport: tr,
Timeout: timeout,
}
// Trying both http and https
bind := fmt.Sprintf("http://%s", base)
var req *http.Request
var er error
req, er = http.NewRequest("GET", bind, nil)
if er != nil {
fmt.Print("Failed to instantiate target over HTTP, trying HTTPS", er)
bind = fmt.Sprintf("https://%s", base)
req, er = http.NewRequest("GET", bind, nil)
if er != nil {
fmt.Print("Failed to initiate target over HTTPS ", er)
}
// target = bind
// } else {
// target = bind
}
trace := &httptrace.ClientTrace{
DNSDone: func(dnsInfo httptrace.DNSDoneInfo) {
fmt.Println("DNS Info: %+v\n", dnsInfo)
},
GotConn: func(connInfo httptrace.GotConnInfo) {
fmt.Println("Got Conn: %+v\n", connInfo)
},
GotFirstResponseByte: func() {
fmt.Println("Got first byte!")
},
}
// IMPORTANT - enables better HTTP(S) discovery, because many browsers block CORS by default.
req.Header.Add("js.fetch:mode", "no-cors")
// fmt.Println("(GO request): ", fmt.Sprintf("%+v", req))
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
if _, err := client.Do(req); err != nil {
// fmt.Println(err)
// fmt.Println("(GO error): ", err.Error())
// TODO: Get more exception strings for major browsers
errString := strings.ToLower(err.Error())
if strings.Contains(errString, "sent an invalid response") ||
strings.Contains(errString, "ERR_SSL_PROTOCOL_ERROR") ||
// errString, "exceeded while awaiting") ||
// strings.Contains(errString, "tls") ||
strings.Contains(errString, "ssl") ||
// strings.Contains(errString, "timeout") ||
strings.Contains(errString, "cors") ||
// strings.Contains(errString, "REFUSED") ||
strings.Contains(errString, "invalid") ||
// strings.Contains(errString, "https") ||
// strings.Contains(errString, "handshake") ||
strings.Contains(errString, "protocol") {
fmt.Println(port, "<filtered (open)>")
portsMapping[port] = true
// Append JS list element
// portString := strconv.Itoa(port)
// openPortsParagraph := js.Global().Get("document").Call("getElementByID", "openPort")
// openPortsParagraph.Set("innerText", portString)
// js.Global().Get("document").Get("body").Call("appendChild", openPortsParagraph)
return
} else {
fmt.Println(port, "<closed>")
portsMapping[port] = false
return
}
}
fmt.Println(port, "<open>")
portsMapping[port] = true
// portString := strconv.Itoa(port)
// openPortsParagraph := js.Global().Get("document").Call("getElementByID", "openPort")
// openPortsParagraph.Set("innerText", portString)
// js.Global().Get("document").Get("body").Call("appendChild", openPortsParagraph)
return
}
func (ps *PortScanner) Start(f int, l int, timeout time.Duration, portsMapping map[int]bool) {
wg := sync.WaitGroup{}
for port := f; port <= l; port++ {
// GO in WASM must be SYNC as of today
ps.lock.Acquire(context.TODO(), 1)
wg.Add(1)
go func(port int) {
defer ps.lock.Release(1)
ScanPort(ps.ip, port, timeout, portsMapping)
defer wg.Done()
}(port)
}
//time.Sleep(10 * time.Second)
wg.Wait()
}
func main() {
portsMapping := make(map[int]bool)
ps := &PortScanner{
ip: "0.0.0.0",
lock: semaphore.NewWeighted(200),
portsMapping: portsMapping,
}
// TODO: Enable port range input
document := js.Global().Get("document")
documentTitle := document.Call("createElement", "h1")
documentTitle.Set("innerText", "TCP Port Scanner, Written in Go, Compiled to WebAssembly.")
document.Get("body").Call("appendChild", documentTitle)
placeHolder := document.Call("createElement", "h1")
placeHolder.Set("innerText", "Scanning...")
placeHolder.Set("id", "counter")
document.Get("body").Call("appendChild", placeHolder)
foundPort := document.Call("createElement", "h3")
foundPort.Set("innerText", "Open: ")
foundPort.Set("id", "openPort")
document.Get("body").Call("appendChild", foundPort)
// scanned := document.Call("createElement", "h3")
// scanned.Set("innerText", "Done: ")
// scanned.Set("id", "scanned")
// document.Get("body").Call("appendChild", scanned)
// Edit the ports,
// ps.Start(1, 10000, 30000 * time.Millisecond, portsMapping)
ps.Start(1, 10000, 500*time.Millisecond, portsMapping)
fmt.Println("Finished. Ports Mapping:")
var openPorts []string
for k, v := range portsMapping {
if v == true {
portString := strconv.Itoa(k)
openPorts = append(openPorts, portString)
document := js.Global().Get("document")
openPortsParagraph := document.Call("createElement", "li")
openPortsParagraph.Set("innerText", portString)
document.Get("body").Call("appendChild", openPortsParagraph)
}
}
// fmt.Println("Open Ports", portsMapping)
placeHolder.Set("innerText", "Open Ports:")
fmt.Println("Scanned Ports: ", portsMapping)
placeHolder.Set("innerText", portsMapping)
}