Files
stellarbear-YaraSharp/YaraTest/Program.cs
T
stellarbears dee6cfd5cb 1.1.3 Release
2018-05-17 16:29:31 +03:00

60 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using YaraSharp;
namespace YaraTest
{
class Program
{
static void Main(string[] args)
{
// All API calls happens here
YaraSharp.CYaraSharp YSInstance = new CYaraSharp();
// Declare external variables (could be null)
Dictionary<string, object> Externals = new Dictionary<string, object>()
{
{ "filename", string.Empty },
{ "filepath", string.Empty },
{ "extension", string.Empty }
};
// Errors occured during rule compilation: ignored_file : List<reasons>
Dictionary<string, List<string>> Errors = new Dictionary<string, List<string>>();
// Context is where yara is initialized
// From yr_initialize() to yr_finalize()
using (YaraSharp.CContext YSContext = new YaraSharp.CContext())
{
// Compiling rules
using (YaraSharp.CRules YSRules = YSInstance.CompileFromFiles(Directory.GetFiles(Path.Combine(Directory.GetCurrentDirectory(), "..\\..\\signatures"), "*.yar", SearchOption.AllDirectories).ToList(), Externals, out Errors))
{
// Some file to test yara rules
string Filename = @"\\?\<filepath>";
// Get matches
List<YaraSharp.CMatches> Matches = YSInstance.ScanFile(Filename, YSRules,
new Dictionary<string, object>()
{
{ "filename", Alphaleonis.Win32.Filesystem.Path.GetFileName(Filename) },
{ "filepath", Alphaleonis.Win32.Filesystem.Path.GetFullPath(Filename) },
{ "extension", Alphaleonis.Win32.Filesystem.Path.GetExtension(Filename) }
},
0);
// Iterate over matches
foreach (YaraSharp.CMatches Match in Matches)
{
//...
}
}
// Log errors
}
}
}
}