查看: 1359|回复: 6
收起左侧

[原创工具] 使用Go开发一款调用各大杀毒软件的脚本

[复制链接]
xyk001
发表于 2024-3-29 11:06:13 | 显示全部楼层 |阅读模式
因业务需要,领导让我收集市面上的杀毒软件命令行版本,目前已收集到Kaspersky、Microsoft_ Defender、Avast、Emsisoft、DrWeb、Sophos、Norton、ESET、ikarust、AdAware、clamav。欢迎大佬们补充。以下为客户端agent源码,小白水平,多多提建议。
  1. package main

  2. import (
  3.         "bytes"
  4.         "context"
  5.         "crypto/sha256"
  6.         "encoding/hex"
  7.         "errors"
  8.         "fmt"
  9.         _ "github.com/go-sql-driver/mysql"
  10.         "github.com/labstack/echo"
  11.         "github.com/labstack/echo/middleware"
  12.         "golang.org/x/text/encoding/simplifiedchinese"
  13.         "io"
  14.         "io/ioutil"
  15.         "log"
  16.         "net/http"
  17.         "os"
  18.         "os/exec"
  19.         "path/filepath"
  20.         "regexp"
  21.         "strings"
  22.         "time"
  23. )

  24. type avinfo struct {
  25.         //DisplayIcon    string
  26.         Filename       string
  27.         Virus          string
  28.         Antivirus_name string
  29.         VirusClass     string
  30.         Status         string
  31.         UploadTime     string
  32.         Hash256        string

  33.         //UninstallString string
  34. }

  35. func main() {
  36.         e := echo.New()
  37.         e.Use(middleware.Logger())
  38.         e.Use(middleware.Recover())
  39.         e.POST("/upload", uploadFile)
  40.         e.Logger.Fatal(e.Start(":8080"))

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

  42. }
  43. func uploadFile(c echo.Context) error {
  44.         // Get file from request
  45.         file, err := c.FormFile("file")
  46.         if err != nil {
  47.                 return err
  48.         }

  49.         // Open the file
  50.         src, err := file.Open()
  51.         if err != nil {
  52.                 return err
  53.         }
  54.         defer src.Close()

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

  57.         // Create destination file
  58.         dst, err := os.Create(dstPath)
  59.         if err != nil {
  60.                 return err
  61.         }
  62.         defer dst.Close()

  63.         // Copy the file content to the destination file
  64.         if _, err = io.Copy(dst, src); err != nil {
  65.                 return err
  66.         }
  67.         ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
  68.         defer cancel()
  69.         //virusClass, isVirus, err := avast(ctx, dstPath)
  70.         virusClass, isVirus, err := Microsoft_Defender(ctx, dstPath)
  71.         //log.Printf("class:%s\n, isVirus:%t\n, err:%v\n", virusClass, isVirus, err)
  72.         now := time.Now()
  73.         format1 := now.Format("2006/01/02 15:04")
  74.         file1, err := os.Open(dstPath)
  75.         defer file1.Close()
  76.         if err != nil {
  77.                 fmt.Errorf("读取文件失败!")
  78.         }
  79.         hash := sha256.New()
  80.         if _, err := io.Copy(hash, file1); err != nil {
  81.                 log.Fatal(err)
  82.         }
  83.         sum := hash.Sum(nil)
  84.         hash256 := hex.EncodeToString(sum[:])
  85.         u := &avinfo{
  86.                 Filename:       file.Filename,
  87.                 Virus:          isVirus,
  88.                 VirusClass:     virusClass,
  89.                 Antivirus_name: "Microsoft_Defender",
  90.                 Status:         "finshed",
  91.                 UploadTime:     format1,
  92.                 Hash256:        hash256,
  93.         }
  94.         // Return success message
  95.         //dbinsert(u.Filename, u.Virus, u.VirusClass, u.Antivirus_name, u.Status)
  96.         return c.JSON(http.StatusOK, u)
  97. }
  98. func kaspersky(ctx context.Context, filePath string) (string, string, error) {
  99.         cmd := exec.Command("avp.com", "SCAN", "/i0", filePath)
  100.         stdout := new(bytes.Buffer)
  101.         cmd.Stdout = stdout
  102.         stderr := new(bytes.Buffer)
  103.         cmd.Stderr = stderr

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

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

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

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

  120.         exp := regexp.MustCompile(`suspicion\s+(.+)`)
  121.         matches := exp.FindStringSubmatch(outStr)
  122.         if len(matches) != 2 {
  123.                 return "", "false", fmt.Errorf("regexp match: %s", outStr)
  124.         }
  125.         return matches[1], "true", nil
  126. }
  127. func avast(ctx context.Context, filePath string) (string, string, error) {
  128.         cmd := exec.Command("ashcmd.exe", filePath)
  129.         stdout := new(bytes.Buffer)
  130.         cmd.Stdout = stdout
  131.         stderr := new(bytes.Buffer)
  132.         cmd.Stderr = stderr

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  240.         exp := regexp.MustCompile(filePath + ":" + ` ([^"]+)` + "FOUND")
  241.         matches := exp.FindStringSubmatch(outStr)
  242.         log.Println(matches)
  243.         if len(matches) != 2 {
  244.                 return "", "false", fmt.Errorf("regexp match: %s", outStr)
  245.         }
  246.         return matches[1], "true", nil
  247. }
  248. func ikarust(ctx context.Context, filePath string) (string, string, error) {
  249.         cmd := exec.Command("t3scan_w64.exe", filePath)
  250.         //stdout := new(bytes.Buffer)
  251.         //cmd.Stdout = stdout
  252.         //stderr := new(bytes.Buffer)
  253.         //cmd.Stderr = stderr

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  354.         // 打开文件
  355.         file, err := os.Open(filePath1)
  356.         if err != nil {
  357.                 fmt.Println("打开文件失败:", err)
  358.                 return "", "", nil
  359.         }
  360.         defer file.Close()

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

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

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

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

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

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

  387.                 }
  388.         }
  389.         return x, "true", nil
  390. }
  391. func Norton(ctx context.Context, filePath string) (string, string, error) {

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

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

  399.                 exitErr := &exec.ExitError{}
  400.                 if !errors.As(err, &exitErr) {
  401.                         return "", "false", fmt.Errorf("exec failed: %w", err)
  402.                 }
  403.         }
  404.         //cmd1 := exec.Command("MCUI32.exe /export C:\\Users\\CNIX\\Desktop\\norton\\log.txt")
  405.         //xx, err := cmd1.CombinedOutput()
  406.         //if err != nil {
  407.         //
  408.         //        exitErr := &exec.ExitError{}
  409.         //        if !errors.As(err, &exitErr) {
  410.         //                return "", "false", fmt.Errorf("exec failed: %w", err)
  411.         //        }
  412.         //}
  413.         //log.Println(xx)
  414.         // 文件路径
  415.         filePath1 := "C:\\Users\\CNIX\\Desktop\\norton\\log.txt"

  416.         // 打开文件
  417.         file, err := os.Open(filePath1)
  418.         if err != nil {
  419.                 fmt.Println("打开文件失败:", err)
  420.                 return "", "", nil
  421.         }
  422.         defer file.Close()

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

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

  432.         // 输出结果
  433.         if !match {
  434.                 return "", "false", nil // no virus
  435.         }
  436.         // 正则表达式模式
  437.         pattern1 := format2 + `(\S+) 检测方 自动防护,已阻止,已解决`

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

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

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

  449.                 }
  450.         }
  451.         return x, "true", nil
  452. }
  453. func Microsoft_Defender(ctx context.Context, filePath string) (string, string, error) {
  454.         //shell := "MpCmdRun.exe -Scan -ScanType 3 -DisableRemediation -File " + filePath
  455.         //filePath1 := "C:\Users\admin\Desktop\test\\tmp\\MicroKMS_v21.12.08_Beta.exe"
  456.         cmd := exec.Command("MpCmdRun.exe", "-Scan", "-ScanType", "3", "-File", filePath, "-DisableRemediation")
  457.         //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")
  458.         //stdout := new(bytes.Buffer)
  459.         //cmd.Stdout = stdout
  460.         //stderr := new(bytes.Buffer)
  461.         //cmd.Stderr = stderr
  462.         buf, err := cmd.CombinedOutput()
  463.         if err != nil {

  464.                 exitErr := &exec.ExitError{}
  465.                 if !errors.As(err, &exitErr) {
  466.                         return "", "false", fmt.Errorf("exec failed: %w", err)
  467.                 }
  468.         }
  469.         // GBK -> UTF-8
  470.         dec := simplifiedchinese.GBK.NewDecoder()
  471.         outBytes, err := io.ReadAll(dec.Reader(bytes.NewReader(buf)))
  472.         if err != nil {
  473.                 return "", "false", fmt.Errorf("decode failed: %w", err)
  474.         }
  475.         outStr := string(outBytes)
  476.         if !strings.Contains(outStr, "found 1 threats") {
  477.                 return "", "false", nil // no virus
  478.         }
  479.         exp := regexp.MustCompile(`Threat\s*:\s*([^[:space:]]+)`)
  480.         matches := exp.FindStringSubmatch(outStr)
  481.         log.Println(matches)
  482.         if len(matches) != 2 {
  483.                 return "", "false", fmt.Errorf("regexp match: %s", outStr)
  484.         }
  485.         return matches[1], "true", nil
  486. }
  487. func filterString(str string) string {
  488.         filteredStr := strings.ReplaceAll(str, "\r", "")
  489.         filteredStr = strings.ReplaceAll(filteredStr, "\t", "")
  490.         return filteredStr
  491. }
