Example #1
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            string domain        = Environment.GetEnvironmentVariable("USERDOMAIN");
            string user          = Environment.GetEnvironmentVariable("USERNAME");
            string domainAccount = $"{NTHelper.CleanDomain(domain)}\\{NTHelper.CleanUsername(user)}";

            return(new ActionResponse(ActionStatus.Success, domainAccount));
        }
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            string domain = string.IsNullOrEmpty(request.DataStore.GetValue("ImpersonationDomain"))
                ? Environment.GetEnvironmentVariable("USERDOMAIN")
                : request.DataStore.GetValue("ImpersonationDomain");
            string user = string.IsNullOrEmpty(request.DataStore.GetValue("ImpersonationUsername"))
                ? Environment.GetEnvironmentVariable("USERNAME")
                : request.DataStore.GetValue("ImpersonationUsername");

            string domainAccount = $"{NTHelper.CleanDomain(domain)}\\{NTHelper.CleanUsername(user)}";

            // This will throw an error if the permission cannot be granted
            NTPermissionUtility.SetRight(Environment.MachineName, domainAccount, NTPermissionUtility.LOGON_AS_BATCH_PERM, false);

            return(new ActionResponse(ActionStatus.Success, JsonUtility.GetEmptyJObject()));
        }
Example #3
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            string domain   = NTHelper.CleanDomain(request.DataStore.GetValue("ImpersonationDomain"));
            string user     = NTHelper.CleanUsername(request.DataStore.GetValue("ImpersonationUsername"));
            string password = request.DataStore.GetValue("ImpersonationPassword");

            bool        isValid;
            ContextType context = Environment.MachineName.EqualsIgnoreCase(domain) ? ContextType.Machine : ContextType.Domain;

            using (PrincipalContext pc = new PrincipalContext(context, domain))
            {
                // validate the credentials
                isValid = pc.ValidateCredentials(user, password, ContextOptions.Negotiate);
            }

            return(isValid
                ? new ActionResponse(ActionStatus.Success)
                : new ActionResponse(ActionStatus.Failure, JsonUtility.GetEmptyJObject(), "IncorrectNTCredentials"));
        }
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            var taskDescription = request.DataStore.GetValue("TaskDescription");
            var taskFile        = request.DataStore.GetValue("TaskFile");
            var taskName        = request.DataStore.GetValue("TaskName");
            var taskParameters  = request.DataStore.GetValue("TaskParameters");
            var taskProgram     = request.DataStore.GetValue("TaskProgram");
            var taskStartTime   = request.DataStore.GetValue("TaskStartTime");

            var taskUsername = request.DataStore.GetValue("ImpersonationUsername") == null || string.IsNullOrEmpty(request.DataStore.GetValue("ImpersonationUsername"))
                ? WindowsIdentity.GetCurrent().Name
                : NTHelper.CleanDomain(request.DataStore.GetValue("ImpersonationDomain")) + "\\" + NTHelper.CleanUsername(request.DataStore.GetValue("ImpersonationUsername"));
            var taskPassword = request.DataStore.GetValue("ImpersonationPassword") == null || string.IsNullOrEmpty(request.DataStore.GetValue("ImpersonationPassword"))
                ? null
                : request.DataStore.GetValue("ImpersonationPassword");

            if (taskPassword == null)
            {
                return(new ActionResponse(ActionStatus.Failure, JsonUtility.GetEmptyJObject(), "CreateTaskPasswordMissing"));
            }

            string workingDirectory = request.DataStore.GetValue("TaskDirectory") == null
                ? FileUtility.GetLocalTemplatePath(request.Info.AppName)
                : FileUtility.GetLocalPath(request.DataStore.GetValue("TaskDirectory"));

            bool isPowerShell = taskProgram.EqualsIgnoreCase("powershell");

            using (TaskService ts = new TaskService())
            {
                TaskCollection tasks = ts.RootFolder.GetTasks(new Regex(taskName));
                foreach (Task task in tasks)
                {
                    if (task.Name.EqualsIgnoreCase(taskName))
                    {
                        ts.RootFolder.DeleteTask(taskName);
                    }
                }

                TaskDefinition td = ts.NewTask();
                td.RegistrationInfo.Description = taskDescription;
                td.Settings.Compatibility       = TaskCompatibility.V2_1;
                td.RegistrationInfo.Author      = taskUsername;
                td.Principal.RunLevel           = TaskRunLevel.LUA;
                td.Settings.StartWhenAvailable  = true;
                td.Settings.RestartCount        = 3;
                td.Settings.RestartInterval     = TimeSpan.FromMinutes(3);
                td.Settings.MultipleInstances   = TaskInstancesPolicy.IgnoreNew;

                td.Triggers.Add(new DailyTrigger
                {
                    DaysInterval  = 1,
                    StartBoundary = DateTime.Parse(taskStartTime)
                });

                string optionalArguments = string.Empty;

                if (isPowerShell)
                {
                    optionalArguments = Path.Combine(workingDirectory, taskFile);
                }
                else
                {
                    taskProgram = taskFile;
                }

                if (isPowerShell)
                {
                    optionalArguments = $"-ExecutionPolicy Bypass -File \"{optionalArguments}\"";
                }

                if (!string.IsNullOrEmpty(taskParameters))
                {
                    optionalArguments += " " + taskParameters;
                }

                td.Actions.Add(new ExecAction(taskProgram, optionalArguments, workingDirectory));
                ts.RootFolder.RegisterTaskDefinition(taskName, td, TaskCreation.CreateOrUpdate, taskUsername, taskPassword, TaskLogonType.Password, null);
            }

            return(new ActionResponse(ActionStatus.Success, JsonUtility.GetEmptyJObject()));
        }