
    h $                         d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZmZ ddlmZ dd	lmZmZ dd
lmZ ddlmZmZmZmZmZ  G d de          Z G d de          ZdS )a~  
Native Microsoft Windows sockets (L3 only)

This uses Raw Sockets from winsock
https://learn.microsoft.com/en-us/windows/win32/winsock/tcp-ip-raw-sockets-2

.. note::

    Don't use this module.
    It is a proof of concept, and a worse-case-scenario failover, but you should
    consider that raw sockets on Windows don't work and install Npcap to avoid using
    it at all cost.
    Nselect_objects)raw)conf)MTU)Scapy_Exceptionlog_runtime)Packet)resolve_iface_GlobInterfaceType)SuperSocket)AnyListOptionalTupleTypec                   x     e Zd ZdZdZdZdZg dZ	 	 	 	 ddZd	 Z	e
fd
Ze
fdZ fdZedd            Z xZS )L3WinSocketaB  
    A L3 raw socket implementation native to Windows.

    Official "Windows Limitations" from MSDN:
        - TCP data cannot be sent over raw sockets.
        - UDP datagrams with an invalid source address cannot be sent over raw sockets.
        - For IPv6 (address family of AF_INET6), an application receives everything
          after the last IPv6 header in each received datagram [...]. The application
          does not receive any IPv6 headers using a raw socket.

    Unofficial limitations:
        - Turns out we actually don't see any incoming TCP data, only the outgoing.
          We do properly see UDP, ICMP, etc. both ways though.
        - To match IPv6 responses, one must use `conf.checkIPaddr = False` as we can't
          get the real destination.

    **To overcome those limitations, install Npcap.**
    z0a native Layer 3 (IPv4) raw socket under WindowsT)promiscclsipv6N   Fc                    ddl m} ddlm} |D ]}t	          j        d|z             |rt          |          pt          j        | _        | j        	                                st	          j        d           |rt          j        nt          j        }	|| _        |r|n|| _        |t          j        }|| _        	 t          j
        |	t          j        t          j                  x| _        | _        n-# t*          $ r }
|
j        dk    rt+          d           d }
~
ww xY w| j                            t          j        t          j        d           | j                            t          j        t          j        d	           | j                            t          j        t          j        d	           | j                            t          j        t          j        |           |r| j                            t          j        t          j        d           	 | j                            t          j        t          j        d           | j                            t          j        t          j         d           n# t*          t          j!        f$ r Y nw xY w| j                            t          j        t          j        d           	 | j                            t          j        t          j"        d           n# t*          t          j!        f$ r Y nw xY w	 | j                            t          j        t          j#        d           n# t*          t          j!        f$ r Y nw xY w|rdd
l$m%}  || j                  }nddl$m&}  || j                  }| j        '                    |pt          j(                    df           |r1| j        )                    t          j*        t          j+                   d S d S )Nr   IPIPv6zDropping unsupported option: %sz%Interface is invalid. This will fail.   zeWindows native L3 Raw sockets are only usable as administrator ! Please install Npcap to workaround !   i   @)get_if_addr6)get_if_addr),scapy.layers.inetr   scapy.layers.inet6r   r	   warningr   r   ifaceis_validsocketAF_INET6AF_INETr   r   sniff_promiscr   SOCK_RAW
IPPROTO_IPoutsinsOSErrorerrno
setsockopt
SOL_SOCKETSO_REUSEADDR	SO_RCVBUF	SO_SNDBUFIP_TTLIPPROTO_IPV6
IP_HDRINCLIPV6_RECVTCLASSIPV6_HOPLIMITerrorIP_RECVDSTADDR
IP_RECVTTL
scapy.archr    r!   bindgethostnameioctl
SIO_RCVALL	RCVALL_ON)selfr%   ttlr   r   kwargsr   r   kwargafer    hostr!   s                 ]/mounts/lovelace/software/anaconda3/lib/python3.11/site-packages/scapy/arch/windows/native.py__init__zL3WinSocket.__init__F   s    	)(((((++++++ 	K 	KE AE IJJJJ3}U33Atz
z""$$ 	I GHHH $8V__&.	'44R?(G	#)=!$ $ DI
  	 	 	w"}} E F F F 	 	F-v/BAFFFF-v/?GGG	V.0@%HHHF-v}cBBB 	I  !4f6GKKK##F$7$*$:A? ? ?##F$7$*$8!= = = =V\*    H 163DaHHH##F$5$*$91> > > >V\*   ##%%   
 V\*     	+//////<
++DD......;tz**Dt3v133Q7888  	@HNN6,f.>?????	@ 	@sO   :6C1 1
D;DDA I2 2J
J?0K0 0L	L	0L> >MMc                 &   t          |          }| j        |vrt          d          ddlm} ||v rt          d          | j        st          d          t          || j                 j                  }| j                            ||df          S )NzNL3WinSocket can only send IP/IPv6 packets ! Install Npcap/Winpcap to send morer   )TCPzw'TCP data cannot be sent over raw socket': https://learn.microsoft.com/en-us/windows/win32/winsock/tcp-ip-raw-sockets-2zSocket not created)	r   r   r   r"   rN   r-   strdstsendto)rD   xdatarN   dst_ips        rK   sendzL3WinSocket.send   s    1vv81! #H I I I))))))!88!_   y 	8!"6777Qtx[_%%yvqk222    c                 N    	 |                                  S # t          $ r Y d S w xY wN)recvIOError)rD   rR   s     rK   nonblock_recvzL3WinSocket.nonblock_recv   s7    	99;; 	 	 	44	s    
$$c                 d   	 | j                             |          \  }}n# t          j        $ r Y dS w xY w| j        rddlm} |\  }}}}t          |          dk    rAt          j	        d|dd                   d         t          |          k    rt          j        }n3|r%|d         t          dd          v rt          j        }nt          j        }t           ||d	||pd
t          |                              }	||	|z   t!          j                    fS ddlm}
 |
