Network Traffic Analysis

Basic PCAP File Parsing

  1. require 'packetfu'
  2. packets = PacketFu::PcapFile.read_packets 'packets.pcap'

Download packets.pcap file.

Find FTP Credentials

  1. #!/usr/bin/env ruby
  2. require 'packetfu'
  3. pcap_file = ARGV[0]
  4. packets = PacketFu::PcapFile.read_packets pcap_file
  5. packets.each_with_index do |packet, i|
  6. if packet.tcp_dport == 21
  7. if packet.payload.match(/(USER|PASS)/)
  8. src = [packet.ip_src].pack('N').unpack('C4').join('.')
  9. dst = [packet.ip_dst].pack('N').unpack('C4').join('.')
  10. puts "#{src} => #{dst}"
  11. print packet.payload
  12. end
  13. end
  14. end

Returns

  1. 192.168.2.127 => 192.168.2.128
  2. USER ayoi
  3. 192.168.2.127 => 192.168.2.128
  4. PASS kambingakuilang

Download ftp.pcap file

Capturing and building PCAP file

Sometime we don’t have the time or option to install external libraries on our environment. Let’s work capture all packets on all interfaces then see how to build a pcap file to write in it.

  1. #!/usr/bin/env ruby
  2. #
  3. # KING SABRI | @KINGSABRI
  4. #
  5. require 'socket'
  6. class Pcap
  7. def initialize(pcap_file)
  8. @pcap_file = open(pcap_file, 'wb')
  9. # Pcap Global https://wiki.wireshark.org/Development/LibpcapFileFormat#Global_Header
  10. global_header = [
  11. 0xa1b2c3d4, # magic_number: used to identify pcap files
  12. 2, # version_major
  13. 4, # version_minor
  14. 0, # thiszone
  15. 0, # sigfigs
  16. 65535, # snaplen
  17. 1 # network (link-layer), 1 for Ethernet
  18. ].pack('ISSIIII')
  19. @pcap_file.write global_header
  20. end
  21. def write(data)
  22. time_stamp = Time.now.to_f.round(2).to_s.split('.').map(&:to_i)
  23. data_length = data.length
  24. # Pcap Record (Packet) Header: https://wiki.wireshark.org/Development/LibpcapFileFormat#Record_.28Packet.29_Header
  25. packet_header = [
  26. time_stamp[0], # ts_sec timestamp seconds
  27. time_stamp[1], # ts_usec timestamp microseconds
  28. data_length, # incl_len the number of bytes of packet data actually captured
  29. data_length # orig_len the length of the packet as it appeared on the network when it was captured
  30. ].pack('IIII')
  31. record = "#{packet_header}#{data}"
  32. @pcap_file.write(record)
  33. rescue
  34. @pcap_file.close
  35. end
  36. end
  37. pcap = Pcap.new(ARGV[0])
  38. socket = Socket.new(Socket::PF_PACKET, Socket::SOCK_RAW, 0x03_00)
  39. loop do
  40. raw_data = socket.recvfrom(65535)[0]
  41. pcap.write raw_data
  42. end

<!—
http://www.behindthefirewalls.com/2014/01/extracting-files-from-network-traffic-pcap.html

http://jarmoc.com/blog/2013/05/22/bsjtf-ctf-writeup-what-in-the-name-of-zeus/

http://hamsa.cs.northwestern.edu/readings/password-cracking2/
—>

<!—

!/usr/bin/env ruby

#

https://www.youtube.com/watch?v=owsr3X453Z4

require ‘packetfu’
require ‘pp’

capture = PacketFu::Capture.new :iface => ‘mon0’, :promisc => true, :start => true

capture.stream.each do |p|

pkt = PacketFu::Packet.parse p
pp pkt
end

\

array 56

include PacketFu
packets = PcapFile.file_to_array ‘/home/KING/wireless.pcap’

packets.eachwith_index do |packet , ref|
puts “
75
puts “Reference: #{ref}”
puts “\
“ _ 75

pkt = Packet.parse(packet)
puts pkt.dissect
sleep 2

end

\

packets = PcapFile.read_packets ‘/home/KING/wireless.pcap’
packet = packets[56]
pkt = Packet.parse(packet)
puts pkt.inspect_hex

=begin
1876
1551
1550
1339
1324
459
458
=end
—->