package nessus import ( "dt_automate/tool" "encoding/json" "fmt" "log" "os" "time" "github.com/Esword618/unioffice/color" "github.com/Esword618/unioffice/document" "github.com/Esword618/unioffice/measurement" "github.com/Esword618/unioffice/schema/soo/wml" "github.com/gocarina/gocsv" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile" tmt "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tmt/v20180321" ) // A1,数据 func CSV_damo() { //读取docx模板 doc, err := document.Open("temp/lousao_temp.docx") if err != nil { log.Panic("打开文件失败", err) } // 读取CSV文件示例 file, err := os.OpenFile("temp/zww_to5b2r.csv", os.O_RDWR|os.O_CREATE, os.ModePerm) if err != nil { fmt.Println("打开文件失败:", err) return } log.Println("打开数据模板") defer file.Close() // var people []interface{} var people []*Person if err := gocsv.UnmarshalFile(file, &people); err != nil { fmt.Println("读取文件失败:", err) return } log.Println("开始将数据填入报告模板") // table := doc.Tables()[3] // var host string // var con int // var tables []document.Table // for _, csvstr := range people { // if csvstr.Host == host { // row := table.AddRow() // row.Properties().SetHeight(22, wml.ST_HeightRuleExact) // // text_s(row.AddCell(), csvstr.Host) //主机IP // text_s(row.AddCell(), Translate(csvstr.Name)) //风险项 // if csvstr.Risk == "None" { // text_s(row.AddCell(), "无") //危险程度 // } else { // text_s(row.AddCell(), Translate(csvstr.Risk)) //危险程度 // } // if csvstr.Risk_Factor == "None" { // text_s(row.AddCell(), "无") //危险因素 // } else { // text_s(row.AddCell(), Translate(csvstr.Risk_Factor)) //危险因素 // } // text_s(row.AddCell(), Translate(csvstr.Synopsisk)) //摘要 // text_s(row.AddCell(), Translate(csvstr.Description)) //描述 // if csvstr.Solution == "n/a" { // text_s(row.AddCell(), "无") //解决方案 // } else { // text_s(row.AddCell(), Translate(csvstr.Solution)) //解决方案 // } // } else { // con++ // doc.Tables()[con] = doc.Tables()[3] // table = doc.Tables()[con] // row := table.AddRow() // row.Properties().SetHeight(22, wml.ST_HeightRuleExact) // // text_s(row.AddCell(), csvstr.Host) //主机IP // text_s(row.AddCell(), Translate(csvstr.Name)) //风险项 // if csvstr.Risk == "None" { // text_s(row.AddCell(), "无") //危险程度 // } else { // text_s(row.AddCell(), Translate(csvstr.Risk)) //危险程度 // } // if csvstr.Risk_Factor == "None" { // text_s(row.AddCell(), "无") //危险因素 // } else { // text_s(row.AddCell(), Translate(csvstr.Risk_Factor)) //危险因素 // } // text_s(row.AddCell(), Translate(csvstr.Synopsisk)) //摘要 // text_s(row.AddCell(), Translate(csvstr.Description)) //描述 // if csvstr.Solution == "n/a" { // text_s(row.AddCell(), "无") //解决方案 // } else { // text_s(row.AddCell(), Translate(csvstr.Solution)) //解决方案 // } // } // host = csvstr.Host // } //根据ip创建表格 // 定义一个 map 用于记录已经出现过的 IP 地址 var ip []string ipMap := make(map[string]bool) for _, csvstr := range people { // 检查当前 IP 是否已经在 map 中 if _, exists := ipMap[csvstr.Host]; !exists { // 如果不存在,则将其添加到数组和 map 中 ip = append(ip, csvstr.Host) ipMap[csvstr.Host] = true } } log.Println(ip) for _, ip := range ip { //创建一个段落添加IP: // 创建一个段落 paragraph := doc.AddParagraph() // 设置段落样式 paragraph.Properties().SetStyle("Normal") // 添加文本 run := paragraph.AddRun() run.AddText("IP:" + ip) // 创建表格 table := doc.AddTable() // 设置表格宽度 table.Properties().SetWidthPercent(100) // 设置表格边框 borderProps := table.Properties().Borders() borderProps.SetTop(wml.ST_BorderSingle, color.Black, measurement.Point) borderProps.SetBottom(wml.ST_BorderSingle, color.Black, measurement.Point) borderProps.SetLeft(wml.ST_BorderSingle, color.Black, measurement.Point) borderProps.SetRight(wml.ST_BorderSingle, color.Black, measurement.Point) // 添加表头 headerRow := table.AddRow() headerRow.Properties().SetHeight(22, wml.ST_HeightRuleExact) text_s(headerRow.AddCell(), "风险项") text_s(headerRow.AddCell(), "危险程度") text_s(headerRow.AddCell(), "危险因素") text_s(headerRow.AddCell(), "摘要") text_s(headerRow.AddCell(), "描述") text_s(headerRow.AddCell(), "解决方案") // 设置表头单元格的边框(包含内边框) for _, cell := range headerRow.Cells() { borderProps := cell.Properties().Borders() borderProps.SetTop(wml.ST_BorderSingle, color.Black, measurement.Point) borderProps.SetBottom(wml.ST_BorderSingle, color.Black, measurement.Point) borderProps.SetLeft(wml.ST_BorderSingle, color.Black, measurement.Point) borderProps.SetRight(wml.ST_BorderSingle, color.Black, measurement.Point) } // 添加数据行 for _, csvstr := range people { if csvstr.Host == ip { row := table.AddRow() row.Properties().SetHeight(22, wml.ST_HeightRuleExact) // text_s(row.AddCell(), csvstr.Host) //主机IP text_s(row.AddCell(), csvstr.Name) //风险项 if csvstr.Risk == "None" { text_s(row.AddCell(), "无") //危险程度 } else { text_s(row.AddCell(), csvstr.Risk) //危险程度 } if csvstr.Risk_Factor == "None" { text_s(row.AddCell(), "无") //危险因素 } else { text_s(row.AddCell(), csvstr.Risk_Factor) //危险因素 } text_s(row.AddCell(), csvstr.Synopsisk) //摘要 text_s(row.AddCell(), csvstr.Description) //描述 if csvstr.Solution == "n/a" { text_s(row.AddCell(), "无") //解决方案 } else { text_s(row.AddCell(), csvstr.Solution) //解决方案 } // 设置单元格的边框(包含内边框) for _, cell := range row.Cells() { borderProps := cell.Properties().Borders() borderProps.SetTop(wml.ST_BorderSingle, color.Black, measurement.Point) borderProps.SetBottom(wml.ST_BorderSingle, color.Black, measurement.Point) borderProps.SetLeft(wml.ST_BorderSingle, color.Black, measurement.Point) borderProps.SetRight(wml.ST_BorderSingle, color.Black, measurement.Point) } } } //添加一个空行 paragraph = doc.AddParagraph() paragraph.Properties().SetStyle("Normal") run = paragraph.AddRun() run.AddBreak() } // for _, csvstr := range people { // if csvstr.Host == host { // row := table.AddRow() // row.Properties().SetHeight(22, wml.ST_HeightRuleExact) // // text_s(row.AddCell(), csvstr.Host) //主机IP // text_s(row.AddCell(), csvstr.Name) //风险项 // if csvstr.Risk == "None" { // text_s(row.AddCell(), "无") //危险程度 // } else { // text_s(row.AddCell(), csvstr.Risk) //危险程度 // } // if csvstr.Risk_Factor == "None" { // text_s(row.AddCell(), "无") //危险因素 // } else { // text_s(row.AddCell(), csvstr.Risk_Factor) //危险因素 // } // text_s(row.AddCell(), csvstr.Synopsisk) //摘要 // text_s(row.AddCell(), csvstr.Description) //描述 // if csvstr.Solution == "n/a" { // text_s(row.AddCell(), "无") //解决方案 // } else { // text_s(row.AddCell(), csvstr.Solution) //解决方案 // } // } else { // con++ // tables[con] = doc.Tables()[3] // table = tables[con] // row := table.AddRow() // row.Properties().SetHeight(22, wml.ST_HeightRuleExact) // // text_s(row.AddCell(), csvstr.Host) //主机IP // text_s(row.AddCell(), csvstr.Name) //风险项 // if csvstr.Risk == "None" { // text_s(row.AddCell(), "无") //危险程度 // } else { // text_s(row.AddCell(), csvstr.Risk) //危险程度 // } // if csvstr.Risk_Factor == "None" { // text_s(row.AddCell(), "无") //危险因素 // } else { // text_s(row.AddCell(), csvstr.Risk_Factor) //危险因素 // } // text_s(row.AddCell(), csvstr.Synopsisk) //摘要 // text_s(row.AddCell(), csvstr.Description) //描述 // if csvstr.Solution == "n/a" { // text_s(row.AddCell(), "无") //解决方案 // } else { // text_s(row.AddCell(), csvstr.Solution) //解决方案 // } // } // host = csvstr.Host // } // cvs_word() //写入CSV // file, err := os.OpenFile("output.csv", os.O_RDWR|os.O_CREATE, os.ModePerm) // if err != nil { // fmt.Println("创建文件失败:", err) // return // } // defer file.Close() // people := []*Person{ // {Name: "Alice", Age: 21, Gender: "F"}, // {Name: "Bob", Age: 25, Gender: "M"}, // } // if err := gocsv.MarshalFile(&people, file); err != nil { // fmt.Println("写入文件失败:", err) // return // } doc.SaveToFile("大同信创云平台漏扫报告" + time.Unix(tool.Timestamp("second"), 0).Format("20060102") + ".docx") } // csv表结构体对应字段 type Person struct { Plugin_ID string `csv:"Plugin ID"` CVE string `csv:"CVE"` CVSS string `csv:"CVSS"` Risk string `csv:"Risk"` Host string `csv:"Host"` Protocol string `csv:"Protocol"` Port string `csv:"Port"` Name string `csv:"Name"` Synopsisk string `csv:"Synopsis"` Description string `csv:"Description"` Solution string `csv:"Solution"` See_Also string `csv:"See Also"` Plugin_Output string `csv:"Plugin Output"` STIG_Severity string `csv:"STIG Severity"` CVSS_Base_Score string `csv:"CVSS v3.0 Base Score"` CVSS_Temporal_Score string `csv:"CVSS v3.0 Temporal Score"` Risk_Factor string `csv:"Risk Factor"` BID string `csv:"BID"` XREF string `csv:"XREF"` MSKB string `csv:"MSKB"` Plugin_Publication_Date CustomDate `csv:"Plugin Publication Date,layout=2006/01/02"` Plugin_Modification_Date CustomDate `csv:"Plugin Modification Date,layout=2006/01/02"` Metasploit string `csv:"Metasploit"` Core_Impact string `csv:"Core Impact"` CANVAS string `csv:"CANVAS"` } // 定义一个自定义日期类型 type CustomDate struct { time.Time } // 实现TypeUnmarshaller接口,用于将CSV字符串转换为time.Time func (cd *CustomDate) UnmarshalCSV(csv string) error { date, err := time.Parse("2006/01/02", csv) if err != nil { return fmt.Errorf("invalid date format: %s", csv) } cd.Time = date return nil } // 批量填入表格内容方法 func text_s(cell document.Cell, ss string) { // 设置单元格垂直居中对齐 // cell.Properties().SetVerticalAlignment(wml.ST_VerticalJcCenter) //在单元格中添加段落 p := cell.AddParagraph().AddRun() p.Properties().SetFontFamily("微软雅黑") p.Properties().SetSize(12) // p.Properties().SetBold(true) p.AddText(ss) } func Translate(text string) string { // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密 // 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305 // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取 credential := common.NewCredential( "AKIDgN8k9LNCpGUVJtFNI0UkmbDblhQvCLL8", "d2GI7lFXcWGoNjvtd9SXP4XmnaEgz5ye", ) // 实例化一个client选项,可选的,没有特殊需求可以跳过 cpf := profile.NewClientProfile() cpf.HttpProfile.Endpoint = "tmt.tencentcloudapi.com" // 实例化要请求产品的client对象,clientProfile是可选的 client, _ := tmt.NewClient(credential, "ap-beijing", cpf) // 实例化一个请求对象,每个接口都会对应一个request对象 request := tmt.NewTextTranslateRequest() request.SourceText = common.StringPtr(text) request.Source = common.StringPtr("en") request.Target = common.StringPtr("zh") request.ProjectId = common.Int64Ptr(0) // 返回的resp是一个TextTranslateResponse的实例,与请求对象对应 response, err := client.TextTranslate(request) if _, ok := err.(*errors.TencentCloudSDKError); ok { fmt.Printf("An API error has returned: %s", err) } if err != nil { panic(err) } // 解析 OutputJSON var outputJSON map[string]interface{} err = json.Unmarshal([]byte(response.ToJsonString()), &outputJSON) if err != nil { log.Fatalf("解析 OutputJSON 失败: %v", err) } return outputJSON["Response"].(map[string]interface{})["TargetText"].(string) } func cvs_word() { doc, err := document.Open("temp/lousao_temp.docx") if err != nil { log.Panic("打开文件失败", err) } // // defer doc.Close() // // 填充模板中的变量 // paragraphs := []document.Paragraph{} // for _, p := range doc.Paragraphs() { // paragraphs = append(paragraphs, p) // } // //此示例文档使用了不常见的结构化文档标记 // //文档模板中的除外。通常情况下,您只需迭代 // //文档的段落。 // for _, sdt := range doc.StructuredDocumentTags() { // for _, p := range sdt.Paragraphs() { // paragraphs = append(paragraphs, p) // } // } log.Println("开始生成漏扫报告模板") // for _, p := range paragraphs { // for _, r := range p.Runs() { // if strings.Contains(r.Text(), "{{DATE_A}}") { // Replacetext(r, "{{DATE_A}}", time.Unix(tool.Timestamp("second"), 0).Format("2006年01月02日")) // } else if strings.Contains(r.Text(), "{{HLW_FW_1}}") { //插入互联网防火墙图片 // Replaceimg(r, doc, "img/hw_fw1.png") // } else if strings.Contains(r.Text(), "{{HLW_AFC2000_1}}") { //插入互联网流量清洗图片 // Replaceimg(r, doc, "img/afc2000_1.png") // } else if strings.Contains(r.Text(), "{{HLW_AFC2000_2}}") { //插入互联网流量清洗图片 // Replaceimg(r, doc, "img/afc2000_2.png") // } else if strings.Contains(r.Text(), "{{HLW_TSGZ}}") { //插入互联网态势感知图片 // Replaceimg(r, doc, "img/hw_tsgz.png") // } else if strings.Contains(r.Text(), "{{HLW_BLJ}}") { //插入互联网堡垒机图片 // Replaceimg(r, doc, "img/hw_baolj.png") // } else if strings.Contains(r.Text(), "{{ZWW_FW_1}}") { //插入政务网防火墙图片 // Replaceimg(r, doc, "img/zww_fw1.png") // } else if strings.Contains(r.Text(), "{{ZWW_TSGZ}}") { //插入政务网态势感知图片 // Replaceimg(r, doc, "img/zww_tsgz.png") // } else if strings.Contains(r.Text(), "{{ZWW_BLJ}}") { //插入政务网堡垒机图片 // Replaceimg(r, doc, "img/zww_baolj.png") // } else { // // log.Println("") // } // } // } // 遍历文档中的表格,替换{{bianliang}} 中的内容 table := doc.Tables()[3] row := table.AddRow() row.Properties().SetHeight(22, wml.ST_HeightRuleExact) text_s(row.AddCell(), "192.168.1.1") text_s(row.AddCell(), "高危") row = table.AddRow() row.Properties().SetHeight(22, wml.ST_HeightRuleExact) text_s(row.AddCell(), "192.168.1.2") text_s(row.AddCell(), "中危") doc.SaveToFile("大同信创云平台漏扫报告" + time.Unix(tool.Timestamp("second"), 0).Format("20060102") + ".docx") }