Scanner

mail@pastecode.io avatar
unknown
sh
a month ago
1.4 kB
1
Indexable
Never
#!/bin/bash

getHelp() {
    echo "Simple TCP-SYN/ICMP scanner"
    echo
    echo "Syntax: scanner -i <target_ip> -p <target_port> -s"
    echo "options:"
    echo "h     Print this Help"
    echo "i     IP (Required)"
    echo "p     Scan specific port (Only TCP)"
    echo "s     TCP mode (SYN)"
    echo
}

function sendRequest() {
    TIMEOUT=1

    if [[ ! $1 =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
        echo "IP is invalid"
        exit
    fi

    if [ -z "$3" ]; then
        if [ "$2" = false ]; then
            ping $1
            exit
        else
            opened_ports=()

            for port in $(seq 0 65535); do
                echo 'Sending SYM packet to port' $port
                if nc -zv -w $TIMEOUT $1 $port 2>&1 | grep -E "succeeded|open"; then
                    opened_ports+=($port)
                fi
            done

            echo "Avaliable ports: ${opened_ports[*]}"
        fi
    else
        nc -zv -w $TIMEOUT $1 $3
    fi

}

if [ -z "$1" ]; then
    getHelp
    exit
fi

ip=
port=
silent=false

while getopts 'i:p:sh' options; do
    case $options in
    h)
        getHelp
        exit
        ;;
    i)
        ip=$OPTARG
        ;;
    p)
        port=$OPTARG
        ;;
    s)
        silent=true
        ;;

    esac

done

if [ -z $ip ]; then
    exit
else
    sendRequest ${ip} ${silent} ${port}
fi
Leave a Comment