-
Notifications
You must be signed in to change notification settings - Fork 0
/
go_egress_check.go
447 lines (329 loc) · 9.76 KB
/
go_egress_check.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package main
import (
"bufio"
"flag"
"fmt"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"
"github.com/miekg/dns"
log "github.com/sirupsen/logrus"
)
func ParseQuery(m *dns.Msg) {
for _, q := range m.Question {
switch q.Qtype {
case dns.TypeA:
log.Info("DNS query received: " + q.Name)
response_string := "what.a.shite.domain. A 0.0.0.0"
if strings.Split(q.Name, ".")[0] == "check" {
log.Info("Egress possible using DNS")
response_string = q.Name + " A 1.1.1.1"
}
rr, err := dns.NewRR(response_string)
if err == nil {
m.Answer = append(m.Answer, rr)
}
}
}
}
func HandleDnsRequest(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)
m.Compress = false
switch r.Opcode {
case dns.OpcodeQuery:
ParseQuery(m)
}
w.WriteMsg(m)
}
func DNSServerGenerator(domain string, port string, wg *sync.WaitGroup) {
port_int, _ := strconv.Atoi(port)
dns.HandleFunc(domain + ".", HandleDnsRequest)
server := &dns.Server{Addr: ":" + strconv.Itoa(port_int), Net: "udp"}
log.Info("DNS server started on port " + port)
err := server.ListenAndServe()
defer server.Shutdown()
if err != nil {
log.Error(err)
}
}
func DNSServer(domain string, ports []string) {
var wg sync.WaitGroup
for i := range ports {
log.Debug("Attempting to start DNS server on port " + ports[i])
wg.Add(1)
go DNSServerGenerator(domain, ports[i], &wg)
log.Debug("Started DNS server")
}
wg.Wait()
}
func TestPortDNS(domain string, port string, dest_ip string, wg *sync.WaitGroup) {
defer wg.Done()
m := new(dns.Msg)
m.SetQuestion( "check." + domain + ".", dns.TypeA)
c := new(dns.Client)
in, _, err := c.Exchange(m, dest_ip + ":" + port)
if err != nil {
log.Error(err)
} else {
log.Debug("DNS response: " + string(in.Answer[0].String()))
if strings.Split(in.Answer[0].String(), "\t")[4] == "1.1.1.1" {
log.Info("Egress possible using DNS on port " + port + " and ip " + dest_ip)
}
}
}
func DNSClient(domain string, ports []string, dest_ip string) {
var wg sync.WaitGroup
for i := range ports {
wg.Add(1)
log.Debug("Attempting to check " + domain + " using nameserver " + dest_ip + ":" + ports[i])
go TestPortDNS(domain, ports[i], dest_ip, &wg)
log.Info("DNS data sent")
}
wg.Wait()
}
func HandleTCPConn(conn net.Conn, src_port string) {
log.Debug("Connection received from " + conn.RemoteAddr().String() + " destination port " + src_port)
for {
data, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
log.Error("Error reading from connection")
return
}
data_cleaned := strings.TrimSpace(string(data))
if data_cleaned == "CHECK" {
log.Info("Egress possible on port " + src_port + " from " + conn.RemoteAddr().String())
break
} else {
log.Debug("Received invalid data on port " + src_port + " from " + conn.RemoteAddr().String())
}
}
conn.Close()
}
func TCPServerGenerator(src_port string, src_ip string) {
l, err := net.Listen("tcp4", src_ip+":"+src_port)
if err != nil {
log.Info("Error starting TCP server on " + src_ip + ":" + src_port)
log.Error(err)
return
}
defer l.Close()
log.Debug("Started TCP server on " + src_ip + ":" + src_port)
for {
conn, err := l.Accept()
if err != nil {
log.Error("Error accepting connection")
return
}
go HandleTCPConn(conn, src_port)
}
}
func TCPServer(ports []string, src_ip string) {
var wg sync.WaitGroup
for i := range ports {
log.Debug("Attempting to start TCP server on port " + ports[i])
wg.Add(1)
go TCPServerGenerator(ports[i], src_ip)
}
wg.Wait()
}
func UDPServerGenerator(src_port string, src_ip string) {
port_int, _ := strconv.Atoi(src_port)
addr := net.UDPAddr{
Port: port_int,
IP: net.ParseIP(src_ip),
}
l, err := net.ListenUDP("udp", &addr)
if err != nil {
log.Error("Error starting UDP listener " + src_ip + ":" + src_port)
log.Error(err)
return
}
defer l.Close()
log.Debug("Started UDP server on " + src_ip + ":" + src_port)
buffer := make([]byte, 1024)
for {
num_bytes, addr, err := l.ReadFromUDP(buffer)
if err != nil {
log.Error("Error reading UDP data")
log.Error(err)
}
data_cleaned := strings.TrimSpace(string(buffer[0:num_bytes]))
log.Debug("Received data: " + data_cleaned)
if data_cleaned == "CHECK" {
log.Info("Egress possible on port " + src_port + " from " + addr.String())
break
} else {
log.Debug("Received invalid data on port " + src_port + " from " + addr.String())
}
}
}
func UDPServer(ports []string, src_ip string) {
var wg sync.WaitGroup
for i := range ports {
log.Debug("Attempting to start UDP server on port " + ports[i])
wg.Add(1)
go UDPServerGenerator(ports[i], src_ip)
}
wg.Wait()
}
func TestPortUDP(dest_port string, dest_ip string, wg *sync.WaitGroup) {
defer wg.Done()
log.SetFormatter(&log.JSONFormatter{})
s, err := net.ResolveUDPAddr("udp4", dest_ip+":"+dest_port)
if err != nil {
log.Error("Error resolving UDP address")
log.Error(err)
}
conn, err := net.DialUDP("udp4", nil, s)
if err != nil {
log.Error("Error dialing UDP connection")
log.Error(err)
return
}
defer conn.Close()
fmt.Fprintf(conn, "CHECK\n")
log.Info("UDP data sent to " + dest_ip + ":" + dest_port)
}
func UDPClient(ports []string, dest_ip string) {
var wg sync.WaitGroup
for i := range ports {
wg.Add(1)
log.Debug("Attempting to check " + dest_ip + ":" + ports[i])
go TestPortUDP(ports[i], dest_ip, &wg)
}
wg.Wait()
}
func TestPortTCP(dest_port string, dest_ip string, wg *sync.WaitGroup) {
defer wg.Done()
log.SetFormatter(&log.JSONFormatter{})
conn, err := net.Dial("tcp", dest_ip+":"+dest_port)
if err != nil {
log.Error("Error dialing connection " + dest_ip + ":" + dest_port)
return
}
log.Debug("TCP connection established to " + dest_ip + ":" + dest_port)
_, err = conn.Write([]byte("CHECK\n"))
if err != nil {
log.Error("Error writing data")
return
}
log.Info("TCP data sent to " + dest_ip + ":" + dest_port)
}
func TCPClient(ports []string, dest_ip string) {
var wg sync.WaitGroup
for i := range ports {
wg.Add(1)
log.Debug("Attempting to check " + dest_ip + ":" + ports[i])
go TestPortTCP(ports[i], dest_ip, &wg)
}
wg.Wait()
}
func HTTPHandler(writer http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/check" {
http.Error(writer, "404 not found.", http.StatusNotFound)
return
}
http.ServeFile(writer, req, "index.html")
port := req.URL.Query().Get("port")
log.Info("Egress possible on port " + port + " from " + req.RemoteAddr)
}
func HTTPServer(ports []string, dest_ip string) {
http.HandleFunc("/check", HTTPHandler)
var wg sync.WaitGroup
for i := range ports {
log.Debug("Attempting to start HTTP server on port " + ports[i])
wg.Add(1)
go http.ListenAndServe(dest_ip+":"+ports[i], nil)
log.Debug("Started HTTP server on " + dest_ip + ":" + ports[i])
}
wg.Wait()
}
func TestPortHTTP(dest_ip string, dest_port string, wg *sync.WaitGroup) {
defer wg.Done()
client := http.Client{
Timeout: time.Duration(1) * time.Second,
}
u, err := url.Parse("http://" + dest_ip + ":" + dest_port + "/check")
if err != nil {
log.Error("Error parsing URL")
log.Error(err)
return
}
u.Scheme = "http"
u.Host = dest_ip + ":" + dest_port
u.Path = "/check"
q := u.Query()
q.Add("port", dest_port)
u.RawQuery = q.Encode()
resp, err := client.Get(u.String())
if err != nil {
log.Error("Error sending HTTP request")
log.Error(err)
return
}
defer resp.Body.Close()
}
func HTTPClient(ports []string, dest_ip string) {
var wg sync.WaitGroup
for i := range ports {
wg.Add(1)
log.Debug("Attempting to check " + dest_ip + ":" + ports[i])
go TestPortHTTP(dest_ip, ports[i], &wg)
log.Info("HTTP data sent to " + dest_ip + ":" + ports[i])
}
wg.Wait()
}
func main() {
var ports_csv string
var ip string
var mode string
var protocol string
var domain string
flag.StringVar(&ports_csv, "ports", "", "A comma-seperated list of ports to test for egress access")
flag.StringVar(&ip, "ip", "", "The IP address to serve on in server mode, or the target IP address to test egress to in client mode")
flag.StringVar(&mode, "mode", "", "Server or client mode (case insensitive)")
flag.StringVar(&protocol, "protocol", "", "Protocol to test: tcp, udp, http, dns")
flag.StringVar(&domain, "domain", "", "The domain name to respond/query if in DNS server/client mode")
flag.Parse()
mode = strings.ToLower(mode)
protocol = strings.ToLower(protocol)
log.SetFormatter(&log.JSONFormatter{})
log.SetLevel(log.DebugLevel)
ports := strings.Split(ports_csv, ",")
for i := range ports {
ports[i] = strings.TrimSpace(ports[i])
}
switch {
case mode == "client" && protocol == "tcp" && len(ports) != 0 && ip != "":
log.Info("Running in TCP client mode")
TCPClient(ports, ip)
case mode == "server" && protocol == "tcp" && len(ports) != 0 && ip != "":
log.Info("Running in TCP server mode")
TCPServer(ports, ip)
case mode == "server" && protocol == "udp" && len(ports) != 0 && ip != "":
log.Info("Running in UDP server mode")
UDPServer(ports, ip)
case mode == "client" && protocol == "udp" && len(ports) != 0 && ip != "":
log.Info("Running in UDP client mode")
UDPClient(ports, ip)
case mode == "server" && protocol == "http" && len(ports) != 0 && ip != "":
log.Info("Running in HTTP server mode")
HTTPServer(ports, ip)
case mode == "client" && protocol == "http" && len(ports) != 0 && ip != "":
log.Info("Running in HTTP client mode")
HTTPClient(ports, ip)
case mode == "server" && protocol == "dns" && len(ports) != 0 && domain != "" && ip != "":
log.Info("Running in DNS server mode")
DNSServer(domain, ports)
case mode == "client" && protocol == "dns" && len(ports) != 0 && domain != "" && ip != "":
log.Info("Running in DNS client mode")
DNSClient(domain, ports, ip)
default:
log.Info("No correct combination of arguments provided")
}
}