Пример #1
0
    static void Main(string[] args)
    {
        Task.Run(async () =>
        {
            var clam = new ClamClient("localhost", 3310);
            var scanResult = await clam.ScanFileOnServerAsync("C:\\test.txt");  //any file you would like!

            switch (scanResult.Result)
            {
                case ClamScanResults.Clean:
                    Console.WriteLine("The file is clean!");
                    break;
                case ClamScanResults.VirusDetected:
                    Console.WriteLine("Virus Found!");
                    Console.WriteLine("Virus name: {0}", scanResult.InfectedFiles.First().VirusName);
                    break;
                case ClamScanResults.Error:
                    Console.WriteLine("Woah an error occured! Error: {0}", scanResult.RawResult);
                    break;
            }
        }).Wait();
    }
        void wlmailApiInit_OnInit()
        {
            wlmailApiCore = new NktWLMailApi.WLMailApi();
            folderManager = new NktWLMailStore.FolderManager();
            accountManager = new NktWLMailStore.MailAccountManager();

            utils = new Utils(wlmailApiCore, folderManager, accountManager);

            this.clamClient = new ClamClient(CLAM_HOST, CLAM_PORT);

            this.wlmailApiCore.OnDatabaseChange += new NktWLMailApi.IWLMailApiEvents_OnDatabaseChangeEventHandler(wlmailApiCore_OnDatabaseChange);
        }
Пример #3
0
        public static void ScanUploads(Config config, TeknikEntities db)
        {
            Output(string.Format("[{0}] Started Virus Scan.", DateTime.Now));
            List<Upload> uploads = db.Uploads.ToList();

            // Initialize ClamAV
            ClamClient clam = new ClamClient(config.UploadConfig.ClamServer, config.UploadConfig.ClamPort);
            clam.MaxStreamSize = config.UploadConfig.MaxUploadSize;

            int totalCount = uploads.Count();
            int totalScans = 0;
            int totalClean = 0;
            int totalViruses = 0;
            foreach (Upload upload in uploads)
            {
                totalScans++;
                string subDir = upload.FileName[0].ToString();
                string filePath = Path.Combine(config.UploadConfig.UploadDirectory, subDir, upload.FileName);
                if (File.Exists(filePath))
                {
                    // Read in the file
                    byte[] data = File.ReadAllBytes(filePath);
                    // If the IV is set, and Key is set, then decrypt it
                    if (!string.IsNullOrEmpty(upload.Key) && !string.IsNullOrEmpty(upload.IV))
                    {
                        // Decrypt the data
                        data = AES.Decrypt(data, upload.Key, upload.IV);
                    }

                    // We have the data, let's scan it
                    ClamScanResult scanResult = clam.SendAndScanFile(data);

                    switch (scanResult.Result)
                    {
                        case ClamScanResults.Clean:
                            totalClean++;
                            string cleanMsg = string.Format("[{0}] Clean Scan: {1}/{2} Scanned | {3} - {4}", DateTime.Now, totalScans, totalCount, upload.Url, upload.FileName);
                            Output(cleanMsg);
                            break;
                        case ClamScanResults.VirusDetected:
                            totalViruses++;
                            string msg = string.Format("[{0}] Virus Detected: {1} - {2} - {3}", DateTime.Now, upload.Url, upload.FileName, scanResult.InfectedFiles.First().VirusName);
                            File.AppendAllLines(virusFile, new List<string> { msg });
                            Output(msg);
                            // Delete from the DB
                            db.Uploads.Remove(upload);
                            db.SaveChanges();

                            // Delete the File
                            if (File.Exists(filePath))
                            {
                                File.Delete(filePath);
                            }
                            break;
                        case ClamScanResults.Error:
                            string errorMsg = string.Format("[{0}] Scan Error: {1}", DateTime.Now, scanResult.RawResult);
                            File.AppendAllLines(errorFile, new List<string> { errorMsg });
                            Output(errorMsg);
                            break;
                        case ClamScanResults.Unknown:
                            string unkMsg = string.Format("[{0}] Unknown Scan Result: {1}", DateTime.Now, scanResult.RawResult);
                            File.AppendAllLines(errorFile, new List<string> { unkMsg });
                            Output(unkMsg);
                            break;
                    }
                }
            }

            if (totalViruses > 0)
            {
                // Add to transparency report if any were found
                Takedown report = db.Takedowns.Create();
                report.Requester = TAKEDOWN_REPORTER;
                report.RequesterContact = config.SupportEmail;
                report.DateRequested = DateTime.Now;
                report.Reason = "Malware Found";
                report.ActionTaken = string.Format("{0} Uploads removed", totalViruses);
                report.DateActionTaken = DateTime.Now;
                db.Takedowns.Add(report);
                db.SaveChanges();
            }

            Output(string.Format("Scanning Complete.  {0} Scanned | {1} Viruses Found | {2} Total Files", totalScans, totalViruses, totalCount));
        }
