diff --git a/raw_python/lib/Tcp.py b/raw_python/lib/Tcp.py index b5098c9..549b261 100755 --- a/raw_python/lib/Tcp.py +++ b/raw_python/lib/Tcp.py @@ -154,7 +154,7 @@ def create_tcp_feilds(self): self.tcp_ack_seq = self.ack # ---- [ Header Length ] - self.tcp_hdr_len = 80 + self.tcp_hdr_len = 20 # ---- [ TCP Flags ] f = self.flags @@ -184,3 +184,4 @@ def create_tcp_feilds(self): self.tcp_urg_ptr = 0 return + diff --git a/raw_python/lib/Udp.py b/raw_python/lib/Udp.py index bca490e..2f948df 100755 --- a/raw_python/lib/Udp.py +++ b/raw_python/lib/Udp.py @@ -34,3 +34,109 @@ # TODO: complete this + +import socket +import struct + +from ..samples import utils + +UDP_STRUCTURE_FMT = '!HHHH' + +class UDPPacket: + def __init__(self, + dport=80, + sport=65535, + dst='127.0.0.1', + src=utils.get_ip(), # '192.168.1.101', + data='' + ): + self.dport = dport + self.sport = sport + self.src_ip = src + self.dst_ip = dst + self.data = data + self.raw = None + self.create_udp_feilds() + self.assemble_udp_feilds() + # self.calculate_chksum() + # self.reassemble_udp_feilds() + + def assemble_udp_feilds(self): + self.raw = struct.pack('!HHHH', # Data Structure Representation + self.sport, # Source Port + self.dport, # Destination Port + self.udp_len, # Total Length + self.udp_chksum, # UDP + ) + + self.calculate_chksum() # Call Calculate CheckSum + return + + def reassemble_udp_feilds(self): + self.raw = struct.pack(UDP_STRUCTURE_FMT, + self.udp_src, + self.udp_dst, + self.udp_len, + socket.htons(self.udp_chksum) + ) + return + + def calculate_chksum(self): + src_addr = socket.inet_aton(self.src_ip) + dest_addr = socket.inet_aton(self.dst_ip) + placeholder = 0 + protocol = socket.IPPROTO_UDP + self.udp_len = len(self.raw) + len(self.data) + + psh = struct.pack('!4s4sBBH', + src_addr, + dest_addr, + placeholder, + protocol, + self.udp_len + ) + + psh = ''.join([psh, self.raw, self.data]) + + self.udp_chksum = self.chksum(psh) + + self.reassemble_udp_feilds() + + return + + def chksum(self, msg): + s = 0 # Binary Sum + + # loop taking 2 characters at a time + for i in range(0, len(msg), 2): + if (i + 1) < len(msg): + a = ord(msg[i]) + b = ord(msg[i + 1]) + s = s + (a + (b << 8)) + elif (i + 1) == len(msg): + s += ord(msg[i]) + else: + raise Exception("Something Wrong here") + + s = (s >> 16) + (s & 0xffff) + # One's Complement + s = s + (s >> 16) + s = ~s & 0xffff + + return s + + def create_udp_feilds(self): + + # ---- [ Source Port ] + self.udp_src = self.sport + + # ---- [ Destination Port ] + self.udp_dst = self.dport + + # ---- [ Header Length ] + self.udp_len = 16 + + # ---- [ UDP CheckSum ] + self.udp_chksum = 0 + + return diff --git a/traceroute.py b/traceroute.py new file mode 100644 index 0000000..7812cd2 --- /dev/null +++ b/traceroute.py @@ -0,0 +1,60 @@ +import ping +import socket +import time + +def main(): + ttl = 1 + tries = 3 + arrived = False + ipfound = False + + # create socket + s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP) + + # take Input + addr = input("Enter Domain Name : ") or "www.sustc.edu.cn" + print('traceroute {0} ({1})'.format(addr, socket.gethostbyname(addr))) + print("No. Address rtts") + + + while not arrived: + s.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl) + + for i in range(0, tries): + # Request sent + ID = ping.single_ping_request(s, addr) + + rtt, reply, icmp_reply = ping.catch_ping_reply(s, ID, time.time()) + #print(rtt) + + if reply: + reply['length'] = reply['Total Length'] - 20 # sub header + + if i == 0: + print(str(ttl) , end=" ") + + if reply == None: + print('* ', end=" ") + continue + + if socket.gethostbyname(addr) == reply["Source Address"]: + #print("arrived") + arrived = True + + if i == 0 or ipfound == False: + print(str(reply["Source Address"]), end=" ") + ipfound = True + + print(('{0:.2f} ms '.format(rtt*1000)), end=" ") + + print("") + ttl += 1 + ipfound = False + + # close socket + s.close() + return + + +if __name__ == '__main__': + main()