Hardcore Powershell Function

Hello, everyone!

Today I bring you the latest of my Powershell code,  the Hardcore Powershell function.The code changes a system variable so that if you input an unrecognized command it will (depending on the switch you use) Disable your AD account or Lock it out.

This is how it works:

$wrongPassword = Get-Random

This line generates a random integer, this is to be used as the incorrect password in the Lock-Account function used later on, to do so it utilises the Get-Random cmdlet

$username = whoami # ignore

this just gets the current logged in users username

$servername = "a11-dc-2"

This is used when locking out the account, the Lock-Account function uses the server set here to fail authentication attempts to. More will be explained in a moment

$modulename = "ActiveDirectory" # this is the module that needs to be loaded for the action to change

This bit is kinda useless, however in theory you could change it to another module to check for, however the script will still require the AD module to be loaded

function Lock-ADAccount{
    Write-Host -ForegroundColor Green "You failed, so I'm now locking your AD Account out.Nice try though!"
do {
    $password = ConvertTo-SecureString $wrongPassword -AsPlainText -Force
    $cred= New-Object System.Management.Automation.PSCredential ($username, $password )
    Enter-PSSession -ComputerName $servername -Credential $cred -ErrorAction SilentlyContinue
    
    
}
until ((Get-ADUser -Identity $env:USERNAME -Properties LockedOut).LockedOut)
 

                        }

This function is one of the functions that causes mischief, it is the one that locks the account.
It writes to the console that you’ve messed up and it’s now locking your account.
It then gets the random integer from the Get-Random command, converts it to a secure string, then passes it to the Enter-Pssession Command which will authenticate under your username but with the wrong password.
It will loop through this until the Get-ADUser command returns saying that the account is locked out. Kinda evil right!

function Start-HardcoreMode {
#params
    Param(
   [int] $hardcoreness
    )
#checking module loaded
    $ADLoaded = Get-Module -Name $modulename
        if ($ADLoaded -like "$modulename")
        { $moduleloaded = "yes" }
        else { $moduleloaded = "no"}
       

if ($hardcoreness -eq "1" -and $moduleloaded -eq "yes")
    {
    Write-Host -ForegroundColor Green "Enabling hardcore mode 1, be careful!"
    $ExecutionContext.InvokeCommand.CommandNotFoundAction = {Lock-ADAccount}
    }
if ($hardcoreness -eq "2" -and $moduleloaded -eq "yes")
    {
    Write-Host -ForegroundColor Green "Enabling hardcore mode 2, be careful!"
    $ExecutionContext.InvokeCommand.CommandNotFoundAction = {Disable-ADAccount -Identity $env:USERNAME}
    }
if ($moduleloaded -eq "no") {
    Write-Host -ForegroundColor Red "$modulename is not loaded, please load and run me again"
    }
}

So, this is the big bit – it asks for the “Hardcoreness” Integer first off, once gained it then checks for the module.
if it is loaded $moduleloaded’s value is yes. if not it is no.
then it heads into a series of if statements. This is where the fun starts.

if ($hardcoreness -eq "1" -and $moduleloaded -eq "yes")
    {
    Write-Host -ForegroundColor Green "Enabling hardcore mode 1, be careful!"
    $ExecutionContext.InvokeCommand.CommandNotFoundAction = {Lock-ADAccount}

this part checks to see what level you entered and if the module is loaded, if so it changes the command not found setting to run the Lock-ADAccount function from earlier.

if ($hardcoreness -eq "2" -and $moduleloaded -eq "yes")
    {
    Write-Host -ForegroundColor Green "Enabling hardcore mode 2, be careful!"
    $ExecutionContext.InvokeCommand.CommandNotFoundAction = {Disable-ADAccount -Identity $env:USERNAME}
    }

the second statement does the same thing, checks what level and if the module is loaded, however runs disable-adaccount if you get something wrong! hehehe even moar evil!

if ($moduleloaded -eq "no") {
    Write-Host -ForegroundColor Red "$modulename is not loaded, please load and run me again"
    }
}

The final statement just checks if the module is loaded, if not it says so and tells you to load it and try again.

To run the function from your prompt, run Start-HardcoreMode -Hardcoreness <level>

To download the full code, please see my github page here

As the Readme mentions on the git repo, thanks to jfrmilner for giving me the idea. You can check out his blog here

Thanks and happy coding!

-Ainsey11

Leave a Reply

Your email address will not be published. Required fields are marked *