mirror of https://github.com/raandree/NTFSSecurity
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
4.5 KiB
136 lines
4.5 KiB
using System.Management.Automation;
|
|
using Security2;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System;
|
|
using System.Security.AccessControl;
|
|
|
|
namespace NTFSSecurity
|
|
{
|
|
#region Enable-Privileges
|
|
[Cmdlet(VerbsLifecycle.Enable, "Privileges")]
|
|
[OutputType(typeof(ProcessPrivileges.PrivilegeAndAttributes))]
|
|
public class EnablePrivileges : BaseCmdletWithPrivControl
|
|
{
|
|
private bool enablePrivileges = false;
|
|
private SwitchParameter passThru;
|
|
|
|
[Parameter]
|
|
public SwitchParameter PassThru
|
|
{
|
|
get { return passThru; }
|
|
set { passThru = value; }
|
|
}
|
|
|
|
protected override void BeginProcessing()
|
|
{
|
|
base.BeginProcessing();
|
|
}
|
|
|
|
protected override void ProcessRecord()
|
|
{
|
|
var psCallStack = (CallStackFrame)this.InvokeCommand.InvokeScript("Get-PSCallStack")[1].BaseObject;
|
|
|
|
// The base class raises one EnablePrivilegesSettingInvalid error for a setting that is not $true or
|
|
// $false; before 5.0.0, this had its own try/catch that raised a ParseException for the same cast, but
|
|
// it never ran, because BeginProcessing always failed first with its own uncaught cast.
|
|
enablePrivileges = GetEnablePrivilegesSetting();
|
|
|
|
//if the command is called from NTFSSecurity.Init.ps1 and EnablePrivileges is set to true in the NTFSSecurity.psd1 or if the cmdlet is called from somewhere else
|
|
if ((psCallStack.InvocationInfo.MyCommand.Name == "NTFSSecurity.Init.ps1" && enablePrivileges == true))
|
|
{
|
|
this.EnableFileSystemPrivileges(false);
|
|
}
|
|
else if (psCallStack.InvocationInfo.MyCommand.Name != "NTFSSecurity.Init.ps1")
|
|
{
|
|
this.EnableFileSystemPrivileges(false);
|
|
}
|
|
|
|
if (passThru)
|
|
{
|
|
this.WriteObject(this.privControl.GetPrivileges(), true);
|
|
}
|
|
}
|
|
|
|
protected override void EndProcessing()
|
|
{
|
|
//nothing as we want to keep the privileges enabled
|
|
}
|
|
|
|
// Keeps the privileges enabled also when the pipeline stops early and PowerShell calls only Dispose.
|
|
protected override bool KeepEnabledPrivileges
|
|
{
|
|
get { return true; }
|
|
}
|
|
}
|
|
#endregion Enable-Privileges
|
|
|
|
#region Disable-Privileges
|
|
[Cmdlet(VerbsLifecycle.Disable, "Privileges")]
|
|
[OutputType(typeof(ProcessPrivileges.PrivilegeAndAttributes))]
|
|
public class DisablePrivileges : BaseCmdletWithPrivControl
|
|
{
|
|
private SwitchParameter passThru;
|
|
|
|
[Parameter]
|
|
public SwitchParameter PassThru
|
|
{
|
|
get { return passThru; }
|
|
set { passThru = value; }
|
|
}
|
|
|
|
protected override void BeginProcessing()
|
|
{
|
|
base.BeginProcessing();
|
|
}
|
|
|
|
protected override void ProcessRecord()
|
|
{
|
|
if (this.privControl.GetPrivileges()
|
|
.Where(p => p.PrivilegeState == ProcessPrivileges.PrivilegeState.Enabled)
|
|
.Where(p => (
|
|
p.Privilege == ProcessPrivileges.Privilege.TakeOwnership) |
|
|
(p.Privilege == ProcessPrivileges.Privilege.Restore) |
|
|
(p.Privilege == ProcessPrivileges.Privilege.Backup))
|
|
.Count() == 0)
|
|
{
|
|
this.WriteError(new ErrorRecord(new AdjustPriviledgeException("Privileges are not enabled"), "Disable Privilege Error", ErrorCategory.SecurityError, null));
|
|
return;
|
|
}
|
|
|
|
this.DisableFileSystemPrivileges();
|
|
this.WriteVerbose("The privileges 'TakeOwnership', 'Restore' and 'Backup' are now disabled.");
|
|
|
|
if (passThru)
|
|
{
|
|
this.WriteObject(this.privControl.GetPrivileges(), true);
|
|
}
|
|
}
|
|
|
|
protected override void EndProcessing()
|
|
{
|
|
//nothing as priviliges should already been cleaned up
|
|
}
|
|
}
|
|
#endregion Enable-Privileges
|
|
|
|
#region Get-Privileges
|
|
[Cmdlet(VerbsCommon.Get, "Privileges")]
|
|
[OutputType(typeof(ProcessPrivileges.PrivilegeAndAttributes))]
|
|
public class GetPrivileges : BaseCmdlet
|
|
{
|
|
|
|
protected override void BeginProcessing()
|
|
{
|
|
base.BeginProcessing();
|
|
}
|
|
|
|
protected override void ProcessRecord()
|
|
{
|
|
var privControl = new PrivilegeControl();
|
|
|
|
this.WriteObject(privControl.GetPrivileges(), true);
|
|
}
|
|
}
|
|
#endregion Get-Privileges
|
|
}
|