Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 2 Jun 2008 05:25:10 GMT
From:      Victor Hugo Bilouro <bilouro@FreeBSD.org>
To:        Perforce Change Reviews <perforce@FreeBSD.org>
Subject:   PERFORCE change 142731 for review
Message-ID:  <200806020525.m525PAJw080103@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=142731

Change 142731 by bilouro@bilouro_tcptest on 2008/06/02 05:24:27

	added copyright	
	this version do complete connection establishment and finalization
	completelly reorganized
	Especial thanks for Anre Oppermann (andre at freebsd dot org)

Affected files ...

.. //depot/projects/soc2008/bilouro_tcptest/src/scripts/tcpconnect.py#4 edit

Differences ...

==== //depot/projects/soc2008/bilouro_tcptest/src/scripts/tcpconnect.py#4 (text+ko) ====

@@ -1,3 +1,38 @@
+# Copyright (c) 2008, Victor Hugo Bilouro
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+# Redistributions of source code must retain the above copyright notice,
+# this list of conditions and the following disclaimer.
+#
+# Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# Neither the name of Victor Hugo Bilouro nor the names of its 
+# contributors may be used to endorse or promote products derived from 
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+# Author: Victor Hugo Bilouro 
+#
+# Description: A simple test of three way handshake 
+#
+
 from pcs.packets import ipv4
 from pcs.packets import tcp
 from pcs.packets import ethernet
@@ -38,172 +73,304 @@
     
     (options, args) = parser.parse_args()
 
-    ##todo: create syn. test exately equals a tcp dump. DONE
-    ##todo: wait with timer the answer possibilities (in this a simple ack-syn) DONE
-    ##todo: send an acknoledgment establishing the connection
+    import random
+
+    ipid = random.randrange(1,(1<<16)-1)
+    tcpsport = random.randrange(50000,60000) #int(options.source_port )
+    tcpsequence = random.randrange(1,(1<<32)-1)   
+    output = pcs.PcapConnector(options.interface)
+
+    replyip = None
+    replytcp = None 
+    reply = None 
+    packet = None
 
-    #
-    # creating a durty-code ether-ip-tcp-syn packet 
-    #
-    import random
+ # SYN
+    what = "SYN"
 
-    ip = ipv4.ipv4()
+    ip1 = ipv4.ipv4()
 
-    ip.version = 4                 # 0100 max 1111 15 *caution* :)
-    ip.hlen = 5                 # 0101 0 ->  min 0101 (20 bytes) 
-    ip.tos = 0     
-    ip.id = random.randrange(1,(1<<16)-1)
-    ip.flags = 0                 #study on book
-    ip.offset = 0
-    ip.ttl = 64
-    ip.protocol = pcs.IPPROTO_TCP                 #1 ICMP #6 TCP
-    ip.src = pcs.inet_atol(options.ip_source)
-    ip.dst = pcs.inet_atol(options.ip_target)
+    ip1.version = 4 
+    ip1.hlen = 5   
+    ip1.tos = 0     
+    ip1.id = ++ipid 
+    ip1.flags = 0 
+    ip1.offset = 0
+    ip1.ttl = 64
+    ip1.protocol = pcs.IPPROTO_TCP
+    ip1.src = pcs.inet_atol(options.ip_source)
+    ip1.dst = pcs.inet_atol(options.ip_target)
     
+    tcp1 = tcp.tcp()
 
-    tcppkt = tcp.tcp()
+    tcp1.sport = tcpsport 
+    tcp1.dport = int(options.destination_port)
+    tcp1.sequence = tcpsequence
+    tcpsequence = tcpsequence + 1  # SYN consumes the sequence
+    tcp1.ack_number = 0
+    tcp1.offset = 5
+    tcp1.urgent = 0
+    tcp1.ack = 0
+    tcp1.push = 0
+    tcp1.reset = 0
+    tcp1.syn = 1
+    tcp1.fin = 0
+    tcp1.window = (1<<16)-1
+    tcp1.urg_point = 0
+    #tcp1.options
 
-    tcppkt.sport = int(options.source_port )
-    tcppkt.dport = int(options.destination_port)
-    tcppkt.sequence = random.randrange(1,(1<<32)-1)   
-    tcppkt.ack_number = 0
-    tcppkt.offset = 5                           #header length
-    tcppkt.urgent = 0
-    tcppkt.ack = 0
-    tcppkt.push = 0
-    tcppkt.reset = 0
-    tcppkt.syn = 1
-    tcppkt.fin = 0
-    tcppkt.window = (1<<16)-1
-    tcppkt.urg_point = 0
-    #tcppkt.options
+    tcp1.checksum = tcp_cksum(tcp1 , ip1)
 
