-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmia.go
More file actions
290 lines (254 loc) · 6.92 KB
/
Copy pathmia.go
File metadata and controls
290 lines (254 loc) · 6.92 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
// main.go
package main
import (
"bufio"
"fmt"
"net/url"
"os"
"sync"
"unicode"
"github.com/spf13/cobra"
)
func printASCIIArt() {
fmt.Println(`
__ ____
/ |/ (_)___ _
/ /|_/ / / __ '/
/ / / / / /_/ /
/_/ /_/_/\__,_/
Codename: Pain
Made with love <3
-Wired & Fuwa
`)
}
var (
payload string
file string
outFile string
bitFlip bool
urlEncoding bool
caseToggle bool
reversal bool
)
func main() {
printASCIIArt() // Print banner first
Execute()
}
var rootCmd = &cobra.Command{
Use: "mia",
Short: "Mia - A payload mutation CLI tool for fuzzers and pentesters",
Long: `Mia is a CLI utility that generates mutated payloads for fuzzing, evading filters, and testing web applications.`,
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
var mutateCmd = &cobra.Command{
Use: "mutate",
Short: "Mutate a given payload using selected techniques",
Run: func(cmd *cobra.Command, args []string) {
opts := MutationOptions{
EnableBitFlip: bitFlip,
EnableURLEncoding: urlEncoding,
EnableCaseToggle: caseToggle,
EnableReversal: reversal,
}
// If a file is provided, process it line by line.
if file != "" {
processFile(file, opts)
} else if payload != "" {
// Process a single payload string.
results := MutatePayload(payload, opts)
outputResults(results)
} else {
fmt.Println("You must provide --payload or --file")
return
}
},
}
func init() {
rootCmd.AddCommand(mutateCmd)
mutateCmd.Flags().StringVar(&payload, "payload", "", "Payload string to mutate")
mutateCmd.Flags().StringVar(&file, "file", "", "Path to file with payload(s)")
mutateCmd.Flags().StringVar(&outFile, "out", "", "File to write output to")
mutateCmd.Flags().BoolVar(&bitFlip, "bitflip", true, "Enable bit-flip mutations")
mutateCmd.Flags().BoolVar(&urlEncoding, "urlencode", true, "Enable URL encoding mutations")
mutateCmd.Flags().BoolVar(&caseToggle, "casetoggle", false, "Enable case toggle mutations")
mutateCmd.Flags().BoolVar(&reversal, "reverse", false, "Enable reverse mutation")
}
func outputResults(results []string) {
if outFile != "" {
err := os.WriteFile(outFile, []byte(joinLines(results)), 0644)
if err != nil {
fmt.Println("Failed to write output file:", err)
}
} else {
for _, m := range results {
fmt.Println(m)
}
}
}
func joinLines(lines []string) string {
result := ""
for _, line := range lines {
result += line + "\n"
}
return result
}
// processFile reads the file line by line, mutates each line, and outputs the results.
func processFile(filename string, opts MutationOptions) {
f, err := os.Open(filename)
if err != nil {
fmt.Println("Failed to open file:", err)
os.Exit(1)
}
defer f.Close()
scanner := bufio.NewScanner(f)
var allResults []string
lineNumber := 0
for scanner.Scan() {
lineNumber++
line := scanner.Text()
// Process each line individually.
results := MutatePayload(line, opts)
allResults = append(allResults, results...)
}
if err := scanner.Err(); err != nil {
fmt.Println("Error reading file:", err)
}
outputResults(allResults)
}
// MutationOptions holds flags for which mutation techniques to apply.
type MutationOptions struct {
EnableBitFlip bool
EnableURLEncoding bool
EnableCaseToggle bool
EnableReversal bool
}
// MutationTask represents a mutation job for the worker pool.
type MutationTask struct {
Name string
Fn func(string) []string
Payload string
}
// runWorkerPool executes mutation tasks concurrently using a fixed number of workers.
func runWorkerPool(tasks []MutationTask, numWorkers int) []string {
taskChan := make(chan MutationTask)
resultChan := make(chan []string)
var wg sync.WaitGroup
// Worker function: processes tasks from the taskChan.
worker := func() {
defer wg.Done()
for task := range taskChan {
res := task.Fn(task.Payload)
resultChan <- res
}
}
// Start workers.
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go worker()
}
// Feed tasks to the workers.
go func() {
for _, task := range tasks {
taskChan <- task
}
close(taskChan)
}()
// Close the result channel once all workers are done.
go func() {
wg.Wait()
close(resultChan)
}()
// Collect and de-duplicate results.
mutationSet := make(map[string]bool)
for res := range resultChan {
for _, m := range res {
mutationSet[m] = true
}
}
var mutations []string
for m := range mutationSet {
mutations = append(mutations, m)
}
return mutations
}
// MutatePayload creates tasks for each enabled mutation technique and uses the worker pool.
func MutatePayload(payload string, opts MutationOptions) []string {
mutationSet := make(map[string]bool)
mutationSet[payload] = true
var tasks []MutationTask
if opts.EnableBitFlip {
tasks = append(tasks, MutationTask{Name: "bitflip", Fn: BitFlipMutations, Payload: payload})
}
if opts.EnableURLEncoding {
tasks = append(tasks, MutationTask{Name: "urlencode", Fn: URLEncodingMutations, Payload: payload})
}
if opts.EnableCaseToggle {
tasks = append(tasks, MutationTask{Name: "casetoggle", Fn: CaseToggleMutations, Payload: payload})
}
if opts.EnableReversal {
tasks = append(tasks, MutationTask{Name: "reverse", Fn: ReverseMutation, Payload: payload})
}
// Use a worker pool to process mutation tasks concurrently.
if len(tasks) > 0 {
numWorkers := len(tasks)
results := runWorkerPool(tasks, numWorkers)
for _, m := range results {
mutationSet[m] = true
}
}
var out []string
for m := range mutationSet {
out = append(out, m)
}
return out
}
// BitFlipMutations generates payload mutations by flipping each bit in the payload.
func BitFlipMutations(payload string) []string {
var mutations []string
bytesPayload := []byte(payload)
for i, b := range bytesPayload {
for bit := 0; bit < 8; bit++ {
mutated := make([]byte, len(bytesPayload))
copy(mutated, bytesPayload)
mutated[i] = b ^ (1 << uint(bit))
mutations = append(mutations, string(mutated))
}
}
return mutations
}
// URLEncodingMutations generates a mutation by applying URL encoding to the payload.
func URLEncodingMutations(payload string) []string {
encoded := url.QueryEscape(payload)
return []string{encoded}
}
// CaseToggleMutations toggles the case of each letter in the payload.
func CaseToggleMutations(payload string) []string {
var mutations []string
runes := []rune(payload)
for i, r := range runes {
if unicode.IsLetter(r) {
toggled := make([]rune, len(runes))
copy(toggled, runes)
if unicode.IsUpper(r) {
toggled[i] = unicode.ToLower(r)
} else {
toggled[i] = unicode.ToUpper(r)
}
mutations = append(mutations, string(toggled))
}
}
return mutations
}
// ReverseMutation returns a single mutation which is the reversed payload.
func ReverseMutation(payload string) []string {
runes := []rune(payload)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return []string{string(runes)}
}