Skip to content

Commit 8a51253

Browse files
committed
cmd 控制台输出美化、report报告美化及体验提升
1 parent 9c809af commit 8a51253

File tree

6 files changed

+99
-23
lines changed

6 files changed

+99
-23
lines changed

cmd/afrog/main.go

+12-15
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package main
22

33
import (
44
"fmt"
5-
"github.com/zan8in/afrog/pkg/utils"
65
"os"
6+
"sync"
77

88
"github.com/urfave/cli/v2"
99
"github.com/zan8in/afrog/internal/runner"
@@ -13,10 +13,13 @@ import (
1313
"github.com/zan8in/afrog/pkg/log"
1414
"github.com/zan8in/afrog/pkg/poc"
1515
"github.com/zan8in/afrog/pkg/upgrade"
16+
"github.com/zan8in/afrog/pkg/utils"
1617
)
1718

1819
var options = &config.Options{}
1920
var htemplate = &html.HtmlTemplate{}
21+
var lock sync.Mutex
22+
var number = 0
2023

2124
func main() {
2225
app := cli.NewApp()
@@ -37,7 +40,7 @@ func main() {
3740
upgrade := upgrade.New()
3841
upgrade.UpgradeAfrogPocs()
3942

40-
showBanner(upgrade.LastestAfrogVersion)
43+
runner.ShowBanner2(upgrade.LastestAfrogVersion)
4144

4245
fmt.Println("PATH:")
4346
fmt.Println(" " + options.Config.GetConfigPath())
@@ -51,25 +54,29 @@ func main() {
5154
err := runner.New(options, func(result interface{}) {
5255
r := result.(*core.Result)
5356

54-
options.OptLock.Lock()
55-
defer options.OptLock.Unlock()
57+
lock.Lock()
58+
defer lock.Unlock()
5659

5760
if !options.Silent {
5861
options.CurrentCount++
5962
}
6063

6164
if r.IsVul {
62-
r.PrintColorResultInfoConsole()
65+
number++
6366

6467
if len(r.Output) > 0 {
6568
htemplate.Result = r
69+
htemplate.Number = utils.GetNumberText(number)
6670
htemplate.Append()
6771
}
72+
73+
r.PrintColorResultInfoConsole(utils.GetNumberText(number))
6874
}
6975

7076
if !options.Silent {
7177
fmt.Printf("\r%d/%d | %d%% ", options.CurrentCount, options.Count, options.CurrentCount*100/options.Count)
7278
}
79+
7380
})
7481
if err != nil {
7582
return err
@@ -84,13 +91,3 @@ func main() {
8491
fmt.Println(log.LogColor.High("Failed to start afrog,", err.Error()))
8592
}
8693
}
87-
88-
func showBanner(afrogLatestversion string) {
89-
title := "NAME:\n " + log.LogColor.Banner(runner.ShowBanner()) + " - v" + config.Version
90-
old := ""
91-
if utils.Compare(afrogLatestversion, ">", config.Version) {
92-
old = log.LogColor.High(" (outdated)")
93-
old += log.LogColor.Title(" --> https://github.com/zan8in/afrog/releases/tag/v" + afrogLatestversion)
94-
}
95-
fmt.Println(title + old + "\n")
96-
}

internal/runner/banner.go

+18
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
package runner
22

3+
import (
4+
"fmt"
5+
6+
"github.com/zan8in/afrog/pkg/config"
7+
"github.com/zan8in/afrog/pkg/log"
8+
"github.com/zan8in/afrog/pkg/utils"
9+
)
10+
311
func ShowBanner() string {
412
return "afrog"
513
}
614

715
func ShowUsage() string {
816
return "\nUSAGE:\n afrog -t example.com -o result.html\n afrog -T urls.txt -o result.html\n afrog -T urls.txt -s -o result.html\n afrog -t example.com -P ./pocs/poc-test.yaml -o result.html\n afrog -t example.com -P ./pocs/ -o result.html\n"
917
}
18+
19+
func ShowBanner2(afrogLatestversion string) {
20+
title := "NAME:\n " + log.LogColor.Banner(ShowBanner()) + " - v" + config.Version
21+
old := ""
22+
if utils.Compare(afrogLatestversion, ">", config.Version) {
23+
old = log.LogColor.High(" (outdated)")
24+
old += log.LogColor.Title(" --> https://github.com/zan8in/afrog/releases/tag/v" + afrogLatestversion)
25+
}
26+
fmt.Println(title + old + "\n")
27+
}

pkg/core/result.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ func (r *Result) PrintResultInfo() string {
8080
return "[" + utils.GetNowDateTime() + "] [" + r.PocInfo.Id + "] [" + r.PocInfo.Info.Severity + "] " + r.Target
8181
}
8282

83-
func (r *Result) PrintColorResultInfoConsole() {
84-
fmt.Printf("\r" + log.LogColor.Time(utils.GetNowDateTime()) + " " +
83+
func (r *Result) PrintColorResultInfoConsole(number string) {
84+
fmt.Printf("\r" + log.LogColor.Time(number+" "+utils.GetNowDateTime()) + " " +
8585
log.LogColor.Vulner(""+r.PocInfo.Id+"") + " " +
8686
log.LogColor.GetColor(r.PocInfo.Info.Severity, ""+
8787
strings.ToUpper(r.PocInfo.Info.Severity)+"") + " " + r.Target + "\r\n")

pkg/html/html.go

+31-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
type HtmlTemplate struct {
1616
Result *core.Result
1717
Filename string
18+
Number string
1819
}
1920

2021
const outputDirectory = "./reports"
@@ -48,10 +49,10 @@ func (ht *HtmlTemplate) Html() string {
4849
}
4950
title := fmt.Sprintf(`<table>
5051
<thead onclick="$(this).next('tbody').toggle()" style="background:#f5f5f5">
51-
<td class="vuln">%s</td>
52+
<td class="vuln">%s&nbsp;&nbsp;%s</td>
5253
<td class="security %s">%s</td>
5354
<td class="url">%s</td>
54-
</thead>`, htResult.PocInfo.Id, htResult.PocInfo.Info.Severity, strings.ToUpper(htResult.PocInfo.Info.Severity), htResult.Target)
55+
</thead>`, ht.Number, htResult.PocInfo.Id, htResult.PocInfo.Info.Severity, strings.ToUpper(htResult.PocInfo.Info.Severity), htResult.Target)
5556

5657
info := fmt.Sprintf("<b>name:</b> %s&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>author:</b> %s&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>security:</b> %s",
5758
htResult.PocInfo.Info.Name, htResult.PocInfo.Info.Author, htResult.PocInfo.Info.Severity,
@@ -74,7 +75,30 @@ func (ht *HtmlTemplate) Html() string {
7475

7576
body := ""
7677
for _, v := range htResult.AllPocResult {
77-
fullurl := fmt.Sprintf("%s://%s%s", v.ResultRequest.Url.Scheme, v.ResultRequest.Url.Host, v.ResultRequest.Url.Path)
78+
if !v.IsVul {
79+
continue
80+
}
81+
schema := ""
82+
host := ""
83+
path := ""
84+
query := ""
85+
frament := ""
86+
reqraw := []byte{}
87+
respraw := []byte{}
88+
if v.ResultRequest.Url != nil {
89+
schema = v.ResultRequest.Url.Scheme
90+
host = v.ResultRequest.Url.Host
91+
path = v.ResultRequest.Url.Path
92+
if len(v.ResultRequest.Url.Query) > 0 {
93+
query = "?" + v.ResultRequest.Url.Query
94+
}
95+
if len(v.ResultRequest.Url.Fragment) > 0 {
96+
frament = "#" + v.ResultRequest.Url.Fragment
97+
}
98+
reqraw = v.ResultRequest.GetRaw()
99+
respraw = v.ResultResponse.GetRaw()
100+
}
101+
fullurl := fmt.Sprintf("%s://%s%s%s%s", schema, host, path, query, frament)
78102
body += fmt.Sprintf(`<tr>
79103
<td colspan="3" style="background:#f8f8f8"><a href="%s" target="_blank">%s</a></td>
80104
</tr><tr>
@@ -91,7 +115,7 @@ func (ht *HtmlTemplate) Html() string {
91115
</div>
92116
</td>
93117
</tr>
94-
`, fullurl, fullurl, v.ResultRequest.GetRaw(), v.ResultResponse.GetRaw())
118+
`, fullurl, fullurl, reqraw, respraw)
95119
}
96120

97121
footer := "</tbody></table>"
@@ -102,8 +126,10 @@ func (ht *HtmlTemplate) Html() string {
102126
func (ht *HtmlTemplate) Append() {
103127
r := ht.Html()
104128
if len(r) > 0 {
105-
utils.BufferWriteAppend(ht.Filename, r)
129+
utils.AppendString(ht.Filename, r)
130+
// fmt.Println(err)
106131
}
132+
// fmt.Println(len(r))
107133
}
108134

109135
func header() string {

pkg/utils/file.go

+25-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"errors"
66
"fmt"
7+
"io"
78
"io/ioutil"
89
"log"
910
"os"
@@ -81,7 +82,7 @@ func WriteFile(filename string, data []byte) error {
8182
}
8283

8384
func BufferWriteAppend(filename string, param string) error {
84-
fileHandle, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
85+
fileHandle, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_APPEND|os.O_SYNC, 0660)
8586
if err != nil {
8687
return err
8788
}
@@ -96,3 +97,26 @@ func BufferWriteAppend(filename string, param string) error {
9697
// 将缓冲中的数据写入
9798
return buf.Flush()
9899
}
100+
101+
const (
102+
NEW_FILE_PERM = 0666
103+
)
104+
105+
// AppendString appends the contents of the string to filename.
106+
func AppendString(filename, content string) error {
107+
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, NEW_FILE_PERM)
108+
if err != nil {
109+
return err
110+
}
111+
data := []byte(content)
112+
n, err := f.Write(data)
113+
if err == nil && n < len(data) {
114+
err = io.ErrShortWrite
115+
// fmt.Println(err)
116+
}
117+
if err1 := f.Close(); err == nil {
118+
err = err1
119+
// fmt.Println(err)
120+
}
121+
return err
122+
}

pkg/utils/utils.go

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66
"net/url"
77
"os"
8+
"strconv"
89
"strings"
910
"time"
1011

@@ -151,3 +152,13 @@ func GetNowDateTime() string {
151152
now := time.Now()
152153
return now.Format("2006-01-02 15:04:05")
153154
}
155+
156+
func GetNumberText(number int) string {
157+
num := strconv.Itoa(number)
158+
if len(num) == 1 {
159+
num = "00" + num
160+
} else if len(num) == 2 {
161+
num = "0" + num
162+
}
163+
return num
164+
}

0 commit comments

Comments
 (0)