dt_automate/wps/csv_safety.go

243 lines
8.9 KiB
Go
Raw Normal View History

2025-03-05 18:55:51 +08:00
package wps
import (
"dt_automate/tool"
"encoding/json"
"fmt"
"log"
"os"
"time"
"github.com/Esword618/unioffice/document"
"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("开始将数据填入报告模板")
for _, csvstr := range people {
table := doc.Tables()[3]
row := table.AddRow()
row.Properties().SetHeight(22, wml.ST_HeightRuleExact)
// :=conn.
text_s(row.AddCell(), csvstr.Host) //主机IP
if csvstr.Risk == "None" {
text_s(row.AddCell(), "无") //危险程度
} else {
text_s(row.AddCell(), Translate(csvstr.Risk)) //危险程度
}
text_s(row.AddCell(), Translate(csvstr.Name)) //脆弱点
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)) //解决方案
}
}
// 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(11)
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")
}