BASH   56

flood simple

Guest on 24th November 2022 03:22:44 PM

  1. #!/bin/sh
  2.  
  3. packet=/tmp/udp
  4.  
  5. if [ -z "$3" ]; then
  6.   echo "Usage: flood payload ip seconds [iface]">&2
  7.   exit 1
  8. fi
  9.  
  10. payload="$1"
  11. ip="$2"
  12. sec="$3"
  13. target="$4"
  14. len=$(($payload + 14 + 20 + 8))
  15. l2len=$(($len+8+4+12))
  16.  
  17. set -f
  18. set `arp -n $ip`
  19.  
  20. case "$4" in
  21. no|*incomplete*)
  22.         echo "$ip: looking for MAC (not cached)"
  23.         ping -W 1000 -c1 -q $ip >/dev/null 2>&1
  24.         set `arp -n $ip`
  25.         ;;
  26. esac
  27.  
  28. if [ "$4" = no ]; then
  29.         echo "$ip: MAC not found, assume it's reachable through gateway"
  30.         eval `route -n get $ip | awk '/interface:/ {printf "iface=%s\n", $2;}'`
  31. else
  32.         iface="$6"
  33. fi
  34.  
  35. echo "$ip: reachable through interface: $iface"
  36.  
  37. fakeip=no
  38. if [ "$4" = '(incomplete)' ]; then
  39.   echo "$ip: MAC not found at $iface, using faked MAC"
  40.   arp -S $ip 01:01:01:01:01:01 && fakeip=yes
  41. fi
  42.  
  43. echo "$ip: preparing a packet for flood..."
  44. rm -f $packet.pcap
  45. [ -n "$target" ] && iface="$target"
  46. tcpdump -w $packet.pcap -i $iface -s0 -c1 -np dst host $ip and udp port 5001 >/dev/null 2>&1 &
  47. tpid=$!
  48. iperf -l $payload -u -t 1 -c $ip -b 10K >/dev/null 2>&1
  49. sleep 1
  50. kill $tpid >/dev/null 2>&1
  51.  
  52. set -- `ls -l $packet.pcap`
  53. if [ -z "$5" -o "$5" -le 24 ]; then
  54.   echo $ip: cannot prepare a packet, stop. >&2
  55.   exit 1
  56. fi
  57.  
  58. tail -c $len $packet.pcap > $packet.raw
  59. [ $fakeip = yes ] && arp -d $ip
  60.  
  61. echo "$ip: flooding through $iface: payload=$payload iplen="$(($payload+20+8))" framelen=$len l2len=$l2len"
  62.  
  63. stop_and_show() {
  64.   ngctl msg $iface:orphans stop
  65.   eval `ngctl msg $iface:orphans getstats |\
  66.     awk '/^Args:/ { print $3, $4, $16, $17 }'`
  67.   echo $ip: flood time was `printf "scale=3\n$tv_sec+${tv_usec}/1000000\n" | bc -l` sec, $outFrames frames sent
  68.   echo $ip: flood L2 speed was `printf "scale=3\n$outFrames/($tv_sec+${tv_usec}/1000000)\n" | bc -l` frames/sec
  69.   echo $ip: flood L2 speed was `printf "scale=3\n$l2len*$outFrames*8/($tv_sec+${tv_usec}/1000000)/1000000\n" | bc -l` Mbit/sec
  70.   ngctl shutdown $iface:orphans
  71. }
  72.  
  73. run_flood() {
  74.   return 0
  75. }
  76.  
  77. trap 'stop_and_show; exit 0' SIGHUP SIGINT SIGTERM
  78.  
  79. count=100000000
  80.  
  81. if ngctl mkpeer $iface: source orphans output >/dev/null 2>&1; then
  82.   nghook $iface:orphans input < $packet.raw >/dev/null 2>&1
  83.   ngctl msg $iface:orphans start $count
  84.   sleep $sec
  85.   stop_and_show
  86.   exit 0
  87. fi
  88. echo $ip: cannot flood through interface $iface

Raw Paste


Login or Register to edit or fork this paste. It's free.