Example #1
0
        public static Dictionary <string, bool> TestExecuters()
        {
            Dictionary <string, bool> results = new Dictionary <string, bool>();

            string serverHost = "10.141.41.1";

            System.IO.File.WriteAllText(@"c:\somefile.vbs", @"CreateObject(""WScript.Shell"").Run(""Notepad.exe"")");

            Tonsil.Files.HttpFilePath httpFilePath = new Tonsil.Files.HttpFilePath()
            {
                Host      = serverHost,
                Port      = 80,
                Directory = "/somedir/",
                Filename  = "somefile.vbs"
            };
            Tonsil.Files.SmbFilePath smbFilePath = new Tonsil.Files.SmbFilePath()
            {
                Host      = serverHost,
                Port      = 445,
                ShareName = "someshare",
                Directory = @"\somedir\",
                Filename  = "somefile.vbs"
            };
            Tonsil.Files.LocalFilePath localFilePath = new Tonsil.Files.LocalFilePath()
            {
                Directory = @"c:\",
                Filename  = "somefile.vbs"
            };

            List <Tonsil.Files.FilePath> filePaths = new List <Tonsil.Files.FilePath>()
            {
                httpFilePath, smbFilePath, localFilePath
            };

            Type     parentType = typeof(Tonsil.Processes.Executers.Executer);
            Assembly assembly   = parentType.Assembly;

            Type[]             types      = assembly.GetTypes();
            IEnumerable <Type> subclasses = types.Where(t => t.IsSubclassOf(parentType));

            foreach (Type type in subclasses)
            {
                foreach (var filePath in filePaths)
                {
                    Tonsil.Files.File sourceFile = new Tonsil.Files.File()
                    {
                        FilePath = filePath
                    };
                    var processUnderTest = (Tonsil.Processes.Executers.Executer)Activator.CreateInstance(type);
                    if (processUnderTest.IsValidSource(sourceFile))
                    {
                        Tonsil.Processes.Process targetProcess = new Notepad(); // The process that sourceFile should spawn
                        processUnderTest.AddFileExecution(sourceFile);
                        bool result = TestExecutionProxy(processUnderTest, targetProcess);
                        Console.WriteLine(string.Format("{1}: \t{0}", processUnderTest.CmdLine.ToString(), result ? "Pass" : "Fail"));
                        results.Add(processUnderTest.CmdLine.ToString(), result);
                    }
                }
            }
            return(results);
        }
