def ingress_loop(packet): raw_packet = packet.get_payload() network = IP(raw_packet) src_ip = inet_to_str(network.src) dst_ip = inet_to_str(network.dst) if src_ip not in TRACKED_CLIENTS: return packet.accept() if not is_tor(network): return packet.accept() print('tracked client trying connect to tor') network.rf = 1 if dst_ip in KNOWN_PEERS: raw_packet = update_cksum(network) packet.set_payload(raw_packet) return packet.accept() peer = random.choice(KNOWN_PEERS) network.dst = str_to_inet(peer) raw_packet = update_cksum(network) packet.set_payload(raw_packet) return packet.accept()
def egress_loop(packet): global connections global blacklist global client_log now = datetime.now() raw_packet = packet.get_payload() network = IP(raw_packet) # modify the packet all you want here # packet.set_payload(str(pkt)) #set the packet content to our modified version transport = network.data src_ip = inet_to_str(network.src) dst_ip = inet_to_str(network.dst) flow = (src_ip, transport.sport, dst_ip, transport.dport) # if flow[3] in [443]: # print('[drop] {}:{} -> {}:{}'.format(flow[0], flow[1], flow[2], flow[3])) # packet.drop() # return if flow in connections: connections[flow] = connections[flow] + transport.data else: connections[flow] = transport.data flow_addresses = '{}:{},{}:{}'.format(src_ip, transport.sport, dst_ip, transport.dport) print(flow_addresses) tracked_client_arrived = client_log.arrived_near(now) # if network.rf or (tracked_client_arrived and dst_ip in KNOWN_PEERS): if tracked_client_arrived and dst_ip in KNOWN_PEERS: print('no RF, setting...') network.rf = 1 network.sum = 0 packet.set_payload(bytes(network)) if transport.dport not in [80]: packet.accept() # if is_marked: print(packet.get_payload()) return try: stream = connections[flow] http = Request(stream) # if src_ip in blacklist: # bad_ip = src_ip # elif dst_ip in blacklist: # bad_ip = dst_ip # else: # bad_ip = 'not listed' bad_host = http.headers['host'] print(flow) if tracked_client_arrived and bad_host in blacklist: print('[drop] blacklisted host: {}, IP: {}'.format( bad_host, dst_ip)) del connections[flow] return packet.drop() # If we reached this part an exception hasn't been thrown stream = stream[len(http):] if len(stream) == 0: del connections[flow] else: connections[flow] = stream except UnpackError: pass packet.accept() # if is_marked: print(packet.get_payload()) return