/
netutil.go
237 lines (210 loc) · 4.83 KB
/
netutil.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
package tun
import (
"io"
"log"
"net"
"net/http"
"strconv"
"strings"
"time"
)
const (
MicrosoftNCSIURL = "http://www.msftncsi.com/ncsi.txt"
MicrosoftNCSIResp = "Microsoft NCSI"
)
// IsPortOpen is this TCP port open?
func IsPortOpen(host string, port string) bool {
timeout := 3 * time.Second
conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, port), timeout)
if err != nil {
return false
}
if conn != nil {
defer conn.Close()
return true
}
return false
}
// ValidateIP is this IP legit?
func ValidateIP(ip string) bool {
return net.ParseIP(ip) != nil
}
// ValidateIPPort check if the host string looks like IP:Port
func ValidateIPPort(to string) bool {
fields := strings.Split(to, ":")
if len(fields) != 2 {
return false
}
host := fields[0]
if !ValidateIP(host) {
return false
}
_, err := strconv.Atoi(fields[1])
return err == nil
}
// IsTor is the C2 on Tor?
func IsTor(addr string) bool {
if !strings.HasPrefix(addr, "http://") &&
!strings.HasPrefix(addr, "https://") {
return false
}
nopath := strings.Split(addr, "/")[2]
fields := strings.Split(nopath, ".")
return fields[len(fields)-1] == "onion"
}
// HasInternetAccess does this machine has internet access,
// does NOT use any proxies
func HasInternetAccess(test_url string) bool {
// use Microsoft NCSI as default
// NCSI is an HTTP service therefore we don't need
// uTLS to talk to it
client := &http.Client{
Timeout: 5 * time.Second,
}
// if not using Microsoft NCSI, we need to use uTLS
if test_url != MicrosoftNCSIURL {
client = HTTPClientWithEmpCA(test_url, "")
if client == nil {
log.Printf("HasInternetAccess: cannot create http client for %s", test_url)
return false
}
}
resp, err := client.Get(test_url)
if err != nil {
return false
}
defer resp.Body.Close()
respData, err := io.ReadAll(resp.Body)
if err != nil {
return false
}
if test_url == MicrosoftNCSIURL {
return string(respData) == MicrosoftNCSIResp
}
return true
}
// IsProxyOK test if the proxy works against the test URL
func IsProxyOK(proxy, test_url string) bool {
client := HTTPClientWithEmpCA(test_url, proxy)
if client == nil {
log.Printf("IsProxyOK: cannot create http client")
return false
}
resp, err := client.Get(test_url)
if err != nil {
return false
}
defer resp.Body.Close()
respData, err := io.ReadAll(resp.Body)
if err != nil {
return false
}
log.Printf("IsProxyOK: testing proxy %s: %s", proxy, respData)
if test_url == MicrosoftNCSIURL {
return string(respData) == MicrosoftNCSIResp
}
return true
}
// IPWithMask net.IP and net.IPMask
type IPWithMask struct {
IP net.IP
Mask net.IPMask
}
// IPa works like `ip addr`, you get a list of IP strings
func IPa() (ips []string) {
netips := IPaddr()
if netips == nil {
return []string{"Unknown"}
}
for _, netip := range netips {
maskLen, _ := netip.Mask.Size()
ip := netip.IP.String() + "/" + strconv.Itoa(maskLen)
ips = append(ips, ip)
}
return
}
// IPaddr returns a list of local IP addresses
func IPaddr() (ips []IPWithMask) {
ifaces := IPIfaces()
if ifaces == nil {
return nil
}
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
continue
}
for _, addr := range addrs {
var ip net.IP
var mask net.IPMask
var ipMask IPWithMask
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
mask = v.Mask
case *net.IPAddr:
ip = v.IP
mask = ip.DefaultMask()
}
ipaddr := ip.String()
if ipaddr == "::1" ||
ipaddr == "127.0.0.1" ||
strings.HasPrefix(ipaddr, "fe80:") ||
strings.HasPrefix(ipaddr, "169.254") {
continue
}
ipMask.IP = ip
ipMask.Mask = mask
ips = append(ips, ipMask)
}
}
return
}
// IPIfaces returns a list of network interfaces
func IPIfaces() (ifaces []net.Interface) {
ifaces, err := net.Interfaces()
if err != nil {
log.Printf("IPIfaces: %v", err)
return nil
}
return
}
// IPbroadcastAddr calculate broadcast address of an IP
func IPbroadcastAddr(ipMask IPWithMask) string {
ip := ipMask.IP
mask := ipMask.Mask
// check if IP is a valid IPv4 address
if ip.To4() == nil {
log.Printf("%s is not a valid IPv4 address", ip.String())
return ""
}
broadcast := net.IP(make([]byte, 4))
for i, p := range ip.To4() {
broadcast[i] = p | ^mask[i]
}
return broadcast.String()
}
// IPr works like `ip r`, covers both IPv4 and IPv6
func IPr() (routes []string) {
return crossPlatformIPr()
}
// IPNeigh works like `ip neigh`, dumps ARP cache
func IPNeigh() []string {
return crossPlatformIPNeigh()
}
// FindIPToUse find an IP that resides in target IP range
// target: 192.168.1.1/24
func FindIPToUse(target string) string {
_, subnet, _ := net.ParseCIDR(target)
for _, ipnetstr := range IPa() {
ipstr := strings.Split(ipnetstr, "/")[0]
ip := net.ParseIP(ipstr)
if ip == nil {
continue
}
if subnet.Contains(ip) {
return ip.String()
}
}
return ""
}