复制代码
以下是服务端接口代码:
  1. package main

  2. import (
  3.         "bytes"
  4.         "context"
  5.         "crypto/sha256"
  6.         "encoding/hex"
  7.         "errors"
  8.         "fmt"
  9.         _ "github.com/go-sql-driver/mysql"
  10.         "github.com/labstack/echo"
  11.         "github.com/labstack/echo/middleware"
  12.         "golang.org/x/text/encoding/simplifiedchinese"
  13.         "io"
  14.         "io/ioutil"
  15.         "log"
  16.         "net/http"
  17.         "os"
  18.         "os/exec"
  19.         "path/filepath"
  20.         "regexp"
  21.         "strings"
  22.         "time"
  23. )

  24. type avinfo struct {
  25.         //DisplayIcon    string
  26.         Filename       string
  27.         Virus          string
  28.         Antivirus_name string
  29.         VirusClass     string
  30.         Status         string
  31.         UploadTime     string
  32.         Hash256        string

  33.         //UninstallString string
  34. }

  35. func main() {
  36.         e := echo.New()
  37.         e.Use(middleware.Logger())
  38.         e.Use(middleware.Recover())
  39.         e.POST("/upload", uploadFile)
  40.         e.Logger.Fatal(e.Start(":8080"))

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

  42. }
  43. func uploadFile(c echo.Context) error {
  44.         // Get file from request
  45.         file, err := c.FormFile("file")
  46.         if err != nil {
  47.                 return err
  48.         }

  49.         // Open the file
  50.         src, err := file.Open()
  51.         if err != nil {
  52.                 return err
  53.         }
  54.         defer src.Close()

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

  57.         // Create destination file
  58.         dst, err := os.Create(dstPath)
  59.         if err != nil {
  60.                 return err
  61.         }
  62.         defer dst.Close()

  63.         // Copy the file content to the destination file
  64.         if _, err = io.Copy(dst, src); err != nil {
  65.                 return err
  66.         }
  67.         ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
  68.         defer cancel()
  69.         //virusClass, isVirus, err := avast(ctx, dstPath)
  70.         virusClass, isVirus, err := Microsoft_Defender(ctx, dstPath)
  71.         //log.Printf("class:%s\n, isVirus:%t\n, err:%v\n", virusClass, isVirus, err)
  72.         now := time.Now()
  73.         format1 := now.Format("2006/01/02 15:04")
  74.         file1, err := os.Open(dstPath)
  75.         defer file1.Close()
  76.         if err != nil {
  77.                 fmt.Errorf("读取文件失败!")
  78.         }
  79.         hash := sha256.New()
  80.         if _, err := io.Copy(hash, file1); err != nil {
  81.                 log.Fatal(err)
  82.         }
  83.         sum := hash.Sum(nil)
  84.         hash256 := hex.EncodeToString(sum[:])
  85.         u := &avinfo{
  86.                 Filename:       file.Filename,
  87.                 Virus:          isVirus,
  88.                 VirusClass:     virusClass,
  89.                 Antivirus_name: "Microsoft_Defender",
  90.                 Status:         "finshed",
  91.                 UploadTime:     format1,
  92.                 Hash256:        hash256,
  93.         }
  94.         // Return success message
  95.         //dbinsert(u.Filename, u.Virus, u.VirusClass, u.Antivirus_name, u.Status)
  96.         return c.JSON(http.StatusOK, u)
  97. }
  98. func kaspersky(ctx context.Context, filePath string) (string, string, error) {
  99.         cmd := exec.Command("avp.com", "SCAN", "/i0", filePath)
  100.         stdout := new(bytes.Buffer)
  101.         cmd.Stdout = stdout
  102.         stderr := new(bytes.Buffer)
  103.         cmd.Stderr = stderr

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

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

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

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

  120.         exp := regexp.MustCompile(`suspicion\s+(.+)`)
  121.         matches := exp.FindStringSubmatch(outStr)
  122.         if len(matches) != 2 {
  123.                 return "", "false", fmt.Errorf("regexp match: %s", outStr)
  124.         }
  125.         return matches[1], "true", nil
  126. }
  127. func avast(ctx context.Context, filePath string) (string, string, error) {
  128.         cmd := exec.Command("ashcmd.exe", filePath)
  129.         stdout := new(bytes.Buffer)
  130.         cmd.Stdout = stdout
  131.         stderr := new(bytes.Buffer)
  132.         cmd.Stderr = stderr

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  240.         exp := regexp.MustCompile(filePath + ":" + ` ([^"]+)` + "FOUND")
  241.         matches := exp.FindStringSubmatch(outStr)
  242.         log.Println(matches)
  243.         if len(matches) != 2 {
  244.                 return "", "false", fmt.Errorf("regexp match: %s", outStr)
  245.         }
  246.         return matches[1], "true", nil
  247. }
  248. func ikarust(ctx context.Context, filePath string) (string, string, error) {
  249.         cmd := exec.Command("t3scan_w64.exe", filePath)
  250.         //stdout := new(bytes.Buffer)
  251.         //cmd.Stdout = stdout
  252.         //stderr := new(bytes.Buffer)
  253.         //cmd.Stderr = stderr

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  348.                 exitErr := &exec.ExitError{}
  349.                 if !errors.As(err, &exitErr) {
  350.                         return "", "false", fmt.Errorf("exec failed: %w", err)
  351.                 }
  352.         }
  353.         //
  354.         //// GBK -> UTF-8
  355.         //dec := simplifiedchinese.GBK.NewDecoder()
  356.         //outBytes, err := io.ReadAll(dec.Reader(bytes.NewReader(buf)))
  357.         //
  358.         //if err != nil {
  359.         //        return "", "false", fmt.Errorf("decode failed: %w", err)
  360.         //}
  361.         //outStr := string(outBytes)
  362.         //// 打开文件以供日志输出
  363.         ////file, err := os.OpenFile("logfile.txt", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
  364.         ////if err != nil {
  365.         ////        log.Fatal("无法打开日志文件:", err)
  366.         ////}
  367.         ////defer file.Close()
  368.         ////// 将日志输出到文件
  369.         ////log.SetOutput(file)
  370.         ////log.Println(outStr)
  371.         ////log.Println("-------------------------------------")
  372.         ////log.Println(string(buf))
  373.         //// 获取当前可执行文件的路径
  374.         //exePath, err := os.Executable()
  375.         //if err != nil {
  376.         //        fmt.Println("获取可执行文件路径失败:", err)
  377.         //        return "", "", nil
  378.         //}
  379.         //
  380.         //// 获取当前可执行文件所在的目录
  381.         //exeDir := filepath.Dir(exePath)
  382.         //// 构建要读取的文件的路径
  383.         //x := filepath.Join(exeDir, "file.log")
  384.         //
  385.         //// 读取文件内容
  386.         //content, err := os.Open(x)
  387.         //if err != nil {
  388.         //        fmt.Println("读取文件失败:", err)
  389.         //        return "", "", nil
  390.         //}
  391.         //
  392.         //// 打印文件内容
  393.         //fmt.Println("文件内容:")
  394.         //fmt.Println(string(content))
  395.         //if !strings.Contains(content, "infected with") {
  396.         //        return "", "false", nil // no virus
  397.         //}
  398.         // 文件路径
  399.         filePath1 := "C:\\Users\\CNIX\\Desktop\\drweb\\file.log"

  400.         // 打开文件
  401.         file, err := os.Open(filePath1)
  402.         if err != nil {
  403.                 fmt.Println("打开文件失败:", err)
  404.                 return "", "", nil
  405.         }
  406.         defer file.Close()

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

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

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

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

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

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

  433.                 }
  434.         }
  435.         return x, "true", nil
  436. }
  437. func Norton(ctx context.Context, filePath string) (string, string, error) {

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

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

  445.                 exitErr := &exec.ExitError{}
  446.                 if !errors.As(err, &exitErr) {
  447.                         return "", "false", fmt.Errorf("exec failed: %w", err)
  448.                 }
  449.         }
  450.         //cmd1 := exec.Command("MCUI32.exe /export C:\\Users\\CNIX\\Desktop\\norton\\log.txt")
  451.         //xx, err := cmd1.CombinedOutput()
  452.         //if err != nil {
  453.         //
  454.         //        exitErr := &exec.ExitError{}
  455.         //        if !errors.As(err, &exitErr) {
  456.         //                return "", "false", fmt.Errorf("exec failed: %w", err)
  457.         //        }
  458.         //}
  459.         //log.Println(xx)
  460.         // 文件路径
  461.         filePath1 := "C:\\Users\\CNIX\\Desktop\\norton\\log.txt"

  462.         // 打开文件
  463.         file, err := os.Open(filePath1)
  464.         if err != nil {
  465.                 fmt.Println("打开文件失败:", err)
  466.                 return "", "", nil
  467.         }
  468.         defer file.Close()

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

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

  478.         // 输出结果
  479.         if !match {
  480.                 return "", "false", nil // no virus
  481.         }
  482.         // 正则表达式模式
  483.         pattern1 := format2 + `(\S+) 检测方 自动防护,已阻止,已解决`

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

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

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

  495.                 }
  496.         }
  497.         return x, "true", nil
  498. }
  499. func Microsoft_Defender(ctx context.Context, filePath string) (string, string, error) {
  500.         //shell := "MpCmdRun.exe -Scan -ScanType 3 -DisableRemediation -File " + filePath
  501.         //filePath1 := "C:\Users\admin\Desktop\test\\tmp\\MicroKMS_v21.12.08_Beta.exe"
  502.         cmd := exec.Command("MpCmdRun.exe", "-Scan", "-ScanType", "3", "-File", filePath, "-DisableRemediation")
  503.         //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")
  504.         //stdout := new(bytes.Buffer)
  505.         //cmd.Stdout = stdout
  506.         //stderr := new(bytes.Buffer)
  507.         //cmd.Stderr = stderr
  508.         buf, err := cmd.CombinedOutput()
  509.         if err != nil {

  510.                 exitErr := &exec.ExitError{}
  511.                 if !errors.As(err, &exitErr) {
  512.                         return "", "false", fmt.Errorf("exec failed: %w", err)
  513.                 }
  514.         }
  515.         // GBK -> UTF-8
  516.         dec := simplifiedchinese.GBK.NewDecoder()
  517.         outBytes, err := io.ReadAll(dec.Reader(bytes.NewReader(buf)))
  518.         if err != nil {
  519.                 return "", "false", fmt.Errorf("decode failed: %w", err)
  520.         }
  521.         outStr := string(outBytes)
  522.         if !strings.Contains(outStr, "found 1 threats") {
  523.                 return "", "false", nil // no virus
  524.         }
  525.         exp := regexp.MustCompile(`Threat\s*:\s*([^[:space:]]+)`)
  526.         matches := exp.FindStringSubmatch(outStr)
  527.         log.Println(matches)
  528.         if len(matches) != 2 {
  529.                 return "", "false", fmt.Errorf("regexp match: %s", outStr)
  530.         }
  531.         return matches[1], "true", nil
  532. }
  533. func filterString(str string) string {
  534.         filteredStr := strings.ReplaceAll(str, "\r", "")
  535.         filteredStr = strings.ReplaceAll(filteredStr, "\t", "")
  536.         return filteredStr
  537. }