Пример #4
0
        void bgw_scanDoWork(object sender, DoWorkEventArgs e)
        {
            try
            {

                if (scanvalue == 2)
                {
                    lstSeledtedScanDrivedetails = JsonConvert.DeserializeObject<List<SelectedDriveeDetails>>(selectedDriveList);

                    foreach (var element in lstSeledtedScanDrivedetails)
                    {
                        string textplb = "Please wait," + ConfigSettings.ClientName + " is preparing for list of files to scan...";
                        chromiumBrowser.ExecuteScript("javascript:setantivirusScannedFileDetails('" + textplb + "');");
                        FileNames = GetFiles(element.DriveName, "*", e).ToArray();
                        string textplbinit = ConfigSettings.ClientName + " virus scan is initializing...";
                        chromiumBrowser.ExecuteScript("javascript:setantivirusScannedFileDetails('" + textplbinit + "');");
                    }
                }
                else if (scanvalue == 1)
                {
                    string textplb = "Please wait," + ConfigSettings.ClientName + " is preparing for list of files to scan...";
                    chromiumBrowser.ExecuteScript("javascript:setantivirusScannedFileDetails('" + textplb + "');");
                    FileNames = GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.System), "*", e).ToArray();
                    string textplbinit = ConfigSettings.ClientName + " virus scan is initializing...";
                    chromiumBrowser.ExecuteScript("javascript:setantivirusScannedFileDetails('" + textplbinit + "');");
                }
                else
                {
                    string textplb = "Please wait," + ConfigSettings.ClientName + " is preparing for list of files to scan...";
                    chromiumBrowser.ExecuteScript("javascript:setantivirusScannedFileDetails('" + textplb + "');");
                    FileNames = GetFiles(scanFile, "*", e).ToArray();
                    string textplbinit = ConfigSettings.ClientName + " virus scan is initializing...";
                    chromiumBrowser.ExecuteScript("javascript:setantivirusScannedFileDetails('" + textplbinit + "');");
                }
                int j = 1;
                scanList.Clear();
                foreach (string path in FileNames)
                {
                    list.Clear();
                    filename = path;
                    var clamVir = new ClamClient(hostname, portno);
                    ClamScanResult scanResult = clamVir.ScanFileOnServer(path);
                    if (uxAntivirusScanWorker.CancellationPending == true)
                    {
                        e.Cancel = true;
                        break;
                    }
                    switch (scanResult.Result)
                    {
                        case ClamScanResults.Clean:
                            string textfilescan = "File Scanned : " + path + "^^" + "Total Files Scanned : " + j;
                            chromiumBrowser.ExecuteScript("javascript:setantivirusScannedFileDetails('" + textfilescan + "');");
                            break;

                        case ClamScanResults.VirusDetected:
                            scanList.Add(new VirusDetails() { IsVirus = "Y", VirusName = scanResult.InfectedFiles.First().VirusName, Path = path });
                            break;
                        case ClamScanResults.Error:
                            break;
                    }
                    j++;
                }

            }
            catch (Exception ex)
            {
                e.Cancel = true;
                ErrorTracker.WriteErrorLog("MainWindow.cs", "bgw_scanDoWork", "", ex.Message, ex.StackTrace, "ERROR");
            }
        }
Пример #5
0
        private static void bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {

                for (int i = 0; i < Drives.Count; i++)
                {
                    FileNames = GetFiles(Drives[i], "*").ToArray();
                    var clamVir = new ClamClient(hostname, portno);
                    foreach (string path in FileNames)
                    {
                        ClamScanResult scanResult = clamVir.ScanFileOnServer(path);
                        if (scanResult.Result == ClamScanResults.VirusDetected)
                        {
                            infectedFiles.Add(path);
                        }
                    }
                }
            }
            catch (Exception)
            {
               // throw ex;
            }
        }
Пример #6
0
 /// <summary>
 /// Scans your data stream for virus
 /// </summary>
 /// <param name="stream">The stream you want to check</param>
 public ScanResult ScanStream(Stream stream)
 {
     var clam = new ClamClient("localhost", 3310);
     return MapScanResult(clam.SendAndScanFile(stream));
 }
