Scripting Basics
Scripting Basics
Scripting automates repetitive tasks, accelerates testing, and glues tools together. A single script can gather data, transform output, launch exploits, and log results in seconds. The three core environments are Bash, Python, and PowerShell; each dominates a different operating system family yet follows the same logic: read input → process → act.
Common Techniques
Parsing tool output and extracting key fields with regular expressions or JSON‐filters Looping through wordlists, IP ranges, or file paths to launch bulk scans Wrapping exploits in functions for re-use across targets Chaining commands with pipes to form one-liner workflows Reading STDIN / STDOUT for live data transformation Generating payloads dynamically (reverse shells, encoded scripts, shellcode) Logging actions and timestamps to CSV or SQLite for later reporting Using environment variables and arguments to create portable modules Invoking REST APIs to pull scope lists, asset inventories, or vulnerability feeds Scheduling scripts via cron, Task Scheduler, or at jobs for continuous monitoring
Tools
- Bash (POSIX shell with extensive native command set)
- Python (multiplatform language with libraries such as subprocess, requests, and pwntools)
- PowerShell (object-centric shell for Windows and cross-platform automation)
- jq (lightweight JSON processor ideal for API or tool output)
- awk & sed (stream editors for quick text manipulation)
- Expect (automates interactive CLI sessions)
- Impacket scripts (Python collection for network exploitation)
- pwntools (CTF-oriented Python framework for exploit development)
- psutil (Python library for process and system information)
- Invoke-Obfuscation (PowerShell module for payload transformation)
- Cron / Task Scheduler (native schedulers for timed script execution)
- tmux / screen (terminal multiplexers that keep long-running scripts alive)
Quick Examples
Bash one-liner: scan subnet and log live hosts
for ip in 10.10.10.{1..254}; do
ping -c1 -W1 $ip &>/dev/null && echo "$ip,up" >> live.csv &
done; wait
# Python: parse nmap XML and list open ports per host
import xml.etree.ElementTree as ET
import sys
tree = ET.parse('scan.xml')
for host in tree.findall('.//host'):
addr = host.find('address').attrib['addr']
ports = [p.attrib['portid'] for p in host.findall('.//port[state/@state="open"]')]
print(addr, ','.join(ports))
# PowerShell: pull AD computer names and ping each
Get-ADComputer -Filter * |
Select-Object -Expand Name |
ForEach-Object {
if (Test-Connection -Quiet $_) {
"$_ reachable"
}
}