Send and Capture icmp packets with type&code using hping
Here TCL script files are run at terminals of different hosts setup in a mininet environment. The code at one host generates packets of different ICMP types and codes (Please refer to different types and codes associated with ICMP packets at https://www.iana.org/assignments/icmp-parameters/icmp-parameters.xhtml). The code run at the second host captures these packets and extract the type and code fields. Here hping3, TCL and mininet are installed on Ubuntu system for the experiment.
I) SCRIPTS
Script for creating mininet hosts
~~~~~~~~~~~~~~~~~~~~
Save the following in net.py
from mininet.topo import Topo
class MyTopo( Topo ):
def __init__( self ):
Topo.__init__( self )
# Add hosts and switches
left_host = self.addHost( 'h0' )
right_host = self.addHost( 'h1' )
centre_switch = self.addSwitch( 's0' )
# Add links
self.addLink( left_host, centre_switch )
self.addLink( right_host, centre_switch )
self.addLink( centre_switch, left_host )
self.addLink( centre_switch, right_host )
topos = { 'mytopo': ( lambda: MyTopo() ) }
class MyTopo( Topo ):
def __init__( self ):
Topo.__init__( self )
# Add hosts and switches
left_host = self.addHost( 'h0' )
right_host = self.addHost( 'h1' )
centre_switch = self.addSwitch( 's0' )
# Add links
self.addLink( left_host, centre_switch )
self.addLink( right_host, centre_switch )
self.addLink( centre_switch, left_host )
self.addLink( centre_switch, right_host )
topos = { 'mytopo': ( lambda: MyTopo() ) }
Script for capturing ICMP packets
~~~~~~~~~~~~~~~~~~~~~
Save the following in receive_ICMP.htcl
while 1 {
set p [lindex [hping recv h2-eth0] 0]
puts "type is [hping getfield icmp type $p] and code is [hping getfield icmp code $p]"
}
set p [lindex [hping recv h2-eth0] 0]
puts "type is [hping getfield icmp type $p] and code is [hping getfield icmp code $p]"
}
Script for sending ICMP packets
~~~~~~~~~~~~~~~~~~~~
The script iterates over different types and codes of ICMP and creates the packets. Save the following in send_ICMP.htcl
foreach i {5 8 9 10 11 12 13 14 15 16 17 18} {
if {$i==5} {
foreach j {0 1 2 3} {
hping send "ip(daddr=10.0.0.2)+icmp(type=$i,code=$j)"
}
} elseif {$i==8 || $i == 9 || $i == 10 || $i == 13 || $i == 14 || $i == 15 || $i == 16 || $i == 17 || $i == 18} {
foreach j {0} {
hping send "ip(daddr=10.0.0.2)+icmp(type=$i,code=$j)"
}
} elseif {$i == 11 || $i == 12} {
foreach j {0 1} {
hping send "ip(daddr=10.0.0.2)+icmp(type=$i,code=$j)"
}
}
}
II) EXECUTION STEPS
1) Create two hosts (h0 and h1) in mininet by running
sudo mn --custom net.py --topo mytopo
2) Listen for packets and print icmp types and codes using tcl script 'receive_ICMP.htcl' from h1 host
hping3 exec receive_ICMPv1.htcl
3) Send icmp packets of different types and codes using tcl script 'send_ICMP.htcl' from h0 host
hping3 exec send_ICMPv1.htcl
Type and code fields of ICMP packets are captured and printed at h1 host
Comments
Post a Comment