-    tcppkt.checksum = tcp_cksum(tcppkt , ip)
+    ip1.length = len(ip1.bytes) + len(tcp1.bytes) 
 
-    ip.length = len(ip.bytes) + len(tcppkt.bytes) 
-
     # important, only calcs the ip checksum after fill length field
-    ip.checksum = ip_cksum(ip)
+    ip1.checksum = ip_cksum(ip1)
 
-    ether = ethernet.ethernet()
-    ether.src = ethernet.ether_atob(options.ether_source)
-    ether.dst = ethernet.ether_atob(options.ether_destination)
-    ether.type = 0x800
+    ether1 = ethernet.ethernet()
+    ether1.src = ethernet.ether_atob(options.ether_source)
+    ether1.dst = ethernet.ether_atob(options.ether_destination)
+    ether1.type = 0x800
 
-    print "\n syn---------------------------------"
-    print tcppkt 
+    packet = pcs.Chain([ether1, ip1, tcp1])
+    
+    print "\n%s---------------------------------" % what
+    print tcp1 
     print "---------------------------------"
 
-    packet = pcs.Chain([ether, ip, tcppkt])
-    
-    output = pcs.PcapConnector(options.interface)
-
-    # SYN SENT
     out = output.write(packet.bytes, len(packet.bytes))
 
+## SYN
+ # SYN+ACK
+    what = "SYN+ACK"
 
+    while 1: 
+    	reply = output.read()
+    	packet = ethernet.ethernet(reply)
+   	try: 
+    		replyip = packet.data
+    		replytcp = replyip.data
+		if (ip1.src==replyip.dst and \
+			ip1.dst==replyip.src and \
+			tcp1.sport==replytcp.dport and \
+			tcp1.dport==replytcp.sport): 
+			break
+	except: #it cannot be a tcp packet (without sport:)
+		pass
 
+    print "\n%s---------------------------------" % what
+    print packet.data.data
+    print "---------------------------------"
 
+## SYN+ACK
+ # ACK 134,187
+    what = "ACK (SYN)"
 
-    reply = output.read()
-    reply = output.read()
+    ip3 = ipv4.ipv4()
+
+    ip3.version = 4 
+    ip3.hlen = 5   
+    ip3.tos = 0     
+    ip3.id = ++ipid
+    ip3.flags = 0 
+    ip3.offset = 0
+    ip3.ttl = 64
+    ip3.protocol = pcs.IPPROTO_TCP
+    ip3.src = pcs.inet_atol(options.ip_source)
+    ip3.dst = pcs.inet_atol(options.ip_target)
+    
+    tcp3 = tcp.tcp()
 
-    packet = ethernet.ethernet(reply)
-    print "\n syn+ack-----------------------------"
-    print packet.data.data
-    print "---------------------------------"
+    tcp3.sport = tcpsport 
+    tcp3.dport = int(options.destination_port)
+    tcp3.sequence = tcpsequence
+    ##tcpsequence = tcpsequence + 1  # SYN DOES NOT consumes the sequence
+    tcp3.ack_number = replytcp.sequence + 1 
+    tcp3.offset = 5
+    tcp3.urgent = 0
+    tcp3.ack = 1
+    tcp3.push = 0
+    tcp3.reset = 0
+    tcp3.syn = 0
+    tcp3.fin = 0
+    tcp3.window = (1<<16)-1
+    tcp3.urg_point = 0
+    #tcp3.options
 
-    #
-    # this commented piece dont work.. don't ask me why. (now) 
-    #
+    tcp3.checksum = tcp_cksum(tcp3 , ip3)
 
-    #tcpreply = packet.data.data
+    ip3.length = len(ip3.bytes) + len(tcp3.bytes) 
 
-    #import copy
-    #ipack = copy.deepcopy(ip)    
-    #ipack.id = ip.id + 1
+    # important, only calcs the ip checksum after fill length field
+    ip3.checksum = ip_cksum(ip3)
 
