/
file.go
277 lines (268 loc) 路 9.12 KB
/
file.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
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"sync"
"time"
spinner "github.com/briandowns/spinner"
model "github.com/hahwul/dalfox/v2/pkg/model"
"github.com/hahwul/dalfox/v2/pkg/printing"
"github.com/hahwul/dalfox/v2/pkg/scanning"
voltFile "github.com/hahwul/volt/file"
voltHar "github.com/hahwul/volt/format/har"
voltUtils "github.com/hahwul/volt/util"
"github.com/spf13/cobra"
)
// fileCmd represents the file command
var fileCmd = &cobra.Command{
Use: "file [filePath] [flags]",
Short: "Use file mode(targets list or rawdata)",
Run: func(cmd *cobra.Command, args []string) {
sf, _ := cmd.Flags().GetBool("silence-force")
if sf {
options.Silence = sf
}
printing.Banner(options)
tMethod := options.Method
options.Method = "FILE Mode"
if len(args) == 0 {
printing.DalLog("ERROR", "Input file path", options)
printing.DalLog("ERROR", "e.g dalfox file ./targets.txt or ./rawdata.raw", options)
return
}
printing.Summary(options, args[0])
options.Method = tMethod
var targets []string
mutex := &sync.Mutex{}
options.Mutex = mutex
if len(args) >= 1 {
rawdata, _ := cmd.Flags().GetBool("rawdata")
har, _ := cmd.Flags().GetBool("har")
if rawdata {
printing.DalLog("SYSTEM", "Using file mode(rawdata)", options)
ff, err := voltFile.ReadLinesOrLiteral(args[0])
_ = err
var path, body, host, target string
bodyswitch := false
for index, line := range ff {
if index == 0 {
parse := strings.Split(line, " ")
if len(parse) > 1 {
options.Method = parse[0]
path = parse[1]
} else {
printing.DalLog("ERROR", "HTTP Raw Request Format Error", options)
os.Exit(1)
}
} else {
if strings.Index(line, "Host: ") != -1 {
host = line[6:]
} else {
parse := strings.Split(line, ":")
if len(parse) > 1 {
options.Header = append(options.Header, line)
}
}
if bodyswitch {
body = body + line
}
if len(line) == 0 {
bodyswitch = true
}
}
}
options.Data = body
http, _ := cmd.Flags().GetBool("http")
if strings.Index(path, "http") == 0 {
target = path
} else {
if host == "" {
printing.DalLog("ERROR", "HTTP Raw Request Format Error - Not found Host", options)
os.Exit(1)
}
if http {
target = "http://" + host + path
} else {
target = "https://" + host + path
}
}
_, _ = scanning.Scan(target, options, "single")
} else if har {
printing.DalLog("SYSTEM", "Using file mode(targets list from HAR)", options)
if (!options.NoSpinner || !options.Silence) && !sf {
options.SpinnerObject = spinner.New(spinner.CharSets[14], 100*time.Millisecond, spinner.WithWriter(os.Stderr)) // Build our new spinner
}
var harObject voltHar.HARObject
harFile, err := ioutil.ReadFile(args[0])
if err == nil {
err = json.Unmarshal(harFile, &harObject)
if options.Format == "json" {
printing.DalLog("PRINT", "[", options)
}
for i, entry := range harObject.Log.Entries {
var turl string
options.NowURL = i + 1
if len(entry.Request.QueryString) > 0 {
var tquery string
for _, query := range entry.Request.QueryString {
tquery = tquery + query.Name + "=" + query.Value + "&"
}
turl = entry.Request.URL + "?" + tquery
} else {
turl = entry.Request.URL
}
if entry.Request.PostData.Text != "" {
options.Data = entry.Request.PostData.Text
}
options.Method = entry.Request.Method
_, _ = scanning.Scan(turl, options, strconv.Itoa(i))
if (!options.NoSpinner || !options.Silence) && !sf {
mutex.Lock()
options.NowURL = options.NowURL + 1
percent := fmt.Sprintf("%0.2f%%", float64(options.NowURL)/float64(options.AllURLS)*100)
options.SpinnerObject.Suffix = " [" + strconv.Itoa(options.NowURL) + "/" + strconv.Itoa(options.AllURLS) + " Tasks][" + percent + "] Multiple scanning from file"
mutex.Unlock()
}
}
if options.Format == "json" {
printing.DalLog("PRINT", "{}]", options)
}
if (!options.NoSpinner || !options.Silence) && !sf {
options.SpinnerObject.Stop()
}
}
} else {
printing.DalLog("SYSTEM", "Using file mode(targets list)", options)
if (!options.NoSpinner || !options.Silence) && !sf {
options.SpinnerObject = spinner.New(spinner.CharSets[14], 100*time.Millisecond, spinner.WithWriter(os.Stderr)) // Build our new spinner
}
ff, err := voltFile.ReadLinesOrLiteral(args[0])
_ = err
for _, target := range ff {
targets = append(targets, target)
}
// Remove Deplicated value
targets = voltUtils.UniqueStringSlice(targets)
printing.DalLog("SYSTEM", "Loaded "+strconv.Itoa(len(targets))+" target urls", options)
multi, _ := cmd.Flags().GetBool("multicast")
mass, _ := cmd.Flags().GetBool("mass")
if multi || mass {
printing.DalLog("SYSTEM", "Using multicasting mode", options)
options.Silence = true
options.MulticastMode = true
t := scanning.MakeTargetSlice(targets)
tt := 0
//mutex := &sync.Mutex{}
for k, v := range t {
_ = k
tt = tt + len(v)
}
if (!options.NoSpinner || !options.Silence) && !sf {
options.SpinnerObject.Prefix = " "
options.SpinnerObject.Suffix = " [" + strconv.Itoa(options.NowURL) + "/" + strconv.Itoa(tt) + " Tasks][0%] Parallel scanning from file"
if !options.NoColor {
options.SpinnerObject.Color("red", "bold")
}
options.SpinnerObject.Start()
}
var wg sync.WaitGroup
tasks := make(chan model.MassJob)
options.NowURL = 0
concurrency, _ := cmd.Flags().GetInt("mass-worker")
for k, v := range t {
if !options.Silence || !sf {
printing.DalLog("SYSTEM-M", "Parallel testing to '"+k+"' => "+strconv.Itoa(len(v))+" urls", options)
}
}
for task := 0; task < concurrency; task++ {
wg.Add(1)
go func() {
defer wg.Done()
for kv := range tasks {
v := kv.URLs
for i := range v {
_, _ = scanning.Scan(v[i], options, strconv.Itoa(len(v)))
if (!options.NoSpinner || !options.Silence) && !sf {
mutex.Lock()
options.NowURL = options.NowURL + 1
percent := fmt.Sprintf("%0.2f%%", float64(options.NowURL)/float64(tt)*100)
options.SpinnerObject.Suffix = " [" + strconv.Itoa(options.NowURL) + "/" + strconv.Itoa(tt) + " Tasks][" + percent + "] Parallel scanning from file"
mutex.Unlock()
}
}
}
}()
}
if options.Format == "json" {
printing.DalLog("PRINT", "[", options)
}
for k, v := range t {
temp := model.MassJob{
Name: k,
URLs: v,
}
tasks <- temp
}
close(tasks)
wg.Wait()
if options.Format == "json" {
printing.DalLog("PRINT", "{}]", options)
}
if (!options.NoSpinner || !options.Silence) && !sf {
options.SpinnerObject.Stop()
}
if !options.Silence || !sf {
printing.DalLog("SYSTEM-M", "Finish massive scan!", options)
}
} else {
options.AllURLS = len(targets)
if (!options.NoSpinner || !options.Silence) && !sf {
options.SpinnerObject.Prefix = " "
options.SpinnerObject.Suffix = " [" + strconv.Itoa(options.NowURL) + "/" + strconv.Itoa(options.AllURLS) + " Tasks][0%] Multiple scanning from file"
if !options.NoColor {
options.SpinnerObject.Color("red", "bold")
}
options.SpinnerObject.Start()
}
if options.Format == "json" {
printing.DalLog("PRINT", "[", options)
}
for i := range targets {
options.NowURL = i + 1
_, _ = scanning.Scan(targets[i], options, strconv.Itoa(i))
if (!options.NoSpinner || !options.Silence) && !sf {
mutex.Lock()
options.NowURL = options.NowURL + 1
percent := fmt.Sprintf("%0.2f%%", float64(options.NowURL)/float64(options.AllURLS)*100)
options.SpinnerObject.Suffix = " [" + strconv.Itoa(options.NowURL) + "/" + strconv.Itoa(options.AllURLS) + " Tasks][" + percent + "] Multiple scanning from file"
mutex.Unlock()
}
}
if options.Format == "json" {
printing.DalLog("PRINT", "{}]", options)
}
if (!options.NoSpinner || !options.Silence) && !sf {
options.SpinnerObject.Stop()
}
}
}
} else {
printing.DalLog("ERROR", "Input file path", options)
printing.DalLog("ERROR", "e.g dalfox file ./targets.txt or ./rawdata.raw", options)
}
},
}
func init() {
rootCmd.AddCommand(fileCmd)
fileCmd.Flags().Bool("rawdata", false, "[FORMAT] Using req rawdata from Burp/ZAP")
fileCmd.Flags().Bool("har", false, "[FORMAT] Using HAR format")
fileCmd.Flags().Bool("http", false, "Using force http on rawdata mode")
fileCmd.Flags().Bool("multicast", false, "Parallel scanning N*Host mode (show only poc code)")
fileCmd.Flags().Bool("mass", false, "Parallel scanning N*Host mode (show only poc code)")
fileCmd.Flags().Bool("silence-force", false, "Only print PoC (not print progress)")
fileCmd.Flags().Int("mass-worker", 10, "Parallel worker of --mass and --multicast option")
}