File size: 2,273 Bytes
06ee4f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env bash
set -euo pipefail

# ssh_port_scan.sh
# Usage: ./ssh_port_scan.sh /path/to/private_key [host]
# Tries SSH on ports 22001..22035 and runs "whoami; hostname; uptime" for successful connections.
# Appends all findings to ~/ssh_port_scan.log

KEY_PATH="${1:-}"
HOST="${2:-paffenroth-23.dyn.wpi.edu}"
USER="student-admin"
PORT_START=22001
PORT_END=22035
CONNECT_TIMEOUT=5
LOGFILE="${HOME}/ssh_port_scan.log"

# Validate key argument
if [[ -z "$KEY_PATH" ]]; then
  echo "Usage: $0 /path/to/private_key [host]" >&2
  exit 2
fi

if [[ ! -f "$KEY_PATH" ]]; then
  echo "Error: Key file not found: $KEY_PATH" >&2
  exit 3
fi

if ! command -v ssh >/dev/null 2>&1; then
  echo "Error: ssh is required but not found in PATH." >&2
  exit 4
fi

# Header for this run in the log
run_ts=$(date --iso-8601=seconds 2>/dev/null || date +"%Y-%m-%dT%H:%M:%S%z")
{
  echo "===================="
  echo "SSH port scan run: ${run_ts}"
  echo "Host: ${HOST}"
  echo "Key: ${KEY_PATH}"
  echo "Ports: ${PORT_START}-${PORT_END}"
  echo ""
} >>"$LOGFILE"

echo "Starting scan ${PORT_START}-${PORT_END} against ${USER}@${HOST} (log: ${LOGFILE})"

found_any=0

for port in $(seq "$PORT_START" "$PORT_END"); do
  printf "Testing port %5d ... " "$port"
  tmp=$(mktemp)
  # run the verification commands on the remote host; BatchMode prevents password prompts
  if ssh -i "$KEY_PATH" -p "$port" \
         -o StrictHostKeyChecking=no -o ConnectTimeout="$CONNECT_TIMEOUT" \
         -o BatchMode=yes -o IdentitiesOnly=yes \
         "${USER}@${HOST}" "whoami; hostname; uptime" >"$tmp" 2>&1; then
    printf "SUCCESS\n"
    found_any=1
    ts=$(date +"%Y-%m-%d %H:%M:%S")
    {
      echo "----"
      echo "Port: $port"
      echo "Time: $ts"
      echo "Result:"
      cat "$tmp"
      echo "----"
      echo ""
    } >>"$LOGFILE"
    echo "  -> Logged successful result for port $port to $LOGFILE"
  else
    ssh_ret=$?
    printf "no connection (exit=%d)\n" "$ssh_ret"
    echo "$(date +"%Y-%m-%d %H:%M:%S") - port $port - exit $ssh_ret" >>"$LOGFILE"
  fi
  rm -f "$tmp"
done

if [[ $found_any -eq 1 ]]; then
  echo "One or more successful SSH connections were logged to: $LOGFILE"
  exit 0
else
  echo "No reachable SSH server found in ports ${PORT_START}-${PORT_END}."
  exit 1
fi