-    #tcpack = copy.deepcopy(tcppkt)    
+    ether3 = ethernet.ethernet()
+    ether3.src = ethernet.ether_atob(options.ether_source)
+    ether3.dst = ethernet.ether_atob(options.ether_destination)
+    ether3.type = 0x800
 
-    #tcpack.sequence = 0 
-    #tcpack.ack_number = tcpreply.sequence + 1
-    #tcpack.ack = 1
-    #tcpack.syn = 0
-    #tcpack.checksum = tcp_cksum(tcpack , ipack)
+    packet = pcs.Chain([ether3, ip3, tcp3])
     
-    # important, only calcs the ip checksum after fill length field
-    #ipack.checksum = ip_cksum(ipack)
-    
-    #packetreply = pcs.Chain([ether, ipack, tcpack])
-    #out = output.write(packetreply.bytes, len(packetreply.bytes))
+    print "\n%s---------------------------------" % what
+    print tcp3 
+    print "---------------------------------"
 
-    #
-    # ANOTHER. this worked! (please don't pay attention in how it was wrote, it's a very durty test, ok?)
-    #
+    out = output.write(packet.bytes, len(packet.bytes))
 
-    ipack = ipv4.ipv4()
+## ACK
+ # FIN 188,241 
+    what = "FIN"
 
-    ipack.version = 4                 # 0100 max 1111 15 *caution* :)
-    ipack.hlen = 5                 # 0101 0 ->  min 0101 (20 bytes) 
-    ipack.tos = 0     
-    ipack.id = ip.id + 1
-    ipack.flags = 0                 #study on book
-    ipack.offset = 0
-    ipack.ttl = 64
-    ipack.protocol = pcs.IPPROTO_TCP                 #1 ICMP #6 TCP
-    ipack.src = pcs.inet_atol(options.ip_source)
-    ipack.dst = pcs.inet_atol(options.ip_target)
+    ip4 = ipv4.ipv4()
 
-    tcpreply = packet.data.data
-    tcpack = tcp.tcp()
+    ip4.version = 4 
+    ip4.hlen = 5   
+    ip4.tos = 0     
+    ip4.id = ++ipid
+    ip4.flags = 0 
+    ip4.offset = 0
+    ip4.ttl = 64
+    ip4.protocol = pcs.IPPROTO_TCP
+    ip4.src = pcs.inet_atol(options.ip_source)
+    ip4.dst = pcs.inet_atol(options.ip_target)
+    
+    tcp4 = tcp.tcp()
 
-    tcpack.sport = int(options.source_port )
-    tcpack.dport = int(options.destination_port)
-    tcpack.sequence = 0
-    tcpack.ack_number = tcpreply.sequence + 1
-    tcpack.offset = 5                           #header length
-    tcpack.urgent = 0
-    tcpack.ack = 1
-    tcpack.push = 0
-    tcpack.reset = 0
-    tcpack.syn = 0
-    tcpack.fin = 0
-    tcpack.window = (1<<16)-1
-    tcpack.urg_point = 0
-    #tcpack.options
+    tcp4.sport = tcpsport 
+    tcp4.dport = int(options.destination_port)
+    tcp4.sequence = tcpsequence
+    tcpsequence = tcpsequence + 1  # FIN consumes the sequence
+    tcp4.ack_number = replytcp.sequence + 1 
+    tcp4.offset = 5
+    tcp4.urgent = 0
+    tcp4.ack = 1
+    tcp4.push = 0
+    tcp4.reset = 0
+    tcp4.syn = 0
+    tcp4.fin = 1
+    tcp4.window = (1<<16)-1
+    tcp4.urg_point = 0
+    #tcp4.options
 
-    tcpack.checksum = tcp_cksum(tcpack , ipack)
+    tcp4.checksum = tcp_cksum(tcp4 , ip4)
 
-    ipack.length = len(ipack.bytes) + len(tcpack.bytes) 
+    ip4.length = len(ip4.bytes) + len(tcp4.bytes) 
 
     # important, only calcs the ip checksum after fill length field
-    ipack.checksum = ip_cksum(ipack)
+    ip4.checksum = ip_cksum(ip4)
+
+    ether4 = ethernet.ethernet()
+    ether4.src = ethernet.ether_atob(options.ether_source)
+    ether4.dst = ethernet.ether_atob(options.ether_destination)
+    ether4.type = 0x800
+
+    packet = pcs.Chain([ether4, ip4, tcp4])
+    
+    print "\n%s---------------------------------" % what
+    print tcp4 
+    print "---------------------------------"
+
+    out = output.write(packet.bytes, len(packet.bytes))
 
