bash:: powershell tricks cheatsheet scripting
bash ยท powershell
one-liners
01 Bash Tricks
Loops / iteration
for f in *.txt; do echo "$f"; done
for i in {1..10}; do echo "$i"; done
while read -r line; do echo "$line"; done < file
seq 1 10 | while read -r i; do echo "$i"; done
Arrays / strings
IFS=, read -ra arr <<< "$csv"
printf '%s\n' "${arr[@]}"
echo "${var^^}" # upper
echo "${var,,}" # lower
echo "${var//old/new}"
echo "${var:0:8}"
Pipes / xargs
cat hosts.txt | xargs -I{} curl -s http://{}
find . -name '*.log' -print0 | xargs -0 grep -H flag
printf '%s\n' *.txt | xargs -n1 wc -l
02 Bash Practical One-Liners
Files / grep
grep -Rni flag .
grep -RIl 'password' .
find . -type f -size -1M
find . -type f -exec file {} \; | grep text
Web / CTF helpers
for p in admin api debug; do curl -skI https://target/$p | head -n1 ; done
curl -s URL | jq
seq 0 20 | xargs -I{} curl -s "https://t/?id={}"
Safer scripting
set -euo pipefail
trap 'echo fail on line $LINENO' ERR
[[ -f file ]] || exit 1
command -v jq >/dev/null || exit 1
03 PowerShell Tricks
Loops / pipelines
1..10 | ForEach-Object { $_ }
Get-ChildItem *.txt | ForEach-Object { $_.Name }
Get-Content .\file.txt | ForEach-Object { $_.ToUpper() }
foreach ($f in Get-ChildItem *.log) { $f.Name }
Objects / filtering
Get-Process | Where-Object {$_.Name -like "*chrome*"}
Get-Service | Select-Object Name,Status
Import-Csv users.csv | Where-Object {$_.Role -eq "admin"}
(Get-Content file -Raw)
Strings / files
"abc123" -replace "\d","X"
Select-String -Path .\* -Pattern "flag{" -SimpleMatch
Get-Content file | Select-String "password"
Get-FileHash file -Algorithm SHA256
04 PowerShell Web / API Helpers
HTTP / JSON
Invoke-WebRequest https://target
Invoke-RestMethod https://api/endpoint
$r = Invoke-RestMethod https://api/users
$r | ConvertTo-Json -Depth 5
Headers
$h = @{Authorization="Bearer $token";"X-Test"="1"}
Invoke-WebRequest https://target -Headers $h
Encoding
[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("test"))
[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($b64))
[Text.Encoding]::Unicode.GetString([Convert]::FromBase64String($b64)) # UTF-16LE / PowerShell -enc case
BASH / POWERSHELL / ONELINERS