-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
335 lines (296 loc) Β· 8.55 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
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
package main
import (
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
var justsuccess bool = false
var successlist map[string][]string
var httpcc http.Client
var formatType string
var outputDir string
func init() {
successlist = make(map[string][]string)
}
func main() {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
address := flag.String("url", "", "URL to scan (e.g., https://example.com)")
format := flag.String("f", "", "output format: json or csv")
outDir := flag.String("o", "", "output directory path")
gitfile := flag.Bool("git", false, "scan git-related files")
Sensfile := flag.Bool("sens", false, "try sens lists")
Envfile := flag.Bool("env", false, "try env lists")
Shellfile := flag.Bool("shell", false, "try shellfile lists")
Allfile := flag.Bool("all", false, "try all lists")
success := flag.Bool("v", false, "show success result only")
flag.Parse()
formatType = *format
outputDir = *outDir
if !*gitfile && !*Sensfile && !*Envfile && !*Shellfile {
*Allfile = true
}
if *Allfile {
*gitfile = true
*Sensfile = true
*gitfile = true
*Envfile = true
}
if *success {
justsuccess = true
}
if *address == "" {
println("please set url with --url or -h for help")
return
}
// Add site availability check
if !checkSiteIsUp(*address) {
fmt.Printf("π¨ Host %s is unreachable, aborting scan\n", *address)
return
}
//ex, err := os.Executable()
//if err != nil {go
// panic(err)
//}
//exPath := filepath.Dir(ex)
appPath, err := os.Executable()
if err != nil {
fmt.Printf("Failed to get application path: %v\n", err)
return
}
appDir := filepath.Dir(appPath) // Directory where the application is running
// Default paths
defaultLocalPath := filepath.Join(appDir, "SensitiveList.json") // ./SensitiveList.json
defaultGlobalPath := "/usr/local/bin/SensitiveList.json" // /usr/local/bin/SensitiveList.json
// Check if the file exists in the application's directory
configfilepath := defaultLocalPath
if _, err := os.Stat(configfilepath); os.IsNotExist(err) {
// If not found in the app directory, fall back to /usr/local/bin
fmt.Printf("SensitiveList.json not found in %s, trying %s\n", appDir, defaultGlobalPath)
configfilepath = defaultGlobalPath
}
jsonFile, err := os.Open(configfilepath)
if err != nil {
fmt.Printf("%s", "Can not read json file")
}
byteValue, _ := ioutil.ReadAll(jsonFile)
// we initialize our Users array
paths := SensitiveList{}
// we unmarshal our byteArray which contains our
// jsonFile's content into 'users' which we defined above
json.Unmarshal(byteValue, &paths)
defer jsonFile.Close()
jar, err := cookiejar.New(nil)
if err != nil {
println(err.Error())
}
httpcc = http.Client{Jar: jar}
if *gitfile {
for i := 0; i < len(paths.Git); i++ {
checkurl(*address+paths.Git[i].Path, paths.Git[i].Content, paths.Git[i].Lentgh, "Git")
}
}
if *Sensfile {
for i := 0; i < len(paths.Sensitive); i++ {
checkurl(*address+paths.Sensitive[i].Path, paths.Sensitive[i].Content, paths.Sensitive[i].Lentgh, "Sensitive")
}
}
if *Envfile {
for i := 0; i < len(paths.Env); i++ {
checkurl(*address+paths.Env[i].Path, paths.Env[i].Content, paths.Env[i].Lentgh, "Env")
}
}
if *Shellfile {
for i := 0; i < len(paths.Shell); i++ {
checkurl(*address+paths.Shell[i].Path, paths.Shell[i].Content, paths.Shell[i].Lentgh, "Shell")
}
}
switch formatType {
case "json":
writeJSONOutput(successlist, outputDir)
case "csv":
writeCSVOutput(successlist, outputDir)
default:
printResults(successlist)
}
}
func checkurl(url string, content string, len string, category string) {
// Set timeout of 20 seconds
httpcc.Timeout = 20 * time.Second
resp, err := httpcc.Head(url)
if err != nil {
println(err.Error())
if strings.Contains(err.Error(), "http: server gave HTTP response to HTTPS clien") {
os.Exit(3)
}
if strings.Contains(err.Error(), "timeout") {
fmt.Printf("Timeout occurred while checking '%s'\n", url)
return
}
resp, err = httpcc.Get(url)
}
if err == nil {
if !justsuccess {
fmt.Printf("Checking '%s', '%s',\n", url, resp.Status)
}
if resp.StatusCode == 200 {
if resp.Header.Get("Content-Type") != "" {
respcontetnt := resp.Header.Get("Content-Type")
var ignore []string = []string{}
if strings.Contains(content, "#") {
arrayslpit := strings.Split(content, "#")
for _, i := range arrayslpit {
if i != "" {
ignore = append(ignore, i)
}
}
}
if respcontetnt == content || content == "*" || checkifinarry(ignore, respcontetnt) {
if len == "*" {
fmt.Printf("Success '%s', '%s', '%s',\n", url, resp.Status, resp.Header.Get("Content-Type"))
if _, exists := successlist[category]; !exists {
successlist[category] = []string{}
}
successlist[category] = append(successlist[category], url)
} else {
lennumber, err := strconv.ParseInt(len, 0, 64)
if err == nil {
if lennumber >= resp.ContentLength {
fmt.Printf("Success '%s', '%s', '%s',\n", url, resp.Status, resp.Header.Get("Content-Type"))
if _, exists := successlist[category]; !exists {
successlist[category] = []string{}
}
successlist[category] = append(successlist[category], url)
}
}
}
}
}
} else {
}
}
}
func checkifinarry(array []string, check string) bool {
if len(array) == 0 {
return false
}
for _, i2 := range array {
if strings.Contains(check, i2) {
return false
}
}
return true
}
type Sensitive struct {
Path string `json:"path"`
Content string `json:"content"`
Lentgh string `json:"lentgh"`
}
type SensitiveList struct {
Sensitive []Sensitive `json:"Sensitive"`
Git []Sensitive `json:"Gitfile"`
Env []Sensitive `json:Env`
Shell []Sensitive `json:shell`
}
func writeJSONOutput(results map[string][]string, outputDir string) {
output := struct {
TotalCount int `json:"total_count"`
Categories map[string][]string `json:"categories"`
Summary map[string]int `json:"summary"`
}{
Categories: results,
Summary: make(map[string]int),
}
for category, files := range results {
output.Summary[category] = len(files)
output.TotalCount += len(files)
}
jsonData, err := json.MarshalIndent(output, "", " ")
if err != nil {
fmt.Printf("Error creating JSON output: %v\n", err)
return
}
if outputDir != "" {
// Create directory if it doesn't exist
dir := filepath.Dir(outputDir)
if err := os.MkdirAll(dir, 0755); err != nil {
fmt.Printf("Error creating directory: %v\n", err)
return
}
// Use outputDir directly as it contains the filename
if err := os.WriteFile(outputDir, jsonData, 0644); err != nil {
fmt.Printf("Error writing JSON file: %v\n", err)
return
}
fmt.Printf("π Results saved to: %s\n", outputDir)
} else {
fmt.Println(string(jsonData))
}
}
func writeCSVOutput(results map[string][]string, outputDir string) {
var output strings.Builder
output.WriteString("Category,URL\n")
for category, urls := range results {
for _, url := range urls {
output.WriteString(fmt.Sprintf("%s,%s\n", category, url))
}
}
if outputDir != "" {
// Create directory if it doesn't exist
dir := filepath.Dir(outputDir)
if err := os.MkdirAll(dir, 0755); err != nil {
fmt.Printf("Error creating directory: %v\n", err)
return
}
// Use outputDir directly as it contains the filename
if err := os.WriteFile(outputDir, []byte(output.String()), 0644); err != nil {
fmt.Printf("Error writing CSV file: %v\n", err)
return
}
fmt.Printf("π Results saved to: %s\n", outputDir)
} else {
fmt.Print(output.String())
}
}
func printResults(results map[string][]string) {
totalFiles := 0
for _, files := range results {
totalFiles += len(files)
}
fmt.Printf("\nπ― Found %d sensitive files:\n\n", totalFiles)
for category, urls := range results {
fmt.Printf("π %s (%d files):\n", category, len(urls))
for _, url := range urls {
fmt.Printf(" ββ %s\n", url)
}
fmt.Println()
}
}
// Add new function for site availability check
func checkSiteIsUp(url string) bool {
client := &http.Client{
Timeout: 20 * time.Second,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
resp, err := client.Head(url)
if err != nil {
return false
}
defer resp.Body.Close()
// Consider any 2xx/3xx status as "up"
if resp.StatusCode >= 200 && resp.StatusCode < 400 {
fmt.Printf("β
Host is reachable (%s)\n", resp.Status)
return true
}
return false
}