Sunday, May 12, 2013

PyTapDEMon - Part 5 Misc Tools

I developed various scripts and tools for the project with the intention of making them into more baked features. However, they remained to be half-baked at this time. I am putting them in this post for now, if they ever grow into a post of their own I will remove them from here.

### DPKT Ping Test meant to be executed from h1 ###

** Script **

#!/usr/bin/env python
#
# Example from:
# http://jon.oberheide.org/blog/2008/08/25/dpkt-tutorial-1-icmp-echo/
#
# More documentation:
# http://www.commercialventvac.com/dpkt.html#mozTocId305148
#
# This file sends ICMP packet to the destination host.
# This is meant to be run on h1 to generate interesting traffic.
# It has the same affect as Mininet "h1 ping h2"
#

import dpkt
import socket, random

echo = dpkt.icmp.ICMP.Echo()
echo.id = random.randint(0, 0xffff)
echo.seq = random.randint(0, 0xffff)
echo.data = 'hello world'

icmp = dpkt.icmp.ICMP()
icmp.type = dpkt.icmp.ICMP_ECHO
icmp.data = echo

destination = '10.0.0.2'
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, dpkt.ip.IP_PROTO_ICMP)
s.connect((destination, 1))
sent = s.send(str(icmp))

print 'sent %d bytes to %s' % (sent, destination)

** end script **
** output **


root@mininet-vm:~/PyTapDEMON/tests# ./dpktTest_h1_ping.py
sent 19 bytes to 10.0.0.2
root@mininet-vm:~/PyTapDEMON/tests#

### Scapy Ping Test meant to be executed from h1 ###

** Script **

#!/usr/bin/env python
#
# Example from:
# http://www.secdev.org/projects/scapy/build_your_own_tools.html
#
# This is meant to be executed on h1 to ping toward h2
#

from scapy.all import sr1, IP, ICMP

dstIP = '10.0.0.2'

packet = sr1(IP(dst=dstIP)/ICMP())
if packet:
    packet.show()

** end script **
** output **


root@mininet-vm:~/PyTapDEMON/tests# ./scapyTest_h1_ping.py
WARNING: No route found for IPv6 destination :: (no default route?)
Begin emission:
.*Finished to send 1 packets.

Received 2 packets, got 1 answers, remaining 0 packets
###[ IP ]###
  version   = 4L
  ihl       = 5L
  tos       = 0x0
  len       = 28
  id        = 51838
  flags     =
  frag      = 0L
  ttl       = 64
  proto     = icmp
  chksum    = 0x9c60
  src       = 10.0.0.2
  dst       = 10.0.0.1
  \options   \
###[ ICMP ]###
     type      = echo-reply
     code      = 0
     chksum    = 0xffff
     id        = 0x0
     seq       = 0x0
root@mininet-vm:~/PyTapDEMON/tests#

### Scapy Ethernet Sniffing Test meant to be executed on h11 ###

** Script **

#!/usr/bin/env python
#
# This test passively sniffs
# and look for Ethernet packets
# using Scapy.
#
# This is meant to be used on the sniffing
# host, h11 in the PyTapDEMon prototype
# setup.
#

from scapy.all import *

def parsePacket(pkt):
    if pkt.haslayer(Ether):
         print "Found Ethernet packet"
         print str(pkt[Ether].dst)

if __name__ == "__main__":
    sniff(prn=parsePacket, store=0)

** end script **
** output **

1. The tool sits quite until h1 pings h2:

mininet> h1 ping -c3 h2
PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.
64 bytes from 10.0.0.2: icmp_req=1 ttl=64 time=0.316 ms
64 bytes from 10.0.0.2: icmp_req=2 ttl=64 time=0.268 ms
64 bytes from 10.0.0.2: icmp_req=3 ttl=64 time=0.266 ms

--- 10.0.0.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 0.266/0.283/0.316/0.026 ms
mininet> 

