Skip to content

Commit 121870d

Browse files
committed
Refactor logging in scan process and enhance terminal width utilities
1 parent 0d060c0 commit 121870d

File tree

6 files changed

+133
-48
lines changed

6 files changed

+133
-48
lines changed

internal/printing/banner.go

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,21 @@ import "github.com/hahwul/dalfox/v2/pkg/model"
55
// Banner is DalFox banner function
66
func Banner(options model.Options) {
77
DalLog("", `
8-
█████████
9-
████ ███
10-
████ ██ █ ███
11-
████ ██████ █ ███
12-
████ ███ █ █ █ ███
13-
██████ █ ███
14-
█████ ██ ██ █
15-
██████ ████ ██████
16-
████ ██ ████████ ██
17-
██████ █ ███
18-
███████████
19-
20-
█████ ██ █ █████ ████ █ █
21-
█ █ ██ █ █ █ █ █ █
22-
█ █ █ █ █ █████ █ █ ██
23-
█ █ █ █ █ █ █ █ █ █
24-
█████ █ ██ ████ █ ████ ██ ██
25-
`, options)
26-
DalLog("", "Powerful open-source XSS scanner and utility focused on automation.", options)
8+
░█▒
9+
████ ▓
10+
▓█████ ▓██▓
11+
████████████ ░
12+
░███████████▓ ▓░
13+
░████████████████ ▒██░
14+
▓██████████▒███████ ░█████▓░
15+
██████████████░ ████ █▓
16+
░█████▓ ░████▒ ░ Dalfox `+VERSION+`
17+
█████ ▓██░
18+
████ ▓██ Powerful open-source XSS scanner
19+
███▓ ▓███████▓▒▓█░ and utility focused on automation.
20+
███▒ █████
21+
▓███ ██████
22+
████ ██████▒
23+
░████ ████████▒
24+
`, options)
2725
}

internal/printing/logger.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strconv"
77
"sync"
88

9+
"github.com/hahwul/dalfox/v2/internal/utils"
910
"github.com/hahwul/dalfox/v2/pkg/model"
1011
)
1112

@@ -25,11 +26,6 @@ func boolToColorStr(b bool, options model.Options) string {
2526

2627
// Summary is printing options
2728
func Summary(options model.Options, target string) {
28-
bavState := false
29-
if options.UseBAV {
30-
bavState = true
31-
}
32-
3329
if !options.Silence {
3430
miningWord := "Gf-Patterns"
3531
if options.MiningWordlist != "" {
@@ -48,23 +44,27 @@ func Summary(options model.Options, target string) {
4844
blindURLStr = options.AuroraObject.BrightBlue(options.BlindURL).String()
4945
}
5046

51-
fmt.Fprintf(os.Stderr, "\n 🎯 Target %s\n", targetStr)
47+
fmt.Fprintf(os.Stderr, " 🎯 Target %s\n", targetStr)
5248
if target == "REST API Mode" {
5349
fmt.Fprintf(os.Stderr, " 🧲 Listen Address %s\n", listenAddrStr)
5450
}
5551
fmt.Fprintf(os.Stderr, " 🏁 Method %s\n", methodStr)
56-
fmt.Fprintf(os.Stderr, " 🖥 Worker %d\n", options.Concurrence)
57-
fmt.Fprintf(os.Stderr, " 🔦 BAV %s\n", boolToColorStr(bavState, options))
58-
fmt.Fprintf(os.Stderr, " ⛏ Mining %s (%s)\n", boolToColorStr(options.Mining, options), miningWord)
59-
fmt.Fprintf(os.Stderr, " 🔬 Mining-DOM %s (mining from DOM)\n", boolToColorStr(options.FindingDOM, options))
52+
fmt.Fprintf(os.Stderr, " 🖥 Performance %d worker / %d cpu\n", options.Concurrence, options.MaxCPU)
53+
fmt.Fprintf(os.Stderr, " ⛏ Mining %s (%s%s)\n", boolToColorStr(options.Mining, options), miningWord,
54+
func() string {
55+
if options.FindingDOM {
56+
return ", DOM Mining Enabled"
57+
}
58+
return ""
59+
}())
6060
if options.BlindURL != "" {
6161
fmt.Fprintf(os.Stderr, " 🛰 Blind XSS Callback %s\n", blindURLStr)
6262
}
6363
fmt.Fprintf(os.Stderr, " ⏱ Timeout %d\n", options.Timeout)
6464
fmt.Fprintf(os.Stderr, " 📤 FollowRedirect %s\n", boolToColorStr(options.FollowRedirect, options))
65-
fmt.Fprintf(os.Stderr, " 🕰 Started at %s\n", options.StartTime.String())
66-
//fmt.Fprintf(os.Stderr, "\n")
67-
fmt.Fprintf(os.Stderr, "\n >>>>>>>>>>>>>>>>>>>>>>>>>\n")
65+
fmt.Fprintf(os.Stderr, " 🕰 Started at %s\n", options.StartTime.Format("2006-01-02 15:04:05"))
66+
fmt.Fprintf(os.Stderr, "\n")
67+
DalLog("SYSTEM-M", utils.GenerateTerminalWidthLine("-"), options)
6868
}
6969
}
7070

internal/printing/scan.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,12 @@ package printing
33
import (
44
"strconv"
55

6+
"github.com/hahwul/dalfox/v2/internal/utils"
67
"github.com/hahwul/dalfox/v2/pkg/model"
7-
"golang.org/x/term"
88
)
99

1010
// ScanSummary prints the summary of the scan.
1111
func ScanSummary(scanResult model.Result, options model.Options) {
12-
if term.IsTerminal(0) {
13-
width, _, err := term.GetSize(0)
14-
if err == nil {
15-
var dash string
16-
for i := 0; i < width-5; i++ {
17-
dash = dash + "-"
18-
}
19-
DalLog("SYSTEM-M", dash, options)
20-
}
21-
}
12+
DalLog("SYSTEM-M", utils.GenerateTerminalWidthLine("-"), options)
2213
DalLog("SYSTEM-M", "[duration: "+scanResult.Duration.String()+"][issues: "+strconv.Itoa(len(scanResult.PoCs))+"] Finish Scan!", options)
2314
}

internal/utils/utils.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package utils
22

33
import (
4+
"os"
45
"strings"
56

7+
"golang.org/x/term"
8+
69
"github.com/hahwul/dalfox/v2/pkg/model"
710
)
811

@@ -60,3 +63,22 @@ func IsAllowType(contentType string) bool {
6063
}
6164
return true
6265
}
66+
67+
// GenerateTerminalWidthLine generates a string that fills the terminal width with the specified character
68+
func GenerateTerminalWidthLine(char string) string {
69+
width := GetTerminalWidth() - 5
70+
return strings.Repeat(char, width)
71+
}
72+
73+
// GetTerminalWidth returns the width of the terminal
74+
func GetTerminalWidth() int {
75+
width := 80 // default width
76+
if term.IsTerminal(int(os.Stdout.Fd())) {
77+
termWidth, _, err := term.GetSize(int(os.Stdout.Fd()))
78+
if err == nil && termWidth > 0 {
79+
width = termWidth
80+
}
81+
}
82+
83+
return width
84+
}

internal/utils/utils_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,77 @@ func Test_IsAllowType(t *testing.T) {
248248
})
249249
}
250250
}
251+
252+
func TestGenerateTerminalWidthLine(t *testing.T) {
253+
type args struct {
254+
char string
255+
}
256+
tests := []struct {
257+
name string
258+
args args
259+
want string
260+
}{
261+
{
262+
name: "Generate line with dash",
263+
args: args{
264+
char: "-",
265+
},
266+
},
267+
{
268+
name: "Generate line with equal",
269+
args: args{
270+
char: "=",
271+
},
272+
},
273+
{
274+
name: "Generate line with star",
275+
args: args{
276+
char: "*",
277+
},
278+
},
279+
}
280+
for _, tt := range tests {
281+
t.Run(tt.name, func(t *testing.T) {
282+
result := GenerateTerminalWidthLine(tt.args.char)
283+
width := GetTerminalWidth() - 5
284+
285+
// Check if the length is correct
286+
if len(result) != width {
287+
t.Errorf("GenerateTerminalWidthLine() length = %v, want %v", len(result), width)
288+
}
289+
290+
// Check if all characters are the expected character
291+
for i, r := range result {
292+
if string(r) != tt.args.char {
293+
t.Errorf("GenerateTerminalWidthLine() character at position %d = %v, want %v", i, string(r), tt.args.char)
294+
}
295+
}
296+
})
297+
}
298+
}
299+
300+
func TestGetTerminalWidth(t *testing.T) {
301+
tests := []struct {
302+
name string
303+
}{
304+
{
305+
name: "Get terminal width",
306+
},
307+
}
308+
for _, tt := range tests {
309+
t.Run(tt.name, func(t *testing.T) {
310+
got := GetTerminalWidth()
311+
312+
// Terminal width should be at least the default value (80) or greater
313+
if got < 1 {
314+
t.Errorf("GetTerminalWidth() = %v, should be > 0", got)
315+
}
316+
317+
// The function should always return a reasonable terminal width
318+
// Most terminals are at least 80 columns wide
319+
if got < 10 || got > 1000 {
320+
t.Errorf("GetTerminalWidth() = %v, value outside reasonable range", got)
321+
}
322+
})
323+
}
324+
}

pkg/scanning/scan.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func Scan(target string, options model.Options, sid string) (model.Result, error
6060

6161
parsedURL, err := url.Parse(target)
6262
if err != nil {
63-
printing.DalLog("SYSTEM", "Unable to parse URL: "+target+". Please ensure it is a valid URL.", options)
63+
printing.DalLog("SYSTEM", "Unable to parse URL: "+target+". Please ensure it is a valid URL.", options)
6464
return scanResult, err
6565
}
6666
treq := optimization.GenerateNewRequest(target, "", options)
@@ -70,8 +70,8 @@ printing.DalLog("SYSTEM", "Unable to parse URL: "+target+". Please ensure it is
7070
client := createHTTPClient(options)
7171
tres, err := client.Do(treq)
7272
if err != nil {
73-
msg := fmt.Sprintf("Request to %s failed: %v", target, err)
74-
printing.DalLog("ERROR", msg, options)
73+
msg := fmt.Sprintf("Request to %s failed: %v", target, err)
74+
printing.DalLog("ERROR", msg, options)
7575
return scanResult, err
7676
}
7777
if options.IgnoreReturn != "" {
@@ -206,7 +206,7 @@ func generatePayloads(target string, options model.Options, policy map[string]st
206206
if (options.SkipDiscovery || utils.IsAllowType(policy["Content-Type"])) && options.CustomPayloadFile != "" {
207207
ff, err := voltFile.ReadLinesOrLiteral(options.CustomPayloadFile)
208208
if err != nil {
209-
printing.DalLog("SYSTEM", "Failed to load custom XSS payload file", options)
209+
printing.DalLog("SYSTEM", "Failed to load custom XSS payload file", options)
210210
} else {
211211
for _, customPayload := range ff {
212212
if customPayload != "" {
@@ -466,7 +466,7 @@ printing.DalLog("SYSTEM", "Failed to load custom XSS payload file", options)
466466
}
467467
}
468468
} else {
469-
printing.DalLog("SYSTEM", "Failed to load remote payloads from "+endpoint, options)
469+
printing.DalLog("SYSTEM", "Failed to load remote payloads from "+endpoint, options)
470470
}
471471
}
472472
}

0 commit comments

Comments
 (0)