Class: IPFrag::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/ip_frag/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size, mtu) ⇒ Generator

Returns a new instance of Generator.



3
4
5
6
7
# File 'lib/ip_frag/generator.rb', line 3

def initialize(size, mtu)
  @size = size
  @mtu = mtu
  check_size_mtu
end

Instance Attribute Details

#dst_ipObject



112
113
114
# File 'lib/ip_frag/generator.rb', line 112

def dst_ip
  @dst_ip ||= '200.200.200.2'
end

#dst_macObject



120
121
122
# File 'lib/ip_frag/generator.rb', line 120

def dst_mac
  @dst_mac ||= '00:00:00:00:00:02'
end

#src_ipObject



108
109
110
# File 'lib/ip_frag/generator.rb', line 108

def src_ip
  @src_ip ||= '200.200.200.1'
end

#src_macObject



116
117
118
# File 'lib/ip_frag/generator.rb', line 116

def src_mac
  @src_mac ||= '00:00:00:00:00:01'
end

Instance Method Details

#check_size_mtuObject



97
98
99
100
101
102
103
104
105
106
# File 'lib/ip_frag/generator.rb', line 97

def check_size_mtu
  
  if @size <= @mtu
    raise "Size#{@size} MUST be big than Mtu#{@mtu}."
  end
  
  if @mtu <= 0
    raise "Mtu MUST be an integer big than ZERO"
  end
end

#contant(size) ⇒ Object



126
127
128
# File 'lib/ip_frag/generator.rb', line 126

def contant(size)
  'a' * size
end

#make_ether_packet {|packet| ... } ⇒ Object

Yields:

  • (packet)


80
81
82
83
84
85
# File 'lib/ip_frag/generator.rb', line 80

def make_ether_packet
  packet = Mu::Pcap::Ethernet.new(src_mac, dst_mac)
  packet.type = Mu::Pcap::Ethernet::ETHERTYPE_IP
  yield packet
  packet
end

#make_ip_packet(size) {|ip| ... } ⇒ Object

Yields:

  • (ip)


72
73
74
75
76
77
78
# File 'lib/ip_frag/generator.rb', line 72

def make_ip_packet(size)
  ip = Mu::Pcap::IPv4.new(src_ip, dst_ip)
  ip.payload = contant(size)
  ip.proto = Mu::Pcap::IP::IPPROTO_UDP
  yield ip if block_given?
  ip
end

#make_offset(offset, last = false) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/ip_frag/generator.rb', line 87

def make_offset(offset, last = false)
  offset = offset >> 3
  
  flag = 1
  if last
    flag = 0
  end
  ( flag << 13) | offset
end

#packet_countObject

Get total packet size, it depends on @size and @mtu



54
55
56
57
58
59
60
# File 'lib/ip_frag/generator.rb', line 54

def packet_count
  count, left = @size.divmod(packet_offset)
  if left != 0
    count += 1
  end
  count
end

#packet_leftObject



66
67
68
69
70
# File 'lib/ip_frag/generator.rb', line 66

def packet_left
  left = @size % packet_offset
  left = packet_offset if left == 0
  left
end

#packet_offsetObject



62
63
64
# File 'lib/ip_frag/generator.rb', line 62

def packet_offset
  @mtu & (~7)
end

#split_to_ethernet_packetObject



32
33
34
35
36
37
38
39
40
# File 'lib/ip_frag/generator.rb', line 32

def split_to_ethernet_packet
  ip_packets = split_to_ip_packet
  ip_packets.map do |ip_packet|
    make_ether_packet do |e|
      e.payload = ip_packet
      e.type
    end
  end
end

#split_to_ip_packetObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ip_frag/generator.rb', line 9

def split_to_ip_packet
  ret = []
  
  offset = 0
  ret << make_ip_packet(packet_offset) do |p|
    p.offset = make_offset(offset)
  end
  offset += packet_offset
  
  (packet_count - 2).times do
    ret << make_ip_packet(packet_offset) do |p|
      p.offset = make_offset(offset)
    end
    offset += packet_offset
  end
  
  ret << make_ip_packet(packet_left) do |p|
    p.offset = make_offset(offset, true)
  end
  
  ret
end

#write_dat(path = '.') ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/ip_frag/generator.rb', line 42

def write_dat(path = '.')
  split_to_ethernet_packet.each_with_index do |p, i|
    file_name = "#{i+1}.dat"
    file = File.open( File.join(path, file_name), 'w') do |f|
      p.write(f)
    end
  end
end