def runDivert(): from pydivert import WinDivert with WinDivert("tcp.SrcPort == 443 and tcp.PayloadLength == 0") as w: try: for packet in w: packet.tcp.rst = False w.send(packet) except: w.close()
def DivertRST(): while True: with WinDivert("tcp.SrcPort == 443 and tcp.PayloadLength == 0") as w: try: for packet in w: packet.tcp.rst = False w.send(packet) except: w.close()
def recv(self, handle: pydivert.WinDivert) -> pydivert.Packet: """ Convenience function that receives a packet from the passed handler and handles error codes. If the process has been shut down, (None, None) is returned. """ try: return handle.recv() except WindowsError as e: if e.winerror == 995: return None else: raise
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.w = WinDivert("ip.DstAddr == {0} || ip.SrcAddr == {0}".format( self.ip))
class WinDivertDriver(DriverBase): """Base implementation of a driver (ip scanner). Uses WinDivert to do its job. Scans a single IP for different connections and identifies them by local ip and port. Parameters ---------- network: :class:`network.scanner.NetworkScanner` The network scanner this driver belongs to to ip: :class:`str` The ip to scan connection: :class:`network.connection.Connection` The connection factory used to represent a connection Attributes ---------- network: :class:`network.scanner.NetworkScanner` The network scanner this driver belongs to to ip: :class:`str` The ip to scan connection: :class:`network.connection.Connection` The connection factory used to represent a connection connections: Dict[:class:`network.connection.Connection`] The connection list w: :class:`pydivert.WinDivert` The WinDivert instance """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.w = WinDivert("ip.DstAddr == {0} || ip.SrcAddr == {0}".format( self.ip)) def scan(self): """A loop that scans the ip until the scanner is closed. """ try: with self.w: for packet in self.w: conn = self.get_connection( (packet.src_addr, packet.src_port), (packet.dst_addr, packet.dst_port), packet.is_outbound) if packet.tcp.fin: conn.close() elif conn.ignored: self.w.send(packet) continue elif packet.payload: if not conn.parse_packet(packet.payload, packet.is_outbound): continue self.w.send(packet) except OSError as e: if e.errno == errno.EACCES: # Missing privileges raise Exception("Run me with admin privileges!") from e elif e.errno == errno.EBADF: # Invalid driver (closed) return raise finally: for conn in self.connections.values(): conn.close() self.close() def close(self): """Closes the scanner. """ if self.w.is_open: try: self.w.close() except Exception: pass # If the thread is stuck waiting for a packet, this will # make it detect a new packet, so it will end. This is # sent using UDP to make it quicker. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(b"\x00", (self.ip, 6666))