From 1164b12afc14429973c98f7378f81259667c8f6e Mon Sep 17 00:00:00 2001 From: Ryze Date: Tue, 2 Apr 2024 13:52:59 +0800 Subject: [PATCH] Fix file saving bug & add html output --- module/finger/output.go | 130 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 117 insertions(+), 13 deletions(-) diff --git a/module/finger/output.go b/module/finger/output.go index b1c75a4..82907d1 100644 --- a/module/finger/output.go +++ b/module/finger/output.go @@ -3,9 +3,11 @@ package finger import ( "encoding/json" "fmt" + "html/template" "os" + "path/filepath" + "sort" "strconv" - "strings" "github.com/360EntSecGroup-Skylar/excelize" ) @@ -48,19 +50,121 @@ func outxlsx(filename string, msg []Outrestul) { } func outfile(filename string, allresult []Outrestul) { - file := strings.Split(filename, ".") - if len(file) == 2 { - if file[1] == "json" { - buf, err := json.MarshalIndent(allresult, "", " ") - if err != nil { - fmt.Println(err.Error()) - return - } - outjson(filename, buf) - } - if file[1] == "xlsx" { - outxlsx(filename, allresult) + //获取后缀名 .json .xlsx .html + fileExt := filepath.Ext(filename) + if fileExt == ".json" { + buf, err := json.MarshalIndent(allresult, "", " ") + if err != nil { + fmt.Println(err.Error()) + return } + outjson(filename, buf) + } + if fileExt == ".xlsx" { + outxlsx(filename, allresult) + } + if fileExt == ".html" { + outhtml(filename, allresult) + } +} + +// 排序规则,将重点资产放到前面,方便在html中查看 +type SortByCms []Outrestul + +func (a SortByCms) Len() int { return len(a) } +func (a SortByCms) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a SortByCms) Less(i, j int) bool { return a[i].Cms != "" && a[j].Cms == "" } + +func outhtml(filename string, msg []Outrestul) { + // 创建HTML文件 + file, err := os.Create(filename) + if err != nil { + fmt.Println(err) } + defer file.Close() + sort.Sort(SortByCms(msg)) + tmpl := ` + + + + + + Ehole + + + +
+

Ehole 资产

+ + + + + + + + + + + {{range .}} + + + + + + + + + + {{end}} +
URLCMSTitleServerSCLen
{{.Url}}{{.Cms}}{{.Title}}{{.Server}}{{.Statuscode}}{{.Length}}
+
+ + + ` + + // 解析HTML模板 + t := template.Must(template.New("html").Parse(tmpl)) + // 将数据写入HTML文件 + err = t.Execute(file, msg) + if err != nil { + fmt.Println(err) + } }