-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcameron.go
More file actions
357 lines (294 loc) · 8.69 KB
/
cameron.go
File metadata and controls
357 lines (294 loc) · 8.69 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Cameron, a web fuzzer
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"unicode"
)
// Flags
var t = flag.String("t", "localhost", "set target IP/URL")
var l = flag.String("l", "", "input wordlist")
var v = flag.Bool("v", false, "enable verbose output")
var r = flag.Int("r", 5, "set requests per second")
var fc = flag.String("fc", "", "filter status code")
var mc = flag.String("mc", "", "match status code")
var maxRequests int
var isVerbose bool
var wordlistFile string
// Start fuzzing
func main() {
var host string = ""
var wg sync.WaitGroup
tokens := make(chan struct{}, *r)
var startTimer time.Time
var scanResults sync.Map
wordlistFile = *l
filterCode := *fc
matchCode := *mc
var progressCounter uint64
if isVerbose {
startTimer = time.Now()
}
// Temporary Debugs
//fmt.Println("l:", *l)
// Validate args input
checkArgs(&host)
// Read wordlist file
wlFile := getFile(wordlistFile)
client := &http.Client{
Timeout: 10 * time.Second,
}
// Progress tests
if !isVerbose {
go progressBar(wlFile, &progressCounter)
}
// Fuzz scan
for _, targetWord := range wlFile {
wg.Add(1)
go fuzz(wlFile, host, targetWord, &wg, &tokens, client, &scanResults, &progressCounter)
}
wg.Wait()
printResults(scanResults, host, filterCode, matchCode)
// Time program execution
stopTimer := time.Now()
if isVerbose {
duration := stopTimer.Sub(startTimer)
fmt.Println("")
fmt.Println("Scan duration: ", duration)
}
}
// Fuzz a URL with words from wordlist
func fuzz(wordlist []string, target string, targetWord string, wg *sync.WaitGroup, tokens *chan struct{}, client *http.Client, scanResults *sync.Map, progressCounter *uint64) {
defer wg.Done()
*tokens <- struct{}{}
// Fuzzing
targetCombined := replaceFUZZ(target, targetWord)
resp, err := client.Get(targetCombined)
if err != nil {
fmt.Printf("\nError fetching URL %s: %s", targetCombined, err)
atomic.AddUint64(progressCounter, 1)
<-*tokens
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("\nError reading response body:", err)
atomic.AddUint64(progressCounter, 1)
<-*tokens
return
}
// Get the size of the response body in bytes
responseSize := len(body)
// Count the number of lines in the response body
lineCount := countLines(string(body))
// Count the number of words in the response body
wordCount := countWords(string(body))
// Store results from response body
respData := [4]int{resp.StatusCode, responseSize, wordCount, lineCount}
scanResults.Store(targetCombined, respData)
atomic.AddUint64(progressCounter, 1)
time.Sleep(1 * time.Second)
<-*tokens
}
// Progress bar
func progressBar(wlFile []string, progressCounter *uint64) {
var count uint64
count = 1
for int(count) <= len(wlFile) {
count = atomic.LoadUint64(progressCounter)
fmt.Printf("\033[2J\033[0;0HProgress: %d %s %d %s", count, "of", len(wlFile), "targets done.")
if int(count) == len(wlFile) {
fmt.Print("\n\n")
time.Sleep(1000 * time.Millisecond)
} else {
time.Sleep(40 * time.Millisecond)
}
}
}
// Read wordlist from file
func getFile(wordlistFile string) []string {
list := []string{}
file, err := os.Open(wordlistFile)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
list = append(list, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return list
}
// Count the lines in a string
func countLines(s string) int {
n := strings.Count(s, "\n")
if !strings.HasSuffix(s, "\n") {
n++
}
return n
}
// Count the words in a string
func countWords(text string) int {
words := strings.FieldsFunc(text, func(c rune) bool {
return !unicode.IsLetter(c) && !unicode.IsNumber(c)
})
return len(words)
}
// Replace FUZZ with word from wordlist
func replaceFUZZ(host string, fuzz string) string {
out := strings.Replace(host, "FUZZ", fuzz, 1)
return out
}
// Pretty print results in table
func printResults(scanResults sync.Map, host string, filterCode string, matchCode string) {
tempMap := map[string][4]int{}
scanResults.Range(func(key, value interface{}) bool {
tempMap[fmt.Sprint(key)] = value.([4]int)
return true
})
// Sort map by string and print
fmt.Printf("%-20v %-6v %-4v %-5v %-5v\n", "HOST", "Status", "Size", "Words", "Lines")
keys := make([]string, 0)
for k := range tempMap {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
trimmedHost := strings.TrimSuffix(host, "/FUZZ")
trimmedHost = strings.TrimPrefix(k, trimmedHost)
isPrint := checkFilters(filterCode, k, tempMap, matchCode, trimmedHost)
if isPrint {
fmt.Printf("%-20s %-6d %-4d %-5d %-5d \n", trimmedHost, tempMap[k][0], tempMap[k][1], tempMap[k][2], tempMap[k][3])
}
}
}
// Check args filters and return true if not filtered
func checkFilters(filterCode string, k string, tempMap map[string][4]int, matchCode string, trimmedHost string) bool {
checked := true
// not in filter and in match
if strings.Contains(filterCode, strconv.Itoa(tempMap[k][0])) && filterCode != "" {
checked = false
}
// not in filter and in match
if !strings.Contains(matchCode, strconv.Itoa(tempMap[k][0])) && matchCode != "" {
checked = false
}
return checked
}
// Check arguments list for valid values
func checkArgs(host *string) {
// No args
if len(os.Args) <= 1 {
prHeader()
os.Exit(0)
}
// Check wordlist
if *l == "" {
fmt.Println("Error: No wordlist. Use -l wordlistname.txt")
os.Exit(0)
}
if _, err := os.Stat(wordlistFile); err != nil {
fmt.Println("Error: Wordlist file path not valid.")
os.Exit(0)
}
// Check target
checkTarget(host)
}
// Check if user input for target is valid IP or URI
func checkTarget(host *string) {
// Check for FUZZ keyword
if !strings.Contains(*t, "FUZZ") {
fmt.Println("Error: Input is missing FUZZ keyword.")
os.Exit(0)
}
// Check for valid IP in input
checkIP := net.ParseIP(*t)
if checkIP != nil {
*host = *t
fmt.Println("Target input validated in: Check IP")
return
}
// Check for valid URI in input
_, err := url.ParseRequestURI(*t)
if err == nil {
*host = *t
fmt.Println("Target input validated in: URI")
return
}
// Check for if input is string localhost
if *t == "localhost" {
tempHost := fmt.Sprintf("%s%s", "http://", *t)
*host = tempHost
fmt.Println("Target input validated in: localhost")
return
}
// Add http prefix to check isURI again
tempHost := fmt.Sprintf("%s%s", "http://", *t)
_, err2 := url.ParseRequestURI(tempHost)
if err2 == nil {
*host = tempHost
fmt.Println("Target input validated in: add http then check URI")
return
}
// Exit program since no valid input
prHeader()
fmt.Println("Error: No valid IP or URI given")
fmt.Println("Error on input target candidate: ", *t)
os.Exit(0)
}
// Set initial values from flags and other values
func init() {
flag.Parse()
if *l != "" {
wordlistFile = *l
}
if *r > 0 {
maxRequests = *r
} else {
// Default on negative input
maxRequests = 5
}
if *v {
isVerbose = true
fmt.Println("Cameron is in a talkative mood right now")
} else {
isVerbose = false
}
if isVerbose {
fmt.Println("Requests per second: ", maxRequests)
}
}
// Print header when no arguments in CLI or on error
func prHeader() {
fmt.Println("Cameron, a web fuzzer by BenPapple")
fmt.Println("")
// ANSI Shadow
fmt.Println(" ██████╗ █████╗ ███╗ ███╗███████╗██████╗ ██████╗ ███╗ ██╗")
fmt.Println("██╔════╝██╔══██╗████╗ ████║██╔════╝██╔══██╗██╔═══██╗████╗ ██║")
fmt.Println("██║ ███████║██╔████╔██║█████╗ ██████╔╝██║ ██║██╔██╗ ██║")
fmt.Println("██║ ██╔══██║██║╚██╔╝██║██╔══╝ ██╔══██╗██║ ██║██║╚██╗██║")
fmt.Println("╚██████╗██║ ██║██║ ╚═╝ ██║███████╗██║ ██║╚██████╔╝██║ ╚████║")
fmt.Println(" ╚═════╝╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝")
fmt.Println("")
fmt.Println("Use -h for help")
fmt.Println("Example use case: go run cameron.go -l ~/yourwordlists.txt -t URL/FUZZ")
fmt.Println("Example use case: go run cameron.go -l ~/yourwordlists.txt -t 127.0.0.1/FUZZ")
fmt.Println("")
}