Skip to content

Commit

Permalink
Merge pull request #600 from hahwul/code-refactoring
Browse files Browse the repository at this point in the history
♻️ Code Refactoring
  • Loading branch information
hahwul authored Dec 6, 2024
2 parents 30cdcd4 + f114da4 commit 3311bb9
Show file tree
Hide file tree
Showing 27 changed files with 1,162 additions and 1,610 deletions.
384 changes: 155 additions & 229 deletions cmd/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,243 +23,169 @@ import (
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")
Run: runFileCmd,
}

} 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 := os.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()
}
}
func runFileCmd(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 {
printFileErrorAndUsage()
return
}
printing.Summary(options, args[0])
options.Method = tMethod
mutex := &sync.Mutex{}
options.Mutex = mutex
if len(args) >= 1 {
rawdata, _ := cmd.Flags().GetBool("rawdata")
har, _ := cmd.Flags().GetBool("har")
if rawdata {
runRawDataMode(args[0], cmd)
} else if har {
runHarMode(args[0], cmd, sf)
} else {
runFileMode(args[0], cmd, sf)
}
} else {
printFileErrorAndUsage()
}
}

func runRawDataMode(filePath string, cmd *cobra.Command) {
printing.DalLog("SYSTEM", "Using file mode(rawdata)", options)
ff, err := voltFile.ReadLinesOrLiteral(filePath)
if err != nil {
printing.DalLog("ERROR", "Failed to read file: "+err.Error(), options)
return
}
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("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
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")
}

ff, err := voltFile.ReadLinesOrLiteral(args[0])
_ = err
targets = append(targets, ff...)

// 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()
}
func runHarMode(filePath string, cmd *cobra.Command, sf bool) {
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 := os.ReadFile(filePath)
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
}
} else {
printing.DalLog("ERROR", "Input file path", options)
printing.DalLog("ERROR", "e.g dalfox file ./targets.txt or ./rawdata.raw", options)
if entry.Request.PostData.Text != "" {
options.Data = entry.Request.PostData.Text
}
options.Method = entry.Request.Method
_, _ = scanning.Scan(turl, options, strconv.Itoa(i))
updateSpinner(options, sf, i, len(harObject.Log.Entries))
}
},
if options.Format == "json" {
printing.DalLog("PRINT", "{}]", options)
}
if (!options.NoSpinner || !options.Silence) && !sf {
options.SpinnerObject.Stop()
}
}
}

func runFileMode(filePath string, cmd *cobra.Command, sf bool) {
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(filePath)
if err != nil {
printing.DalLog("ERROR", "Failed to read file: "+err.Error(), options)
return
}
targets := voltUtils.UniqueStringSlice(ff)
printing.DalLog("SYSTEM", "Loaded "+strconv.Itoa(len(targets))+" target urls", options)
multi, _ := cmd.Flags().GetBool("multicast")
mass, _ := cmd.Flags().GetBool("mass")
if multi || mass {
runMulticastMode(targets, cmd, sf)
} else {
runSingleMode(targets, sf)
}
}

func updateSpinner(options model.Options, sf bool, current, total int) {
if (!options.NoSpinner || !options.Silence) && !sf {
options.Mutex.Lock()
options.NowURL++
percent := fmt.Sprintf("%0.2f%%", float64(options.NowURL)/float64(total)*100)
options.SpinnerObject.Suffix = " [" + strconv.Itoa(options.NowURL) + "/" + strconv.Itoa(total) + " Tasks][" + percent + "] Parallel scanning from file"
options.Mutex.Unlock()
}
}

func printFileErrorAndUsage() {
printing.DalLog("ERROR", "Input target file", options)
printing.DalLog("ERROR", "e.g dalfox file ./targets.txt or ./rawdata.raw", options)
}

func init() {
Expand Down
Loading

0 comments on commit 3311bb9

Please sign in to comment.