USD ($)
$
United States Dollar
Euro Member Countries
India Rupee
د.إ
United Arab Emirates dirham
ر.س
Saudi Arabia Riyal

Automating Recon & Scans (Python/Bash/PowerShell Basics)

Lesson 33/44 | Study Time: 25 Min

In cybersecurity operations, reconnaissance and scanning are crucial initial steps that help assess the security posture of networks, systems, and applications.

Automating these processes enhances efficiency, consistency, and coverage, allowing security professionals and penetration testers to perform rapid, large-scale assessments with minimal manual effort.

Common scripting languages like Python, Bash, and PowerShell provide robust capabilities for automating a variety of scanning tasks, from port and service discovery to vulnerability enumeration.

By leveraging these scripting tools, organizations can streamline their security workflows, respond faster to threats, and maintain continuous security monitoring.

Importance of Automation in Recon and Scanning

Common use cases: Port scanning to identify live hosts and open ports, as well as service detection and enumeration to determine running applications and their versions.

They also involve subdomain or domain discovery, web application reconnaissance to map site structures and functionalities, and performing vulnerability checks or exploit attempts to assess security weaknesses.

Python for Recon & Scans

Python is widely used in cybersecurity due to its readability, extensive libraries, and community support.


Basic Python Techniques:


1. Network Scanning with socket and scapy:

socket library allows TCP/UDP communication and port checks.

scapy enables crafting custom packets for advanced network interactions.


2. Automation with subprocessRun external tools like Nmap, Nikto, or custom scripts from Python.

3. Web Requests in Reconnaissance: Use requests or http.client for automating HTTP/HTTPS checks and crawling.


Sample Python Snippet (Port Scanner):

python
import socket

def scan_port(host, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1)
result = sock.connect_ex((host, port))
return result == 0

host = "192.168.1.1"
for port in range(1, 1024):
if scan_port(host, port):
print(f"Port {port} is open")

Bash for Recon & Scans

Bash scripts are lightweight and effective for automating system commands and integrating existing tools on Linux/Unix systems.


Common Bash Tasks:


1. Running Nmap or Masscan for large network scans.

2. Automating the fetching of DNS records or WHOIS information.

3. Batch processing IP or subnet ranges.

4. Cron jobs for scheduled scans.


Example Bash Script (Basic Port Scan)

bash

#!/bin/bash
network="192.168.1"
for ip in {1..254}
do
for port in 22 80 443
do
nc -z -w1 $network.$ip $port && echo "Open: $network.$ip:$port"
done
done

PowerShell for Recon & Scans

PowerShell provides deep system access on Windows and can leverage Windows-native security tools and APIs.


Key Capabilities:


1. Using Test-NetConnection for port scanning.

2. Querying DNS, Active Directory, and network configurations.

3. Automating Vulnerability Scanning using modules like PowerShellGet or third-party tools.

4. Integration with Windows Event Logs for monitoring.


Sample PowerShell Port Check:

powershell

$host = "192.168.1.1"
$ports = 80, 443, 22
foreach ($port in $ports) {
$result = Test-NetConnection -ComputerName $host -Port $port -InformationLevel Quiet
if ($result) { Write-Output "Port $port is open" }
}

Best Practices for Automation


Jake Carter

Jake Carter

Product Designer
Profile

Class Sessions

1- Deep Passive Reconnaissance 2- Active Reconnaissance Techniques 3- Traffic Analysis & Packet Crafting Fundamentals 4- Identifying Attack Surface Expansion Paths 5- Advanced Network Mapping & Host Discovery 6- Bypassing Firewalls & IDS/IPS 7- Man-in-the-Middle Attacks (ARP Spoofing, DNS Manipulation) 8- VLAN Hopping, Port Security Weaknesses, and Network Segmentation Testing 9- Windows & Linux Privilege Escalation: Advanced Enumeration & Kernel-Level Attack Paths 10- Exploiting Misconfigurations & File/Service Permission Abuse 11- Bypassing UAC, sudo, and Restricted Shells 12- Credential Dumping & Token/Key Abuse 13- Persistence Techniques (Registry, Scheduled Tasks, SSH Keys) 14- Tunneling & Port Forwarding (SOCKS Proxy, SSH Tunnels, Chisel Basics) 15- Pivoting in Multi-Layered Networks 16- Data Exfiltration Concepts & OPSEC Considerations 17- Server-Side Attacks (Advanced SQL Injection, Template Injection, Server-Side Template Injection - SSTI) 18- Authentication & Authorization Attacks (JWT Abuse, Session Misconfigurations) 19- SSRF, XXE, Deserialization & Logic Flaw Identification 20- Advanced API Security Testing (Token Handling, Rate-Limiting Bypass Concepts) 21- Wi-Fi Security Attacks (WPA3 Considerations, Enterprise Networks) 22- Rogue APs & Evil Twin Concepts 23- Mobile App Security Overview (Android & iOS Attack Surface, Static/Dynamic Testing) 24- IoT Device Weaknesses (Firmware Analysis Basics, Insecure Protocols, Hardcoded Credentials) 25- Cloud Service Models & Shared Responsibility (AWS, Azure, GCP basics) 26- Cloud Misconfigurations (IAM, Storage Buckets, Exposed Services) 27- Container & Kubernetes Security (Namespaces, Privilege Escalations, Misconfigurations) 28- Virtualization Weaknesses & Hypervisor Attack Concepts 29- Malware Behavior Analysis (Dynamic vs Static) 30- Exploit Development Concepts (Buffer Overflow Fundamentals, Shellcode Basics) 31- Reverse Engineering Essentials (Strings, Disassembly, Logic Flow Understanding) 32- Detection & Evasion Techniques (Sandbox Evasion Concepts) 33- Automating Recon & Scans (Python/Bash/PowerShell Basics) 34- Writing Custom Enumeration Scripts 35- Tool Customization (Modifying Payloads, Extending Existing Tools Ethically) 36- Data Parsing, Reporting & Workflow Automation 37- Threat Intelligence Integration & TTP Mapping 38- Attack Path Mapping (MITRE ATT&CK Alignment) 39- Social Engineering Campaign Planning (Ethical Boundaries & Simulations) 40- Blue Team Evasion Concepts (OPSEC, Log Evasion Principles) 41- Structuring Professional Penetration Test Reports 42- Mapping Findings to Risk Ratings (CVSS, Impact Assessment) 43- Presenting Findings to Executives and Technical Teams 44- Prioritizing Remediation and Security Hardening Guidance