Beispiel #1
0
    def recv(self, bufsize=DEFAULT_PACKET_BUFFER_SIZE):
        """
        Receives a diverted packet that matched the filter.

        The remapped function is WinDivertRecv::

            BOOL WinDivertRecv(
                __in HANDLE handle,
                __out PVOID pPacket,
                __in UINT packetLen,
                __out_opt PWINDIVERT_ADDRESS pAddr,
                __out_opt UINT *recvLen
            );

        For more info on the C call visit: http://reqrypt.org/windivert-doc.html#divert_recv

        :return: The return value is a `pydivert.Packet`.
        """
        if self._handle is None:
            raise RuntimeError("WinDivert handle is not open")

        packet = bytearray(bufsize)
        packet_ = (c_char * bufsize).from_buffer(packet)
        address = windivert_dll.WinDivertAddress()
        recv_len = c_uint(0)
        windivert_dll.WinDivertRecv(self._handle, packet_, bufsize, byref(address), byref(recv_len))
        return Packet(
            memoryview(packet)[:recv_len.value],
            (address.IfIdx, address.SubIfIdx),
            Direction(address.Direction)
        )
Beispiel #2
0
 def wd_addr(self):
     """
     Gets the interface and direction as a `WINDIVERT_ADDRESS` structure.
     :return: The `WINDIVERT_ADDRESS` structure.
     """
     address = windivert_dll.WinDivertAddress()
     address.IfIdx, address.SubIfIdx = self.interface
     address.Direction = self.direction
     return address
Beispiel #3
0
 def wd_addr(self):
     """
     Gets the interface and direction as a `WINDIVERT_ADDRESS` structure.
     :return: The `WINDIVERT_ADDRESS` structure.
     """
     address = windivert_dll.WinDivertAddress()
     address.Timestamp = self.qpctimestamp
     address.IfIdx, address.SubIfIdx = self.interface
     address.Direction = self.direction
     address.Loopback = self.loopback
     address.Impostor = self.impostor
     address.PseudoIPChecksum, address.PseudoTCPChecksum, address.PseudoUDPChecksum = self.checksumflag
     return address
Beispiel #4
0
    def send(self, packet, recalculate_checksum=True):
        """
        Injects a packet into the network stack.
        Recalculates the checksum before sending unless recalculate_checksum=False is passed.
        The return value is the number of bytes actually sent.

        The injected packet may be one received from recv(), or a modified version, or a completely new packet.
        Injected packets can be captured and diverted again by other WinDivert handles with lower priorities.

        The remapped function is WinDivertSend::

            BOOL WinDivertSend(
                __in HANDLE handle,
                __in PVOID pPacket,
                __in UINT packetLen,
                __in PWINDIVERT_ADDRESS pAddr,
                __out_opt UINT *sendLen
            );

        For more info on the C call visit: http://reqrypt.org/windivert-doc.html#divert_send
        """
        if recalculate_checksum:
            packet.recalculate_checksums()

        address = windivert_dll.WinDivertAddress()
        address.IfIdx, address.SubIfIdx = packet.interface
        address.Direction = packet.direction

        send_len = c_uint(0)
        if PY2:
            # .from_buffer(memoryview) does not work on PY2
            buff = bytearray(packet.raw)
        else:
            buff = packet.raw
        buff = (c_char * len(packet.raw)).from_buffer(buff)
        windivert_dll.WinDivertSend(self._handle, buff, len(packet.raw),
                                    byref(address), byref(send_len))
        return send_len