Пример #7
0
 /// <summary>
 /// Scans a file for viruses
 /// </summary>
 /// <param name="pathToFile">The full path to the file</param>
 public ScanResult ScanFile(string pathToFile)
 {
     var clam = new ClamClient("localhost", 3310);
     return MapScanResult(clam.ScanFileOnServer(pathToFile));
 }
Пример #8
0
 /// <summary>
 /// Scans some bytes for virus
 /// </summary>
 /// <param name="data">byte data to scan</param>
 public ScanResult ScanBytes(byte[] data)
 {
     var clam = new ClamClient("localhost", 3310);
     return MapScanResult(clam.SendAndScanFile(data));
 }
Пример #9
0
        public ActionResult Upload(string fileType, string fileExt, string iv, int keySize, int blockSize, bool encrypt, bool saveKey, HttpPostedFileWrapper data, string key = null)
        {
            try
            {
                if (Config.UploadConfig.UploadEnabled)
                {
                    if (data.ContentLength <= Config.UploadConfig.MaxUploadSize)
                    {
                        // convert file to bytes
                        byte[] fileData = null;
                        int contentLength = data.ContentLength;
                        using (var binaryReader = new BinaryReader(data.InputStream))
                        {
                            fileData = binaryReader.ReadBytes(data.ContentLength);
                        }

                        // Scan the file to detect a virus
                        if (Config.UploadConfig.VirusScanEnable)
                        {
                            byte[] scanData = fileData;
                            // If it was encrypted client side, decrypt it
                            if (!encrypt && key != null)
                            {
                                // If the IV is set, and Key is set, then decrypt it
                                if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(iv))
                                {
                                    // Decrypt the data
                                    scanData = AES.Decrypt(scanData, key, iv);
                                }
                            }
                            ClamClient clam = new ClamClient(Config.UploadConfig.ClamServer, Config.UploadConfig.ClamPort);
                            clam.MaxStreamSize = Config.UploadConfig.MaxUploadSize;
                            ClamScanResult scanResult = clam.SendAndScanFile(scanData);

                            switch (scanResult.Result)
                            {
                                case ClamScanResults.Clean:
                                    break;
                                case ClamScanResults.VirusDetected:
                                    return Json(new { error = new { message = string.Format("Virus Detected: {0}. As per our <a href=\"{1}\">Terms of Service</a>, Viruses are not permited.", scanResult.InfectedFiles.First().VirusName, Url.SubRouteUrl("tos", "TOS.Index")) } });
                                case ClamScanResults.Error:
                                    return Json(new { error = new { message = string.Format("Error scanning the file upload for viruses.  {0}", scanResult.RawResult) } });
                                case ClamScanResults.Unknown:
                                    return Json(new { error = new { message = string.Format("Unknown result while scanning the file upload for viruses.  {0}", scanResult.RawResult) } });
                            }
                        }

                        // if they want us to encrypt it, we do so here
                        if (encrypt)
                        {
                            // Generate key and iv if empty
                            if (string.IsNullOrEmpty(key))
                            {
                                key = Utility.RandomString(keySize / 8);
                            }

                            fileData = AES.Encrypt(fileData, key, iv);
                            if (fileData == null || fileData.Length <= 0)
                            {
                                return Json(new { error = new { message = "Unable to encrypt file" } });
                            }
                        }
                        Models.Upload upload = Uploader.SaveFile(db, Config, fileData, fileType, contentLength, fileExt, iv, (saveKey) ? key : null, keySize, blockSize);
                        if (upload != null)
                        {
                            if (User.Identity.IsAuthenticated)
                            {
                                Users.Models.User user = UserHelper.GetUser(db, User.Identity.Name);
                                if (user != null)
                                {
                                    upload.UserId = user.UserId;
                                    db.Entry(upload).State = EntityState.Modified;
                                    db.SaveChanges();
                                }
                            }
                            return Json(new { result = new { name = upload.Url, url = Url.SubRouteUrl("u", "Upload.Download", new { file = upload.Url }), key = key } }, "text/plain");
                        }
                        return Json(new { error = new { message = "Unable to upload file" } });
                    }
                    else
                    {
                        return Json(new { error = new { message = "File Too Large" } });
                    }
                }
                return Json(new { error = new { message = "Uploads are disabled" } });
            }
            catch (Exception ex)
            {
                return Json(new { error = new { message = "Exception while uploading file: " + ex.GetFullMessage(true) } });
            }
        }
