Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance YaraContext management with Singleton pattern and exception handling #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 28 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,49 +110,45 @@ static void Main(string[] args)
@"e:\speficic-samples\sample1.exe" // file
};

// Initialize yara context
using (YaraContext ctx = new YaraContext())

// Compile list of yara rules
CompiledRules rules = null;
using (var compiler = new Compiler())
{
// Compile list of yara rules
CompiledRules rules = null;
using (var compiler = new Compiler())
foreach (var yara in ruleFiles)
{
foreach (var yara in ruleFiles)
{
compiler.AddRuleFile(yara);
}
compiler.AddRuleFile(yara);
}

rules = compiler.Compile();
rules = compiler.Compile();

Console.WriteLine($"* Compiled");
}
Console.WriteLine($"* Compiled");
}

if (rules != null)
{
// Initialize the scanner
var scanner = new Scanner();
if (rules != null)
{
// Initialize the scanner
var scanner = new Scanner();

// Go through all samples
foreach (var sample in samples)
// Go through all samples
foreach (var sample in samples)
{
// If item is file, scan the file
if (File.Exists(sample))
{
// If item is file, scan the file
if (File.Exists(sample))
{
ScanFile(scanner, sample, rules);
}
// If item is directory, scan the directory
else
ScanFile(scanner, sample, rules);
}
// If item is directory, scan the directory
else
{
if (Directory.Exists(sample))
{
if (Directory.Exists(sample))
{
DirectoryInfo dirInfo = new DirectoryInfo(sample);
DirectoryInfo dirInfo = new DirectoryInfo(sample);

foreach (FileInfo fi in dirInfo.EnumerateFiles("*", SearchOption.AllDirectories))
ScanFile(scanner, fi.FullName, rules);
}
foreach (FileInfo fi in dirInfo.EnumerateFiles("*", SearchOption.AllDirectories))
ScanFile(scanner, fi.FullName, rules);
}
}

}

}
Expand Down
24 changes: 10 additions & 14 deletions Samples/YaraInteractive/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,22 @@ class Program
{
static void Main(string[] args)
{
using (var ctx = new YaraContext())
{
Console.WriteLine("# Welcome to Yara Interactive Console...");
Console.WriteLine("# Welcome to Yara Interactive Console...");

while (true)
{
Console.Write("> ");
while (true)
{
Console.Write("> ");

string command = Console.ReadLine();
string command = Console.ReadLine();

if (string.IsNullOrWhiteSpace(command))
continue;
if (string.IsNullOrWhiteSpace(command))
continue;

bool isManagedCmd = CmdHandler.ExecuteCmd(command);
bool isManagedCmd = CmdHandler.ExecuteCmd(command);

if (!isManagedCmd)
Console.WriteLine(":Err: Unknown command...");
}
if (!isManagedCmd)
Console.WriteLine(":Err: Unknown command...");
}
}

}
}
Loading