perf: 优化WebScan.go的代码,添加注释,规范输出

This commit is contained in:
ZacharyZcR 2024-12-19 14:49:52 +08:00
parent 6d499dae10
commit 9296ad0846

View File

@ -18,15 +18,22 @@ var Pocs embed.FS
var once sync.Once var once sync.Once
var AllPocs []*lib.Poc var AllPocs []*lib.Poc
// WebScan 执行Web漏洞扫描
func WebScan(info *Config.HostInfo) { func WebScan(info *Config.HostInfo) {
// 确保POC只初始化一次
once.Do(initpoc) once.Do(initpoc)
var pocinfo = Common.Pocinfo
buf := strings.Split(info.Url, "/")
pocinfo.Target = strings.Join(buf[:3], "/")
// 构建扫描信息
var pocinfo = Common.Pocinfo
urlParts := strings.Split(info.Url, "/")
pocinfo.Target = strings.Join(urlParts[:3], "/")
// 执行扫描
if pocinfo.PocName != "" { if pocinfo.PocName != "" {
// 指定POC扫描
Execute(pocinfo) Execute(pocinfo)
} else { } else {
// 根据指纹信息选择POC扫描
for _, infostr := range info.Infostr { for _, infostr := range info.Infostr {
pocinfo.PocName = lib.CheckInfoPoc(infostr) pocinfo.PocName = lib.CheckInfoPoc(infostr)
Execute(pocinfo) Execute(pocinfo)
@ -34,69 +41,80 @@ func WebScan(info *Config.HostInfo) {
} }
} }
// Execute 执行具体的POC检测
func Execute(PocInfo Common.PocInfo) { func Execute(PocInfo Common.PocInfo) {
// 创建基础HTTP请求
req, err := http.NewRequest("GET", PocInfo.Target, nil) req, err := http.NewRequest("GET", PocInfo.Target, nil)
if err != nil { if err != nil {
errlog := fmt.Sprintf("[-] webpocinit %v %v", PocInfo.Target, err) Common.LogError(fmt.Sprintf("初始化请求失败 %v: %v", PocInfo.Target, err))
Common.LogError(errlog)
return return
} }
// 设置请求头
req.Header.Set("User-agent", Common.UserAgent) req.Header.Set("User-agent", Common.UserAgent)
req.Header.Set("Accept", Common.Accept) req.Header.Set("Accept", Common.Accept)
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9") req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9")
if Common.Cookie != "" { if Common.Cookie != "" {
req.Header.Set("Cookie", Common.Cookie) req.Header.Set("Cookie", Common.Cookie)
} }
// 根据名称筛选POC并执行
pocs := filterPoc(PocInfo.PocName) pocs := filterPoc(PocInfo.PocName)
lib.CheckMultiPoc(req, pocs, Common.PocNum) lib.CheckMultiPoc(req, pocs, Common.PocNum)
} }
// initpoc 初始化POC加载
func initpoc() { func initpoc() {
if Common.PocPath == "" { if Common.PocPath == "" {
// 从嵌入的POC目录加载
entries, err := Pocs.ReadDir("pocs") entries, err := Pocs.ReadDir("pocs")
if err != nil { if err != nil {
fmt.Printf("[-] init poc error: %v", err) Common.LogError(fmt.Sprintf("加载内置POC失败: %v", err))
return return
} }
for _, one := range entries {
path := one.Name() // 加载YAML格式的POC文件
if strings.HasSuffix(path, ".yaml") || strings.HasSuffix(path, ".yml") { for _, entry := range entries {
if poc, _ := lib.LoadPoc(path, Pocs); poc != nil { filename := entry.Name()
if strings.HasSuffix(filename, ".yaml") || strings.HasSuffix(filename, ".yml") {
if poc, err := lib.LoadPoc(filename, Pocs); err == nil && poc != nil {
AllPocs = append(AllPocs, poc) AllPocs = append(AllPocs, poc)
} }
} }
} }
} else { } else {
fmt.Println("[+] load poc from " + Common.PocPath) // 从指定目录加载POC
err := filepath.Walk(Common.PocPath, Common.LogSuccess(fmt.Sprintf("[*] 从目录加载POC: %s", Common.PocPath))
func(path string, info os.FileInfo, err error) error { err := filepath.Walk(Common.PocPath, func(path string, info os.FileInfo, err error) error {
if err != nil || info == nil { if err != nil || info == nil {
return err return err
}
if !info.IsDir() && (strings.HasSuffix(path, ".yaml") || strings.HasSuffix(path, ".yml")) {
if poc, err := lib.LoadPocbyPath(path); err == nil && poc != nil {
AllPocs = append(AllPocs, poc)
} }
if !info.IsDir() { }
if strings.HasSuffix(path, ".yaml") || strings.HasSuffix(path, ".yml") { return nil
poc, _ := lib.LoadPocbyPath(path) })
if poc != nil {
AllPocs = append(AllPocs, poc)
}
}
}
return nil
})
if err != nil { if err != nil {
fmt.Printf("[-] init poc error: %v", err) Common.LogError(fmt.Sprintf("[-] 加载外部POC失败: %v", err))
} }
} }
} }
func filterPoc(pocname string) (pocs []*lib.Poc) { // filterPoc 根据POC名称筛选
func filterPoc(pocname string) []*lib.Poc {
if pocname == "" { if pocname == "" {
return AllPocs return AllPocs
} }
var matchedPocs []*lib.Poc
for _, poc := range AllPocs { for _, poc := range AllPocs {
if strings.Contains(poc.Name, pocname) { if strings.Contains(poc.Name, pocname) {
pocs = append(pocs, poc) matchedPocs = append(matchedPocs, poc)
} }
} }
return return matchedPocs
} }