예제 #1
0
 public CommandModuleController()
 {
     _commandModuleServices = new ServiceCommandModule();
     _auditLogService       = new ServiceAuditLog();
     _userId = Convert.ToInt32(((ClaimsIdentity)User.Identity).Claims.Where(c => c.Type == "user_id")
                               .Select(c => c.Value).SingleOrDefault());
 }
예제 #2
0
        private void CopyCommandModule(EntityPolicyModules policyModule)
        {
            var commandModuleExport = new DtoCommandModuleExport();
            var commandModule       = new ServiceCommandModule().GetModule(policyModule.ModuleId);

            commandModuleExport.Description           = commandModule.Description;
            commandModuleExport.Order                 = policyModule.Order;
            commandModuleExport.Command               = commandModule.Command;
            commandModuleExport.Arguments             = commandModule.Arguments;
            commandModuleExport.DisplayName           = commandModule.Name;
            commandModuleExport.Timeout               = commandModule.Timeout;
            commandModuleExport.RedirectOutput        = commandModule.RedirectStdOut;
            commandModuleExport.RedirectError         = commandModule.RedirectStdError;
            commandModuleExport.WorkingDirectory      = commandModule.WorkingDirectory;
            commandModuleExport.SuccessCodes          = commandModule.SuccessCodes;
            commandModuleExport.Guid                  = commandModule.Guid;
            commandModuleExport.ConditionFailedAction = policyModule.ConditionFailedAction;
            commandModuleExport.ConditionNextOrder    = policyModule.ConditionNextModule;

            var uploadedFiles = new ServiceUploadedFile().GetFilesForModule(commandModule.Guid);

            foreach (var file in uploadedFiles.OrderBy(x => x.Name))
            {
                var uploadedFile = new DtoUploadedFileExport();
                uploadedFile.FileName   = file.Name;
                uploadedFile.Md5Hash    = file.Hash;
                uploadedFile.ModuleGuid = file.Guid;
                commandModuleExport.UploadedFiles.Add(uploadedFile);
            }

            var externalFiles = new ServiceExternalDownload().GetForModule(commandModule.Guid);

            foreach (var file in externalFiles.OrderBy(x => x.FileName))
            {
                var externalFile = new DtoExternalFileExport();
                externalFile.FileName   = file.FileName;
                externalFile.Sha256Hash = file.Sha256Hash;
                externalFile.Url        = file.Url;
                externalFile.ModuleGuid = file.ModuleGuid;
                commandModuleExport.ExternalFiles.Add(externalFile);
            }

            if (policyModule.ConditionId != -1)
            {
                commandModuleExport.Condition = GetCondition(policyModule.ConditionId);
            }


            _policyExport.CommandModules.Add(commandModuleExport);
        }
