|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "encoding/json" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "io/ioutil" |
| 9 | + "net/http" |
| 10 | + "net/http/cookiejar" |
| 11 | + "os" |
| 12 | + "path/filepath" |
| 13 | + "strconv" |
| 14 | + "strings" |
| 15 | + "time" |
| 16 | +) |
| 17 | + |
| 18 | +var justsuccess bool = false |
| 19 | +var successlist map[string][]string |
| 20 | +var httpcc http.Client |
| 21 | +var formatType string |
| 22 | +var outputDir string |
| 23 | + |
| 24 | +func init() { |
| 25 | + successlist = make(map[string][]string) |
| 26 | +} |
| 27 | + |
| 28 | +func main() { |
| 29 | + |
| 30 | + http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} |
| 31 | + |
| 32 | + address := flag.String("url", "", "URL to scan (e.g., https://example.com)") |
| 33 | + configfile := flag.String("config", "", "custom config JSON file path") |
| 34 | + format := flag.String("f", "", "output format: json or csv") |
| 35 | + outDir := flag.String("o", "", "output directory path") |
| 36 | + gitfile := flag.Bool("git", false, "scan git-related files") |
| 37 | + Sensfile := flag.Bool("sens", false, "try sens lists") |
| 38 | + Envfile := flag.Bool("env", false, "try env lists") |
| 39 | + Shellfile := flag.Bool("shell", false, "try shellfile lists") |
| 40 | + Allfile := flag.Bool("all", false, "try all lists") |
| 41 | + success := flag.Bool("v", false, "show success result only") |
| 42 | + flag.Parse() |
| 43 | + formatType = *format |
| 44 | + outputDir = *outDir |
| 45 | + if !*gitfile && !*Sensfile && !*Envfile && !*Shellfile { |
| 46 | + |
| 47 | + *Allfile = true |
| 48 | + } |
| 49 | + if *Allfile { |
| 50 | + *gitfile = true |
| 51 | + *Sensfile = true |
| 52 | + *gitfile = true |
| 53 | + *Envfile = true |
| 54 | + } |
| 55 | + if *success { |
| 56 | + justsuccess = true |
| 57 | + } |
| 58 | + if *address == "" { |
| 59 | + println("please set url with --url or -h for help") |
| 60 | + return |
| 61 | + } |
| 62 | + //ex, err := os.Executable() |
| 63 | + //if err != nil { |
| 64 | + // panic(err) |
| 65 | + //} |
| 66 | + //exPath := filepath.Dir(ex) |
| 67 | + |
| 68 | + configfilepath := "./SensitiveList.json" |
| 69 | + if *configfile != "" { |
| 70 | + configfilepath = *configfile |
| 71 | + } |
| 72 | + jsonFile, err := os.Open(configfilepath) |
| 73 | + if err != nil { |
| 74 | + fmt.Printf("%s", "Can not read json file") |
| 75 | + } |
| 76 | + byteValue, _ := ioutil.ReadAll(jsonFile) |
| 77 | + |
| 78 | + // we initialize our Users array |
| 79 | + paths := SensitiveList{} |
| 80 | + |
| 81 | + // we unmarshal our byteArray which contains our |
| 82 | + // jsonFile's content into 'users' which we defined above |
| 83 | + json.Unmarshal(byteValue, &paths) |
| 84 | + defer jsonFile.Close() |
| 85 | + jar, err := cookiejar.New(nil) |
| 86 | + if err != nil { |
| 87 | + println(err.Error()) |
| 88 | + } |
| 89 | + |
| 90 | + httpcc = http.Client{Jar: jar} |
| 91 | + if *gitfile { |
| 92 | + for i := 0; i < len(paths.Git); i++ { |
| 93 | + checkurl(*address+paths.Git[i].Path, paths.Git[i].Content, paths.Git[i].Lentgh, "Git") |
| 94 | + } |
| 95 | + } |
| 96 | + if *Sensfile { |
| 97 | + for i := 0; i < len(paths.Sensitive); i++ { |
| 98 | + checkurl(*address+paths.Sensitive[i].Path, paths.Sensitive[i].Content, paths.Sensitive[i].Lentgh, "Sensitive") |
| 99 | + } |
| 100 | + } |
| 101 | + if *Envfile { |
| 102 | + for i := 0; i < len(paths.Env); i++ { |
| 103 | + checkurl(*address+paths.Env[i].Path, paths.Env[i].Content, paths.Env[i].Lentgh, "Env") |
| 104 | + } |
| 105 | + } |
| 106 | + if *Shellfile { |
| 107 | + for i := 0; i < len(paths.Shell); i++ { |
| 108 | + checkurl(*address+paths.Shell[i].Path, paths.Shell[i].Content, paths.Shell[i].Lentgh, "Shell") |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + totalFiles := 0 |
| 113 | + for _, files := range successlist { |
| 114 | + totalFiles += len(files) |
| 115 | + } |
| 116 | + |
| 117 | + if totalFiles > 0 { |
| 118 | + switch formatType { |
| 119 | + case "json": |
| 120 | + writeJSONOutput(successlist, outputDir) |
| 121 | + case "csv": |
| 122 | + writeCSVOutput(successlist, outputDir) |
| 123 | + default: |
| 124 | + printResults(successlist) |
| 125 | + } |
| 126 | + } else { |
| 127 | + fmt.Println("\n🔍 No sensitive files found.") |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func checkurl(url string, content string, len string, category string) { |
| 132 | + // Set timeout of 20 seconds |
| 133 | + httpcc.Timeout = 20 * time.Second |
| 134 | + |
| 135 | + resp, err := httpcc.Head(url) |
| 136 | + |
| 137 | + if err != nil { |
| 138 | + println(err.Error()) |
| 139 | + if strings.Contains(err.Error(), "http: server gave HTTP response to HTTPS clien") { |
| 140 | + os.Exit(3) |
| 141 | + } |
| 142 | + if strings.Contains(err.Error(), "timeout") { |
| 143 | + fmt.Printf("Timeout occurred while checking '%s'\n", url) |
| 144 | + return |
| 145 | + } |
| 146 | + |
| 147 | + resp, err = httpcc.Get(url) |
| 148 | + |
| 149 | + } |
| 150 | + if err == nil { |
| 151 | + if !justsuccess { |
| 152 | + fmt.Printf("Checking '%s', '%s',\n", url, resp.Status) |
| 153 | + } |
| 154 | + if resp.StatusCode == 200 { |
| 155 | + if resp.Header.Get("Content-Type") != "" { |
| 156 | + respcontetnt := resp.Header.Get("Content-Type") |
| 157 | + var ignore []string = []string{} |
| 158 | + if strings.Contains(content, "#") { |
| 159 | + arrayslpit := strings.Split(content, "#") |
| 160 | + for _, i := range arrayslpit { |
| 161 | + if i != "" { |
| 162 | + ignore = append(ignore, i) |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + if respcontetnt == content || content == "*" || checkifinarry(ignore, respcontetnt) { |
| 168 | + if len == "*" { |
| 169 | + fmt.Printf("Success '%s', '%s', '%s',\n", url, resp.Status, resp.Header.Get("Content-Type")) |
| 170 | + if _, exists := successlist[category]; !exists { |
| 171 | + successlist[category] = []string{} |
| 172 | + } |
| 173 | + successlist[category] = append(successlist[category], url) |
| 174 | + } else { |
| 175 | + lennumber, err := strconv.ParseInt(len, 0, 64) |
| 176 | + if err == nil { |
| 177 | + if lennumber >= resp.ContentLength { |
| 178 | + fmt.Printf("Success '%s', '%s', '%s',\n", url, resp.Status, resp.Header.Get("Content-Type")) |
| 179 | + if _, exists := successlist[category]; !exists { |
| 180 | + successlist[category] = []string{} |
| 181 | + } |
| 182 | + successlist[category] = append(successlist[category], url) |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + } else { |
| 189 | + |
| 190 | + } |
| 191 | + } |
| 192 | +} |
| 193 | +func checkifinarry(array []string, check string) bool { |
| 194 | + if len(array) == 0 { |
| 195 | + return false |
| 196 | + } |
| 197 | + for _, i2 := range array { |
| 198 | + if strings.Contains(check, i2) { |
| 199 | + return false |
| 200 | + } |
| 201 | + } |
| 202 | + return true |
| 203 | +} |
| 204 | + |
| 205 | +type Sensitive struct { |
| 206 | + Path string `json:"path"` |
| 207 | + Content string `json:"content"` |
| 208 | + Lentgh string `json:"lentgh"` |
| 209 | +} |
| 210 | +type SensitiveList struct { |
| 211 | + Sensitive []Sensitive `json:"Sensitive"` |
| 212 | + Git []Sensitive `json:"Gitfile"` |
| 213 | + Env []Sensitive `json:Env` |
| 214 | + Shell []Sensitive `json:shell` |
| 215 | +} |
| 216 | + |
| 217 | +func writeJSONOutput(results map[string][]string, outputDir string) { |
| 218 | + output := struct { |
| 219 | + TotalCount int `json:"total_count"` |
| 220 | + Categories map[string][]string `json:"categories"` |
| 221 | + Summary map[string]int `json:"summary"` |
| 222 | + }{ |
| 223 | + Categories: results, |
| 224 | + Summary: make(map[string]int), |
| 225 | + } |
| 226 | + |
| 227 | + for category, files := range results { |
| 228 | + output.Summary[category] = len(files) |
| 229 | + output.TotalCount += len(files) |
| 230 | + } |
| 231 | + |
| 232 | + jsonData, err := json.MarshalIndent(output, "", " ") |
| 233 | + if err != nil { |
| 234 | + fmt.Printf("Error creating JSON output: %v\n", err) |
| 235 | + return |
| 236 | + } |
| 237 | + |
| 238 | + if outputDir != "" { |
| 239 | + filename := filepath.Join(outputDir, "scan_results.json") |
| 240 | + if err := os.WriteFile(filename, jsonData, 0644); err != nil { |
| 241 | + fmt.Printf("Error writing JSON file: %v\n", err) |
| 242 | + return |
| 243 | + } |
| 244 | + fmt.Printf("📝 Results saved to: %s\n", filename) |
| 245 | + } else { |
| 246 | + fmt.Println(string(jsonData)) |
| 247 | + } |
| 248 | +} |
| 249 | + |
| 250 | +func writeCSVOutput(results map[string][]string, outputDir string) { |
| 251 | + var output strings.Builder |
| 252 | + output.WriteString("Category,URL\n") |
| 253 | + |
| 254 | + for category, urls := range results { |
| 255 | + for _, url := range urls { |
| 256 | + output.WriteString(fmt.Sprintf("%s,%s\n", category, url)) |
| 257 | + } |
| 258 | + } |
| 259 | + |
| 260 | + if outputDir != "" { |
| 261 | + filename := filepath.Join(outputDir, "scan_results.csv") |
| 262 | + if err := os.WriteFile(filename, []byte(output.String()), 0644); err != nil { |
| 263 | + fmt.Printf("Error writing CSV file: %v\n", err) |
| 264 | + return |
| 265 | + } |
| 266 | + fmt.Printf("📝 Results saved to: %s\n", filename) |
| 267 | + } else { |
| 268 | + fmt.Print(output.String()) |
| 269 | + } |
| 270 | +} |
| 271 | + |
| 272 | +func printResults(results map[string][]string) { |
| 273 | + totalFiles := 0 |
| 274 | + for _, files := range results { |
| 275 | + totalFiles += len(files) |
| 276 | + } |
| 277 | + |
| 278 | + fmt.Printf("\n🎯 Found %d sensitive files:\n\n", totalFiles) |
| 279 | + |
| 280 | + for category, urls := range results { |
| 281 | + fmt.Printf("📁 %s (%d files):\n", category, len(urls)) |
| 282 | + for _, url := range urls { |
| 283 | + fmt.Printf(" └─ %s\n", url) |
| 284 | + } |
| 285 | + fmt.Println() |
| 286 | + } |
| 287 | +} |
0 commit comments