设为首页收藏本站

 找回密码
 立即注册

只需一步,快速开始

搜索
查看: 75|回复: 0

[实用软件] 利用Go开发一款调用各大杀毒软件的脚本

[复制链接]
累计签到:7 天
连续签到:1 天
灌水成绩
1
43
2034
主题
帖子
积分

等级头衔

ID : 1104

高级技术员

积分成就 测量币 : 2034
在线时间 : 0 小时
注册时间 : 2025-11-28
最后登录 : 2026-6-23

勋章
测量员UID勋章测量学徒
发表于 2024-3-29 11:06:13 | 显示全部楼层 |阅读模式 IP:广东东莞
因业务需要,领导让我收集市面上的杀毒软件命令行版本,目前已收集到Kaspersky、Microsoft_ Defender、Avast、Emsisoft、DrWeb、Sophos、Norton、ESET、ikarust、AdAware、clamav。接待大佬们补充。以下为客户端agent源码,小白水平,多多提建议。

  • package main

  • import (
  •         "bytes"
  •         "context"
  •         "crypto/sha256"
  •         "encoding/hex"
  •         "errors"
  •         "fmt"
  •         _ "github.com/go-sql-driver/mysql"
  •         "github.com/labstack/echo"
  •         "github.com/labstack/echo/middleware"
  •         "golang.org/x/text/encoding/simplifiedchinese"
  •         "io"
  •         "io/ioutil"
  •         "log"
  •         "net/http"
  •         "os"
  •         "os/exec"
  •         "path/filepath"
  •         "regexp"
  •         "strings"
  •         "time"
  • )

  • type avinfo struct {
  •         //DisplayIcon    string
  •         Filename       string
  •         Virus          string
  •         Antivirus_name string
  •         VirusClass     string
  •         Status         string
  •         UploadTime     string
  •         Hash256        string

  •         //UninstallString string
  • }

  • func main() {
  •         e := echo.New()
  •         e.Use(middleware.Logger())
  •         e.Use(middleware.Recover())
  •         e.POST("/upload", uploadFile)
  •         e.Logger.Fatal(e.Start(":8080"))

  •         //log.Printf("class:%s", virusClass)

  • }
  • func uploadFile(c echo.Context) error {
  •         // Get file from request
  •         file, err := c.FormFile("file")
  •         if err != nil {
  •                 return err
  •         }

  •         // Open the file
  •         src, err := file.Open()
  •         if err != nil {
  •                 return err
  •         }
  •         defer src.Close()

  •         // Destination file path
  •         dstPath := filepath.Join("C:\\Users\\admin\\Desktop\\test\\tmp", file.Filename)

  •         // Create destination file
  •         dst, err := os.Create(dstPath)
  •         if err != nil {
  •                 return err
  •         }
  •         defer dst.Close()

  •         // Copy the file content to the destination file
  •         if _, err = io.Copy(dst, src); err != nil {
  •                 return err
  •         }
  •         ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
  •         defer cancel()
  •         //virusClass, isVirus, err := avast(ctx, dstPath)
  •         virusClass, isVirus, err := Microsoft_Defender(ctx, dstPath)
  •         //log.Printf("class:%s\n, isVirus:%t\n, err:%v\n", virusClass, isVirus, err)
  •         now := time.Now()
  •         format1 := now.Format("2006/01/02 15:04")
  •         file1, err := os.Open(dstPath)
  •         defer file1.Close()
  •         if err != nil {
  •                 fmt.Errorf("读取文件失败!")
  •         }
  •         hash := sha256.New()
  •         if _, err := io.Copy(hash, file1); err != nil {
  •                 log.Fatal(err)
  •         }
  •         sum := hash.Sum(nil)
  •         hash256 := hex.EncodeToString(sum[:])
  •         u := &avinfo{
  •                 Filename:       file.Filename,
  •                 Virus:          isVirus,
  •                 VirusClass:     virusClass,
  •                 Antivirus_name: "Microsoft_Defender",
  •                 Status:         "finshed",
  •                 UploadTime:     format1,
  •                 Hash256:        hash256,
  •         }
  •         // Return success message
  •         //dbinsert(u.Filename, u.Virus, u.VirusClass, u.Antivirus_name, u.Status)
  •         return c.JSON(http.StatusOK, u)
  • }
  • func kaspersky(ctx context.Context, filePath string) (string, string, error) {
  •         cmd := exec.Command("avp.com", "SCAN", "/i0", filePath)
  •         stdout := new(bytes.Buffer)
  •         cmd.Stdout = stdout
  •         stderr := new(bytes.Buffer)
  •         cmd.Stderr = stderr

  •         if err := cmd.Run(); err != nil {

  •                 exitErr := &exec.ExitError{}
  •                 if !errors.As(err, &exitErr) {
  •                         return "", "false", fmt.Errorf("exec failed: %w", err)
  •                 }
  •         }

  •         // GBK -> UTF-8
  •         dec := simplifiedchinese.GBK.NewDecoder()
  •         outBytes, err := io.ReadAll(dec.Reader(stdout))
  •         if err != nil {
  •                 return "", "false", fmt.Errorf("decode failed: %w", err)
  •         }
  •         outStr := string(outBytes)

  •         if !strings.Contains(outStr, "suspicion") {
  •                 return "", "false", nil // no virus
  •         }

  •         exp := regexp.MustCompile(`suspicion\s+(.+)`)
  •         matches := exp.FindStringSubmatch(outStr)
  •         if len(matches) != 2 {
  •                 return "", "false", fmt.Errorf("regexp match: %s", outStr)
  •         }
  •         return matches[1], "true", nil
  • }
  • func avast(ctx context.Context, filePath string) (string, string, error) {
  •         cmd := exec.Command("ashcmd.exe", filePath)
  •         stdout := new(bytes.Buffer)
  •         cmd.Stdout = stdout
  •         stderr := new(bytes.Buffer)
  •         cmd.Stderr = stderr

  •         if err := cmd.Run(); err != nil {

  •                 exitErr := &exec.ExitError{}
  •                 if !errors.As(err, &exitErr) {
  •                         return "", "false", fmt.Errorf("exec failed: %w", err)
  •                 }
  •         }

  •         // GBK -> UTF-8
  •         dec := simplifiedchinese.GBK.NewDecoder()
  •         outBytes, err := io.ReadAll(dec.Reader(stdout))
  •         if err != nil {
  •                 return "", "false", fmt.Errorf("decode failed: %w", err)
  •         }
  •         outStr := string(outBytes)

  •         if strings.Contains(outStr, "OK") {
  •                 return "", "false", nil // no virus
  •         }

  •         exp := regexp.MustCompile(strings.Replace(filePath, `\`, `\\`, -1) + `(.+)`)
  •         matches := exp.FindStringSubmatch(outStr)
  •         //log.Println(matches)
  •         if len(matches) != 2 {
  •                 return "", "false", fmt.Errorf("regexp match: %s", outStr)
  •         }
  •         str := filterString(matches[0])
  •         return strings.Replace(str, filePath, ``, -1), "true", nil
  • }
  • func eset(ctx context.Context, filePath string) (string, string, error) {
  •         cmd := exec.Command("ecls.exe", "/files", filePath)
  •         stdout := new(bytes.Buffer)
  •         cmd.Stdout = stdout
  •         stderr := new(bytes.Buffer)
  •         cmd.Stderr = stderr

  •         if err := cmd.Run(); err != nil {

  •                 exitErr := &exec.ExitError{}
  •                 if !errors.As(err, &exitErr) {
  •                         return "", "false", fmt.Errorf("exec failed: %w", err)
  •                 }
  •         }

  •         // GBK -> UTF-8
  •         dec := simplifiedchinese.GBK.NewDecoder()
  •         outBytes, err := io.ReadAll(dec.Reader(stdout))
  •         if err != nil {
  •                 return "", "false", fmt.Errorf("decode failed: %w", err)
  •         }
  •         outStr := string(outBytes)

  •         if !strings.Contains(outStr, "威胁") {
  •                 return "", "false", nil // no virus
  •         }

  •         exp := regexp.MustCompile(`威胁="([^"]+)"`)
  •         matches := exp.FindStringSubmatch(outStr)
  •         //log.Println(matches)
  •         if len(matches) != 2 {
  •                 return "", "false", fmt.Errorf("regexp match: %s", outStr)
  •         }
  •         return matches[1], "true", nil
  • }
  • func emsisoft(ctx context.Context, filePath string) (string, string, error) {
  •         cmd := exec.Command("a2cmd.exe", "/files", filePath)
  •         stdout := new(bytes.Buffer)
  •         cmd.Stdout = stdout
  •         stderr := new(bytes.Buffer)
  •         cmd.Stderr = stderr

  •         if err := cmd.Run(); err != nil {

  •                 exitErr := &exec.ExitError{}
  •                 if !errors.As(err, &exitErr) {
  •                         return "", "false", fmt.Errorf("exec failed: %w", err)
  •                 }
  •         }

  •         // GBK -> UTF-8
  •         dec := simplifiedchinese.GBK.NewDecoder()
  •         outBytes, err := io.ReadAll(dec.Reader(stdout))
  •         if err != nil {
  •                 return "", "false", fmt.Errorf("decode failed: %w", err)
  •         }
  •         outStr := string(outBytes)

  •         if !strings.Contains(outStr, "detected:") {
  •                 return "", "false", nil // no virus
  •         }

  •         exp := regexp.MustCompile(`detected:([^"]+)Scanned`)
  •         matches := exp.FindStringSubmatch(outStr)
  •         //log.Println(matches)
  •         if len(matches) != 2 {
  •                 return "", "false", fmt.Errorf("regexp match: %s", outStr)
  •         }
  •         return matches[1], "true", nil
  • }
  • func clamav(ctx context.Context, filePath string) (string, string, error) {
  •         cmd := exec.Command("clamscan", "-r", filePath)
  •         stdout := new(bytes.Buffer)
  •         cmd.Stdout = stdout
  •         stderr := new(bytes.Buffer)
  •         cmd.Stderr = stderr

  •         if err := cmd.Run(); err != nil {

  •                 exitErr := &exec.ExitError{}
  •                 if !errors.As(err, &exitErr) {
  •                         return "", "false", fmt.Errorf("exec failed: %w", err)
  •                 }
  •         }

  •         // GBK -> UTF-8
  •         dec := simplifiedchinese.GBK.NewDecoder()
  •         outBytes, err := io.ReadAll(dec.Reader(stdout))
  •         if err != nil {
  •                 return "", "false", fmt.Errorf("decode failed: %w", err)
  •         }
  •         outStr := string(outBytes)

  •         if strings.Contains(outStr, "OK") {
  •                 return "", "false", nil // no virus
  •         }

  •         exp := regexp.MustCompile(filePath + ":" + ` ([^"]+)` + "FOUND")
  •         matches := exp.FindStringSubmatch(outStr)
  •         log.Println(matches)
  •         if len(matches) != 2 {
  •                 return "", "false", fmt.Errorf("regexp match: %s", outStr)
  •         }
  •         return matches[1], "true", nil
  • }
  • func ikarust(ctx context.Context, filePath string) (string, string, error) {
  •         cmd := exec.Command("t3scan_w64.exe", filePath)
  •         //stdout := new(bytes.Buffer)
  •         //cmd.Stdout = stdout
  •         //stderr := new(bytes.Buffer)
  •         //cmd.Stderr = stderr

  •         buf, err := cmd.CombinedOutput()
  •         if err != nil {

  •                 exitErr := &exec.ExitError{}
  •                 if !errors.As(err, &exitErr) {
  •                         return "", "false", fmt.Errorf("exec failed: %w", err)
  •                 }
  •         }

  •         // GBK -> UTF-8
  •         dec := simplifiedchinese.GBK.NewDecoder()
  •         outBytes, err := io.ReadAll(dec.Reader(bytes.NewReader(buf)))
  •         log.Println(string(buf))
  •         if err != nil {
  •                 return "", "false", fmt.Errorf("decode failed: %w", err)
  •         }
  •         outStr := string(outBytes)

  •         if !strings.Contains(outStr, "found") {
  •                 return "", "false", nil // no virus
  •         }

  •         exp := regexp.MustCompile(`'([^"]+)'`)
  •         matches := exp.FindStringSubmatch(outStr)
  •         log.Println(matches)
  •         if len(matches) != 2 {
  •                 return "", "false", fmt.Errorf("regexp match: %s", outStr)
  •         }
  •         return matches[1], "true", nil
  • }
  • func AdAware(ctx context.Context, filePath string) (string, string, error) {
  •         cmd := exec.Command("AdAwareCommandLineScanner.exe", "--scan-result-stdout", "--custom", filePath)
  •         //stdout := new(bytes.Buffer)
  •         //cmd.Stdout = stdout
  •         //stderr := new(bytes.Buffer)
  •         //cmd.Stderr = stderr

  •         buf, err := cmd.CombinedOutput()
  •         if err != nil {

  •                 exitErr := &exec.ExitError{}
  •                 if !errors.As(err, &exitErr) {
  •                         return "", "false", fmt.Errorf("exec failed: %w", err)
  •                 }
  •         }

  •         // GBK -> UTF-8
  •         dec := simplifiedchinese.GBK.NewDecoder()
  •         outBytes, err := io.ReadAll(dec.Reader(bytes.NewReader(buf)))
  •         log.Println(string(buf))
  •         if err != nil {
  •                 return "", "false", fmt.Errorf("decode failed: %w", err)
  •         }
  •         outStr := string(outBytes)

  •         if !strings.Contains(outStr, "ScanStatus="Infected"") {
  •                 return "", "false", nil // no virus
  •         }

  •         exp := regexp.MustCompile(`ThreatName="([^"]+)"`)
  •         matches := exp.FindStringSubmatch(outStr)
  •         log.Println(matches)
  •         if len(matches) != 2 {
  •                 return "", "false", fmt.Errorf("regexp match: %s", outStr)
  •         }
  •         return matches[1], "true", nil
  • }
  • func Sophos(ctx context.Context, filePath string) (string, string, error) {
  •         cmd := exec.Command("SophosInterceptXCLI.exe", "scan", filePath, "--noui")
  •         //stdout := new(bytes.Buffer)
  •         //cmd.Stdout = stdout
  •         //stderr := new(bytes.Buffer)
  •         //cmd.Stderr = stderr

  •         buf, err := cmd.CombinedOutput()
  •         if err != nil {

  •                 exitErr := &exec.ExitError{}
  •                 if !errors.As(err, &exitErr) {
  •                         return "", "false", fmt.Errorf("exec failed: %w", err)
  •                 }
  •         }

  •         // GBK -> UTF-8
  •         dec := simplifiedchinese.GBK.NewDecoder()
  •         outBytes, err := io.ReadAll(dec.Reader(bytes.NewReader(buf)))
  •         log.Println(string(buf))
  •         if err != nil {
  •                 return "", "false", fmt.Errorf("decode failed: %w", err)
  •         }
  •         outStr := string(outBytes)

  •         if !strings.Contains(outStr, "type:") {
  •                 return "", "false", nil // no virus
  •         }

  •         exp := regexp.MustCompile(`\(Detected as '([^"]+)' type`)
  •         matches := exp.FindStringSubmatch(outStr)
  •         log.Println(matches)
  •         if len(matches) != 2 {
  •                 return "", "false", fmt.Errorf("regexp match: %s", outStr)
  •         }
  •         return matches[1], "true", nil
  • }
  • func Drweb(ctx context.Context, filePath string) (string, string, error) {
  •         cmd := exec.Command("dwscancl.exe", filePath, "/RP:C:\\Users\\CNIX\\Desktop\\drweb\\file.log")
  •         _, err := cmd.CombinedOutput()
  •         if err != nil {

  •                 exitErr := &exec.ExitError{}
  •                 if !errors.As(err, &exitErr) {
  •                         return "", "false", fmt.Errorf("exec failed: %w", err)
  •                 }
  •         }

  •         filePath1 := "C:\\Users\\CNIX\\Desktop\\drweb\\file.log"

  •         // 打开文件
  •         file, err := os.Open(filePath1)
  •         if err != nil {
  •                 fmt.Println("打开文件失败:", err)
  •                 return "", "", nil
  •         }
  •         defer file.Close()

  •         // 读取文件内容
  •         content, err := ioutil.ReadAll(file)
  •         if err != nil {
  •                 fmt.Println("读取文件失败:", err)
  •                 return "", "", nil
  •         }

  •         // 正则表达式匹配
  •         pattern := "infected with"
  •         match := regexp.MustCompile(pattern).Match(content)

  •         // 输出结果
  •         if !match {
  •                 return "", "false", nil // no virus
  •         }
  •         // 正则表达式模式
  •         pattern1 := `infected with (\S+)`

  •         // 编译正则表达式
  •         regexpPattern := regexp.MustCompile(pattern1)

  •         // 使用正则表达式查找所有匹配项
  •         matches := regexpPattern.FindAllStringSubmatch(string(content), -1)

  •         // 输出匹配结果
  •         x := string("")
  •         for _, match := range matches {
  •                 if len(match) > 1 {
  •                         // 第一个捕获组的内容
  •                         result := match[1]
  •                         x = result

  •                 }
  •         }
  •         return x, "true", nil
  • }
  • func Norton(ctx context.Context, filePath string) (string, string, error) {

  •         log.Println(filePath)
  •         cmd := exec.Command("navw32.exe", filePath)
  •         now := time.Now()
  •         format2 := now.Format("2006/01/02 15:04")

  •         log.Println(format2)
  •         _, err := cmd.CombinedOutput()
  •         if err != nil {

  •                 exitErr := &exec.ExitError{}
  •                 if !errors.As(err, &exitErr) {
  •                         return "", "false", fmt.Errorf("exec failed: %w", err)
  •                 }
  •         }
  •         //cmd1 := exec.Command("MCUI32.exe /export C:\\Users\\CNIX\\Desktop\\norton\\log.txt")
  •         //xx, err := cmd1.CombinedOutput()
  •         //if err != nil {
  •         //
  •         //        exitErr := &exec.ExitError{}
  •         //        if !errors.As(err, &exitErr) {
  •         //                return "", "false", fmt.Errorf("exec failed: %w", err)
  •         //        }
  •         //}
  •         //log.Println(xx)
  •         // 文件路径
  •         filePath1 := "C:\\Users\\CNIX\\Desktop\\norton\\log.txt"

  •         // 打开文件
  •         file, err := os.Open(filePath1)
  •         if err != nil {
  •                 fmt.Println("打开文件失败:", err)
  •                 return "", "", nil
  •         }
  •         defer file.Close()

  •         // 读取文件内容
  •         content, err := ioutil.ReadAll(file)
  •         if err != nil {
  •                 fmt.Println("读取文件失败:", err)
  •                 return "", "", nil
  •         }

  •         // 正则表达式匹配
  •         pattern := format2
  •         match := regexp.MustCompile(pattern).Match(content)

  •         // 输出结果
  •         if !match {
  •                 return "", "false", nil // no virus
  •         }
  •         // 正则表达式模式
  •         pattern1 := format2 + `(\S+) 检测方 自动防护,已制止,已办理`

  •         // 编译正则表达式
  •         regexpPattern := regexp.MustCompile(pattern1)

  •         // 使用正则表达式查找所有匹配项
  •         matches := regexpPattern.FindAllStringSubmatch(string(content), -1)

  •         // 输出匹配结果
  •         x := string("")
  •         for _, match := range matches {
  •                 if len(match) > 1 {
  •                         // 第一个捕获组的内容
  •                         result := match[1]
  •                         x = result

  •                 }
  •         }
  •         return x, "true", nil
  • }
  • func Microsoft_Defender(ctx context.Context, filePath string) (string, string, error) {
  •         //shell := "MpCmdRun.exe -Scan -ScanType 3 -DisableRemediation -File " + filePath
  •         //filePath1 := "C:\Users\admin\Desktop\test\\tmp\\MicroKMS_v21.12.08_Beta.exe"
  •         cmd := exec.Command("MpCmdRun.exe", "-Scan", "-ScanType", "3", "-File", filePath, "-DisableRemediation")
  •         //cmd := exec.Command("C:\\ProgramData\\Microsoft\\Windows Defender\\Platform\\4.18.23110.3-0\\MpCmdRun.exe -Scan -ScanType 3 -File C:\\Users\\admin\\Desktop\\test\\tmp\\MicroKMS_v21.12.08_Beta.exe -DisableRemediation")
  •         //stdout := new(bytes.Buffer)
  •         //cmd.Stdout = stdout
  •         //stderr := new(bytes.Buffer)
  •         //cmd.Stderr = stderr
  •         buf, err := cmd.CombinedOutput()
  •         if err != nil {

  •                 exitErr := &exec.ExitError{}
  •                 if !errors.As(err, &exitErr) {
  •                         return "", "false", fmt.Errorf("exec failed: %w", err)
  •                 }
  •         }
  •         // GBK -> UTF-8
  •         dec := simplifiedchinese.GBK.NewDecoder()
  •         outBytes, err := io.ReadAll(dec.Reader(bytes.NewReader(buf)))
  •         if err != nil {
  •                 return "", "false", fmt.Errorf("decode failed: %w", err)
  •         }
  •         outStr := string(outBytes)
  •         if !strings.Contains(outStr, "found 1 threats") {
  •                 return "", "false", nil // no virus
  •         }
  •         exp := regexp.MustCompile(`Threat\s*:\s*([^[:space:]]+)`)
  •         matches := exp.FindStringSubmatch(outStr)
  •         log.Println(matches)
  •         if len(matches) != 2 {
  •                 return "", "false", fmt.Errorf("regexp match: %s", outStr)
  •         }
  •         return matches[1], "true", nil
  • }
  • func filterString(str string) string {
  •         filteredStr := strings.ReplaceAll(str, "\r", "")
  •         filteredStr = strings.ReplaceAll(filteredStr, "\t", "")
  •         return filteredStr
  • }