-    etherack = ethernet.ethernet()
-    etherack.src = ethernet.ether_atob(options.ether_source)
-    etherack.dst = ethernet.ether_atob(options.ether_destination)
-    etherack.type = 0x800
+## FIN
+ # ACK (FIN) 
+    what = "ACK (FIN)"
 
-    packetack = pcs.Chain([etherack, ipack, tcpack])
-    out = output.write(packetack.bytes, len(packetack.bytes))
+    while 1: 
+    	reply = output.read()
+    	packet = ethernet.ethernet(reply)
+   	try: 
+    		replyip = packet.data
+    		replytcp = replyip.data
+		if (ip1.src==replyip.dst and \
+			ip1.dst==replyip.src and \
+			tcp1.sport==replytcp.dport and \
+			tcp1.dport==replytcp.sport): 
+			break
+	except: #it cannot be a tcp packet (without sport:)
+		pass
 
-    print "\n ack---------------------------------"
-    print tcpack 
+    print "\n%s---------------------------------" % what
+    print packet.data.data
     print "---------------------------------"
-    # /ANOTHER
+
+## ACK (FIN)
+ # FIN 
+    what = "FIN"
 
-    reply = output.read()
     while 1: 
-	try: 
-		reply = output.read()
-    		packet = ethernet.ethernet(reply)
-		if packet.data.data.sport == 22022:
-    			print "\n-----------------------------"
-    			print packet.data.data
-    			print "---------------------------------"
-	except:
+    	reply = output.read()
+    	packet = ethernet.ethernet(reply)
+   	try: 
+    		replyip = packet.data
+    		replytcp = replyip.data
+		if (ip1.src==replyip.dst and \
+			ip1.dst==replyip.src and \
+			tcp1.sport==replytcp.dport and \
+			tcp1.dport==replytcp.sport): 
+			break
+	except: #it cannot be a tcp packet (without sport:)
 		pass
 
+    print "\n%s---------------------------------" % what
+    print packet.data.data
+    print "---------------------------------"
+
+## FIN
+ # ACK (FIN) 288,341
+    what = "ACK (FIN)"
+
+    ip7 = ipv4.ipv4()
+
+    ip7.version = 4 
+    ip7.hlen = 5   
+    ip7.tos = 0     
+    ip7.id = ++ipid
+    ip7.flags = 0 
+    ip7.offset = 0
+    ip7.ttl = 64
+    ip7.protocol = pcs.IPPROTO_TCP
+    ip7.src = pcs.inet_atol(options.ip_source)
+    ip7.dst = pcs.inet_atol(options.ip_target)
+    
+    tcp7 = tcp.tcp()
+
+    tcp7.sport = tcpsport 
+    tcp7.dport = int(options.destination_port)
+    tcp7.sequence = tcpsequence
+    ##tcpsequence = tcpsequence + 1  # SYN DOES NOT consumes the sequence
+    tcp7.ack_number = replytcp.sequence + 1 
+    tcp7.offset = 5
+    tcp7.urgent = 0
+    tcp7.ack = 1
+    tcp7.push = 0
+    tcp7.reset = 0
+    tcp7.syn = 0
+    tcp7.fin = 0
+    tcp7.window = (1<<16)-1
+    tcp7.urg_point = 0
+    #tcp7.options
+
+    tcp7.checksum = tcp_cksum(tcp7 , ip7)
+
+    ip7.length = len(ip7.bytes) + len(tcp7.bytes) 
+
+    # important, only calcs the ip checksum after fill length field
+    ip7.checksum = ip_cksum(ip7)
+
+    ether7 = ethernet.ethernet()
+    ether7.src = ethernet.ether_atob(options.ether_source)
+    ether7.dst = ethernet.ether_atob(options.ether_destination)
+    ether7.type = 0x800
+
+    packet = pcs.Chain([ether7, ip7, tcp7])
+    
+    print "\n%s---------------------------------" % what
+    print tcp7 
+    print "---------------------------------"
+
+    out = output.write(packet.bytes, len(packet.bytes))
+
+## ACK (FIN)
 
 def tcp_cksum(self, ip, data = ""):  #TODO: add this method to pcs tcp.py
     """return tcpv4 checksum"""



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200806020525.m525PAJw080103>