|t!          j                    fS )N)NNNr   r      z!H   r      z::   )srcrP   flnhplenr   )r.   recvfromioBlockingIOErrorr   r#   r   lenstructunpackr'   IPPROTO_UDPrangeIPPROTO_ICMPV6IPPROTO_TCPr   timer"   r   )rD   rR   rS   addressr   rJ   _flowinfoprotoheaderr   s              rK   recv_rawzL3WinSocket.recv_raw   sc   	$ H--a00MD''! 	$ 	$ 	$###	$9 	)////// $+ D!Xq 4yy1}}tT!A#Y!?!?!Bc$ii!O!O* +$q'U3__44-*}T   F $	33,,,,,,tTY[[((s     33c                     | j         s6| j        r/| j                            t          j        t          j                   t          t          |           	                                 d S rX   )
closedr   r.   rA   r'   rB   
RCVALL_OFFsuperr   close)rD   	__class__s    rK   rz   zL3WinSocket.close   sV    { 	At| 	AHNN6,f.?@@@k4  &&(((((rV   c                 "    t          | |          S rX   r   )socketsremains     rK   selectzL3WinSocket.select   s     gv...rV   )Nr   FTrX   )__name__
__module____qualname____doc__descnonblocking_socket__selectable_force_select__	__slots__rL   rU   r   r[   ru   rz   staticmethodr   __classcell__r{   s   @rK   r   r   .   s         $ >D"&***I 	U@ U@ U@ U@n3 3 3" "       ) ) ) )B) ) ) ) ) / / / \/ / / / /rV   r   c                   "     e Zd ZdZ fdZ xZS )L3WinSocket6z0a native Layer 3 (IPv6) raw socket under Windowsc                 L     t          t          |           j        dddi| d S )Nr   T )ry   r   rL   )rD   rF   r{   s     rK   rL   zL3WinSocket6.__init__   sC    *lD!!* 	
 	
	
	
 	
 	
 	
 	
rV   )r   r   r   r   rL   r   r   s   @rK   r   r      s=        =D
 
 
 
 
 
 
 
 
rV   r   )r   rf   r'   ri   ro   scapy.automatonr   scapy.compatr   scapy.configr   
scapy.datar   scapy.errorr   r	   scapy.packetr
   scapy.interfacesr   r   scapy.supersocketr   typingr   r   r   r   r   r   r   r   rV   rK   <module>r      s~    
			    * * * * * *                   4 4 4 4 4 4 4 4       > > > > > > > > ) ) ) ) ) )             z/ z/ z/ z/ z/+ z/ z/ z/z
 
 
 
 
; 
 
 
 
 
rV   