관리-도구
편집 파일: ipv6_map.cpython-39.opt-1.pyc
a ����-�_gl-����������������������@���sR���d�dl�Z�d�dlmZ�d�dlmZ�dd��Zdd��ZG�dd ��d e�ZG�d d��d�ZdS�)�����N��getrandbits)�SoSMapc�����������������C���s���dd��|�D��S�)a���Generate a random set of hextets, based on the length of the source hextet. If any hextets are compressed, keep that compression. E.G. '::1234:bcd' will generate a leading empty '' hextet, followed by two 4-character hextets. :param hextets: The extracted hextets from a source address :type hextets: ``list`` :returns: A set of randomized hextets for use in an obfuscated address :rtype: ``list`` c�����������������S���s���g�|�]}|rt�d��nd�qS�)������)� random_hex)�.0�h��r ����A/usr/lib/python3.9/site-packages/sos/cleaner/mappings/ipv6_map.py� <listcomp>��������z$generate_hextets.<locals>.<listcomp>r ���)�hextetsr ���r ���r����generate_hextets���s����r���c�����������������C���s���t�d|���d|���d��S�)z�Generate a string of size length of random hex characters. :param length: The number of characters to generate :type length: ``int`` :returns: A string of ``length`` hex characters :rtype: ``str`` r����0�xr���)�lengthr ���r ���r���r���"���s���� r���c�������������������@���sD���e�Zd�ZdZi�Zg�d�ZdgZdZdZdd��Z dd ��Z ddd�Zd S�)� SoSIPv6Mapat��Mapping for IPv6 addresses and networks. Much like the IP map handles IPv4 addresses, this map is designed to take IPv6 strings and obfuscate them consistently to maintain network topology. To do this, addresses will be manipulated by the ipaddress library. If an IPv6 address is encountered without a netmask, it is assumed to be a /64 address. )z^::1/.*z::/0zfd53:.*z^53..:�534fF����c�����������������C���s����d|vrdS�|d�D�]x}t��|�}|d�|�d�}|��||�}|j|�j|j<�|d�|�d�D�].}|d�|�d�|�}|�||��||�j|<�q\qdS�)z�Override the base conf_update() so that we can load the existing networks into ObfuscatedIPv6Network() objects for the current run. �networksN� obfuscated�hosts)� ipaddress� ip_network�_get_network�obfuscated_address�dataset�original_address�add_obfuscated_host_address)�selfZconfig�networkZ_origZ_obfuscated�_net�host�_ob_hostr ���r ���r����conf_updateG���s���� zSoSIPv6Map.conf_updatec�����������������C���s����d|v�r|��d�d�nd}|}|s*|d7�}zt�|�}|��|�}|j}W�nb�ty����tj|dd�}|��|�}|j|�jvr�|j|�j|j<�t� |��d�d��}|� |�}Y�n0�|r�d|vr�|��d|���S�|S�)N�/���r���z/64F)�strictr���)�splitr���r���r���r���� ValueError�network_addrr���r���� ip_address�obfuscate_host_address)r ����item�_prefixZ_ipaddr�_addrr"���Z _hostaddrr ���r ���r���� sanitize_itemW���s$���� zSoSIPv6Map.sanitize_itemr���c�����������������C���s.���|j�}||�jvr$t|||�j�|�j|<�|�j|�S�)z�Attempt to find an existing ObfuscatedIPv6Network object from which to either find an existing obfuscated match, or create a new one. If no such object already exists, create it. )� compressedr����ObfuscatedIPv6Network�first_hexes)r ����addressr���r0���r ���r ���r���r���t���s���� � zSoSIPv6Map._get_networkN)r���)�__name__� __module__�__qualname__�__doc__r���Zignore_matchesr4���Zcompile_regexes�versionr%���r1���r���r ���r ���r ���r���r���.���s��� r���c�������������������@���sZ���e�Zd�ZdZddd�Zedd���Zedd ���Zd d��Zdd ��Z dd��Z dd��Zdd��ZdS�)r3���a���An abstraction class that represents a network that is (to be) handled by sos. Each distinct IPv6 network that we encounter will have a representative instance of this class, from which new obfuscated subnets and host addresses will be generated. This class should be built from an ``ipaddress.IPv6Network`` object. If an obfuscation string is not passed, one will be created during init. r���Nc�����������������C���s����t�|tj�std��||�_|j|�_|jj|�_ i�|�_ |du�rDdg|�_n||�_|sZ|����|�_ n,t�|t�svtdt|������|�d�d�|�_ dS�)a���Basic setup for the obfuscated network. Minor validation on the addr used to create the instance, as well as on an optional ``obfuscation`` which if set, will serve as the obfuscated_network address. :param addr: The *un*obfuscated network to be handled :type addr: ``ipaddress.IPv6Network`` :param obfuscation: An optional pre-determined string representation of the obfuscated network address :type obfuscation: ``str`` :param used_hexes: A list of already used hexes for the first hextet of a potential global address obfuscation :type used_hexes: ``list`` z*Invalid network: not an IPv6Network objectNr���z;Pre-determined obfuscated network address must be str, not r&���r���)� isinstancer����IPv6Network� Exception�addr� prefixlen�prefix�network_addressr2���r+���r���r4����_obfuscate_network_address�_obfuscated_network�str� TypeError�typer)���)r ���r>���ZobfuscationZ used_hexesr ���r ���r����__init__����s ���� �zObfuscatedIPv6Network.__init__c�����������������C���s���|�j���d|�j���S�)Nr&���)rC���r@����r ���r ���r ���r���r�������s����z(ObfuscatedIPv6Network.obfuscated_addressc�����������������C���s���|�j�jS��N)r>���r2���rH���r ���r ���r���r�������s����z&ObfuscatedIPv6Network.original_addressc�����������������C���s4���|�j�jr|����S�|�j�jr|�jS�|�j�jr.|����S�|�jS�)z�Generate the obfuscated pair for the network address. This is determined based on the netmask of the network this class was built on top of. )r>���� is_global�_obfuscate_global_address� is_link_localr+���� is_private�_obfuscate_private_addressrH���r ���r ���r���rB�������s����z0ObfuscatedIPv6Network._obfuscate_network_addressc�����������������C���s����|�j��d�dd��}dg}tdd��|D���r|dt|�j�d��}|��td ����}||�jv�rj|��td ����}qN|�j�|��|g}|�t|���d� |�S�) a��Global unicast addresses have a 48-bit global routing prefix and a 16-bit subnet. We set the global routing prefix to a static sos-specific identifier that could never be seen in the wild, '534f:' We then randomize the subnet hextet. �:r���Nr���c�����������������s���s���|�]}|�V��qd�S�rI���r ���)r����cr ���r ���r���� <genexpr>����r ���zBObfuscatedIPv6Network._obfuscate_global_address.<locals>.<genexpr>�5�����������) r+���r)����all�lenr4���r����append�extendr����join)r ����_hextets�_ob_hexZ_startr ���r ���r���rK�������s���� z/ObfuscatedIPv6Network._obfuscate_global_addressc�����������������C���s2���|�j��d�dd��}dg}|�t|���d�|�S�)a ��The first 8 bits will always be 'fd', the next 40 bits are meant to be a global ID, followed by 16 bits for the subnet. To keep things relatively simply we maintain the first hextet as 'fd53', and then randomize any remaining hextets rO���r���NZfd53)r+���r)���rX���r���rY���)r ���rZ���r[���r ���r ���r���rN�������s����z0ObfuscatedIPv6Network._obfuscate_private_addressc��������������������sv�����fdd�}|j���jvrj��j�d�}|j�t|�d���d�}||�}|��j���v�r\||�}qD���|j�|����j|j��S�)a���Given an unobfuscated address, generate an obfuscated match for it, and save it to this network for tracking during the execution of clean. Note: another way to do this would be to convert the obfuscated network to bytes, and add a random amount to that based on the number of addresses that the network can support and from that new bytes count craft a new IPv6 address. This has the advantage of absolutely guaranteeing the new address is within the network space (whereas the method employed below could *theoretically* generate an overlapping address), but would in turn remove any ability to compress obfuscated addresses to match the general format/syntax of the address it is replacing. For the moment, it is assumed that being able to maintain a quick mental note of "unobfuscated device ff00::1 is obfuscated device 53ad::a1b2" is more desireable than "ff00::1 is now obfuscated as 53ad::1234:abcd:9876:a1b2:". :param addr: The unobfuscated IPv6 address :type addr: ``ipaddress.IPv6Address`` :returns: An obfuscated address within this network :rtype: ``str`` c�������������� ������s ���d����jd��t|��d���g�S�)Nr���rO���)rY���rC���r���r)���)r#���rH���r ���r����_generate_address��s�����zGObfuscatedIPv6Network.obfuscate_host_address.<locals>._generate_addressrO���N)r2���r���r+����rstriprV����lstrip�valuesr���)r ���r>���r\���Z_nZ_hostr$���r ���rH���r���r-�������s���� z,ObfuscatedIPv6Network.obfuscate_host_addressc�����������������C���s���||�j�|<�dS�)zjAdds an obfuscated pair to the class for tracking and ongoing consistency in obfuscation. N)r���)r ���r#���r���r ���r ���r���r�����s����z1ObfuscatedIPv6Network.add_obfuscated_host_address)r���N) r6���r7���r8���r9���rG����propertyr���r���rB���rK���rN���r-���r���r ���r ���r ���r���r3�������s��� " 'r3���) r���Zrandomr���Zsos.cleaner.mappingsr���r���r���r���r3���r ���r ���r ���r����<module>���s���R