A ICAP client code in C# to virus scan a file using Symantec Scan Server
November 19, 2007
public class VirusCheck
{
private string retDesc;
private Socket soc;
public string ReturnDescription
{
get { return retDesc; }
set { retDesc = value; }
}
public VirusCheck()
{
soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
public int ScanStream(string scanServer, int scanPort, string fileName, byte[] fileStream)
{
byte[] buffer = new byte[1024];
int iRx;
try
{
if (scanPort == 0)
scanPort = 1344;
System.Net.IPAddress remoteIPAddress = System.Net.IPAddress.Parse(scanServer);
System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(remoteIPAddress, scanPort);
soc.Connect(remoteEndPoint);
if (soc.Connected)
{
string cmd = “RESPMOD icap://” + scanServer + “:” + scanPort + “/AVSCAN ICAP/1.0\n”;
cmd = cmd + “Host: ” + scanServer + “:” + scanPort + “\n”;
cmd = cmd + “Allow: 204\n”;
cmd = cmd + “Encapsulated: req-hdr=0, res-hdr=84, res-body=131\n”;
cmd = cmd + “\n”;
cmd = cmd + “GET http://” + scanServer + “/” + fileName + ” HTTP/1.1\n”;
cmd = cmd + “Host: ” + scanServer + “\n”;
cmd = cmd + “\n”;
cmd = cmd + “HTTP/1.1 200 OK\n”;
cmd = cmd + “Transfer-Encoding: chunked\n”;
cmd = cmd + “\n”;
cmd = cmd + String.Format(“{0:X2}”, fileStream.Length) + “\n”;
soc.Send(System.Text.Encoding.ASCII.GetBytes(cmd));
soc.Send(fileStream);
cmd = “\n”;
cmd = cmd + “\n”;
cmd = cmd + “0\n”;
cmd = cmd + “\n”;
soc.Send(System.Text.Encoding.ASCII.GetBytes(cmd));
while ((soc.Connected) && ((iRx = soc.Receive(buffer)) > 0))
{
char[] chars = new char[iRx];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(buffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
retDesc = retDesc + szData;
}
soc.Close();
if (retDesc.Contains(“X-Violations-Found:”))
return 1;
else
return 0;
}
else
return -1;
}
catch (Exception e)
{
if (soc != null)
soc.Close();
retDesc = e.Message;
return -1;
}
}
}
February 12, 2008 at 6:54 pm
Vaibhav,
Thanks so much for sharing. You source was very helpful as very little C# code was available for Scan Engine.
Thanks again!
July 31, 2008 at 12:52 pm
Thanks a lot!!! i already had the “C” code – and you saved my a precious time of converting!!
thanks again
August 24, 2008 at 4:56 pm
0oThank’s for greate post.4s I compleatly disagree with last post . wtr
паркетная доска 8m
September 22, 2009 at 10:24 pm
Totally cool. I needed a c# ICAP client for Metascan and had this up and running in an hour with your code. Thanks so much for sharing. Very, very cool.
October 8, 2010 at 1:43 pm
Thanks for this. But when I try to run the code I get the following exception message on the 2nd soc.Send(Encoding.ASCII.GetBytes(cmd)); call. An established connection was aborted by the software in your host machine. I’ve disabled windows defender and my antivirus software but still no luck. Any ideas what else might cause this? Thanks again.
January 3, 2011 at 7:34 am
MetaScan provide the core for scaning and uploading the file using asp.net
try out opswhat.com
April 21, 2011 at 6:10 am
I could not understand this coding….
so pls give step by step procedure to scan the file in client side
June 9, 2014 at 6:04 pm
Thanks a lot. Your source was very helpful to me.
April 9, 2016 at 8:33 am
I am sorry you hurt yourself baby, but it is the typical Clark uncoordination at work. It’s an old family trait. Try and be more careful and Vicki and Click http://s.intmainreturn0.com/people3091630