예제 #3
0
        private void CommandModule(EntityPolicyModules policyModule)
        {
            var clientCommandModule = new DtoClientCommandModule();
            var commandModule       = new ServiceCommandModule().GetModule(policyModule.ModuleId);

            clientCommandModule.Order            = policyModule.Order;
            clientCommandModule.Guid             = commandModule.Guid;
            clientCommandModule.Command          = commandModule.Command;
            clientCommandModule.Arguments        = commandModule.Arguments;
            clientCommandModule.DisplayName      = commandModule.Name;
            clientCommandModule.Timeout          = commandModule.Timeout;
            clientCommandModule.RedirectOutput   = commandModule.RedirectStdOut;
            clientCommandModule.RedirectError    = commandModule.RedirectStdError;
            clientCommandModule.WorkingDirectory = commandModule.WorkingDirectory;
            foreach (var successCode in commandModule.SuccessCodes.Split(','))
            {
                clientCommandModule.SuccessCodes.Add(successCode);
            }

            if (commandModule.ImpersonationId != -1)
            {
                var impersonationGuid = new ServiceImpersonationAccount().GetGuid(commandModule.ImpersonationId);
                if (!string.IsNullOrEmpty(impersonationGuid))
                {
                    clientCommandModule.RunAs = impersonationGuid;
                }
            }

            if (policyModule.ConditionId != -1)
            {
                var conditionScript = new ServiceScriptModule().GetModule(policyModule.ConditionId);
                if (conditionScript != null)
                {
                    clientCommandModule.ConditionFailedAction    = policyModule.ConditionFailedAction;
                    clientCommandModule.ConditionNextOrder       = policyModule.ConditionNextModule;
                    clientCommandModule.Condition                = new DtoClientModuleCondition();
                    clientCommandModule.Condition.Arguments      = conditionScript.Arguments;
                    clientCommandModule.Condition.DisplayName    = conditionScript.Name;
                    clientCommandModule.Condition.Guid           = conditionScript.Guid;
                    clientCommandModule.Condition.RedirectError  = conditionScript.RedirectStdError;
                    clientCommandModule.Condition.RedirectOutput = conditionScript.RedirectStdOut;
                    if (conditionScript.ImpersonationId != -1)
                    {
                        var scriptImpersonationGuid = new ServiceImpersonationAccount().GetGuid(conditionScript.ImpersonationId);
                        if (!string.IsNullOrEmpty(scriptImpersonationGuid))
                        {
                            clientCommandModule.Condition.RunAs = scriptImpersonationGuid;
                        }
                    }
                    clientCommandModule.Condition.ScriptType = conditionScript.ScriptType;
                    foreach (var successCode in conditionScript.SuccessCodes.Split(','))
                    {
                        clientCommandModule.Condition.SuccessCodes.Add(successCode);
                    }
                    clientCommandModule.Condition.Timeout          = conditionScript.Timeout;
                    clientCommandModule.Condition.WorkingDirectory = conditionScript.WorkingDirectory;
                }
            }

            var moduleFiles = new ServiceModule().GetModuleFiles(commandModule.Guid);

            foreach (var file in moduleFiles.OrderBy(x => x.FileName))
            {
                var clientFile = new DtoClientFileHash();
                clientFile.FileName = file.FileName;
                clientFile.FileHash = file.Md5Hash;
                clientCommandModule.Files.Add(clientFile);
            }

            _clientPolicy.CommandModules.Add(clientCommandModule);
        }
