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;
}
}
}

9 Responses to “A ICAP client code in C# to virus scan a file using Symantec Scan Server”

  1. Lirender Says:

    Vaibhav,

    Thanks so much for sharing. You source was very helpful as very little C# code was available for Scan Engine.

    Thanks again!

  2. eli zvik Says:

    Thanks a lot!!! i already had the “C” code – and you saved my a precious time of converting!!

    thanks again


  3. 0oThank’s for greate post.4s I compleatly disagree with last post . wtr
    паркетная доска 8m

  4. Karuna Says:

    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.

  5. geoff Says:

    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.

  6. Abdul Wakeel Says:

    MetaScan provide the core for scaning and uploading the file using asp.net

    try out opswhat.com

  7. madhu Says:

    I could not understand this coding….
    so pls give step by step procedure to scan the file in client side

  8. Sagar Says:

    Thanks a lot. Your source was very helpful to me.


  9. 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


Leave a comment