You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过代码在ElectronJS应用中检测Mac是否安装杀毒软件且为最新版

Mac端Electron应用杀毒软件检测实现方案

一、检测逻辑说明

Mac没有Windows系统内置的SecurityCenter2统一杀毒产品查询接口,我们通过匹配系统内杀毒软件的通用特征实现无预设全量名单的检测,全程代码自动化执行无需人工介入。

二、核心检测命令

1. 杀毒软件存在性检测

执行以下任意命令返回非空结果即可判定存在已安装的杀毒软件,通过通用关键词匹配覆盖绝大多数主流杀毒产品,无需预设完整名单:

  • 检测系统安全扩展:systemextensionsctl list | grep -iE "antivirus|security|endpoint|protect|防病毒"
  • 检测后台常驻服务:ls /Library/LaunchDaemons/ | grep -iE "antivirus|com.*av|com.*protect|com.*security|com.*endpoint"
  • 检测应用目录安装包:ls /Applications/ | grep -iE "antivirus|avast|avg|kaspersky|symantec|microsoft defender|趋势|火绒|protect|endpoint"

2. 杀毒软件版本检测

不同杀毒软件的版本查询逻辑不同,以下是主流产品的版本查询命令,拿到本地版本后和你内置维护的对应软件最新版本号对比即可判断是否为最新:

  • 微软Defender:/Applications/Microsoft\ Defender.app/Contents/MacOS/wdav --version | grep -oE "version [0-9.]+" | awk '{print $2}'
  • Avast:defaults read ~/Library/Preferences/com.avast.Avast.plist LastUpdateVersion 2>/dev/null
  • 卡巴斯基:defaults read /Applications/Kaspersky\ Internet\ Security.app/Contents/Info.plist CFBundleShortVersionString 2>/dev/null
  • 通用应用版本查询:defaults read /Applications/[杀毒软件包名].app/Contents/Info.plist CFBundleShortVersionString 2>/dev/null

三、Electron代码实现

通过Node.js内置的child_process模块执行上述shell命令即可,示例代码如下:

const { exec } = require('child_process');
const util = require('util');
const execPromise = util.promisify(exec);

// 检测是否存在已安装的杀毒软件
async function hasAntivirus() {
  const detectCommands = [
    'systemextensionsctl list | grep -iE "antivirus|security|endpoint|protect"',
    'ls /Library/LaunchDaemons/ | grep -iE "antivirus|com.*av|com.*protect|com.*endpoint"',
    'ls /Applications/ | grep -iE "antivirus|avast|avg|kaspersky|symantec|defender|protect|endpoint"'
  ];

  for (const cmd of detectCommands) {
    try {
      const { stdout } = await execPromise(cmd);
      if (stdout.trim().length) return true;
    } catch (err) {
      // 无匹配结果时命令返回非0退出码,直接忽略即可
    }
  }
  return false;
}

// 示例:检测微软Defender是否为最新版本,可扩展其他杀毒软件逻辑
async function isAntivirusLatest() {
  try {
    // 获取本地安装的Defender版本号
    const { stdout: localVerOutput } = await execPromise(
      '/Applications/Microsoft\\ Defender.app/Contents/MacOS/wdav --version | grep -oE "version [0-9.]+" | awk \'{print $2}\''
    );
    const localVersion = localVerOutput.trim();
    // 这里替换为你维护的对应杀毒软件最新版本号,可定期更新内置规则
    const officialLatestVersion = '10.1.2306.0';

    // 版本号大小对比
    const localSegments = localVersion.split('.').map(Number);
    const latestSegments = officialLatestVersion.split('.').map(Number);
    for (let i = 0; i < Math.max(localSegments.length, latestSegments.length); i++) {
      const local = localSegments[i] || 0;
      const latest = latestSegments[i] || 0;
      if (local < latest) return false;
      if (local > latest) return true;
    }
    return true;
  } catch (err) {
    // 未安装对应杀毒软件或查询失败返回null
    return null;
  }
}

四、注意事项

  • 打包Electron应用时如果开启了沙箱,需要在 entitlements 配置中添加com.apple.security.cs.allow-unsigned-executable-memory权限,避免命令执行被拦截。
  • 可根据业务需求扩展关键词匹配规则,覆盖更多小众杀毒软件的特征,无需维护全量软件名单。

内容的提问来源于stack exchange,提问作者Questioner Answerer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 07:15:06