How to use escape characters in a string in C#?

Asked

Viewed 487 times

4

I am unable to scan a file that contains the EICAR (Standard Test File for Antivirus Scanning Methods) characters, as it contains escape characters such as " ", "()", "[]". I need a help to identify these characters in my program as being normal within a string, as other common ones like "a", "b", "1", "2", etc. Follow the code below:

string[] Lista = new string[] { "X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*"};

string dir = @"C:\Program Files (x86)";

private void btnScan_Click(object sender, EventArgs e)
{
   List<string> busca= Directory.GetFiles(dir, "*.dll*", SearchOption.AllDirectories).ToList();                  
   foreach (string item in busca)
   {                                              
     StreamReader stream = new StreamReader(item);
     string ler = stream.ReadToEnd();
            foreach (string st in Lista)
            {
              if (Regex.IsMatch(ler, st))
              {
                btnDelete.Visible = true;
                btnQuarentena.Visible = true;
              }                             
            }
   }
}

I’ve tried to put the string EICAR in this way, but did not help:

string[] Lista = new string[] { @"X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*"};

Visual Studio always returns me the same error, of exhaust, affected by the characters " ", "[]" and "()".

1 answer

4


There is a problem there. You are trying to read a file that is essentially a binary as if it were text. This is not going to work. You need to use the class BinaryReader. Alternatively you can use the File.ReadAllBytes().

byte[] array = File.ReadAllBytes(@"C:\programa.exe");

You will play the content in a buffer that is nothing more than a array of bytes (byte[]) with the method ReadBytes(). In this way any character is allowed.

A byte array allows many of the operations allowed in strings, after all a string at the bottom is a array of characters. Of course you can’t use Regex on it. Perhaps by luck, so you will achieve more efficiency practicing the use of raw techniques.

var path = @"C:\programa.exe"
BinaryReader reader = new BinaryReader(path);
//não é muito elegante, pode dar problema em casos extremos
//mas tentar carregar arquivos inteiros na memória sempre pode dar problema
//truque para ler todos os bytes até o final
byte[] buffer = reader.ReadBytes((int)new FileInfo(path).Length));
reader.Close();

There is the alternative to convert the content to Base64 and play in a string. But I doubt you’ll be able to do anything useful with it.

There is also the alternative to read the file with stream generic crude:

var fs = new FileStream(@"C:\programa.exe", FileMode.OpenOrCreate, FileAccess.Read);
byte[] buffer= new byte[fs.Length];
fs.Read(buffer, 0, Convert.ToInt32(fs.Length);
fs.Close();

Note that I did the access in an inappropriate way following the line of what you had already done. The ideal is to use the statement using which ensures the closure of the stream when he finishes his operation. So:

using (var fs = new FileStream(@"C:\programa.exe", FileMode.OpenOrCreate, FileAccess.Read)) {
    byte[] buffer= new byte[fs.Length];
    fs.Read(buffer, 0, Convert.ToInt32(fs.Length);
}

I put in the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.