复制代码


评分

参与人数 1人气 +1 收起 理由
smz2011 + 1

查看全部评分

kafan988
发表于 2024-3-30 16:36:32 | 显示全部楼层
居然占了沙发
xyk001
 楼主| 发表于 2024-4-1 09:24:12 | 显示全部楼层

写脚本太菜了,没人看
zjzyhong
发表于 2024-4-1 20:28:57 | 显示全部楼层
小白问下,这个怎么用呢?
xyk001
 楼主| 发表于 2024-4-2 09:23:24 | 显示全部楼层
zjzyhong 发表于 2024-4-1 20:28
小白问下,这个怎么用呢?

需要把agent编译,分别在安装不同杀毒软件电脑上运行,通过服务端接口上传文件获取扫描结果。
lonesomexz
发表于 2024-4-2 14:09:37 | 显示全部楼层
感谢分享了
zjzyhong
发表于 2024-4-8 14:40:07 | 显示全部楼层
xyk001 发表于 2024-4-2 09:23
需要把agent编译,分别在安装不同杀毒软件电脑上运行,通过服务端接口上传文件获取扫描结果。

学习啦,谢谢回复。
您需要登录后才可以回帖 登录 | 快速注册

本版积分规则

手机版|杀毒软件|软件论坛| 卡饭论坛

Copyright © KaFan  KaFan.cn All Rights Reserved.

Powered by Discuz! X3.4( 沪ICP备2020031077号-2 ) GMT+8, 2024-5-10 01:16 , Processed in 0.128907 second(s), 17 queries .

卡饭网所发布的一切软件、样本、工具、文章等仅限用于学习和研究,不得将上述内容用于商业或者其他非法用途,否则产生的一切后果自负,本站信息来自网络,版权争议问题与本站无关,您必须在下载后的24小时之内从您的电脑中彻底删除上述信息,如有问题请通过邮件与我们联系。

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