# Get-WinEvent -LogName Microsoft-Windows-PowerShell/Operational | where {(($_.message -match "Payload.*.*Add-ConstrainedDelegationBackdoor" -or $_.message -match "Payload.*.*Set-DCShadowPermissions" -or $_.message -match "Payload.*.*DNS_TXT_Pwnage" -or $_.message -match "Payload.*.*Execute-OnTime" -or $_.message -match "Payload.*.*HTTP-Backdoor" -or $_.message -match "Payload.*.*Set-RemotePSRemoting" -or $_.message -match "Payload.*.*Set-RemoteWMI" -or $_.message -match "Payload.*.*Invoke-AmsiBypass" -or $_.message -match "Payload.*.*Out-CHM" -or $_.message -match "Payload.*.*Out-HTA" -or $_.message -match "Payload.*.*Out-SCF" -or $_.message -match "Payload.*.*Out-SCT" -or $_.message -match "Payload.*.*Out-Shortcut" -or $_.message -match "Payload.*.*Out-WebQuery" -or $_.message -match "Payload.*.*Out-Word" -or $_.message -match "Payload.*.*Enable-Duplication" -or $_.message -match "Payload.*.*Remove-Update" -or $_.message -match "Payload.*.*Download-Execute-PS" -or $_.message -match "Payload.*.*Download_Execute" -or $_.message -match "Payload.*.*Execute-Command-MSSQL" -or $_.message -match "Payload.*.*Execute-DNSTXT-Code" -or $_.message -match "Payload.*.*Out-RundllCommand" -or $_.message -match "Payload.*.*Copy-VSS" -or $_.message -match "Payload.*.*FireBuster" -or $_.message -match "Payload.*.*FireListener" -or $_.message -match "Payload.*.*Get-Information" -or $_.message -match "Payload.*.*Get-PassHints" -or $_.message -match "Payload.*.*Get-WLAN-Keys" -or $_.message -match "Payload.*.*Get-Web-Credentials" -or $_.message -match "Payload.*.*Invoke-CredentialsPhish" -or $_.message -match "Payload.*.*Invoke-MimikatzWDigestDowngrade" -or $_.message -match "Payload.*.*Invoke-SSIDExfil" -or $_.message -match "Payload.*.*Invoke-SessionGopher" -or $_.message -match "Payload.*.*Keylogger" -or $_.message -match "Payload.*.*Invoke-Interceptor" -or $_.message -match "Payload.*.*Create-MultipleSessions" -or $_.message -match "Payload.*.*Invoke-NetworkRelay" -or $_.message -match "Payload.*.*Run-EXEonRemote" -or $_.message -match "Payload.*.*Invoke-Prasadhak" -or $_.message -match "Payload.*.*Invoke-BruteForce" -or $_.message -match "Payload.*.*Password-List" -or $_.message -match "Payload.*.*Invoke-JSRatRegsvr" -or $_.message -match "Payload.*.*Invoke-JSRatRundll" -or $_.message -match "Payload.*.*Invoke-PoshRatHttps" -or $_.message -match "Payload.*.*Invoke-PowerShellIcmp" -or $_.message -match "Payload.*.*Invoke-PowerShellUdp" -or $_.message -match "Payload.*.*Invoke-PSGcat" -or $_.message -match "Payload.*.*Invoke-PsGcatAgent" -or $_.message -match "Payload.*.*Remove-PoshRat" -or $_.message -match "Payload.*.*Add-Persistance" -or $_.message -match "Payload.*.*ExetoText" -or $_.message -match "Payload.*.*Invoke-Decode" -or $_.message -match "Payload.*.*Invoke-Encode" -or $_.message -match "Payload.*.*Parse_Keys" -or $_.message -match "Payload.*.*Remove-Persistence" -or $_.message -match "Payload.*.*StringtoBase64" -or $_.message -match "Payload.*.*TexttoExe" -or $_.message -match "Payload.*.*Powerpreter" -or $_.message -match "Payload.*.*Nishang" -or $_.message -match "Payload.*.*DataToEncode" -or $_.message -match "Payload.*.*LoggedKeys" -or $_.message -match "Payload.*.*OUT-DNSTXT" -or $_.message -match "Payload.*.*ExfilOption" -or $_.message -match "Payload.*.*DumpCerts" -or $_.message -match "Payload.*.*DumpCreds" -or $_.message -match "Payload.*.*Shellcode32" -or $_.message -match "Payload.*.*Shellcode64" -or $_.message -match "Payload.*.*NotAllNameSpaces" -or $_.message -match "Payload.*.*exfill" -or $_.message -match "Payload.*.*FakeDC")) } | select TimeCreated,Id,RecordId,ProcessId,MachineName,Message