root@mininet-vm:~/PyTapDEMON/tests# ./scapyTest_eth_sniff.py
WARNING: No route found for IPv6 destination :: (no default route?)
Found Ethernet packet
2e:40:29:25:03:45
Found Ethernet packet
12:fd:b3:e2:3c:f7
Found Ethernet packet
12:fd:b3:e2:3c:f7
Found Ethernet packet
2e:40:29:25:03:45
Found Ethernet packet
12:fd:b3:e2:3c:f7
Found Ethernet packet
12:fd:b3:e2:3c:f7
Found Ethernet packet
2e:40:29:25:03:45
Found Ethernet packet
2e:40:29:25:03:45
Found Ethernet packet
12:fd:b3:e2:3c:f7
Found Ethernet packet
12:fd:b3:e2:3c:f7
Found Ethernet packet
2e:40:29:25:03:45
Found Ethernet packet
2e:40:29:25:03:45
Found Ethernet packet
12:fd:b3:e2:3c:f7
Found Ethernet packet
12:fd:b3:e2:3c:f7
Found Ethernet packet
2e:40:29:25:03:45
Found Ethernet packet
2e:40:29:25:03:45

### Twisted EchoServer and EchoClient ###

** Echoserver **
#
# Example taken from:
# Twisted Network Programming Essentials, 2nd Edition
# By: Jessica McKellar, Abe Fettig
# OReilly Media, Ebook ISBN:978-1-4493-3330-0
#

from twisted.internet import protocol, reactor
import json

class Echo(protocol.Protocol):
    def dataReceived(self, data):
        print "Received This Data: ", data
        ports = json.loads(data)
        for key in ports: 
            print key, ports[key]
        self.transport.write(data)

class EchoFactory(protocol.Factory):
    def buildProtocol(self, addr):
        print "Received from addr: ", addr
        return Echo()

reactor.listenTCP(8000, EchoFactory())
reactor.run()

*** EchoClient ***
#
# Example taken from:
# Twisted Network Programming Essentials, 2nd Edition
# By: Jessica McKellar, Abe Fettig
# OReilly Media, Ebook ISBN:978-1-4493-3330-0
#

from twisted.internet import reactor, protocol
import json

msg = json.dumps({"s1": {'s1MirrorSrc': [1], 's1MirrorDst': [6,7]}, "s2": {'s2MirrorSrc': [2], 's2MrrorDst': [7]}})

class EchoClient(protocol.Protocol):
    def connectionMade(self):
        print "Sending: ", msg
        self.transport.write(msg)

    def dataReceived(self, data):
        print "Server returned:", data
        self.transport.loseConnection()

class EchoFactory(protocol.ClientFactory):
    def buildProtocol(self, addr):
        print "Build on addr: ", addr
        return EchoClient()

    def clientConnectionFailed(self, connector, reason):
        print "Connection failed."
        reactor.stop()

    def clientConnctionLost(self, connector, reason):
        print "Connection lost."
        reactor.stop()

reactor.connectTCP("localhost", 8000, EchoFactory())
reactor.run()

*** output ***

1. start echoserver:
mininet@mininet-vm:~/PyTapDEMON/twisted$ python echoserver.py 

2. execute echoclient:
mininet@mininet-vm:~/PyTapDEMON/twisted$ python echoclient.py 
Build on addr:  IPv4Address(TCP, '127.0.0.1', 8000)
Sending:  {"s2": {"s2MrrorDst": [7], "s2MirrorSrc": [2]}, "s1": {"s1MirrorDst": [6, 7], "s1MirrorSrc": [1]}}
Server returned: {"s2": {"s2MrrorDst": [7], "s2MirrorSrc": [2]}, "s1": {"s1MirrorDst": [6, 7], "s1MirrorSrc": [1]}}

3. echoserver output:
Received from addr:  IPv4Address(TCP, '127.0.0.1', 35325)
Received This Data:  {"s2": {"s2MrrorDst": [7], "s2MirrorSrc": [2]}, "s1": {"s1MirrorDst": [6, 7], "s1MirrorSrc": [1]}}
s2 {u's2MrrorDst': [7], u's2MirrorSrc': [2]}
s1 {u's1MirrorDst': [6, 7], u's1MirrorSrc': [1]}








No comments:

Post a Comment