以下是服务端接口代码:

  • package main

  • import (
  •         "bytes"
  •         "context"
  •         "crypto/sha256"
  •         "encoding/hex"
  •         "errors"
  •         "fmt"
  •         _ "github.com/go-sql-driver/mysql"
  •         "github.com/labstack/echo"
  •         "github.com/labstack/echo/middleware"
  •         "golang.org/x/text/encoding/simplifiedchinese"
  •         "io"
  •         "io/ioutil"
  •         "log"
  •         "net/http"
  •         "os"
  •         "os/exec"
  •         "path/filepath"
  •         "regexp"
  •         "strings"
  •         "time"
  • )

  • type avinfo struct {
  •         //DisplayIcon    string
  •         Filename       string
  •         Virus          string
  •         Antivirus_name string
  •         VirusClass     string
  •         Status         string
  •         UploadTime     string
  •         Hash256        string

  •         //UninstallString string
  • }

  • func main() {
  •         e := echo.New()
  •         e.Use(middleware.Logger())
  •         e.Use(middleware.Recover())
  •         e.POST("/upload", uploadFile)
  •         e.Logger.Fatal(e.Start(":8080"))

  •         //log.Printf("class:%s", virusClass)

  • }
  • func uploadFile(c echo.Context) error {
  •         // Get file from request
  •         file, err := c.FormFile("file")
  •         if err != nil {
  •                 return err
  •         }

  •         // Open the file
  •         src, err := file.Open()
  •         if err != nil {
  •                 return err
  •         }
  •         defer src.Close()

  •         // Destination file path
  •         dstPath := filepath.Join("C:\\Users\\admin\\Desktop\\test\\tmp", file.Filename)

  •         // Create destination file
  •         dst, err := os.Create(dstPath)
  •         if err != nil {
  •                 return err
  •         }
  •         defer dst.Close()

  •         // Copy the file content to the destination file
  •         if _, err = io.Copy(dst, src); err != nil {
  •                 return err
  •         }
  •         ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
  •         defer cancel()
  •         //virusClass, isVirus, err := avast(ctx, dstPath)
  •         virusClass, isVirus, err := Microsoft_Defender(ctx, dstPath)
  •         //log.Printf("class:%s\n, isVirus:%t\n, err:%v\n", virusClass, isVirus, err)
  •         now := time.Now()
  •         format1 := now.Format("2006/01/02 15:04")
  •         file1, err := os.Open(dstPath)
  •         defer file1.Close()
  •         if err != nil {
  •                 fmt.Errorf("读取文件失败!")
  •         }
  •         hash := sha256.New()
  •         if _, err := io.Copy(hash, file1); err != nil {
  •                 log.Fatal(err)
  •         }
  •         sum := hash.Sum(nil)
  •         hash256 := hex.EncodeToString(sum[:])
  •         u := &avinfo{
  •                 Filename:       file.Filename,
  •                 Virus:          isVirus,
  •                 VirusClass:     virusClass,
  •                 Antivirus_name: "Microsoft_Defender",
  •                 Status:         "finshed",
  •                 UploadTime:     format1,
  •                 Hash256:        hash256,
  •         }
  •         // Return success message
  •         //dbinsert(u.Filename, u.Virus, u.VirusClass, u.Antivirus_name, u.Status)
  •         return c.JSON(http.StatusOK, u)
  • }
  • func kaspersky(ctx context.Context, filePath string) (string, string, error) {
  •         cmd := exec.Command("avp.com", "SCAN", "/i0", filePath)
  •         stdout := new(bytes.Buffer)
  •         cmd.Stdout = stdout
  •         stderr := new(bytes.Buffer)
  •         cmd.Stderr = stderr

  •         if err := cmd.Run(); err != nil {

  •                 exitErr := &exec.ExitError{}
  •                 if !errors.As(err, &exitErr) {
  •                         return "", "false", fmt.Errorf("exec failed: %w", err)
  •                 }
  •         }

  •         // GBK -> UTF-8
  •         dec := simplifiedchinese.GBK.NewDecoder()
  •         outBytes, err := io.ReadAll(dec.Reader(stdout))
  •         if err != nil {
  •                 return "", "false", fmt.Errorf("decode failed: %w", err)
  •         }
  •         outStr := string(outBytes)

  •         if !strings.Contains(outStr, "suspicion") {
  •                 return "", "false", nil // no virus
  •         }

  •         exp := regexp.MustCompile(`suspicion\s+(.+)`)
  •         matches := exp.FindStringSubmatch(outStr)
  •         if len(matches) != 2 {
  •                 return "", "false", fmt.Errorf("regexp match: %s", outStr)
  •         }
  •         return matches[1], "true", nil
  • }
  • func avast(ctx context.Context, filePath string) (string, string, error) {
  •         cmd := exec.Command("ashcmd.exe", filePath)
  •         stdout := new(bytes.Buffer)
  •         cmd.Stdout = stdout
  •         stderr := new(bytes.Buffer)
  •         cmd.Stderr = stderr

  •         if err := cmd.Run(); err != nil {

  •                 exitErr := &exec.ExitError{}
  •                 if !errors.As(err, &exitErr) {
  •                         return "", "false", fmt.Errorf("exec failed: %w", err)
  •                 }
  •         }

  •         // GBK -> UTF-8
  •         dec := simplifiedchinese.GBK.NewDecoder()
  •         outBytes, err := io.ReadAll(dec.Reader(stdout))
  •         if err != nil {
  •                 return "", "false", fmt.Errorf("decode failed: %w", err)
  •         }
  •         outStr := string(outBytes)

  •         if strings.Contains(outStr, "OK") {
  •                 return "", "false", nil // no virus
  •         }

  •         exp := regexp.MustCompile(strings.Replace(filePath, `\`, `\\`, -1) + `(.+)`)
  •         matches := exp.FindStringSubmatch(outStr)
  •         //log.Println(matches)
  •         if len(matches) != 2 {
  •                 return "", "false", fmt.Errorf("regexp match: %s", outStr)
  •         }
  •         str := filterString(matches[0])
  •         return strings.Replace(str, filePath, ``, -1), "true", nil
  • }
  • func eset(ctx context.Context, filePath string) (string, string, error) {
  •         cmd := exec.Command("ecls.exe", "/files", filePath)
  •         stdout := new(bytes.Buffer)
  •         cmd.Stdout = stdout
  •         stderr := new(bytes.Buffer)
  •         cmd.Stderr = stderr

  •         if err := cmd.Run(); err != nil {

  •                 exitErr := &exec.ExitError{}
  •                 if !errors.As(err, &exitErr) {
  •                         return "", "false", fmt.Errorf("exec failed: %w", err)
  •                 }
  •         }

  •         // GBK -> UTF-8
  •         dec := simplifiedchinese.GBK.NewDecoder()
  •         outBytes, err := io.ReadAll(dec.Reader(stdout))
  •         if err != nil {
  •                 return "", "false", fmt.Errorf("decode failed: %w", err)
  •         }
  •         outStr := string(outBytes)

  •         if !strings.Contains(outStr, "威胁") {
  •                 return "", "false", nil // no virus
  •         }

  •         exp := regexp.MustCompile(`威胁="([^"]+)"`)
  •         matches := exp.FindStringSubmatch(outStr)
  •         //log.Println(matches)
  •         if len(matches) != 2 {
  •                 return "", "false", fmt.Errorf("regexp match: %s", outStr)
  •         }
  •         return matches[1], "true", nil
  • }
  • func emsisoft(ctx context.Context, filePath string) (string, string, error) {
  •         cmd := exec.Command("a2cmd.exe", "/files", filePath)
  •         stdout := new(bytes.Buffer)
  •         cmd.Stdout = stdout
  •         stderr := new(bytes.Buffer)
  •         cmd.Stderr = stderr

  •         if err := cmd.Run(); err != nil {

  •                 exitErr := &exec.ExitError{}
  •                 if !errors.As(err, &exitErr) {
  •                         return "", "false", fmt.Errorf("exec failed: %w", err)
  •                 }
  •         }

  •         // GBK -> UTF-8
  •         dec := simplifiedchinese.GBK.NewDecoder()
  •         outBytes, err := io.ReadAll(dec.Reader(stdout))
  •         if err != nil {
  •                 return "", "false", fmt.Errorf("decode failed: %w", err)
  •         }
  •         outStr := string(outBytes)

  •         if !strings.Contains(outStr, "detected:") {
  •                 return "", "false", nil // no virus
  •         }

  •         exp := regexp.MustCompile(`detected:([^"]+)Scanned`)
  •         matches := exp.FindStringSubmatch(outStr)
  •         //log.Println(matches)
  •         if len(matches) != 2 {
  •                 return "", "false", fmt.Errorf("regexp match: %s", outStr)
  •         }
  •         return matches[1], "true", nil
  • }
  • func clamav(ctx context.Context, filePath string) (string, string, error) {
  •         cmd := exec.Command("clamscan", "-r", filePath)
  •         stdout := new(bytes.Buffer)
  •         cmd.Stdout = stdout
  •         stderr := new(bytes.Buffer)
  •         cmd.Stderr = stderr

  •         if err := cmd.Run(); err != nil {

  •                 exitErr := &exec.ExitError{}
  •                 if !errors.As(err, &exitErr) {
  •                         return "", "false", fmt.Errorf("exec failed: %w", err)
  •                 }
  •         }

  •         // GBK -> UTF-8
  •         dec := simplifiedchinese.GBK.NewDecoder()
  •         outBytes, err := io.ReadAll(dec.Reader(stdout))
  •         if err != nil {
  •                 return "", "false", fmt.Errorf("decode failed: %w", err)
  •         }
  •         outStr := string(outBytes)

  •         if strings.Contains(outStr, "OK") {
  •                 return "", "false", nil // no virus
  •         }

  •         exp := regexp.MustCompile(filePath + ":" + ` ([^"]+)` + "FOUND")
  •         matches := exp.FindStringSubmatch(outStr)
  •         log.Println(matches)
  •         if len(matches) != 2 {
  •                 return "", "false", fmt.Errorf("regexp match: %s", outStr)
  •         }
  •         return matches[1], "true", nil
  • }
  • func ikarust(ctx context.Context, filePath string) (string, string, error) {
  •         cmd := exec.Command("t3scan_w64.exe", filePath)
  •         //stdout := new(bytes.Buffer)
  •         //cmd.Stdout = stdout
  •         //stderr := new(bytes.Buffer)
  •         //cmd.Stderr = stderr

  •         buf, err := cmd.CombinedOutput()
  •         if err != nil {

  •                 exitErr := &exec.ExitError{}
  •                 if !errors.As(err, &exitErr) {
  •                         return "", "false", fmt.Errorf("exec failed: %w", err)
  •                 }
  •         }

  •         // GBK -> UTF-8
  •         dec := simplifiedchinese.GBK.NewDecoder()
  •         outBytes, err := io.ReadAll(dec.Reader(bytes.NewReader(buf)))
  •         log.Println(string(buf))
  •         if err != nil {
  •                 return "", "false", fmt.Errorf("decode failed: %w", err)
  •         }
  •         outStr := string(outBytes)

  •         if !strings.Contains(outStr, "found") {
  •                 return "", "false", nil // no virus
  •         }

  •         exp := regexp.MustCompile(`'([^"]+)'`)
  •         matches := exp.FindStringSubmatch(outStr)
  •         log.Println(matches)
  •         if len(matches) != 2 {
  •                 return "", "false", fmt.Errorf("regexp match: %s", outStr)
  •         }
  •         return matches[1], "true", nil
  • }
  • func AdAware(ctx context.Context, filePath string) (string, string, error) {
  •         cmd := exec.Command("AdAwareCommandLineScanner.exe", "--scan-result-stdout", "--custom", filePath)
  •         //stdout := new(bytes.Buffer)
  •         //cmd.Stdout = stdout
  •         //stderr := new(bytes.Buffer)
  •         //cmd.Stderr = stderr

  •         buf, err := cmd.CombinedOutput()
  •         if err != nil {

  •                 exitErr := &exec.ExitError{}
  •                 if !errors.As(err, &exitErr) {
  •                         return "", "false", fmt.Errorf("exec failed: %w", err)
  •                 }
  •         }

  •         // GBK -> UTF-8
  •         dec := simplifiedchinese.GBK.NewDecoder()
  •         outBytes, err := io.ReadAll(dec.Reader(bytes.NewReader(buf)))
  •         log.Println(string(buf))
  •         if err != nil {
  •                 return "", "false", fmt.Errorf("decode failed: %w", err)
  •         }
  •         outStr := string(outBytes)

  •         if !strings.Contains(outStr, "ScanStatus="Infected"") {
  •                 return "", "false", nil // no virus
  •         }

  •         exp := regexp.MustCompile(`ThreatName="([^"]+)"`)
  •         matches := exp.FindStringSubmatch(outStr)
  •         log.Println(matches)
  •         if len(matches) != 2 {
  •                 return "", "false", fmt.Errorf("regexp match: %s", outStr)
  •         }
  •         return matches[1], "true", nil
  • }
  • func Sophos(ctx context.Context, filePath string) (string, string, error) {
  •         cmd := exec.Command("SophosInterceptXCLI.exe", "scan", filePath, "--noui")
  •         //stdout := new(bytes.Buffer)
  •         //cmd.Stdout = stdout
  •         //stderr := new(bytes.Buffer)
  •         //cmd.Stderr = stderr

  •         buf, err := cmd.CombinedOutput()
  •         if err != nil {

  •                 exitErr := &exec.ExitError{}
  •                 if !errors.As(err, &exitErr) {
  •                         return "", "false", fmt.Errorf("exec failed: %w", err)
  •                 }
  •         }

  •         // GBK -> UTF-8
  •         dec := simplifiedchinese.GBK.NewDecoder()
  •         outBytes, err := io.ReadAll(dec.Reader(bytes.NewReader(buf)))
  •         log.Println(string(buf))
  •         if err != nil {
  •                 return "", "false", fmt.Errorf("decode failed: %w", err)
  •         }
  •         outStr := string(outBytes)

  •         if !strings.Contains(outStr, "type:") {
  •                 return "", "false", nil // no virus
  •         }

  •         exp := regexp.MustCompile(`\(Detected as '([^"]+)' type`)
  •         matches := exp.FindStringSubmatch(outStr)
  •         log.Println(matches)
  •         if len(matches) != 2 {
  •                 return "", "false", fmt.Errorf("regexp match: %s", outStr)
  •         }
  •         return matches[1], "true", nil
  • }
  • func Drweb(ctx context.Context, filePath string) (string, string, error) {
  •         cmd := exec.Command("dwscancl.exe", filePath, "/RP:C:\\Users\\CNIX\\Desktop\\drweb\\file.log")
  •         _, err := cmd.CombinedOutput()
  •         if err != nil {

  •                 exitErr := &exec.ExitError{}
  •                 if !errors.As(err, &exitErr) {
  •                         return "", "false", fmt.Errorf("exec failed: %w", err)
  •                 }
  •         }
  •         //
  •         //// GBK -> UTF-8
  •         //dec := simplifiedchinese.GBK.NewDecoder()
  •         //outBytes, err := io.ReadAll(dec.Reader(bytes.NewReader(buf)))
  •         //
  •         //if err != nil {
  •         //        return "", "false", fmt.Errorf("decode failed: %w", err)
  •         //}
  •         //outStr := string(outBytes)
  •         //// 打开文件以供日志输出
  •         ////file, err := os.OpenFile("logfile.txt", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
  •         ////if err != nil {
  •         ////        log.Fatal("无法打开日志文件:", err)
  •         ////}
  •         ////defer file.Close()
  •         ////// 将日志输出到文件
  •         ////log.SetOutput(file)
  •         ////log.Println(outStr)
  •         ////log.Println("-------------------------------------")
  •         ////log.Println(string(buf))
  •         //// 获取当前可执行文件的路径
  •         //exePath, err := os.Executable()
  •         //if err != nil {
  •         //        fmt.Println("获取可执行文件路径失败:", err)
  •         //        return "", "", nil
  •         //}
  •         //
  •         //// 获取当前可执行文件所在的目录
  •         //exeDir := filepath.Dir(exePath)
  •         //// 构建要读取的文件的路径
  •         //x := filepath.Join(exeDir, "file.log")
  •         //
  •         //// 读取文件内容
  •         //content, err := os.Open(x)
  •         //if err != nil {
  •         //        fmt.Println("读取文件失败:", err)
  •         //        return "", "", nil
  •         //}
  •         //
  •         //// 打印文件内容
  •         //fmt.Println("文件内容:")
  •         //fmt.Println(string(content))
  •         //if !strings.Contains(content, "infected with") {
  •         //        return "", "false", nil // no virus
  •         //}
  •         // 文件路径
  •         filePath1 := "C:\\Users\\CNIX\\Desktop\\drweb\\file.log"

  •         // 打开文件
  •         file, err := os.Open(filePath1)
  •         if err != nil {
  •                 fmt.Println("打开文件失败:", err)
  •                 return "", "", nil
  •         }
  •         defer file.Close()

  •         // 读取文件内容
  •         content, err := ioutil.ReadAll(file)
  •         if err != nil {
  •                 fmt.Println("读取文件失败:", err)
  •                 return "", "", nil
  •         }

  •         // 正则表达式匹配
  •         pattern := "infected with"
  •         match := regexp.MustCompile(pattern).Match(content)

  •         // 输出结果
  •         if !match {
  •                 return "", "false", nil // no virus
  •         }
  •         // 正则表达式模式
  •         pattern1 := `infected with (\S+)`

  •         // 编译正则表达式
  •         regexpPattern := regexp.MustCompile(pattern1)

  •         // 使用正则表达式查找所有匹配项
  •         matches := regexpPattern.FindAllStringSubmatch(string(content), -1)

  •         // 输出匹配结果
  •         x := string("")
  •         for _, match := range matches {
  •                 if len(match) > 1 {
  •                         // 第一个捕获组的内容
  •                         result := match[1]
  •                         x = result

  •                 }
  •         }
  •         return x, "true", nil
  • }
  • func Norton(ctx context.Context, filePath string) (string, string, error) {

  •         log.Println(filePath)
  •         cmd := exec.Command("navw32.exe", filePath)
  •         now := time.Now()
  •         format2 := now.Format("2006/01/02 15:04")

  •         log.Println(format2)
  •         _, err := cmd.CombinedOutput()
  •         if err != nil {

  •                 exitErr := &exec.ExitError{}
  •                 if !errors.As(err, &exitErr) {
  •                         return "", "false", fmt.Errorf("exec failed: %w", err)
  •                 }
  •         }
  •         //cmd1 := exec.Command("MCUI32.exe /export C:\\Users\\CNIX\\Desktop\\norton\\log.txt")
  •         //xx, err := cmd1.CombinedOutput()
  •         //if err != nil {
  •         //
  •         //        exitErr := &exec.ExitError{}
  •         //        if !errors.As(err, &exitErr) {
  •         //                return "", "false", fmt.Errorf("exec failed: %w", err)
  •         //        }
  •         //}
  •         //log.Println(xx)
  •         // 文件路径
  •         filePath1 := "C:\\Users\\CNIX\\Desktop\\norton\\log.txt"

  •         // 打开文件
  •         file, err := os.Open(filePath1)
  •         if err != nil {
  •                 fmt.Println("打开文件失败:", err)
  •                 return "", "", nil
  •         }
  •         defer file.Close()

  •         // 读取文件内容
  •         content, err := ioutil.ReadAll(file)
  •         if err != nil {
  •                 fmt.Println("读取文件失败:", err)
  •                 return "", "", nil
  •         }

  •         // 正则表达式匹配
  •         pattern := format2
  •         match := regexp.MustCompile(pattern).Match(content)

  •         // 输出结果
  •         if !match {
  •                 return "", "false", nil // no virus
  •         }
  •         // 正则表达式模式
  •         pattern1 := format2 + `(\S+) 检测方 自动防护,已制止,已办理`

  •         // 编译正则表达式
  •         regexpPattern := regexp.MustCompile(pattern1)

  •         // 使用正则表达式查找所有匹配项
  •         matches := regexpPattern.FindAllStringSubmatch(string(content), -1)

  •         // 输出匹配结果
  •         x := string("")
  •         for _, match := range matches {
  •                 if len(match) > 1 {
  •                         // 第一个捕获组的内容
  •                         result := match[1]
  •                         x = result

  •                 }
  •         }
  •         return x, "true", nil
  • }
  • func Microsoft_Defender(ctx context.Context, filePath string) (string, string, error) {
  •         //shell := "MpCmdRun.exe -Scan -ScanType 3 -DisableRemediation -File " + filePath
  •         //filePath1 := "C:\Users\admin\Desktop\test\\tmp\\MicroKMS_v21.12.08_Beta.exe"
  •         cmd := exec.Command("MpCmdRun.exe", "-Scan", "-ScanType", "3", "-File", filePath, "-DisableRemediation")
  •         //cmd := exec.Command("C:\\ProgramData\\Microsoft\\Windows Defender\\Platform\\4.18.23110.3-0\\MpCmdRun.exe -Scan -ScanType 3 -File C:\\Users\\admin\\Desktop\\test\\tmp\\MicroKMS_v21.12.08_Beta.exe -DisableRemediation")
  •         //stdout := new(bytes.Buffer)
  •         //cmd.Stdout = stdout
  •         //stderr := new(bytes.Buffer)
  •         //cmd.Stderr = stderr
  •         buf, err := cmd.CombinedOutput()
  •         if err != nil {

  •                 exitErr := &exec.ExitError{}
  •                 if !errors.As(err, &exitErr) {
  •                         return "", "false", fmt.Errorf("exec failed: %w", err)
  •                 }
  •         }
  •         // GBK -> UTF-8
  •         dec := simplifiedchinese.GBK.NewDecoder()
  •         outBytes, err := io.ReadAll(dec.Reader(bytes.NewReader(buf)))
  •         if err != nil {
  •                 return "", "false", fmt.Errorf("decode failed: %w", err)
  •         }
  •         outStr := string(outBytes)
  •         if !strings.Contains(outStr, "found 1 threats") {
  •                 return "", "false", nil // no virus
  •         }
  •         exp := regexp.MustCompile(`Threat\s*:\s*([^[:space:]]+)`)
  •         matches := exp.FindStringSubmatch(outStr)
  •         log.Println(matches)
  •         if len(matches) != 2 {
  •                 return "", "false", fmt.Errorf("regexp match: %s", outStr)
  •         }
  •         return matches[1], "true", nil
  • }
  • func filterString(str string) string {
  •         filteredStr := strings.ReplaceAll(str, "\r", "")
  •         filteredStr = strings.ReplaceAll(filteredStr, "\t", "")
  •         return filteredStr
  • }
快速回复换一批
好贴支持!
遇见神贴岂能不顶
路过留名
先赞后看,养成习惯! 感谢大佬指路,回帖留名以备日后查阅。 📝🚀
日常暖贴。 优质好贴不能沉,我来帮楼主顶上去! 📈🎈
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|精密测量技术论坛 ( 桂ICP备2026007449号-1 )

GMT+8, 2026-7-5 15:09 , Processed in 0.168484 second(s), 43 queries .

Powered by 精密测量技术论坛

© 2025-2026 联系站长

快速回复 返回顶部 返回列表