function Add-Rule {

    $ruleName = "powershell_nishang_malicious_commandlets";
    $detectRule = {
        
        function Search-DetectableEvents {
            param (
                $event
            )
            
            $ruleName = "powershell_nishang_malicious_commandlets";
            $detectedMessage = "Detects Commandlet names and arguments from the Nishang exploitation framework";
            $result = $event |  where { (($_.message -match "Payload.*.*Add-ConstrainedDelegationBackdoor" -or $_.message -match "Payload.*.*Set-DCShadowPermissions" -or $_.message -match "Payload.*.*DNS_TXT_Pwnage" -or $_.message -match "Payload.*.*Execute-OnTime" -or $_.message -match "Payload.*.*HTTP-Backdoor" -or $_.message -match "Payload.*.*Set-RemotePSRemoting" -or $_.message -match "Payload.*.*Set-RemoteWMI" -or $_.message -match "Payload.*.*Invoke-AmsiBypass" -or $_.message -match "Payload.*.*Out-CHM" -or $_.message -match "Payload.*.*Out-HTA" -or $_.message -match "Payload.*.*Out-SCF" -or $_.message -match "Payload.*.*Out-SCT" -or $_.message -match "Payload.*.*Out-Shortcut" -or $_.message -match "Payload.*.*Out-WebQuery" -or $_.message -match "Payload.*.*Out-Word" -or $_.message -match "Payload.*.*Enable-Duplication" -or $_.message -match "Payload.*.*Remove-Update" -or $_.message -match "Payload.*.*Download-Execute-PS" -or $_.message -match "Payload.*.*Download_Execute" -or $_.message -match "Payload.*.*Execute-Command-MSSQL" -or $_.message -match "Payload.*.*Execute-DNSTXT-Code" -or $_.message -match "Payload.*.*Out-RundllCommand" -or $_.message -match "Payload.*.*Copy-VSS" -or $_.message -match "Payload.*.*FireBuster" -or $_.message -match "Payload.*.*FireListener" -or $_.message -match "Payload.*.*Get-Information" -or $_.message -match "Payload.*.*Get-PassHints" -or $_.message -match "Payload.*.*Get-WLAN-Keys" -or $_.message -match "Payload.*.*Get-Web-Credentials" -or $_.message -match "Payload.*.*Invoke-CredentialsPhish" -or $_.message -match "Payload.*.*Invoke-MimikatzWDigestDowngrade" -or $_.message -match "Payload.*.*Invoke-SSIDExfil" -or $_.message -match "Payload.*.*Invoke-SessionGopher" -or $_.message -match "Payload.*.*Keylogger" -or $_.message -match "Payload.*.*Invoke-Interceptor" -or $_.message -match "Payload.*.*Create-MultipleSessions" -or $_.message -match "Payload.*.*Invoke-NetworkRelay" -or $_.message -match "Payload.*.*Run-EXEonRemote" -or $_.message -match "Payload.*.*Invoke-Prasadhak" -or $_.message -match "Payload.*.*Invoke-BruteForce" -or $_.message -match "Payload.*.*Password-List" -or $_.message -match "Payload.*.*Invoke-JSRatRegsvr" -or $_.message -match "Payload.*.*Invoke-JSRatRundll" -or $_.message -match "Payload.*.*Invoke-PoshRatHttps" -or $_.message -match "Payload.*.*Invoke-PowerShellIcmp" -or $_.message -match "Payload.*.*Invoke-PowerShellUdp" -or $_.message -match "Payload.*.*Invoke-PSGcat" -or $_.message -match "Payload.*.*Invoke-PsGcatAgent" -or $_.message -match "Payload.*.*Remove-PoshRat" -or $_.message -match "Payload.*.*Add-Persistance" -or $_.message -match "Payload.*.*ExetoText" -or $_.message -match "Payload.*.*Invoke-Decode" -or $_.message -match "Payload.*.*Invoke-Encode" -or $_.message -match "Payload.*.*Parse_Keys" -or $_.message -match "Payload.*.*Remove-Persistence" -or $_.message -match "Payload.*.*StringtoBase64" -or $_.message -match "Payload.*.*TexttoExe" -or $_.message -match "Payload.*.*Powerpreter" -or $_.message -match "Payload.*.*Nishang" -or $_.message -match "Payload.*.*DataToEncode" -or $_.message -match "Payload.*.*LoggedKeys" -or $_.message -match "Payload.*.*OUT-DNSTXT" -or $_.message -match "Payload.*.*ExfilOption" -or $_.message -match "Payload.*.*DumpCerts" -or $_.message -match "Payload.*.*DumpCreds" -or $_.message -match "Payload.*.*Shellcode32" -or $_.message -match "Payload.*.*Shellcode64" -or $_.message -match "Payload.*.*NotAllNameSpaces" -or $_.message -match "Payload.*.*exfill" -or $_.message -match "Payload.*.*FakeDC")) } | select TimeCreated, Id, RecordId, ProcessId, MachineName, Message;
            if ($result -and $result.Count -ne 0) {
                Write-Output ""; 
                Write-Output "Detected! RuleName:$ruleName";
                Write-Output $detectedMessage;
                Write-Output $result;
                Write-Output ""; 
            }
        };
        . Search-DetectableEvents $args;
    };
    if (! $ruleStack[$ruleName]) {
        $ruleStack.Add($ruleName, $detectRule);
    }
    else {
        Write-Host "Rule Import Error"  -Foreground Yellow;
    }
}
