
    ~ gv                     6    S SK r S SKrS SKJr   " S S\5      rg)    N)SoSMapc                   d    \ rS rSrSr/ SQr0 rSr/ SQrSr	S r
S rS	 rS
 rS rS rS rS rSrg)SoSIPMap   a  A mapping store for IP addresses

Each IP address added to this map is chcked for subnet membership. If that
subnet already exists in the map, then IP addresses are deterministically
generated sequentially within that subnet. For example, if a given IP is
matched to subnet 192.168.1.0/24 then 192.168.1 may be obfuscated to
100.11.12.0/24. Each IP address in the original 192.168.1.0/24 subnet
will then be assigned an address in 100.11.12.0/24 sequentially, such as
100.11.12.1, 100.11.12.2, etc...


Internally, the ipaddress library is used to manipulate the address objects
however, when retrieved by SoSCleaner any values will be strings.
)z127.*z::1z0\.(.*)?z1\.(.*)?z8.8.8.8z8.8.4.4z	169.254.*z255.*d   )127169172192Fc                     U R                   R                  5        H7  n[        U5      R                  SSS9S   UR                  S5      S   :X  d  M7    g   g)zThere are multiple ways in which an ip address could be handed to us
in a way where we're matching against a previously obfuscated address.

Here, match the ip address to any of the obfuscated addresses we've
already created
/   )maxsplitr   TF)datasetvaluesstrsplit)selfipaddr_ips      =/usr/lib/python3/dist-packages/sos/cleaner/mappings/ip_map.pyip_in_datasetSoSIPMap.ip_in_dataset1   sQ     <<&&(C6{  q 1!4		#q8II )     c                    SnUR                  U5      (       a   UR                  SR                  U5      5      nXR                  ;   a  U R                  U   $ U R	                  U5      (       d  U R                  U5      (       a  U$ SU;  aO  U R                  R                  5        H1  u  p4UR                  U5      (       d  M  UR                  S5      S   s  $    U R                  U5      $ )zoEnsure that when requesting an obfuscated address, we return a str
object instead of an IPv(4|6)Address object
)r   =]) r   r   )	
startswithlstripjoinr   ignore_itemr   itemsr   add)r   item
filt_startkeyvalues        r   getSoSIPMap.get=   s     *
??:&&;;rwwz23D<<<<%%D!!T%7%7%=%=K
 d?"ll002
>>$'' ;;s+A.. 3
 xx~r   c                     / nU R                    HH  nUR                  UR                  :X  a  X1l          gUR                  U;   d  M7  UR	                  U5        MJ     U(       a  UR                  S SS9  US   Ul        gg)a  Determine if a given address is in a subnet of an already obfuscated
network and if it is, then set the address' network to the network
object we're tracking. This allows us to match ip addresses with or
without a CIDR notation and maintain proper network relationships.
Nc                     U R                   $ )N)	prefixlen)ns    r   <lambda>;SoSIPMap.set_ip_cidr_from_existing_subnet.<locals>.<lambda>h   s    AKKr   T)r(   reverser   )	_networksipbroadcast_addressnetworkappendsort)r   addrnetsnets       r    set_ip_cidr_from_existing_subnet)SoSIPMap.set_ip_cidr_from_existing_subnetV   sn     >>Cww#///"ww#~C  " II/I>7DL r   c                 6    [         R                  " U5      nUR
                  n[        UR                  5      S:X  a  U R                  U5        OU R                  U5        U R                  U5      $ ! [         a    U R                  R	                  U5        e f = f)zYGiven an IP address, sanitize it to an obfuscated network or host
address as appropriate
z255.255.255.255)	ipaddressip_interface
ValueErrorignore_matchesr7   r6   r   netmaskr<   sanitize_networksanitize_ipaddr)r   r&   r9   r6   s       r   sanitize_itemSoSIPMap.sanitize_itemk   s    
	))$/D
 ,,w#44 11$7
 !!'*##D))!  	&&t,	s   A1 1'Bc                 F    XR                   ;  a  U R                  U5        gg)zoObfuscate the network address provided, and if there are host bits
in the address then obfuscate those as well
N)r3   _new_obfuscated_network)r   r6   s     r   rD   SoSIPMap.sanitize_network   s!    
 ..(((1 )r   c                    UR                   U R                  ;   a  U R                  UR                      nUR                  UR                   R                  :X  a  [	        UR                  5      $ UR                  5        H5  nU R                  U5      (       a  M  [	        U5       SUR                   3s  $    U R                  5       $ )zEObfuscate the IP address within the known obfuscated network
        r   )	r6   r3   r4   r5   r   hostsr   r.   _new_obfuscated_single_address)r   r9   _obf_networkr   s       r   rE   SoSIPMap.sanitize_ipaddr   s     <<4>>)>>$,,7L
 ww$,,888<99:: $))+))#.. "#hZq)?)?(@AA	 , 2244r   c                 t    S nU" 5       nX R                   R                  5       ;   a  U R                  5       $ U$ )Nc                      / n [        SS5       H)  nU R                  [        R                  " SS5      5        M+     U S    SU S    SU S    SU S    3$ )	Nr         c   .r         )ranger7   randomrandint)_octets_s     r   _gen_address=SoSIPMap._new_obfuscated_single_address.<locals>._gen_address   s[    G1a[v~~b"56 !aj\71:,a
|1WQZLIIr   )r   r   rM   )r   r]   _addrs      r   rM   'SoSIPMap._new_obfuscated_single_address   s8    	J LL''))6688r   c                    Sn[        U[        R                  5      (       a  U R                  U R                  ;   a  U =R                  S-  sl        U R                   S3nUR
                  R                  S5      S   n[        R                  " U SU 35      nU =R                  S-  sl        [        U[        R                  5      (       a   U(       a0  X R                  U'   [        U5      U R                  [        U5      '   gg)zGenerate an obfuscated network address for the network address given
which will allow us to maintain network relationships without divulging
actual network details

Positional arguments:

    :param network:     An ipaddress.IPv{4|6)Network object
Nr   z.0.0.0r   )
isinstancer?   IPv4Networknetwork_first_octetskip_network_octetswith_netmaskr   IPv6Networkr3   r   r   )r   r6   rN   _obf_address	_obf_masks        r   rI    SoSIPMap._new_obfuscated_network   s     gy4455''4+C+CC((A-("667v>L,,2237:I$00L>9+1NOL$$)$gy4455&2NN7#),\):DLLW& r    N)__name__
__module____qualname____firstlineno____doc__rB   r3   rd   re   compile_regexesr   r*   r<   rF   rD   rE   rM   rI   __static_attributes__rk   r   r   r   r      sN    	N I6O
2#**2250
;r   r   )r?   rY   sos.cleaner.mappingsr   r   rk   r   r   <module>rt      s      'x;v x;r   