hacking-tutorial

IP & Port Scanning


Why learn scanning?


High-level approach

  1. Discover hosts (IP discovery) Methods include ARP queries on a local network, ICMP ping sweeps, and crafted TCP/UDP probes. On routed networks not under your control, many hosts will drop ICMP so discovery techniques vary.

  2. Choose scan type (port probing strategy)

    • Connect scan - attempt to establish a TCP connection to the target port and observe success/failure. Simple and reliable
    • UDP scan - send UDP packets and infer open/closed from responses or lack thereof (slow and noisy).
    • Version/service detection - after finding open ports, probe them in a safe way to identify the service and version (helps prioritize fixes).
    • Timing and stealth - adjust probe rate and timing to reduce load or evade simplistic detection
  3. Interpret results

    • Open ports usually indicate a listening service.
    • Filtered/closed results can mean a firewall or host that drops probes.
    • Combine scans with banner/version checks and authenticated checks (where authorized) to assess risk.

How to scan

1) Find your own IP

ip addr show

OR

ifconfig

What to look for

OUTPUT1


2) Install nmap

What is nmap

Install

sudo apt update
sudo apt install -y nmap

3) Quick nmap primer

A) Discover live hosts on your LAN

# replace 192.168.1.15/24 with the subnet you found in step 1
nmap -sn 192.168.1.15/24

OUTPUT2

B) Full port range on localhost

nmap -sT -p 1-65535 127.0.0.1

OUTPUT3

Before we proceed, install & start an SSH server so that we can see a real port open in the scan

sudo apt update
sudo apt install -y openssh-server
sudo systemctl enable --now ssh

C) Service/version detection

nmap -sT -sV --top-ports 50 192.168.1.15

OUTPUT4


5) Minimal Python Implementation local_probe.py

# local_probe.py
import socket, ipaddress, sys

def is_allowed_target(ip):
    try:
        addr = ipaddress.ip_address(ip)
        return addr.is_private or ip == "127.0.0.1"
    except ValueError:
        return False

target = input("Target IP (127.0.0.1 or private range): ").strip()
if not is_allowed_target(target):
    print("Refusing non-private target. Use only localhost or private IPs.")
    sys.exit(1)

ports_in = input("Enter ports (comma separated, e.g. 22,80,443): ").strip()
try:
    ports = [int(p) for p in ports_in.split(",") if p.strip()]
except ValueError:
    print("Invalid port list")
    sys.exit(1)

timeout = 0.8
for p in ports:
    if not (0 < p < 65536):
        print(f"port {p}: invalid")
        continue
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(timeout)
    try:
        s.connect((target, p))
        print(f"port {p}: open")
    except socket.timeout:
        print(f"port {p}: filtered/timeout")
    except ConnectionRefusedError:
        print(f"port {p}: closed")
    except Exception as e:
        print(f"port {p}: error {e}")
    finally:
        s.close()
print("[+] Done")

6) Interpretation & next steps

7) Final safety & etiquette reminders