def handle_ul(self, ipbuf): # check if we have a TCP SYN ip_proto, ip_pay = ord(ipbuf[9:10]), ipbuf[20:] if ip_proto != 6: # not TCP return if ip_pay[13:14] != b'\x02': # not SYN return # build the TCP SYN-ACK: invert src / dst ports, seq num (random), # ack num (SYN seq num + 1) tcpsrc, tcpdst, seq = unpack('!HHI', ip_pay[:8]) tcp_synack = TCP(val={ 'seq': randint(1, 4294967295), 'ack': (1 + seq) % 4294967296, 'src': tcpdst, 'dst': tcpsrc, 'SYN': 1, 'ACK': 1, 'win': 0x1000 }, hier=1) # build the IPv4 header: invert src / dst addr ipsrc, ipdst = inet_ntoa(ipbuf[12:16]), inet_ntoa(ipbuf[16:20]) iphdr = IPv4(val={'src': ipdst, 'dst': ipsrc}, hier=0) # pkt = Envelope('p', GEN=(iphdr, tcp_synack)) # send back the TCP SYN-ACK self.GTPUd.transfer_to_int(pkt.to_bytes())
def handle_ul(self, ipbuf): # check if we have an UDP/53 request ip_proto, (udpsrc, udpdst) = ord(ipbuf[9]), unpack('!HH', ipbuf[20:24]) if ip_proto != 17: # not UDP return if udpdst != 53: # not DNS return # build the UDP / DNS response: invert src / dst UDP ports if self.UDP_CS: udp = UDP(val={'src': udpdst, 'dst': udpsrc}, hier=1) else: udp = UDP(val={'src': udpdst, 'dst': udpsrc, 'cs': 0}, hier=1) # DNS request: transaction id, flags, questions, queries dnsreq = ipbuf[28:] transac_id, questions, queries = dnsreq[0:2], \ unpack('!H', dnsreq[4:6])[0], \ dnsreq[12:] if questions > 1: # not supported self._log('WNG', '%i questions, unsupported' % questions) # DNS response: transaction id, flags, questions, answer RRs, # author RRs, add RRs, queries, answers, autor nameservers, add records if self.RAND: ip_resp = _urandom(4) else: ip_resp = inet_aton(self.IP_RESP) dnsresp = b''.join( (transac_id, b'\x81\x80\0\x01\0\x01\0\0\0\0', queries, b'\xc0\x0c\0\x01\0\x01\0\0\0\x20\0\x04', ip_resp)) # build the IPv4 header: invert src / dst addr ipsrc, ipdst = inet_ntoa(ipbuf[12:16]), inet_ntoa(ipbuf[16:20]) iphdr = IPv4(val={'src': ipdst, 'dst': ipsrc}, hier=0) # pkt = Envelope('p', GEN=(iphdr, udp, Buf('dns', val=dnsresp, hier=2))) # send back the DNS response self.GTPUd.transfer_to_int(pkt.to_bytes())