Exemplo n.º 1
0
        public void SetUp()
        {
            if (!TestHelpers.TestFilesDirectoryExists())
                Assert.Ignore("TestFiles directory is missing.");

            _clamEngine = new ClamEngine();
        }
Exemplo n.º 2
0
        public static void Main(string[] args)
        {
            using (ClamEngine e = new ClamEngine())
            {
                foreach (string file in args)
                {
                    ClamResult result = e.ScanFile(file);                     //pretty simple!

                    if (result != null && result.ReturnCode == ClamReturnCode.CL_VIRUS)
                    {
                        Console.WriteLine("Found: " + result.VirusName);
                    }
                    else
                    {
                        Console.WriteLine("File Clean!");
                    }
                }
            }             //engine is disposed of here and the allocated engine freed

            //these test clamd bindings
            using (ClamdSession session = new ClamdSession("127.0.0.1", 3310))
            {
                using (ClamdManager manager = new ClamdManager(session))
                {
                    Console.WriteLine(manager.GetVersion());
                    Console.WriteLine(manager.ScanWithArchiveSupport("/home/bperry/tmp"));
                }
            }
        }
        public void SetUpClamEngine()
        {
            if (!TestHelpers.NativeLibraryExists())
                Assert.Ignore("libclamav dynamic library is missing from the unit test binary directory.");

            _clamEngine = new ClamEngine();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Asynchronously scans a file for viruses.
        /// </summary>
        /// <param name="engine">ClamAV engine instance.</param>
        /// <param name="path">Path to the file to be scanned.</param>
        /// <param name="options">Scan options.</param>
        /// <returns>The task object representing the asynchronous operation. The Result property on the task returns a scan result.</returns>
        public static async Task <FileScanResult> ScanFileAsync(this ClamEngine engine, string path, ScanOptions options)
        {
            var virusName  = string.Empty;
            var scanResult = await Task.Factory.StartNew(() => engine.ScanFile(path, options, out virusName));

            return(new FileScanResult(path, scanResult == ScanResult.Virus, virusName));
        }
Exemplo n.º 5
0
 public void TearDownClamEngine()
 {
     if (_clamEngine != null)
     {
         _clamEngine.Dispose();
         _clamEngine = null;
     }
 }
 public void TearDownClamEngine()
 {
     if (_clamEngine != null)
     {
         _clamEngine.Dispose();
         _clamEngine = null;
     }
 }
Exemplo n.º 7
0
        public void SetUp()
        {
            if (!TestHelpers.TestFilesDirectoryExists())
            {
                Assert.Ignore("TestFiles directory is missing.");
            }

            _clamEngine = new ClamEngine();
        }
Exemplo n.º 8
0
        public void SetUpClamEngine()
        {
            if (!TestHelpers.NativeLibraryExists())
            {
                Assert.Ignore("libclamav dynamic library is missing from the unit test binary directory.");
            }

            _clamEngine = new ClamEngine();
        }
Exemplo n.º 9
0
        /// <summary>
        /// Creates a new ClamAV engine instance.
        /// </summary>
        protected override void ProcessRecord()
        {
            var clamEngine = new ClamEngine(DebugMode.IsPresent);

            if (!String.IsNullOrEmpty(WithDatabase))
                clamEngine.LoadDatabase(WithDatabase);

            WriteObject(clamEngine);
        }
        /// <summary>
        /// Creates a new ClamAV engine instance.
        /// </summary>
        protected override void ProcessRecord()
        {
            var clamEngine = new ClamEngine(DebugMode.IsPresent);

            if (!String.IsNullOrEmpty(WithDatabase))
            {
                clamEngine.LoadDatabase(WithDatabase);
            }

            WriteObject(clamEngine);
        }
Exemplo n.º 11
0
        public ScanForm()
        {
            // Windows Forms setup.
            InitializeComponent();

            this.Icon = SystemIcons.Shield;

            // Instantiate ClamAV engine.
            _clamAV = new ClamEngine();

            logTextBox.AppendText("ClamAV Version: " + _clamAV.Version + "\r\n");

            logTextBox.AppendText("Loading signatures from default location...\r\n");

            // Disable user input while loading.
            scanPathTextBox.Enabled = false;
            browseButton.Enabled = false;
            scanButton.Enabled = false;

            // Load signatures on a new thread.
            Thread setupThread = new Thread(() =>
                {
                    try
                    {
                        // Load database from the default location.
                        _clamAV.LoadDatabase();

                        // Signal database load is complete.
                        this.Invoke(new Action(() =>
                        {
                            logTextBox.AppendText("Signatures loaded.\r\n");

                            scanPathTextBox.Enabled = true;
                            browseButton.Enabled = true;
                            scanButton.Enabled = true;
                        }));
                    }
                    catch (Exception ex)
                    {
                        // Report exception to user.
                        this.Invoke(new Action(() =>
                        {
                            logTextBox.AppendText("Failed to load signatures:\r\n");
                            logTextBox.AppendText(ex.ToString());
                        }));
                    }
                });

            // Begin work.
            setupThread.Start();
        }
Exemplo n.º 12
0
        public ScanForm()
        {
            // Windows Forms setup.
            InitializeComponent();

            this.Icon = SystemIcons.Shield;

            // Instantiate ClamAV engine.
            _clamAV = new ClamEngine();

            logTextBox.AppendText("ClamAV Version: " + ClamEngine.Version + "\r\n");

            logTextBox.AppendText("Loading signatures from default location...\r\n");

            // Disable user input while loading.
            scanPathTextBox.Enabled = false;
            browseButton.Enabled    = false;
            scanButton.Enabled      = false;

            // Load signatures on a new thread.
            Thread setupThread = new Thread(() =>
            {
                try
                {
                    // Load database from the default location.
                    _clamAV.LoadDatabase();

                    // Signal database load is complete.
                    this.Invoke(new Action(() =>
                    {
                        logTextBox.AppendText("Signatures loaded.\r\n");

                        scanPathTextBox.Enabled = true;
                        browseButton.Enabled    = true;
                        scanButton.Enabled      = true;
                    }));
                }
                catch (Exception ex)
                {
                    // Report exception to user.
                    this.Invoke(new Action(() =>
                    {
                        logTextBox.AppendText("Failed to load signatures:\r\n");
                        logTextBox.AppendText(ex.ToString());
                    }));
                }
            });

            // Begin work.
            setupThread.Start();
        }
Exemplo n.º 13
0
        /// <summary>
        /// Asynchronously scans a directory for viruses, optionally recursing into subdirectories.
        /// </summary>
        /// <param name="engine">ClamAV engine instance.</param>
        /// <param name="path">Path to scan.</param>
        /// <param name="options">Scan options.</param>
        /// <param name="recurse">Whether to enter subdirectories.</param>
        /// <param name="maxDepth">Maximum depth to scan, or zero for unlimited.</param>
        /// <returns>The task object representing the asynchronous operation. The Result property on the task returns the scan results.</returns>
        public static async Task <IEnumerable <FileScanResult> > ScanDirectoryAsync(this ClamEngine engine, string path, ScanOptions options, bool recurse, int maxDepth)
        {
            var scanQueue = new Queue <string>();

            var pathStack = new Stack <Tuple <string /* path */, int /* depth */> >();

            // Push the starting directory onto the stack.
            pathStack.Push(Tuple.Create(path, 1));

            while (pathStack.Count > 0)
            {
                var stackState = pathStack.Pop();

                var currentPath  = stackState.Item1;
                var currentDepth = stackState.Item2;

                var attributes = File.GetAttributes(currentPath);

                // If we're in a directory, push all files and subdirectories to the stack.
                if ((attributes & FileAttributes.Directory) == FileAttributes.Directory)
                {
                    // Check if we're not about to go too deep.
                    if (maxDepth == 0 || currentDepth < maxDepth)
                    {
                        var subFiles = Directory.GetFiles(currentPath);
                        foreach (var file in subFiles)
                        {
                            pathStack.Push(Tuple.Create(file, currentDepth + 1));
                        }

                        var subDirectories = Directory.GetDirectories(currentPath);
                        foreach (var directory in subDirectories)
                        {
                            pathStack.Push(Tuple.Create(directory, currentDepth + 1));
                        }
                    }
                }
                // If this is a file, enqueue it for scanning.
                else
                {
                    scanQueue.Enqueue(currentPath);
                }
            }

            var scanTasks   = scanQueue.Select(engine.ScanFileAsync);
            var scanResults = await Task.WhenAll(scanTasks);

            return(scanResults);
        }
Exemplo n.º 14
0
        public static void Main(string[] args)
        {
            using (ClamEngine e = new ClamEngine())
            {
                foreach (string file in args)
                {
                    ClamResult result = e.ScanFile(file);                     //pretty simple!

                    if (result != null && result.ReturnCode == ClamReturnCode.CL_VIRUS)
                    {
                        Console.WriteLine("Found: " + result.VirusName);
                    }
                    else
                    {
                        Console.WriteLine("File Clean!");
                    }
                }
            }             //engine is disposed of here and the allocated engine freed automatically
        }
Exemplo n.º 15
0
        public MainWindowViewModel()
        {
            ScanCommand = new RelayCommand(OnScan, CanScan);
            LoadCommand = new RelayCommand(OnLoad);

            try
            {
                Busy = true;

                _clamEngine = new ClamEngine();
                WriteLogLine("ClamAV version " + ClamEngine.Version);
            }
            catch (Exception ex)
            {
                WriteLogLine("Failed to load engine:");
                WriteLogLine(ex.ToString());
            }
            finally
            {
                Busy = false;
            }
        }
Exemplo n.º 16
0
        public Main()
        {
            InitializeComponent();
            clamAV = new ClamEngine();
            Text   = Text + " ClamAV Engine Version : " + ClamEngine.Version;
            Icon   = SystemIcons.Shield;
            Thread SetupThread = new Thread(() =>
            {
                try
                {
                    clamAV.LoadDatabase(AppDomain.CurrentDomain.BaseDirectory + @"\database\main.cvd", LoadOptions.OfficialOnly);
                }
                catch (Exception ex)
                {
                    if (InvokeRequired)
                    {
                        Text = "Failed to load signature";
                    }
                }
            });

            SetupThread.Start();
        }
Exemplo n.º 17
0
 /// <summary>
 /// Asynchronously load databases from the default hardcoded path using standard options.
 /// </summary>
 /// <param name="engine">ClamAV engine instance.</param>
 /// <returns>The task object representing the asynchronous operation.</returns>
 public static async Task LoadDatabaseAsync(this ClamEngine engine)
 {
     await Task.Factory.StartNew(engine.LoadDatabase);
 }
Exemplo n.º 18
0
 /// <summary>
 /// Asynchronously loads a database file or directory into the engine.
 /// </summary>
 /// <param name="engine">ClamAV engine instance.</param>
 /// <param name="path">Path to the database file or a directory containing database files.</param>
 /// <param name="options">Options with which to load the database.</param>
 /// <returns>The task object representing the asynchronous operation.</returns>
 public static async Task LoadDatabaseAsync(this ClamEngine engine, string path, LoadOptions options)
 {
     await Task.Factory.StartNew(() => engine.LoadDatabase(path, options));
 }
 public void SetUpClamEngine()
 {
     _clamEngine = new ClamEngine();
 }
Exemplo n.º 20
0
 /// <summary>
 /// Asynchronously scans a file for viruses with the default scan options.
 /// </summary>
 /// <param name="engine">ClamAV engine instance.</param>
 /// <param name="path">Path to the file to be scanned.</param>
 /// <returns>The task object representing the asynchronous operation. The Result property on the task returns a scan result.</returns>
 public static async Task <FileScanResult> ScanFileAsync(this ClamEngine engine, string path)
 {
     return(await engine.ScanFileAsync(path, ScanOptions.StandardOptions));
 }
Exemplo n.º 21
0
        static void Main(string[] args)
        {
            //Always wrap anything that could possibly throw an exception.
            try
            {
                Console.WriteLine("MClam scan file example.");
                Console.WriteLine("========================");
                Console.WriteLine();

                Helpers.InitializeClamAV();

                //Use "using" to avoid memory leak when exception is thrown.
                Console.Write("Creating new engine...");
                using (var cl_engine = ClamEngine.CreateNew())
                {
                    Console.WriteLine("SUCCESS!");

                    //Load database files from directory.
                    Console.Write("Loading CVD into engine...");
                    cl_engine.LoadCvdDirectory(@"D:\MClam\CVD");
                    Console.WriteLine("SUCCESS!");

                    //Compile engine.
                    Console.Write("Compiling engine...");
                    cl_engine.Compile();
                    Console.WriteLine("SUCCESS!");

                    //Open a file to scan.
                    Console.Write("Opening a file...");
                    var fileToScan = FileEntry.Open(@"D:\MClam\eicar.com");
                    Console.WriteLine("SUCCESS!");

                    //Scan the file.
                    Console.Write("Scanning file...");
                    var result = cl_engine.ScanFile(fileToScan);
                    Console.WriteLine("COMPLETED!");
                    Console.WriteLine();

                    //Show file scan info.
                    Console.Write("FullPath: ");
                    Console.WriteLine(result.FullPath);
                    Console.Write("IsVirus: ");
                    Console.WriteLine(result.IsVirus);
                    Console.Write("Scanned: ");
                    Console.WriteLine(result.Scanned);
                    Console.Write("VirusName: ");
                    Console.WriteLine(result.VirusName);
                    Console.WriteLine();

                    //Completed!
                    Console.WriteLine("Scan has been completed!");
                }
            }
            catch (Exception ex)
            {
                //We've got an exception. Write it on screen.
                Console.WriteLine(ex.ToString());
            }
            //Pause here...
            Console.Write("Press any key to exit...");
            Console.ReadKey();
        }
Exemplo n.º 22
0
        public MainWindowViewModel()
        {
            ScanCommand = new RelayCommand(OnScan, CanScan);
            LoadCommand = new RelayCommand(OnLoad);

            try
            {
                Busy = true;

                _clamEngine = new ClamEngine();
                WriteLogLine("ClamAV version " + ClamEngine.Version);
            }
            catch (Exception ex)
            {
                WriteLogLine("Failed to load engine:");
                WriteLogLine(ex.ToString());
            }
            finally
            {
                Busy = false;
            }
        }
Exemplo n.º 23
0
 /// <summary>
 /// Asynchronously scans a directory for viruses, recursing into subdirectories.
 /// </summary>
 /// <param name="engine">ClamAV engine instance.</param>
 /// <param name="path">Path to scan.</param>
 /// <param name="options">Scan options.</param>
 /// <returns>The task object representing the asynchronous operation. The Result property on the task returns the scan results.</returns>
 public static async Task <IEnumerable <FileScanResult> > ScanDirectoryAsync(this ClamEngine engine, string path, ScanOptions options)
 {
     return(await engine.ScanDirectoryAsync(path, options, true, 0));
 }