def connection(self, host, port, iface): conf.iface = iface self.state = 'SYN_SENT' pprint.state(self.state) # Send SYN self.packet[IP].dst = host self.packet[TCP].dport = port self.packet[TCP].flags = 'S' res = sr1(self.packet, timeout=10) if res is None: # TODO: handle error pprint.error('Cannot reach host {host} on port {port}'.format( host=self.packet[IP].dst, port=self.packet[TCP].dport)) elif res[TCP].flags == constant.SYN | constant.ACK: self.checkAckValue(self.packet[TCP].seq + 1, res[TCP].ack) # Send ACK self.packet[TCP].seq = res[TCP].ack self.packet[TCP].ack = res[TCP].seq + 1 self.packet[TCP].flags = 'A' send(self.packet) # Start communication self.communication() else: self.deconnection() self.state = 'CLOSED' pprint.state(self.state)
def __init__(self): self.state = 'CLOSED' pprint.state(self.state) os.system('iptables -A OUTPUT -p tcp --tcp-flags RST RST -j DROP') self.packet = IP() / TCP() self.packet[TCP].sport = random.randint(1025, 65000) self.packet[TCP].seq = random.randint(1, 2048) # TODO: Check RFC
def communication(self): self.state = 'ESTABLISHED' pprint.state(self.state) # TODO: Check ack and seq value # TODO: Connexion established try: nb_msg = 0 while True: data = None # TODO: Check ack/seq and flags and reply a ACK data = sniff(filter='tcp and dst port ' + str(self.port), count=1, iface=self.iface) if data and len(data) <= 0: # TODO: Handle timeout print(colors.FAIL + '[!]\tTIMEOUT' + colors.ENDC) # TODO: Close connexion break if self.isFin(data): self.sendACK(data[0], data[0][TCP].seq + 1) time.sleep(1) self.deconnection(data) break elif self.containsMessage(data): nb_msg += 1 print(colors.BOLD + colors.WARNING + '[' + str(nb_msg) + ']: ' + data[0][Raw].load + colors.ENDC) # Send ACK ackvalue = data[0][TCP].seq + len(data[0][Raw].load) self.sendACK(data[0], ackvalue) except KeyboardInterrupt: print('Total number of message receive: ' + str(nb_msg))
def __init__(self, port): self.state = 'CLOSED' pprint.state(self.state) self.port = port self.sniffers = [] self.sessions = [] os.system('iptables -A OUTPUT -p tcp --tcp-flags RST RST -j DROP')
def connection(self, request): self.state = 'SYN_RCVD' pprint.state(self.state) self.packet[TCP].ack = request[0].seq + 1 self.packet[TCP].flags = 'SA' answer = sr1(self.packet, iface=self.iface, retry=5, timeout = 10) if answer is None: # TODO: handle error pprint.error('Did not receive the ACK for finish the connexion') elif answer[TCP].flags == constant.ACK: self.communication()
def deconnection(self): self.packet[TCP].flags = 'F' self.state = 'FIN_WAIT_1' pprint.state(self.state) fin_ack = sr1(self.packet) if fin_ack[TCP].flags == constant.ACK: filter_opt = 'tcp and dst port ' + str(self.packet[TCP].sport) data = sniff(filter=filter_opt, count=1, timeout=10) self.packet[TCP].seq = data[0][TCP].ack self.packet[TCP].ack = data[0][TCP].seq + 1 elif fin_ack[TCP].flags == constant.FIN: self.packet[TCP].seq = fin_ack[TCP].ack self.packet[TCP].ack = fin_ack[TCP].seq + 1 self.packet[TCP].flags = 'A' send(self.packet) self.state = 'FIN_WAIT_2' pprint.state(self.state) self.state = 'TIME_WAIT'
def communication(self): self.state = 'ESTABLISHED' pprint.state(self.state) try: while True: message = raw_input('Put the message you want to send: ') self.packet[TCP].flags = 'PA' ack = sr1(self.packet / Raw(message), timeout=10) if ack is None: # TODO: Handle error pprint.error('Did not received ack for data') elif self.checkAck(ack, message): self.checkAckValue(ack[TCP].ack, self.packet[TCP].seq + len(message)) self.packet[TCP].seq = ack[TCP].ack pprint.information('Message received') except KeyboardInterrupt: self.deconnection() self.state = 'CLOSED' pprint.state(self.state)
def start(self): self.state = 'LISTEN' pprint.state(self.state) try: filter_options = 'tcp and dst port ' + str( self.port ) + ' and tcp[tcpflags] & (tcp-syn|tcp-ack) == tcp-syn' for iface in netifaces.interfaces(): sniffer = AsyncSniffer(filter=filter_options, prn=self.newSession(iface), count=1, iface=iface) sniffer.start() self.sniffers.append(sniffer) pprint.information('sniffing on ' + iface) while len(self.sessions) <= 0: time.sleep(1) while True: for session in self.sessions: if session.getState() == 'ESTABLISHED': session_iface = session.getInterface() sniffer = AsyncSniffer( filter=filter_options, prn=self.newSession(session_iface), count=1, iface=session_iface) sniffer.start() pprint.information('sniffing on ' + session_iface) self.sniffers.append(sniffer) self.sessions.remove(session) except KeyboardInterrupt: for sniffer in self.sniffers: sniffer.stop()
def deconnection(self, data): self.state = 'CLOSE_WAIT' pprint.state(self.state) self.packet[TCP].seq = data[0][TCP].ack self.packet[TCP].ack = data[0][TCP].seq + 1 self.packet[TCP].flags = 'F' ackreceived = sr1(self.packet, timeout=10, iface=self.iface) if self.isAck(ackreceived): self.checkAckValue(ackreceived[TCP].ack, self.packet[TCP].seq + 1) self.state = 'LAST_ACK' pprint.state(self.state) else: pprint.error('Did not receive the ACK for finish the deconnection') self.state = 'CLOSED' pprint.state(self.state)