Nmap

dnf -y install nmap nmap-ncat
dnf -y install https://nmap.org/dist/zenmap-7.92-1.noarch.rpm

Man beachte: Zenmap ist in Python 2 geschrieben. Falls Python 3 der Default auf dem System ist, auf dem Zenmap installiert wurde, kann man den Shebang /usr/bin/env python auf #!/usr/bin/env python2 anpassen.

nmap Cheat Sheet

nmap -n -Pn -oG /tmp/nmap-1.1.1.1-1450321831 -sT -sU -p T:1-65535,U:1-65535 -T4 1.1.1.1

-n Never do DNS resolution
-Pn No ping, skips the Nmap discovery stage altogether
-oG grepable Output
-sT TCP connect scan, operating system establishes a connection with the target machine
-sU UDP scans
-p port ranges
# scan subnet
nmap 192.168.1.0/24

# inkl. UDP-Ports
nmap -sU

# no port scan after host dicovery (null scan)
nmap -sn 192.168.1.0/24

# specify custom ports for scanning to probe specific ports with SYN/UDP packets
sudo nmap -sn -PS22,3389 192.168.1.0/24
sudo nmap -sn -PU161 192.168.1.0/24

# disable reverse DNS resolution
nmap -n ...

# don't show "setup_target: failed to determine route to ...
nmap ... 2> /dev/null

# nur ausgewählte TCP- und UDP-Ports, Intense Scan:
nmap -sS -sU -T4 -A -v -p T:80,443,3479,U:3479 turn.linuxfabrik.io

Schnellster Scan ohne Fehlermeldungen:

sudo nmap -Pn -sn -PS22,3389 10.0.0.0/8 2> /dev/null
sudo nmap -Pn -sn -PU161 10.0.0.0/8 2> /dev/null

Bemerkung

Wird Nmap als root ausgeführt (evtl. per „sudo“), arbeitet es wesentlich schneller, da so ARP-Scans möglich sind.

Ein Fast Scan gegen ein Netz mit 540’672 IP-Adressen mit nmap -sn -n -PS22,3389 10.0.0.0/8 2> /dev/null kann drei Stunden und länger gehen.

Wie testet man, ob ein Host auf einem Port einen Dienst anbietet bzw. ob der Port erreichbar ist?

dnf -y install nmap-ncat

# TCP
ncat -z --verbose --wait 3 $HOST $PORT

# UDP
ncat -z --udp --verbose --wait 3 $HOST $PORT

Wie bietet man selbst einen Service an, der auf die Ports 1024 bis 65535 hört?

for j in {1024..65536}; do nc -lvnp $j & done

Wie testet man automatisiert auf die Erreichbarkeit von Ports?

nmap -p 1-65535 -T4 -A -v

Portscan:

# UDP
nmap -sU 192.168.1.1

# TCP, just a few ports
nmap -sS 192.168.1.1 -p U:53,111,137,T:21-25,80,139,8080

Simples Bash-Script, welches einen Service neu startet, sobald eine entfernte Maschine per Ping und/oder auf einem bestimmten Port (per nc geprüft) nicht mehr erreichbar ist. Nützlich, um beispielsweise VPN-Tunnel neu zu starten. Das Skipt lässt sich beispielsweise wie folgt aufrufen: ./restart-service-if-unreachable --host 192.0.2.74 --port 636 --service strongswan.service --wait-time 1 --disable-ping

restart-service-if-unreachable
#!/bin/bash

# Function to check if the host is reachable
check_ping() {
    echo "ping -c 1 -W $WAIT_TIME $HOST > /dev/null 2>&1"
    ping -c 1 -W $WAIT_TIME $HOST > /dev/null 2>&1
    return $?
}

# Function to check if the port is reachable
check_port() {
    echo "nc -z -w $WAIT_TIME $HOST $PORT"
    nc -z -w $WAIT_TIME $HOST $PORT
    return $?
}

# Function to check both ping and port
check_both() {
    check_ping
    ping_result=$?
    check_port
    port_result=$?

    # Return the logical OR of the two results
    return $((ping_result || port_result))
}

# Default values
WAIT_TIME=1
SERVICE_NAME="example.service"
ENABLE_PING=true  # Default is to enable ping

# Debug line: Print all arguments
echo "Debug: $0 $@"

# Parse command-line arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        -h|--host)
            HOST=$2
            shift 2
            ;;
        -p|--port)
            PORT=$2
            shift 2
            ;;
        -w|--wait-time)
            WAIT_TIME=$2
            shift 2
            ;;
        -s|--service)
            SERVICE_NAME=$2
            shift 2
            ;;
        --disable-ping)
            ENABLE_PING=false
            shift
            ;;
        *)
            echo "Invalid argument: $1"
            exit 1
            ;;
    esac
done

if [ -z "$HOST" ]; then
    echo "Error: Host or port not provided."
    exit 1
fi

# Check if both host and port are provided
if [ -n "$PORT" ] && [ "$ENABLE_PING" = true ]; then
    check_both
else
    if [ "$ENABLE_PING" = true ]; then
        check_ping
    else
        check_port
    fi
else
CHECK_RESULT=$?

# Check the result and take action accordingly
if [ $CHECK_RESULT -eq 0 ]; then
    echo "Check successful. No action needed."
else
    echo "Check failed. Taking action."

    # Restart the service
    systemctl restart $SERVICE_NAME

    # Check if the restart was successful
    if [ $? -eq 0 ]; then
        echo "Service $SERVICE_NAME restarted successfully."
    else
        echo "Failed to restart $SERVICE_NAME."
    fi
fi

Built on 2024-04-18