Example #2
0
        public static Dictionary <string, bool> TestExecutionProxyDownloaders()
        {
            Dictionary <string, bool> results = new Dictionary <string, bool>();

            string serverHost = "10.141.41.1";

            Tonsil.Files.HttpFilePath httpFilePath = new Tonsil.Files.HttpFilePath()
            {
                Host      = serverHost,
                Port      = 80,
                Directory = "/somedir/",
                Filename  = "somefile.vbs"
            };
            Tonsil.Files.SmbFilePath smbFilePath = new Tonsil.Files.SmbFilePath()
            {
                Host      = serverHost,
                Port      = 445,
                ShareName = "someshare",
                Directory = @"\somedir\",
                Filename  = "somefile.vbs"
            };
            Tonsil.Files.LocalFilePath localFilePath = new Tonsil.Files.LocalFilePath()
            {
                Directory = @"c:\",
                Filename  = "somefile.vbs"
            };

            List <Tonsil.Files.FilePath> filePaths = new List <Tonsil.Files.FilePath>()
            {
                httpFilePath, smbFilePath
            };

            Tonsil.Files.File destinationFile = new Tonsil.Files.File()
            {
                FileType = Tonsil.Files.FileType.VBScript,
                FilePath = localFilePath
            };

            Type     downloaderParentType = typeof(Tonsil.Processes.Downloaders.Downloader);
            Assembly assembly             = downloaderParentType.Assembly;

            Type[]             types = assembly.GetTypes();
            IEnumerable <Type> downloaderSubclasses     = types.Where(t => t.IsSubclassOf(downloaderParentType));
            Type executionProxyParentType               = typeof(Tonsil.Processes.ExecutionProxys.ExecutionProxy);
            IEnumerable <Type> executionProxySubclasses = types.Where(t => t.IsSubclassOf(executionProxyParentType));

            foreach (Type executionProxyType in executionProxySubclasses)
            {
                foreach (Type downloaderType in downloaderSubclasses)
                {
                    foreach (var filePath in filePaths)
                    {
                        Tonsil.Files.File sourceFile = new Tonsil.Files.File()
                        {
                            FilePath = filePath
                        };
                        Tonsil.Processes.Downloaders.Downloader downloadProcessUnderTest = (Tonsil.Processes.Downloaders.Downloader)Activator.CreateInstance(downloaderType);
                        if (downloadProcessUnderTest.IsValidSource(sourceFile) && downloadProcessUnderTest.IsValidDestination(sourceFile, destinationFile))
                        {
                            downloadProcessUnderTest.AddDownload(sourceFile, destinationFile);

                            Tonsil.Processes.Process targetProcess = downloadProcessUnderTest;
                            Tonsil.Processes.ExecutionProxys.ExecutionProxy processUnderTest = (Tonsil.Processes.ExecutionProxys.ExecutionProxy)Activator.CreateInstance(executionProxyType);
                            processUnderTest.AddCmdlineExecution(targetProcess);
                            bool result = TestDownloader(processUnderTest, sourceFile, destinationFile);
                            Console.WriteLine(string.Format("{1}: \t{0}", processUnderTest.CmdLine.ToString(), result ? "Pass" : "Fail"));
                            results.Add(processUnderTest.CmdLine.ToString(), result);
                        }
                    }
                }
            }
            return(results);
        }
Example #3
0
        public void OnExecute()
        {
            var fi       = new FileInfo(FileUri.LocalPath);
            var fileName = fi.Name;
            var fileExt  = fi.Extension.ToLower();

            var fileTypeMap = new Dictionary <string, Tonsil.Files.FileType>
            {
                { ".vbs", Tonsil.Files.FileType.VBScript },
                { ".js", Tonsil.Files.FileType.JavaScript },
                { ".elf", Tonsil.Files.FileType.Elf }
            };


            Tonsil.Files.FileType fileType;

            try
            {
                fileType = FileType;
            }
            catch (System.ArgumentNullException e)
            {
                if (!fileTypeMap.ContainsKey(fileExt))
                {
                    throw new Exception("Unsupported File type");
                }
                fileType = fileTypeMap[fileExt];
            }
            catch (System.ArgumentException e)
            {
                throw new Exception("Invalid FileType");
            }

            Tonsil.Files.FilePath filePath = null;

            if (FileUri.Scheme.StartsWith("http"))
            {
                filePath = new Tonsil.Files.HttpFilePath()
                {
                    Host      = FileUri.Host,
                    Port      = FileUri.Port,
                    ssl       = FileUri.Scheme.EndsWith("s"),
                    Directory = string.Join("", FileUri.LocalPath.Split(FileUri.Segments[FileUri.Segments.Length - 1])[0]),
                    Filename  = fileName
                };
            }
            else if (FileUri.IsUnc)
            {
                var shareName = FileUri.Segments[1].Split("/")[0];
                var directory = FileUri.LocalPath.Split(shareName)[1].Split(fileName)[0];
                filePath = new Tonsil.Files.SmbFilePath()
                {
                    Host      = FileUri.Host,
                    Port      = 445,
                    ShareName = shareName,
                    Directory = directory,
                    Filename  = fileName
                };
            }
            else
            {
                throw new Exception();
            }

            Tonsil.Files.File source = new Tonsil.Files.File()
            {
                FileType = fileType,
                FilePath = filePath
            };

            var processLists = Tonsil.Utils.GetAllWaysToExecuteFile(source);

            foreach (var processList in processLists)
            {
                foreach (var process in processList)
                {
                    Console.WriteLine(process.CmdLine.ToString());
                }
                Console.WriteLine("");
            }
            Console.WriteLine("{0} total results", processLists.Count());
        }