3 ܓi!@s(ddlZddlmZGdddeZdS)N)SoSMapc seZdZdZdddgZddgZd#ZdZdZdZ dZ iZ iZ fddZ ddZddZfddZddZddZdd Zd!d"ZZS)$SoSHostnameMapaLMapping store for hostnames and domain names Hostnames are obfuscated using an incrementing counter based on the total number of hosts matched regardless of domain name. Domain names are obfuscated based on the host's hostname, plus any user defined domains passed in by the `--domains` options. Domains are obfuscated as whole units, meaning the domains 'example.com' and 'host.foo.example.com' will be separately obfuscated with no relation for example as 'obfuscatedomdain1.com' and 'obfuscatedomain2.com'. Top-level domains are left untouched. Z localhostz.*localdomain.*z^com..*ZwwwZapi.yaml.yml.crt.key.pem.log.repo.rules.conf.cfgTrcs d|kr|jdd}tj|S)zOverride the base get_regex_result() to provide a regex that, if this is an FQDN or a straight domain, will include an underscore formatted regex as well. .z(\.|_))replacesuperget_regex_result)selfitem) __class__"/usr/lib/python3.6/hostname_map.pyr5s zSoSHostnameMap.get_regex_resultcCsy2t|jjdddjdd}t|d|_Wntk rFYnXys  z!SoSHostnameMap.set_initial_countscsj||jkrdS|jd}dj|jdddt|dkrJ|d|jkStfdd|jDrfdSdS) zkCheck if a potential domain is in one of the domains we've loaded and should be obfuscated Trrrc3s|]}j|VqdS)N)endswith).0Z_d)no_tldrr ]sz?SoSHostnameMap.domain_name_in_loaded_domains..F)r"rjoinlenrany)rrrr)r)rdomain_name_in_loaded_domainsRs   z,SoSHostnameMap.domain_name_in_loaded_domainsc sd}d}d}x&|jd r2||d7}|dd}qWx&|jd rZ||d 7}|dd }q6W||jkrp|j|S|j|jsdj|||gS|j|jrd|jdd }|j|d}||7}||jkrxt |jj dt dD]}d}|j|}|jd} | dr| d|j krd}t |dks|d r6q| rh|djdsZ|j|rht j|}P|j|dry,|j|dd} |dt j| }PWqtk rYqXqW|st j|}|||S)Nr_rrT)rkeyF)rr1)rr1r+r+r+) startswithr'datasetr/lowerr, strip_extsrrrkeysr-rrget Exception) rrprefixsuffixfinalZextZ _existingZ _host_substrZ_testZ_hZitm)rrrr8bsR                zSoSHostnameMap.getc Cs|jd}t|dkr(|j|djSt|dkr\|j|}tdd|DrX|j}|St|dkr|d}|dd}t|dkr|j|j}nd}|j|}||j|<dj||g}tdd|Dr|j}|SdS) Nrrrcss|]}|jVqdS)N)isupper)r(r$rrrr*sz/SoSHostnameMap.sanitize_item..unknowncss|]}|jVqdS)N)r>)r(r$rrrr*s) rr-sanitize_short_namer5sanitize_domainallupperr4r,) rrrdnamehostnamerZ ob_hostname ob_domainZ_fqdnrrr sanitize_items*         zSoSHostnameMap.sanitize_itemcCs`| s||jkr|S||jkrVd|j}||j|<|jd7_||j|<|j||j|S)zObfuscate the short name of the host with an incremented counter based on the total number of obfuscated host names rr) skip_keysr4r rZadd_regex_item)rrEZob_hostrrrr@s     z"SoSHostnameMap.sanitize_short_namecCszx*|jD] }tj|dj|rdj|SqW|dj}dj|ddj}|j|}dj||g}||jdj|<|S)zeObfuscate the domainname, broken out into subdomains. Top-level domains are ignored. rrrr+r+)ignore_matchesrematchr,r5_new_obfuscated_domainr4)rrZ_skipZ top_domainrDrFrrrrAs   zSoSHostnameMap.sanitize_domaincCs4||jkr*d|j|j|<|jd7_|j|S)zDGenerate an obfuscated domain for each subdomain name given Zobfuscateddomainr)r"r#)rrDrrrrLs z%SoSHostnameMap._new_obfuscated_domain) rrrrrr r r r r )__name__ __module__ __qualname____doc__rIrHr6Zignore_short_itemsZmatch_full_words_onlyr r#r"rrr&r/r8rGr@rArL __classcell__rr)rrrs,  = r)rJZsos.cleaner.mappingsrrrrrr s