예제 #4
0
        public DtoActionResult Restore(int moduleId, EnumModule.ModuleType moduleType)
        {
            var result = _moduleServices.RestoreModule(moduleId, moduleType);

            if (result == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            if (result.Success)
            {
                var auditLog = new EntityAuditLog();
                switch (moduleType)
                {
                case EnumModule.ModuleType.Command:
                    var cModule = new ServiceCommandModule().GetModule(moduleId);
                    auditLog.ObjectName = cModule.Name;
                    auditLog.ObjectJson = JsonConvert.SerializeObject(cModule);
                    break;

                case EnumModule.ModuleType.FileCopy:
                    var fModule = new ServiceFileCopyModule().GetModule(moduleId);
                    auditLog.ObjectName = fModule.Name;
                    auditLog.ObjectJson = JsonConvert.SerializeObject(fModule);
                    break;

                case EnumModule.ModuleType.Printer:
                    var pModule = new ServicePrinterModule().GetModule(moduleId);
                    auditLog.ObjectName = pModule.Name;
                    auditLog.ObjectJson = JsonConvert.SerializeObject(pModule);
                    break;

                case EnumModule.ModuleType.Script:
                    var scModule = new ServiceScriptModule().GetModule(moduleId);
                    auditLog.ObjectName = scModule.Name;
                    auditLog.ObjectJson = JsonConvert.SerializeObject(scModule);
                    break;

                case EnumModule.ModuleType.Software:
                    var sModule = new ServiceSoftwareModule().GetModule(moduleId);
                    auditLog.ObjectName = sModule.Name;
                    auditLog.ObjectJson = JsonConvert.SerializeObject(sModule);
                    break;

                case EnumModule.ModuleType.Wupdate:
                    var uModule = new ServiceWuModule().GetModule(moduleId);
                    auditLog.ObjectName = uModule.Name;
                    auditLog.ObjectJson = JsonConvert.SerializeObject(uModule);
                    break;

                case EnumModule.ModuleType.Message:
                    var messageModule = new ServiceMessageModule().GetModule(moduleId);
                    auditLog.ObjectName = messageModule.Name;
                    auditLog.ObjectJson = JsonConvert.SerializeObject(messageModule);
                    break;
                }


                auditLog.ObjectType = moduleType.ToString();
                auditLog.ObjectId   = result.Id;


                auditLog.UserId    = _userId;
                auditLog.AuditType = EnumAuditEntry.AuditType.Restore;
                _auditLogService.AddAuditLog(auditLog);
            }
            return(result);
        }
예제 #5
0
        private string VerifyCommand(EntityPolicyModules policyModule)
        {
            var commandModule = new ServiceCommandModule().GetModule(policyModule.ModuleId);

            if (commandModule == null)
            {
                return("An Assigned Command Module No Longer Exists");
            }

            if (string.IsNullOrEmpty(commandModule.Name))
            {
                return("A Command Module Has An Invalid Name");
            }

            if (commandModule.Archived)
            {
                return("Command Module: " + commandModule.Name + " Is Archived");
            }

            if (string.IsNullOrEmpty(commandModule.Guid))
            {
                return("Command Module: " + commandModule.Name + " Has An Invalid GUID");
            }

            if (string.IsNullOrEmpty(commandModule.Command))
            {
                return("Command Module: " + commandModule.Name + " Has An Invalid Command");
            }

            int value;

            if (!int.TryParse(commandModule.Timeout.ToString(), out value))
            {
                return("Command Module: " + commandModule.Name + " Has An Invalid Timeout");
            }

            List <string> successCodes = new List <string>();

            foreach (var successCode in commandModule.SuccessCodes.Split(','))
            {
                successCodes.Add(successCode);
            }

            if (successCodes.Count == 0)
            {
                return("Command Module: " + commandModule.Name + " Has An Invalid Success Code");
            }

            if (successCodes.Any(code => !int.TryParse(code, out value)))
            {
                return("Command Module: " + commandModule.Name + " Has An Invalid Success Code");
            }

            if (!string.IsNullOrEmpty(commandModule.WorkingDirectory))
            {
                try
                {
                    Path.GetFullPath(commandModule.WorkingDirectory);
                }
                catch
                {
                    return("Command Module: " + commandModule.Name + " Has An Invalid Working Directory");
                }
            }

            if (commandModule.ImpersonationId != -1)
            {
                var impAccount = new ServiceImpersonationAccount().GetAccount(commandModule.ImpersonationId);
                if (impAccount == null)
                {
                    return("Command Module: " + commandModule.Name + " Has An Invalid Impersonation Account");
                }
            }

            if (!int.TryParse(policyModule.Order.ToString(), out value))
            {
                return("Command Module: " + commandModule.Name + " Has An Invalid Order");
            }

            var uploadedFiles = new ServiceUploadedFile().GetFilesForModule(commandModule.Guid);
            var externalFiles = new ServiceExternalDownload().GetForModule(commandModule.Guid);

            var basePath = Path.Combine(ServiceSetting.GetSettingValue(SettingStrings.StoragePath), "software_uploads");

            using (var unc = new UncServices())
            {
                if (unc.NetUseWithCredentials() || unc.LastError == 1219)
                {
                    try
                    {
                        foreach (var file in uploadedFiles.OrderBy(x => x.Name))
                        {
                            if (string.IsNullOrEmpty(file.Hash))
                            {
                                return("Command Module: " + commandModule.Name + " " + file.Name + " Does Not Have An MD5 Hash");
                            }
                            var fullPath = Path.Combine(basePath, file.Guid, file.Name);
                            if (!File.Exists(fullPath))
                            {
                                return("Command Module: " + commandModule.Name + " " + fullPath + " Does Not Exist");
                            }
                        }


                        foreach (var file in externalFiles.OrderBy(x => x.FileName))
                        {
                            if (file.Status != EnumFileDownloader.DownloadStatus.Complete)
                            {
                                return("Command Module: " + commandModule.Name + " " + file.FileName + " Has Not Finished Downloading Or Is In An Error State");
                            }
                            if (string.IsNullOrEmpty(file.Md5Hash))
                            {
                                return("Command Module: " + commandModule.Name + " " + file.FileName + " Does Not Have An MD5 Hash");
                            }
                            var fullPath = Path.Combine(basePath, file.ModuleGuid, file.FileName);
                            if (!File.Exists(fullPath))
                            {
                                return("Command Module: " + commandModule.Name + " " + fullPath + " Does Not Exist");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Debug(ex.Message);
                        return("Command Module: " + commandModule.Name + " Unknown Error Trying To Verify Files");
                    }
                }
                else
                {
                    return("Could Not Reach Storage Path");
                }
            }

            return(null);
        }
예제 #6
0
        private DtoActionResult CreateCommands()
        {
            foreach (var commandModule in _export.CommandModules)
            {
                if (_export.Instructions.Contains("[generate-module-guid]"))
                {
                    commandModule.Guid = Guid.NewGuid().ToString();
                }

                if (_uow.CommandModuleRepository.Exists(h => h.Guid.Equals(commandModule.Guid)))
                {
                    return(new DtoActionResult()
                    {
                        ErrorMessage = "A Command Module With This Guid Already Exists.  " + commandModule.Guid
                    });
                }

                var command = new EntityCommandModule();
                command.Name        = commandModule.DisplayName;
                command.Description = "Added Via Policy Template " + _export.Name + "  On " + DateTime.Now +
                                      "\r\n" + commandModule.Description;
                command.Guid             = commandModule.Guid;
                command.Arguments        = commandModule.Arguments;
                command.Command          = commandModule.Command;
                command.RedirectStdError = commandModule.RedirectError;
                command.RedirectStdOut   = commandModule.RedirectOutput;
                command.SuccessCodes     = commandModule.SuccessCodes;
                command.Timeout          = commandModule.Timeout;
                command.WorkingDirectory = commandModule.WorkingDirectory;
                command.ImpersonationId  = -1;

                if (commandModule.UploadedFiles.Any())
                {
                    _policyHasInternalFiles = true;
                }

                if (commandModule.ExternalFiles.Any())
                {
                    _policyHasExternalFiles = true;
                }

                if (_uow.CommandModuleRepository.Exists(h => h.Name.Equals(command.Name)))
                {
                    for (var c = 1; c <= 100; c++)
                    {
                        if (c == 100)
                        {
                            return new DtoActionResult()
                                   {
                                       ErrorMessage = "Could Not Determine A Command Name"
                                   }
                        }
                        ;

                        var newName = command.Name + "_" + c;
                        if (!_uow.CommandModuleRepository.Exists(h => h.Name == newName))
                        {
                            command.Name = newName;

                            break;
                        }
                    }
                }

                var addResult = new ServiceCommandModule().AddModule(command);
                if (!addResult.Success)
                {
                    return(addResult);
                }

                var policyModule = new EntityPolicyModules();
                policyModule.Guid                  = command.Guid;
                policyModule.ModuleId              = addResult.Id;
                policyModule.ModuleType            = EnumModule.ModuleType.Command;
                policyModule.Name                  = command.Name;
                policyModule.Order                 = commandModule.Order;
                policyModule.PolicyId              = _policy.Id;
                policyModule.ConditionFailedAction = commandModule.ConditionFailedAction;
                policyModule.ConditionNextModule   = commandModule.ConditionNextOrder;


                var conditionId = CreateCondition(commandModule.Condition);
                if (conditionId != 0)
                {
                    policyModule.ConditionId = conditionId;
                }
                else
                {
                    policyModule.ConditionId = -1;
                }

                _uow.PolicyModulesRepository.Insert(policyModule);
            }

            return(new DtoActionResult()
            {
                Success = true
            });
        }