-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
324 lines (295 loc) · 7.68 KB
/
main.go
File metadata and controls
324 lines (295 loc) · 7.68 KB
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
package main
import (
"encoding/csv"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"strings"
"sync/atomic"
"time"
)
const baseURL = "https://buckets.grayhatwarfare.com/api/v2"
type File struct {
ID any `json:"id"`
Bucket string `json:"bucket"`
BucketID any `json:"bucketId"`
Name string `json:"name"`
URL string `json:"url"`
Size int64 `json:"size"`
Type string `json:"type"`
LastModified int64 `json:"lastModified"`
}
type FilesResponse struct {
Files []File `json:"files"`
Meta struct {
Results int `json:"results"`
} `json:"meta"`
}
type Bucket struct {
ID any `json:"id"`
Bucket string `json:"bucket"`
FileCount int `json:"fileCount"`
Type string `json:"type"`
}
type BucketsResponse struct {
Buckets []Bucket `json:"buckets"`
Meta struct {
Results int `json:"results"`
} `json:"meta"`
}
type StatsResponse struct {
Stats struct {
FilesCount int64 `json:"filesCount"`
AwsCount int `json:"awsCount"`
AzureCount int `json:"azureCount"`
DosCount int `json:"dosCount"`
GcpCount int `json:"gcpCount"`
AliCount int `json:"aliCount"`
} `json:"stats"`
}
func main() {
apiKey := flag.String("apikey", os.Getenv("GHW_API_KEY"), "API key (or set env GHW_API_KEY)")
cmd := flag.String("cmd", "files", "Command: files|buckets|stats")
keywords := flag.String("keywords", "", "Search keywords")
ext := flag.String("ext", "", "comma separated extensions filter, e.g. pdf,docx")
noext := flag.String("noext", "", "comma separated extensions to exclude")
bucket := flag.String("bucket", "", "Bucket id or url")
limit := flag.Int("limit", 1000, "Page size (1-1000). All pages will be fetched until results exhausted")
start := flag.Int("start", 0, "Start offset (files/buckets)")
output := flag.String("o", "", "Output csv file path. If empty, print json")
cloudType := flag.String("type", "", "Bucket cloud type filter: aws|azure|dos|gcp|ali")
onlyBucket := flag.Bool("onlybucket", false, "Output only bucket names (one per line or single column CSV)")
flag.Parse()
if *apiKey == "" {
log.Fatalln("missing api key")
}
client := &http.Client{Timeout: 15 * time.Second}
switch strings.ToLower(*cmd) {
case "files":
handleFiles(client, *apiKey, *keywords, *bucket, *ext, *noext, *limit, *start, *output)
case "buckets":
handleBuckets(client, *apiKey, *keywords, *cloudType, *limit, *start, *output, *onlyBucket)
case "stats":
handleStats(client, *apiKey, *output)
default:
log.Fatalf("unknown cmd %s\n", *cmd)
}
}
func buildURL(path string, params map[string]string) string {
u, _ := url.Parse(baseURL + path)
q := u.Query()
for k, v := range params {
if v != "" {
q.Set(k, v)
}
}
u.RawQuery = q.Encode()
return u.String()
}
func doGet(client *http.Client, apiKey, urlStr string) ([]byte, error) {
req, _ := http.NewRequest("GET", urlStr, nil)
req.Header.Set("Authorization", "Bearer "+apiKey)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http %d", resp.StatusCode)
}
return io.ReadAll(resp.Body)
}
func handleFiles(client *http.Client, apiKey, keywords, bucket, ext, noext string, limit, start int, output string) {
pageSize := limit
if pageSize <= 0 || pageSize > 1000 {
pageSize = 1000
}
var allFiles []File
var w *csv.Writer
var fetched int64
if output != "" {
f, err := os.Create(output)
if err != nil {
log.Fatalf("create csv: %v", err)
}
defer f.Close()
w = csv.NewWriter(f)
defer w.Flush()
w.Write([]string{"id", "bucket", "bucketId", "name", "url", "size", "type", "lastModified"})
}
offset := start
total := -1
for {
urlStr := buildURL("/files", map[string]string{
"keywords": keywords,
"bucket": bucket,
"extensions": ext,
"stopextensions": noext,
"limit": fmt.Sprintf("%d", pageSize),
"start": fmt.Sprintf("%d", offset),
})
data, err := doGet(client, apiKey, urlStr)
if err != nil {
log.Fatalf("request error: %v", err)
}
var resp FilesResponse
if err := json.Unmarshal(data, &resp); err != nil {
log.Fatalf("decode: %v", err)
}
// write/collect
if w != nil {
for _, file := range resp.Files {
w.Write([]string{
fmt.Sprint(file.ID),
file.Bucket,
fmt.Sprint(file.BucketID),
file.Name,
file.URL,
fmt.Sprintf("%d", file.Size),
file.Type,
time.Unix(file.LastModified, 0).Format(time.RFC3339),
})
}
w.Flush()
} else {
allFiles = append(allFiles, resp.Files...)
}
atomic.AddInt64(&fetched, int64(len(resp.Files)))
if total == -1 {
total = resp.Meta.Results
}
if total > 0 {
fmt.Printf("\r已获取 %d / %d 条", fetched, total)
} else {
fmt.Printf("\r已获取 %d 条", fetched)
}
if len(resp.Files) < pageSize || (total > 0 && offset+pageSize >= total) {
break
}
offset += pageSize
}
fmt.Println()
if w != nil {
fmt.Printf("completed, saved to %s\n", output)
} else {
out, _ := json.MarshalIndent(allFiles, "", " ")
os.Stdout.Write(out)
}
}
func handleBuckets(client *http.Client, apiKey, keywords, cloudType string, limit, start int, output string, onlyBucket bool) {
pageSize := limit
if pageSize <= 0 || pageSize > 1000 {
pageSize = 1000
}
var allBuckets []Bucket
var w *csv.Writer
var fetched int64
if output != "" {
f, err := os.Create(output)
if err != nil {
log.Fatalf("create csv: %v", err)
}
defer f.Close()
w = csv.NewWriter(f)
defer w.Flush()
if onlyBucket {
w.Write([]string{"bucket"})
} else {
w.Write([]string{"id", "bucket", "fileCount", "type"})
}
}
offset := start
total := -1
for {
urlStr := buildURL("/buckets", map[string]string{
"keywords": keywords,
"type": cloudType,
"limit": fmt.Sprintf("%d", pageSize),
"start": fmt.Sprintf("%d", offset),
})
data, err := doGet(client, apiKey, urlStr)
if err != nil {
log.Fatalf("request error: %v", err)
}
var resp BucketsResponse
if err := json.Unmarshal(data, &resp); err != nil {
log.Fatalf("decode: %v", err)
}
// client-side filter if cloudType specified
filtered := resp.Buckets
if cloudType != "" {
var tmp []Bucket
for _, b := range resp.Buckets {
if strings.EqualFold(b.Type, cloudType) {
tmp = append(tmp, b)
}
}
filtered = tmp
}
if w != nil {
if onlyBucket {
for _, b := range filtered {
w.Write([]string{b.Bucket})
}
} else {
for _, b := range filtered {
w.Write([]string{
fmt.Sprint(b.ID),
b.Bucket,
fmt.Sprintf("%d", b.FileCount),
b.Type,
})
}
}
w.Flush()
} else {
allBuckets = append(allBuckets, filtered...)
}
atomic.AddInt64(&fetched, int64(len(filtered)))
if total == -1 {
total = resp.Meta.Results
}
if total > 0 {
fmt.Printf("\r已获取 %d / %d 条", fetched, total)
} else {
fmt.Printf("\r已获取 %d 条", fetched)
}
if len(resp.Buckets) < pageSize || (total > 0 && offset+pageSize >= total) {
break
}
offset += pageSize
}
fmt.Println()
if w != nil {
fmt.Printf("completed, saved to %s\n", output)
} else {
if onlyBucket {
for _, b := range allBuckets {
fmt.Println(b.Bucket)
}
} else {
out, _ := json.MarshalIndent(allBuckets, "", " ")
os.Stdout.Write(out)
}
}
}
func handleStats(client *http.Client, apiKey, output string) {
urlStr := baseURL + "/stats"
data, err := doGet(client, apiKey, urlStr)
if err != nil {
log.Fatalf("request error: %v", err)
}
if output == "" {
os.Stdout.Write(data)
return
}
if err := os.WriteFile(output, data, 0644); err != nil {
log.Fatalf("write file: %v", err)
}
fmt.Printf("stats saved to %s\n", output)
}