Пример #10
0
        public ActionResult Upload(HttpPostedFileWrapper file, string contentType = null, bool encrypt = true, bool saveKey = true, string key = null, int keySize = 0, string iv = null, int blockSize = 0, bool genDeletionKey = false, bool doNotTrack = false)
        {
            try
            {
                ViewBag.Title = "Upload";
                if (file != null)
                {
                    if (file.ContentLength <= Config.UploadConfig.MaxUploadSize)
                    {
                        // convert file to bytes
                        byte[] fileData = null;
                        string fileExt = Path.GetExtension(file.FileName);
                        int contentLength = file.ContentLength;
                        using (var binaryReader = new BinaryReader(file.InputStream))
                        {
                            fileData = binaryReader.ReadBytes(file.ContentLength);
                        }

                        // Scan the file to detect a virus
                        if (Config.UploadConfig.VirusScanEnable)
                        {
                            byte[] scanData = fileData;
                            // If it was encrypted client side, decrypt it
                            if (!encrypt && key != null)
                            {
                                // If the IV is set, and Key is set, then decrypt it
                                if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(iv))
                                {
                                    // Decrypt the data
                                    scanData = AES.Decrypt(scanData, key, iv);
                                }
                            }
                            ClamClient clam = new ClamClient(Config.UploadConfig.ClamServer, Config.UploadConfig.ClamPort);
                            clam.MaxStreamSize = Config.UploadConfig.MaxUploadSize;
                            ClamScanResult scanResult = clam.SendAndScanFile(scanData);

                            switch (scanResult.Result)
                            {
                                case ClamScanResults.Clean:
                                    break;
                                case ClamScanResults.VirusDetected:
                                    return Json(new { error = new { message = string.Format("Virus Detected: {0}. As per our <a href=\"{1}\">Terms of Service</a>, Viruses are not permited.", scanResult.InfectedFiles.First().VirusName, Url.SubRouteUrl("tos", "TOS.Index")) } });
                                case ClamScanResults.Error:
                                    break;
                                case ClamScanResults.Unknown:
                                    break;
                            }
                        }

                        // Need to grab the contentType if it's empty
                        if (string.IsNullOrEmpty(contentType))
                        {
                            contentType = (string.IsNullOrEmpty(file.ContentType)) ? "application/octet-stream" : file.ContentType;
                        }

                        // Initialize the key size and block size if empty
                        if (keySize <= 0)
                            keySize = Config.UploadConfig.KeySize;
                        if (blockSize <= 0)
                            blockSize = Config.UploadConfig.BlockSize;

                        byte[] data = null;
                        // If they want us to encrypt the file first, do that here
                        if (encrypt)
                        {
                            // Generate key and iv if empty
                            if (string.IsNullOrEmpty(key))
                            {
                                key = Utility.RandomString(keySize / 8);
                            }
                            if (string.IsNullOrEmpty(iv))
                            {
                                iv = Utility.RandomString(blockSize / 8);
                            }

                            data = AES.Encrypt(fileData, key, iv);
                            if (data == null || data.Length <= 0)
                            {
                                return Json(new { error = new { message = "Unable to encrypt file" } });
                            }
                        }

                        // Save the file data
                        Upload.Models.Upload upload = Uploader.SaveFile(db, Config, (encrypt) ? data : fileData, contentType, contentLength, fileExt, iv, (saveKey) ? key : null, keySize, blockSize);

                        if (upload != null)
                        {
                            // Generate delete key if asked to
                            if (genDeletionKey)
                            {
                                string delKey = Utility.RandomString(Config.UploadConfig.DeleteKeyLength);
                                upload.DeleteKey = delKey;
                                db.Entry(upload).State = EntityState.Modified;
                                db.SaveChanges();
                            }

                            // Pull all the information together 
                            string fullUrl = Url.SubRouteUrl("upload", "Upload.Download", new { file = upload.Url });
                            var returnData = new
                            {
                                url = (saveKey || string.IsNullOrEmpty(key)) ? fullUrl : fullUrl + "#" + key,
                                fileName = upload.Url,
                                contentType = contentType,
                                contentLength = contentLength,
                                key = key,
                                keySize = keySize,
                                iv = iv,
                                blockSize = blockSize,
                                deletionKey = upload.DeleteKey

                            };
                            return Json(new { result = returnData });
                        }
                        return Json(new { error = new { message = "Unable to save file" } });
                    }
                    else
                    {
                        return Json(new { error = new { message = "File Too Large" } });
                    }
                }

                return Json(new { error = new { message = "Invalid Upload Request" } });
            }
            catch(Exception ex)
            {
                return Json(new { error = new { message = "Exception: " + ex.Message } });
            }
        }