Packet manipulation

    PacketFu Features

    • Manipulating TCP protocol
    • Manipulating UDP protocol
    • Manipulating ICMP protocol
    • Packet Capturing - Support TCPdump style[^2]
    • Read and write PCAP files

    Before installing packetfu gem you’ll need to install and libpcap-dev

    • Install packetfu & pcaprub gems
      1. gem install packetfu pcaprub

    Get your interface information

    1. require 'packetfu'
    2. ifconfig = PacketFu::Utils.ifconfig("wlan0")
    3. ifconfig[:iface]
    4. ifconfig[:ip_saddr]
    5. ifconfig[:eth_saddr]

    Get MAC address of a remote host

    Read Pcap file

    1. PacketFu::PcapFile.read_packets("file.pcap")
    1. require 'packetfu'
    2. def pkts
    3. #$config = PacketFu::Config.new(PacketFu::Utils.whoami?(:iface=> "wlan0")).config # set interface
    4. $config = PacketFu::Config.new(:iface=> "wlan0").config # use this line instead of above if you face `whoami?': uninitialized constant PacketFu::Capture (NameError)
    5. #--> Build TCP/IP
    6. #
    7. #- Build Ethernet header:---------------------------------------
    8. pkt = PacketFu::TCPPacket.new(:config => $config , :flavor => "Linux") # IP header
    9. # pkt.eth_src = "00:11:22:33:44:55" # Ether header: Source MAC ; you can use: pkt.eth_header.eth_src
    10. # pkt.eth_dst = "FF:FF:FF:FF:FF:FF" # Ether header: Destination MAC ; you can use: pkt.eth_header.eth_dst
    11. pkt.eth_proto # Ether header: Protocol ; you can use: pkt.eth_header.eth_proto
    12. #- Build IP header:---------------------------------------------
    13. pkt.ip_v = 4 # IP header: IPv4 ; you can use: pkt.ip_header.ip_v
    14. pkt.ip_tos = 0 # IP header: Type of service ; you can use: pkt.ip_header.ip_tos
    15. pkt.ip_len = 20 # IP header: Total Length ; you can use: pkt.ip_header.ip_len
    16. pkt.ip_id # IP header: Identification ; you can use: pkt.ip_header.ip_id
    17. pkt.ip_frag = 0 # IP header: Don't Fragment ; you can use: pkt.ip_header.ip_frag
    18. pkt.ip_ttl = 115 # IP header: TTL(64) is the default ; you can use: pkt.ip_header.ip_ttl
    19. pkt.ip_proto = 6 # IP header: Protocol = tcp (6) ; you can use: pkt.ip_header.ip_proto
    20. pkt.ip_sum # IP header: Header Checksum ; you can use: pkt.ip_header.ip_sum
    21. pkt.ip_saddr = "2.2.2.2" # IP header: Source IP. use $config[:ip_saddr] if you want your real IP ; you can use: pkt.ip_header.ip_saddr
    22. pkt.ip_daddr = "10.20.50.45" # IP header: Destination IP ; you can use: pkt.ip_header.ip_daddr
    23. #- TCP header:-------------------------------------------------
    24. pkt.payload = "Hacked!" # TCP header: packet header(body)
    25. pkt.tcp_flags.ack = 0 # TCP header: Acknowledgment
    26. pkt.tcp_flags.fin = 0 # TCP header: Finish
    27. pkt.tcp_flags.psh = 0 # TCP header: Push
    28. pkt.tcp_flags.rst = 0 # TCP header: Reset
    29. pkt.tcp_flags.syn = 1 # TCP header: Synchronize sequence numbers
    30. pkt.tcp_flags.urg = 0 # TCP header: Urgent pointer
    31. pkt.tcp_ecn = 0 # TCP header: ECHO
    32. pkt.tcp_win = 8192 # TCP header: Window
    33. pkt.tcp_src = 5555 # TCP header: Source Port (random is the default )
    34. pkt.tcp_dst = 4444 # TCP header: Destination Port (make it random/range for general scanning)
    35. pkt.recalc # Recalculate/re-build whole pkt (should be at the end)
    36. #--> End of Build TCP/IP
    37. return pkt_to_a
    38. end
    39. def scan
    40. pkt_array = pkts.sort_by{rand}
    41. puts "-" * " [-] Send Syn flag".length + "\n" + " [-] Send Syn flag " + "\n"
    42. inj = PacketFu::Inject.new(:iface => $config[:iface] , :config => $config, :promisc => false)
    43. inj.array_to_wire(:array => pkt_array) # Send/Inject the packet through connection
    44. puts " [-] Done" + "\n" + "-" * " [-] Send Syn flag".length
    45. end
    46. scan

    Lets see how we can

    This is a simple IDS will print source and destination of any communication has “hacked” payload

    1. require 'packetfu'
    2. capture = PacketFu::Capture.new(:iface => "wlan0", :start => true, :filter => "ip")
    3. loop do
    4. capture.stream.each do |pkt|
    5. packet = PacketFu::Packet.parse(pkt)
    6. puts "#{Time.now}: " + "Source IP: #{packet.ip_saddr}" + " --> " + "Destination IP: #{packet.ip_daddr}" if packet.payload =~ /hacked/i
    7. end
    1. echo "Hacked" | nc -nv 192.168.0.15 4444

    return


    [^1]:
    [^2]: TCPdump Cheat sheet