-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
301 lines (242 loc) · 6.56 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
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
// go-out
//
// egress busting using:
// letmeoutofyour.net by @mubix
// allports.exposed by @bhinfosecurity
//
// 2018 @leonjza
package main
import (
"crypto/tls"
"flag"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"sync"
"sync/atomic"
"text/template"
"time"
"github.com/reconquest/barely"
)
var version = "1.3"
var (
servicePtr *string
startPortPtr *int
endPortPtr *int
concurrentPtr *int
useHTTPSPtr *bool
throttlePtr *bool
ignoreCertificatePtr *bool
invertPtr *bool
timeoutPtr *int
randomisePortsPtr *bool
printVersion *bool
)
type service struct {
url string
match string
}
var services = map[string]service{
"letmeout": {url: "go-out.letmeoutofyour.net", match: "w00tw00t"},
"allports": {url: "allports.exposed", match: "<p>Open Port</p>"},
}
// maxedWaitGroup is a type to control the maximum
// number of goroutines in a wait group
type maxedWaitGroup struct {
current chan int
wg sync.WaitGroup
}
func (m *maxedWaitGroup) Add() {
m.current <- 1
m.wg.Add(1)
}
func (m *maxedWaitGroup) Done() {
<-m.current
m.wg.Done()
}
func (m *maxedWaitGroup) Wait() {
m.wg.Wait()
}
// validService ensures that we got a valid service from the
// -service commandline flag.
func validService(s *string) bool {
for b := range services {
if b == *s {
return true
}
}
return false
}
// validPort checks that we got a valid port from one of the
// port commandline flags.
func validPort(p int) bool {
if p > 0 && p <= 65535 {
return true
}
return false
}
// makes a number range slice
func makePortRange(min, max int) []int {
a := make([]int, max-min+1)
for i := range a {
a[i] = min + i
}
return a
}
// shufflePorts randomizes port order
func shufflePorts(ports []int) []int {
r := rand.New(rand.NewSource(time.Now().Unix()))
ret := make([]int, len(ports))
perm := r.Perm(len(ports))
for i, ix := range perm {
ret[i] = ports[ix]
}
return ret
}
// testHTTPEgress tests if a specific port is allowed to connect
// to the internet via http by matching the specific services' matcher
func (service *service) testHTTPEgress(port int) {
var scheme string
if *useHTTPSPtr {
scheme = "https://"
} else {
scheme = "http://"
}
url, err := url.Parse(scheme + service.url + ":" + strconv.Itoa(port))
if err != nil {
panic(err)
}
transport := &http.Transport{}
if *ignoreCertificatePtr {
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
timeout := time.Duration(*timeoutPtr) * time.Second
client := http.Client{
Timeout: timeout,
Transport: transport,
}
resp, err := client.Get(url.String())
if err != nil {
if *invertPtr {
_, err := client.Get(url.String())
if err != nil {
fmt.Printf("[!] Looks like we have no egress using %s on port %d\n", url.String(), port)
}
return
}
return // if the first one errored already, don't continue
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
if strings.Contains(string(body), service.match) && !*invertPtr {
fmt.Printf("[!] Looks like we have egress using %s on port %d\n", url.String(), port)
}
}
func validateFlags() bool {
// Flag Validation
if !validService(servicePtr) {
fmt.Printf("%s is an invalid service. Please choose 'letmeout' or 'allports'\n", *servicePtr)
return false
}
if *useHTTPSPtr && *servicePtr != "letmeout" {
fmt.Println("Only the 'letmeout' service supports HTTPS, disabling HTTPS checking.")
*useHTTPSPtr = false
}
if !*useHTTPSPtr && *ignoreCertificatePtr {
fmt.Println("HTTPs is disabled, will not verify certificates.")
*ignoreCertificatePtr = false
}
if !validPort(*startPortPtr) || !validPort(*endPortPtr) {
fmt.Println("Either the start port or end port was invalid / out of range.")
return false
}
if *endPortPtr < *startPortPtr {
fmt.Println("End port should be larger than the start port.")
return false
}
return true
}
func main() {
servicePtr = flag.String("service", "letmeout", "Use 'letmeout' or 'allports' for this run.")
startPortPtr = flag.Int("start", 1, "The start port to use.")
endPortPtr = flag.Int("end", 65535, "The end port to use.")
concurrentPtr = flag.Int("w", 5, "Number of concurrent workers to spawn.")
useHTTPSPtr = flag.Bool("https", true, "Egress bust using HTTPs. (letmeout only)")
ignoreCertificatePtr = flag.Bool("insecure", false, "Don't verify the certificate when using HTTPs.")
throttlePtr = flag.Bool("throttle", false, "Throttle request speed. (random for a max of 10sec)")
invertPtr = flag.Bool("invert", false, "Invert results of the egress bust.")
timeoutPtr = flag.Int("timeout", 5, "Timeout in seconds.")
randomisePortsPtr = flag.Bool("r", false, "Randomise port scanning order")
printVersion = flag.Bool("version", false, "Print the version and exit")
flag.Parse()
if !validateFlags() {
return
}
if *printVersion {
fmt.Printf("go-out version %s\n", version)
return
}
fmt.Println("===== Configuration =====")
fmt.Printf("Service: %s\n", *servicePtr)
fmt.Printf("Start Port: %d\n", *startPortPtr)
fmt.Printf("End Port: %d\n", *endPortPtr)
fmt.Printf("Workers: %d\n", *concurrentPtr)
fmt.Printf("HTTPS On: %t\n", *useHTTPSPtr)
fmt.Printf("Ignore Certs: %t\n", *ignoreCertificatePtr)
fmt.Printf("Invert: %t\n", *invertPtr)
fmt.Printf("Timeout: %d\n", *timeoutPtr)
fmt.Printf("Throttle: %t\n", *throttlePtr)
fmt.Printf("Random Ports: %t\n", *randomisePortsPtr)
fmt.Printf("=========================\n\n")
tester := services[*servicePtr]
start := time.Now()
mwg := maxedWaitGroup{
current: make(chan int, *concurrentPtr),
wg: sync.WaitGroup{},
}
format, err := template.New("status-bar").
Parse(" > Processing range: {{if .Updated}}{{end}}{{.Done}}/{{.Total}}")
if err != nil {
log.Fatalf("Unable to parse progress bar")
}
bar := barely.NewStatusBar(format)
status := &struct {
Total int
Done int64
Updated int64
}{
Total: *endPortPtr - *startPortPtr + 1,
}
bar.SetStatus(status)
bar.Render(os.Stdout)
portRange := makePortRange(*startPortPtr, *endPortPtr)
if *randomisePortsPtr {
portRange = shufflePorts(portRange)
}
for _, port := range portRange {
mwg.Add()
go func(p int) {
defer mwg.Done()
if *throttlePtr {
time.Sleep(time.Second * time.Duration(rand.Intn(10)))
}
tester.testHTTPEgress(p)
atomic.AddInt64(&status.Done, 1)
atomic.AddInt64(&status.Updated, 1)
bar.Render(os.Stdout)
}(port)
}
// Wait for the work to complete
mwg.Wait()
bar.Clear(os.Stdout)
fmt.Printf("Done in %s\n", time.Since(start))
}