From d5c85add896e67a1dee438c59c3ef18fe1898bc8 Mon Sep 17 00:00:00 2001 From: Uncled1023 Date: Mon, 9 May 2016 13:01:41 -0700 Subject: [PATCH] Added Server Maintainence program. Fixed client side encrypted uploads that save the key from not being scanned. --- ServerMaint/Program.cs | 120 +++++++++++++++ ServerMaint/Properties/AssemblyInfo.cs | 36 +++++ ServerMaint/ServerMaint.csproj | 137 ++++++++++++++++++ ServerMaint/app.config | 33 +++++ ServerMaint/packages.config | 11 ++ Teknik.sln | 12 +- .../Areas/API/Controllers/APIv1Controller.cs | 13 +- .../Upload/Controllers/UploadController.cs | 13 +- Teknik/Configuration/Config.cs | 7 +- 9 files changed, 378 insertions(+), 4 deletions(-) create mode 100644 ServerMaint/Program.cs create mode 100644 ServerMaint/Properties/AssemblyInfo.cs create mode 100644 ServerMaint/ServerMaint.csproj create mode 100644 ServerMaint/app.config create mode 100644 ServerMaint/packages.config diff --git a/ServerMaint/Program.cs b/ServerMaint/Program.cs new file mode 100644 index 0000000..8466ca2 --- /dev/null +++ b/ServerMaint/Program.cs @@ -0,0 +1,120 @@ +using nClam; +using System; +using System.Collections.Generic; +using System.Data.Entity; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using Teknik.Areas.Upload.Models; +using Teknik.Configuration; +using Teknik.Helpers; +using Teknik.Models; + +namespace ServerMaint +{ + public class Program + { + public static event Action OutputEvent; + + public static void Main(string[] args) + { + string currentPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); + string parentPath = Directory.GetParent(currentPath).FullName; + string logFile = Path.Combine(currentPath, "virusLogs.txt"); + string errorFile = Path.Combine(currentPath, "errorLogs.txt"); + string configPath = Path.Combine(parentPath, "App_Data"); + + // Let's clean some stuff!! + try + { + Config config = Config.Load(configPath); + TeknikEntities db = new TeknikEntities(); + + // Scan all the uploads for viruses, and remove the bad ones + if (config.UploadConfig.VirusScanEnable) + { + List uploads = db.Uploads.ToList(); + + 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 + ClamClient clam = new ClamClient(config.UploadConfig.ClamServer, config.UploadConfig.ClamPort); + clam.MaxStreamSize = config.UploadConfig.MaxUploadSize; + 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(logFile, new List { 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 { 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 { unkMsg }); + Output(unkMsg); + break; + } + } + } + + Output(string.Format("Scanning Complete. {0} Scanned | {1} Viruses Found | {2} Total Files", totalScans, totalViruses, totalCount)); + } + } + catch (Exception ex) + { + string msg = string.Format("[{0}] Exception: {1}", DateTime.Now, ex.Message); + File.AppendAllLines(errorFile, new List { msg }); + Output(msg); + } + } + + public static void Output(string message) + { + Console.WriteLine(message); + if (OutputEvent != null) + { + OutputEvent(message); + } + } + } +} diff --git a/ServerMaint/Properties/AssemblyInfo.cs b/ServerMaint/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..2ce9b44 --- /dev/null +++ b/ServerMaint/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Teknik Server Maintainence")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Teknik")] +[assembly: AssemblyProduct("Teknik")] +[assembly: AssemblyCopyright("Copyright © 2015 - 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("e08975f9-1b84-41b0-875a-cec9778c4f9e")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/ServerMaint/ServerMaint.csproj b/ServerMaint/ServerMaint.csproj new file mode 100644 index 0000000..1847449 --- /dev/null +++ b/ServerMaint/ServerMaint.csproj @@ -0,0 +1,137 @@ + + + + + Debug + AnyCPU + {E08975F9-1B84-41B0-875A-CEC9778C4F9E} + Exe + Properties + ServerMaint + ServerMaint + v4.5.2 + 512 + + + + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + false + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + false + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + false + + + + ..\packages\BouncyCastle.1.7.0\lib\Net40-Client\BouncyCastle.Crypto.dll + True + + + ..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll + True + + + ..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.dll + True + + + ..\packages\Microsoft.AspNet.Identity.Core.2.2.1\lib\net45\Microsoft.AspNet.Identity.Core.dll + True + + + ..\packages\Microsoft.AspNet.Identity.EntityFramework.2.2.1\lib\net45\Microsoft.AspNet.Identity.EntityFramework.dll + True + + + ..\packages\nClam.2.0.6.0\lib\net40-Client\nClam.dll + True + + + ..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll + True + + + ..\packages\Inferno.1.1.0\lib\net451\SecurityDriven.Inferno.dll + True + + + + + + + + + + + + + + + + + {b20317cd-76c6-4a7b-bce1-e4bef8e4f964} + Teknik + + + + + + + + + False + Microsoft .NET Framework 4.5.2 %28x86 and x64%29 + true + + + False + .NET Framework 3.5 SP1 + false + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + \ No newline at end of file diff --git a/ServerMaint/app.config b/ServerMaint/app.config new file mode 100644 index 0000000..610d53a --- /dev/null +++ b/ServerMaint/app.config @@ -0,0 +1,33 @@ + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ServerMaint/packages.config b/ServerMaint/packages.config new file mode 100644 index 0000000..a885806 --- /dev/null +++ b/ServerMaint/packages.config @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Teknik.sln b/Teknik.sln index 6d2d8f4..f2c9de9 100644 --- a/Teknik.sln +++ b/Teknik.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 14 -VisualStudioVersion = 14.0.24720.0 +VisualStudioVersion = 14.0.25123.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Teknik", "Teknik\Teknik.csproj", "{B20317CD-76C6-4A7B-BCE1-E4BEF8E4F964}" EndProject @@ -11,6 +11,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution README.md = README.md EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServerMaint", "ServerMaint\ServerMaint.csproj", "{E08975F9-1B84-41B0-875A-CEC9778C4F9E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -28,6 +30,14 @@ Global {B20317CD-76C6-4A7B-BCE1-E4BEF8E4F964}.Release|Any CPU.Build.0 = Release|Any CPU {B20317CD-76C6-4A7B-BCE1-E4BEF8E4F964}.Release|x64.ActiveCfg = Release|x64 {B20317CD-76C6-4A7B-BCE1-E4BEF8E4F964}.Release|x64.Build.0 = Release|x64 + {E08975F9-1B84-41B0-875A-CEC9778C4F9E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E08975F9-1B84-41B0-875A-CEC9778C4F9E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E08975F9-1B84-41B0-875A-CEC9778C4F9E}.Debug|x64.ActiveCfg = Debug|Any CPU + {E08975F9-1B84-41B0-875A-CEC9778C4F9E}.Debug|x64.Build.0 = Debug|Any CPU + {E08975F9-1B84-41B0-875A-CEC9778C4F9E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E08975F9-1B84-41B0-875A-CEC9778C4F9E}.Release|Any CPU.Build.0 = Release|Any CPU + {E08975F9-1B84-41B0-875A-CEC9778C4F9E}.Release|x64.ActiveCfg = Release|Any CPU + {E08975F9-1B84-41B0-875A-CEC9778C4F9E}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Teknik/Areas/API/Controllers/APIv1Controller.cs b/Teknik/Areas/API/Controllers/APIv1Controller.cs index 6a91e3c..0585753 100644 --- a/Teknik/Areas/API/Controllers/APIv1Controller.cs +++ b/Teknik/Areas/API/Controllers/APIv1Controller.cs @@ -50,9 +50,20 @@ namespace Teknik.Areas.API.Controllers // 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(fileData); + ClamScanResult scanResult = clam.SendAndScanFile(scanData); switch (scanResult.Result) { diff --git a/Teknik/Areas/Upload/Controllers/UploadController.cs b/Teknik/Areas/Upload/Controllers/UploadController.cs index 4d4029d..3df31f4 100644 --- a/Teknik/Areas/Upload/Controllers/UploadController.cs +++ b/Teknik/Areas/Upload/Controllers/UploadController.cs @@ -63,9 +63,20 @@ namespace Teknik.Areas.Upload.Controllers // 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(fileData); + ClamScanResult scanResult = clam.SendAndScanFile(scanData); switch (scanResult.Result) { diff --git a/Teknik/Configuration/Config.cs b/Teknik/Configuration/Config.cs index 09c8cef..1c3bb30 100644 --- a/Teknik/Configuration/Config.cs +++ b/Teknik/Configuration/Config.cs @@ -137,8 +137,13 @@ namespace Teknik.Configuration public static Config Load() { - Config config = new Config(); string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString(); + return Load(path); + } + + public static Config Load(string path) + { + Config config = new Config(); if (!File.Exists(Path.Combine(path, "Config.json"))) { Save(Path.Combine(path, "Config.json"), config);