3 ܓiA"@s(ddlZddlmZGdddeZdS)N)SoSMapcseZdZdZdddddddd gZiZeZd Zd d d dgZ dZ dZ fddZ ddZ ddZddZddZddZddZdd Zd!d"Zd#d$ZZS)%SoSIPMapaA 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. z127.*z::1z0\.(.*)?z1\.(.*)?z8.8.8.8z8.8.4.4z 169.254.*z255.*dZ127Z169Z172Z192Fl"Xcs<x*|jD]}|jj|jddddq Wtj|dS)z^Override the base conf_update() so that we can add items into obfuscated_ips. /)maxsplitrN)valuesobfuscated_ipsaddsplitsuper conf_update)selfconfigvalue) __class__/usr/lib/python3.6/ip_map.pyr 6szSoSIPMap.conf_updatecCs(|jj|jdddd||j|<dS)Nrr)rr)r r r dataset)ritemrrrrinsert_to_dataset>szSoSIPMap.insert_to_datasetcCs t|jdddd}||jkS)aThere 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 rr)rr)strr r )rZipaddrZaddr_strrrr ip_in_datasetBszSoSIPMap.ip_in_datasetcCsd}|j|r|jdj|}||jkr2|j|S|j|sF|j|rJ|Sd|krx.|jjD] \}}|j|r^|jddSq^W|j|S)zEnsure that when requesting an obfuscated address, we return a str object instead of an IPv(4|6)Address object r=])r)rrrr) startswithlstripjoinrZ ignore_itemritemsr r )rrZ filt_startkeyrrrrgetLs    z SoSIPMap.getcCsbg}x8|jD].}|j|jkr&||_dS|j|kr |j|q W|r^|jdddd|d|_dS)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. NcSs|jS)N) prefixlen)nrrrwsz;SoSIPMap.set_ip_cidr_from_existing_subnet..T)r!reverser) _networksipbroadcast_addressnetworkappendsort)raddrZnetsZnetrrr set_ip_cidr_from_existing_subnetes   z)SoSIPMap.set_ip_cidr_from_existing_subnetc Csfytj|}Wn"tk r0|jj|YnX|j}t|jdkrR|j|n |j ||j |S)ziGiven an IP address, sanitize it to an obfuscated network or host address as appropriate z255.255.255.255) ipaddressZ ip_interface ValueErrorignore_matchesr+r*rZnetmaskr.sanitize_networksanitize_ipaddr)rrr-r*rrr sanitize_itemzs   zSoSIPMap.sanitize_itemcCs||jkr|j|dS)zObfuscate the network address provided, and if there are host bits in the address then obfuscate those as well N)r'_new_obfuscated_network)rr*rrrr2s zSoSIPMap.sanitize_networkcCsh|j|jkr`|j|j}|j|jjkr0t|jSx.|jD]"}|j|s:t|d|jSq:W|jS)zEObfuscate the IP address within the known obfuscated network r) r*r'r(r)rZhostsrr#_new_obfuscated_single_address)rr- _obf_networkZ_iprrrr3s    zSoSIPMap.sanitize_ipaddrcCs|jd7_x |jdd kr.|jd7_qW|jd?d|jd?dd|jd?dd|jd}||jjkr|jS|S) Nrr.)rr9) _saddr_cntrrr6)rZ_addrrrrr6s:z'SoSIPMap._new_obfuscated_single_addresscCsd}t|tjrh|j|jkr*|jd7_|jd}|jjdd}tj|d|}|jd7_t|tjrt|rt|}||j |<||j t|<|j j |jdddddS)a Generate 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 Nrz.0.0.0r)rr) isinstancer/Z IPv4Networknetwork_first_octetskip_network_octetsZ with_netmaskr Z IPv6Networkrr'rr r )rr*r7Z _obf_addressZ _obf_maskZ_obf_network_srrrr5s      z SoSIPMap._new_obfuscated_network)__name__ __module__ __qualname____doc__r1r'setr r@rAZcompile_regexesr>r rrr"r.r4r2r3r6r5 __classcell__rr)rrrs2    r)r/Zsos.cleaner.mappingsrrrrrr s