【发布时间】:2016-01-28 05:58:31
【问题描述】:
我正在尝试对 ASP.Net Web 应用程序中上传的文件运行防病毒扫描。我们正在使用 Sophos,因此可以访问他们的命令行 API sav32cli。在我使用的代码中:
Process proc = new Process();
proc.StartInfo.FileName = @"C:\Program Files (x86)\Sophos\Sophos Anti-Virus\sav32cli.exe";
proc.StartInfo.Arguments = @"-remove -nc " + SavedFile;
proc.StartInfo.Verb = "runas";
proc.Start();
proc.WaitForExit();
int exitCode = proc.ExitCode;
当单步执行代码时,当附加到开发服务器上的w3wp 进程时,代码只是从一行跳转到下一行,似乎什么也没做。从开发服务器上的代码运行时,它会按预期执行扫描文件并在被视为病毒时删除。
服务器运行 IIS 8.0,应用程序内置于 .Net Framework 4。按照这些说明,我已更改机器配置以允许进程以 SYSTEM 帐户运行。 https://support.microsoft.com/en-us/kb/317012#%2Fen-us%2Fkb%2F317012
<processModel userName="SYSTEM" password="AutoGenerate" />
我有什么遗漏吗?这种实施的最佳做法是什么?
编辑:调用时,Process 返回 2(错误停止执行)的 ExitCode,而不是预期的 0(扫描有效,无病毒)或 3(扫描有效) ,发现病毒)。
编辑 2:根据下面的评论,我将代码更改为:
Process proc = new Process();
proc.StartInfo.FileName = @"C:\Program Files (x86)\Sophos\Sophos Anti-Virus\sav32cli.exe";
proc.StartInfo.Arguments = @"-remove -nc " + SavedFile;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
StringBuilder output = new StringBuilder();
while (!proc.StandardOutput.EndOfStream)
{
string line = proc.StandardOutput.ReadLine();
output.AppendLine(line);
}
proc.WaitForExit();
int exitCode = proc.ExitCode;
ASPxMemo2.Text = exitCode.ToString() + Environment.NewLine + output.ToString();
output 在 IIS 上运行时始终为空,但从代码运行时会正确填充。
编辑 3:我们没有查看 StandardOutput,而是查看了 StandardError,它发现了这个错误:
错误初始化检测引擎 [0xa0040200] (可能是用户管理员权限不足。)
目前我们将转向另一种病毒检查方法,但如果有人有的话,我们仍然想知道一个可能的解决方案。
【问题讨论】:
-
“进程返回一个 ExitCode” - 只是 read the process's output...
-
进程通过 IIS 运行时输出为空白,但从本地代码运行时正确填充。
-
您设置的 IIS 用户还需要 Sophos 路径的管理员权限。我建议不要使用任何 IIS 内置用户,并让系统管理员创建一个可用于此任务的服务帐户
-
第一个问题:在服务环境中是否支持sophos。他们支持这个配置吗?无论如何,您可以编写一个小的自定义网关 Windows 服务,该服务以足够的权限运行,可以与这个 sophos exe 一起使用。然后,您可以使用更简单的方法(例如 HTTP/REST/WCF/Remoting 等)从 IIS 与该服务进行通信,因此不需要 IIS 和该服务之间的特殊权限。
-
通常这种错误可以通过使用Process Monitor来解决。它可以跟踪您启动的
sav32cli.exe进程的所有文件/注册表/进程/网络活动。您将看到哪些请求失败。你能创建痕迹吗?
标签: c# asp.net iis process antivirus