mirror of
https://git.teknik.io/Teknikode/Teknik.git
synced 2023-08-02 14:16:22 +02:00
Made upload scanning asynchronously in server maint program
This commit is contained in:
parent
446c74d45b
commit
12b5f76738
@ -15,6 +15,7 @@ using Teknik.Areas.Users.Utility;
|
|||||||
using Teknik.Configuration;
|
using Teknik.Configuration;
|
||||||
using Teknik.Utilities;
|
using Teknik.Utilities;
|
||||||
using Teknik.Models;
|
using Teknik.Models;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace ServerMaint
|
namespace ServerMaint
|
||||||
{
|
{
|
||||||
@ -27,6 +28,9 @@ namespace ServerMaint
|
|||||||
|
|
||||||
private const string TAKEDOWN_REPORTER = "Teknik Automated System";
|
private const string TAKEDOWN_REPORTER = "Teknik Automated System";
|
||||||
|
|
||||||
|
private static readonly object dbLock = new object();
|
||||||
|
private static readonly object scanStatsLock = new object();
|
||||||
|
|
||||||
public static event Action<string> OutputEvent;
|
public static event Action<string> OutputEvent;
|
||||||
|
|
||||||
public static int Main(string[] args)
|
public static int Main(string[] args)
|
||||||
@ -124,84 +128,104 @@ namespace ServerMaint
|
|||||||
Output(string.Format("[{0}] Started Virus Scan.", DateTime.Now));
|
Output(string.Format("[{0}] Started Virus Scan.", DateTime.Now));
|
||||||
List<Upload> uploads = db.Uploads.ToList();
|
List<Upload> uploads = db.Uploads.ToList();
|
||||||
|
|
||||||
|
int totalCount = uploads.Count();
|
||||||
|
int totalScans = 0;
|
||||||
|
int totalViruses = 0;
|
||||||
|
List<Task> runningTasks = new List<Task>();
|
||||||
|
foreach (Upload upload in uploads)
|
||||||
|
{
|
||||||
|
int currentScan = totalScans++;
|
||||||
|
Task scanTask = Task.Factory.StartNew(() => ScanUpload(config, db, upload, totalCount, currentScan, ref totalViruses));
|
||||||
|
if (scanTask != null)
|
||||||
|
{
|
||||||
|
runningTasks.Add(scanTask);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bool running = true;
|
||||||
|
|
||||||
|
while (running)
|
||||||
|
{
|
||||||
|
running = runningTasks.Exists(s => s != null && !s.IsCompleted && !s.IsCanceled && !s.IsFaulted);
|
||||||
|
}
|
||||||
|
|
||||||
|
Output(string.Format("Scanning Complete. {0} Scanned | {1} Viruses Found | {2} Total Files", totalScans, totalViruses, totalCount));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ScanUpload(Config config, TeknikEntities db, Upload upload, int totalCount, int currentCount, ref int totalViruses)
|
||||||
|
{
|
||||||
// Initialize ClamAV
|
// Initialize ClamAV
|
||||||
ClamClient clam = new ClamClient(config.UploadConfig.ClamServer, config.UploadConfig.ClamPort);
|
ClamClient clam = new ClamClient(config.UploadConfig.ClamServer, config.UploadConfig.ClamPort);
|
||||||
clam.MaxStreamSize = config.UploadConfig.MaxUploadSize;
|
clam.MaxStreamSize = config.UploadConfig.MaxUploadSize;
|
||||||
|
|
||||||
int totalCount = uploads.Count();
|
string subDir = upload.FileName[0].ToString();
|
||||||
int totalScans = 0;
|
string filePath = Path.Combine(config.UploadConfig.UploadDirectory, subDir, upload.FileName);
|
||||||
int totalClean = 0;
|
if (File.Exists(filePath))
|
||||||
int totalViruses = 0;
|
|
||||||
foreach (Upload upload in uploads)
|
|
||||||
{
|
{
|
||||||
totalScans++;
|
// Read in the file
|
||||||
string subDir = upload.FileName[0].ToString();
|
byte[] data = File.ReadAllBytes(filePath);
|
||||||
string filePath = Path.Combine(config.UploadConfig.UploadDirectory, subDir, upload.FileName);
|
// If the IV is set, and Key is set, then decrypt it
|
||||||
if (File.Exists(filePath))
|
if (!string.IsNullOrEmpty(upload.Key) && !string.IsNullOrEmpty(upload.IV))
|
||||||
{
|
{
|
||||||
// Read in the file
|
// Decrypt the data
|
||||||
byte[] data = File.ReadAllBytes(filePath);
|
data = AES.Decrypt(data, upload.Key, upload.IV);
|
||||||
// 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
|
// We have the data, let's scan it
|
||||||
ClamScanResult scanResult = clam.SendAndScanFile(data);
|
ClamScanResult scanResult = clam.SendAndScanFile(data);
|
||||||
|
|
||||||
switch (scanResult.Result)
|
switch (scanResult.Result)
|
||||||
{
|
{
|
||||||
case ClamScanResults.Clean:
|
case ClamScanResults.Clean:
|
||||||
totalClean++;
|
string cleanMsg = string.Format("[{0}] Clean Scan: {1}/{2} Scanned | {3} - {4}", DateTime.Now, currentCount, totalCount, upload.Url, upload.FileName);
|
||||||
string cleanMsg = string.Format("[{0}] Clean Scan: {1}/{2} Scanned | {3} - {4}", DateTime.Now, totalScans, totalCount, upload.Url, upload.FileName);
|
Output(cleanMsg);
|
||||||
Output(cleanMsg);
|
break;
|
||||||
break;
|
case ClamScanResults.VirusDetected:
|
||||||
case ClamScanResults.VirusDetected:
|
lock (scanStatsLock)
|
||||||
|
{
|
||||||
totalViruses++;
|
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 });
|
string msg = string.Format("[{0}] Virus Detected: {1} - {2} - {3}", DateTime.Now, upload.Url, upload.FileName, scanResult.InfectedFiles.First().VirusName);
|
||||||
Output(msg);
|
File.AppendAllLines(virusFile, new List<string> { msg });
|
||||||
|
Output(msg);
|
||||||
|
|
||||||
|
lock (dbLock)
|
||||||
|
{
|
||||||
|
string urlName = upload.Url;
|
||||||
// Delete from the DB
|
// Delete from the DB
|
||||||
db.Uploads.Remove(upload);
|
db.Uploads.Remove(upload);
|
||||||
db.SaveChanges();
|
|
||||||
|
|
||||||
// Delete the File
|
// Delete the File
|
||||||
if (File.Exists(filePath))
|
if (File.Exists(filePath))
|
||||||
{
|
{
|
||||||
File.Delete(filePath);
|
File.Delete(filePath);
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
case ClamScanResults.Error:
|
// Add to transparency report if any were found
|
||||||
string errorMsg = string.Format("[{0}] Scan Error: {1}", DateTime.Now, scanResult.RawResult);
|
Takedown report = db.Takedowns.Create();
|
||||||
File.AppendAllLines(errorFile, new List<string> { errorMsg });
|
report.Requester = TAKEDOWN_REPORTER;
|
||||||
Output(errorMsg);
|
report.RequesterContact = config.SupportEmail;
|
||||||
break;
|
report.DateRequested = DateTime.Now;
|
||||||
case ClamScanResults.Unknown:
|
report.Reason = "Malware Found";
|
||||||
string unkMsg = string.Format("[{0}] Unknown Scan Result: {1}", DateTime.Now, scanResult.RawResult);
|
report.ActionTaken = string.Format("Upload removed: {0}", urlName);
|
||||||
File.AppendAllLines(errorFile, new List<string> { unkMsg });
|
report.DateActionTaken = DateTime.Now;
|
||||||
Output(unkMsg);
|
db.Takedowns.Add(report);
|
||||||
break;
|
|
||||||
}
|
// Save Changes
|
||||||
|
db.SaveChanges();
|
||||||
|
}
|
||||||
|
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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void WarnInvalidAccounts(Config config, TeknikEntities db)
|
public static void WarnInvalidAccounts(Config config, TeknikEntities db)
|
||||||
|
Loading…
Reference in New Issue
Block a user