-
Notifications
You must be signed in to change notification settings - Fork 1
/
resolver.go
355 lines (292 loc) · 9.47 KB
/
resolver.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
package slamdunk
import (
"bufio"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/beevik/etree"
)
const (
NoBucket = "No bucket found"
SomeBucket = "Some S3 Bucket"
NoRegion = "No region found"
)
// Result status for a given target URL
type ResolverStatus struct {
// original url
Url string
// resolved bucket name, if found.
Bucket string
// bucket region, if found
Region string
// set if bucket takeover is possible
Takeover bool
}
// Given a returned status, create an entry that can be used for display as a row in an ASCII table
func (r *ResolverStatus) Row() []string {
return []string{r.Url, r.Bucket, r.Region, strconv.FormatBool(r.Takeover)}
}
type Resolver struct {
// buckets successfully parsed out
Buckets []ResolverStatus
// number of URLs successfully processed
UrlsProcessed int
// number of URLS failed to process (ie timeout)
UrlsFailed int
// S3 endpoints identified, even if name can't be found
Endpoints int
// how many endpoints can be taken over
TakeoverPossible int
}
func NewResolver() *Resolver {
return &Resolver{
Buckets: []ResolverStatus{},
UrlsProcessed: 0,
UrlsFailed: 0,
Endpoints: 0,
TakeoverPossible: 0,
}
}
// Given a single URL, run a set of actions against it in order to resolve a bucket name, while also
// attempting to detect if subdomain takeover is possible.
//
// 1. Check HTTP GET response for S3 metadata
// 2. Check DNS records for a S3 URL CNAME
// 3. Check if URL itself is a bucket name
// 4. Parse data as XML and check tags for any S3 metadata
func (r *Resolver) Resolve(url string) error {
log.Println("Sanity check if already an AWS URL")
if strings.Contains(url, "amazonaws.com") {
r.UrlsFailed += 1
return errors.New("Already a S3 URL, no need to resolve further.")
}
// get both a qualified URL and normal relative URL
log.Println("Creating relative and full URLs for HTTP and DNS.")
fullUrl, relativeUrl := GenerateUrlPair(url)
// default status, nothing found
status := ResolverStatus{
Url: relativeUrl,
Bucket: NoBucket,
Region: NoRegion,
Takeover: false,
}
// stop hanging on requests that time out
client := http.Client{
Timeout: 3 * time.Second,
}
// GET request to url and parse out data
log.Printf("Sending GET to %s\n", fullUrl)
resp, err := client.Get(fullUrl)
if err != nil {
r.UrlsFailed += 1
return err
}
defer resp.Body.Close()
bytedata, err := io.ReadAll(resp.Body)
if err != nil {
r.UrlsFailed += 1
return err
}
// can successfully ping the endpoint
r.UrlsProcessed += 1
/////////////////////////////////
// FIRST CHECK: Request Headers
/////////////////////////////////
log.Println("Starting First Check: Request Headers")
// skip if Google Cloud headers are present
if resp.Header.Get("X-GUploader-UploadID") != "" {
r.UrlsFailed += 1
return errors.New("Cannot deal with Google Cloud Storage yet.")
}
// check for `Server` header to be AmazonS3, but may be changed by proxy or CDN
server := resp.Header.Get("Server")
if server == "AmazonS3" {
status.Bucket = SomeBucket
log.Println("Detected AWS S3 bucket from URL")
}
// check if region is set in headers as well
region := resp.Header.Get("x-amz-bucket-region")
if region != "" {
status.Region = region
log.Println("Detected AWS S3 bucket region from URL")
}
///////////////////////////////
// SECOND CHECK: CNAME Records
///////////////////////////////
log.Println("Starting Second Check: CNAME Records")
// check if URL points to a S3 URL in any CNAME records. A bucket may use a CDN that
// masks the original S3 URL, so this may not return anything even if it is a bucket
potentialCname, _ := GetCNAME(relativeUrl)
if strings.Contains(potentialCname, ".amazonaws.com") {
log.Println("Found AWS URL in CNAME, parsing further")
// s3-<REGION>.amazonaws.com/<BUCKET_NAME>/<OBJECTS>
expr1 := regexp.MustCompile(`s3-(?P<region>[^.]+).amazonaws.com/(?P<bucket>[^/]+)`)
expr1Matches := expr1.FindStringSubmatch(potentialCname)
if len(expr1Matches) != 0 {
status.Region = expr1Matches[1]
status.Bucket = expr1Matches[2]
log.Printf("Matched: s3-%s.amazonaws.com/%s\n", status.Region, status.Bucket)
}
// <BUCKET_NAME>.s3.<REGION>.amazonaws.com/<OBJECTS>
expr2 := regexp.MustCompile(`(?P<bucket>[^/]+).s3.(?P<region>[^.]+).amazonaws.com`)
expr2Matches := expr2.FindStringSubmatch(potentialCname)
if len(expr2Matches) != 0 {
status.Region = expr2Matches[2]
status.Bucket = expr2Matches[1]
log.Printf("Matched: %s.s3.%s.amazonaws.com\n", status.Bucket, status.Region)
}
// shouldn't happen, but continue checks if bucket name couldn't be found
if status.Bucket == NoBucket {
log.Println("Continuing checks, parsing CNAME didn't work out")
goto bodyCheck
}
// if bucket name found but no region, region must be us-east-1
if status.Bucket != NoBucket && status.Region == NoRegion {
status.Region = "us-east-1"
}
// otherwise do a quick takeover check and return.
log.Println("Checking for takeover")
if strings.Contains(string(bytedata), "NoSuchBucket") {
r.TakeoverPossible += 1
status.Takeover = true
log.Println("Takeover is possible for parsed bucket")
}
log.Println("Adding successful entry and returning")
r.Endpoints += 1
r.Buckets = append(r.Buckets, status)
return nil
}
bodyCheck:
///////////////////////////////////
/// THIRD CHECK: URL AS BUCKET NAME
///////////////////////////////////
log.Println("Starting Third Check: URL as Bucket Name")
// status.Region being set helps make this faster, otherwise will enumerate through all regions
if val, region := CheckBucketExists(relativeUrl, status.Region); val {
status.Bucket = relativeUrl
status.Region = region
}
///////////////////////////////////
/// FINAL CHECK: HTTP XML RESPONSE
///////////////////////////////////
// attempt to serialize into proper XML, if not, return
xml := etree.NewDocument()
if err := xml.ReadFromBytes(bytedata); err != nil {
goto end
}
// TODO: Check for GCloud error
// if `Error` root is present, encountered a S3 error page
if errTag := xml.FindElement("Error"); errTag != nil {
log.Println("Starting Final Check: Parsing XML Error")
// get string for Code tag used to indicate error
code := errTag.SelectElement("Code").Text()
// NoSuchBucket: bucket deleted, but takeover is possible!
if code == "NoSuchBucket" {
status.Bucket = errTag.SelectElement("BucketName").Text()
status.Takeover = true
r.TakeoverPossible += 1
// PermanentRedirect: wrong region, shouldn't be reached
} else if code == "PermanentRedirect" {
status.Bucket = errTag.SelectElement("BucketName").Text()
// AccessDenied | NoSuchKey | etc: bucket exists, can't parse name
} else {
status.Bucket = SomeBucket
}
}
// if `ListBucketResult` is present, encountered an open bucket
if resTag := xml.FindElement("ListBucketResult"); resTag != nil {
log.Println("Starting Final Check: Parsing Open Bucket")
status.Bucket = resTag.SelectElement("Name").Text()
}
end:
// if name isn't unknown increment endpoint
if status.Bucket != NoBucket {
r.Endpoints += 1
}
r.Buckets = append(r.Buckets, status)
return nil
}
// Helper that takes a URL in any format and generates a FQDN and a relative URL
func GenerateUrlPair(url string) (string, string) {
var fullUrl, relativeUrl string
// if input is relative, construct full and save both
if !strings.Contains(url, "http") {
fullUrl = "http://" + url
relativeUrl = url
// other way around
} else {
fullUrl = url
// remove prepended protocol
if strings.Contains(url, "http://") {
relativeUrl = strings.TrimPrefix(url, "http://")
} else if strings.Contains(url, "https://") {
relativeUrl = strings.TrimPrefix(url, "https://")
}
relativeUrl = strings.TrimSuffix(relativeUrl, "/")
}
return fullUrl, relativeUrl
}
// Traverse a CNAME chain to the end and return the resultant URL
func GetCNAME(url string) (string, error) {
// do lookup
cname, err := net.LookupCNAME(url)
if err != nil {
return "", errors.New("Domain name doesn't exist")
}
// remove trailing dots and compare
cname = strings.TrimSuffix(cname, ".")
url = strings.TrimSuffix(url, ".")
if cname == "" || cname == url {
return "", errors.New("Domain name is not a CNAME")
}
return cname, nil
}
func (r *Resolver) Table() [][]string {
var contents [][]string
for _, status := range r.Buckets {
if status.Bucket != NoBucket {
contents = append(contents, status.Row())
}
}
return contents
}
// Finalize by writing bucket names to a filepath, and displaying stats to user.
func (r *Resolver) OutputStats(path string) error {
// if path is specified write bucket names to path
if path != "" {
file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer file.Close()
// write each entry as a line, ignore takeovers since they don't exist
writer := bufio.NewWriter(file)
for _, data := range r.Buckets {
if !data.Takeover && data.Bucket != SomeBucket {
_, _ = writer.WriteString(data.Bucket + "\n")
}
}
writer.Flush()
}
var nameCount int
for _, data := range r.Buckets {
if data.Bucket != SomeBucket {
nameCount += 1
}
}
// output rest of the stats
fmt.Printf("\nURLs Processed: %d\n", r.UrlsProcessed)
fmt.Printf("URLs Failed: %d\n\n", r.UrlsFailed)
fmt.Printf("S3 Endpoints Found: %d\n", r.Endpoints)
fmt.Printf("Bucket Names Identified: %d\n", nameCount)
fmt.Printf("Bucket Takeovers Possible: %d\n\n", r.TakeoverPossible)
return nil
}