NineSec Team Shell
Server IP : 184.107.3.203  /  Your IP : 216.73.216.51
Web Server : Apache
System : Linux dedicated2.avenfashion.com.ph 4.18.0-553.40.1.el8_10.x86_64 #1 SMP Mon Feb 10 12:11:18 EST 2025 x86_64
User : adminteladeoro ( 1015)
PHP Version : 8.2.28
Disable Function : exec,passthru,shell_exec,system
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0755) :  /usr/lib/python3.6/site-packages/sos/cleaner/__pycache__/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : //usr/lib/python3.6/site-packages/sos/cleaner/__pycache__/__init__.cpython-36.pyc
3

~�g���@s$ddlZddlZddlZddlZddlZddlZddlZddlmZddl	m	Z	ddl
mZddlZ
ddl
mZddlmZddlmZddlmZdd	lmZdd
lmZddlmZddlmZdd
lmZmZm Z m!Z!ddl"m#Z#m$Z$ddl%m&Z&ddl'm(Z(m)Z)m*Z*m+Z+Gdd�de�Z,dS)�N)�ThreadPoolExecutor)�datetime)�getpwuid)�__version__)�SoSComponent)�SoSIPParser)�SoSMacParser)�SoSHostnameParser)�SoSKeywordParser)�SoSUsernameParser)�
SoSIPv6Parser)�SoSReportArchive�SoSReportDirectory�SoSCollectorArchive�SoSCollectorDirectory)�DataDirArchive�TarballArchive)�InsightsArchive)�get_human_readable�
import_module�ImporterHelper�file_is_binaryc
sZeZdZdZdZdgggdgdddddgd	�ZdL�fd
d�	ZdMdd
�ZdNdd�ZdOdd�Z	dPdd�Z
edd��Zdd�Z
dd�Zedd��Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�ZdQd.d/�Zd0d1�Zd2d3�Zd4d5�Zd6d7�Zd8d9�Zd:d;�Zd<d=�Z d>d?�Z!dRd@dA�Z"dBdC�Z#dDdE�Z$dFdG�Z%dSdHdI�Z&dJdK�Z'�Z(S)T�
SoSCleanera
    This function is designed to obfuscate potentially sensitive information
    from an sos report archive in a consistent and reproducible manner.

    It may either be invoked during the creation of a report by using the
    --clean option in the report command, or may be used on an already existing
    archive by way of 'sos clean'.

    The target of obfuscation are items such as IP addresses, MAC addresses,
    hostnames, usernames, and also keywords provided by users via the
    --keywords and/or --keyword-file options.

    For every collection made in a report the collection is parsed for such
    items, and when items are found SoS will generate an obfuscated replacement
    for it, and in all places that item is found replace the text with the
    obfuscated replacement mapped to it. These mappings are saved locally so
    that future iterations will maintain the same consistent obfuscation
    pairing.

    In the case of IP addresses, support is for IPv4 and IPv6 - effort is made
    to keep network topology intact so that later analysis is as accurate and
    easily understandable as possible. If an IP address is encountered that we
    cannot determine the netmask for, a random IP address is used instead.

    For IPv6, note that IPv4-mapped addresses, e.g. ::ffff:10.11.12.13, are
    NOT supported currently, and will remain unobfuscated.

    For hostnames, domains are obfuscated as whole units, leaving the TLD in
    place.

    For instance, 'example.com' may be obfuscated to 'obfuscateddomain0.com'
    and 'foo.example.com' may end up being 'obfuscateddomain1.com'.

    Users will be notified of a 'mapping' file that records all items and the
    obfuscated counterpart mapped to them for ease of reference later on. This
    file should be kept private.
    z6Obfuscate sensitive networking information in a report�auto�Nz /etc/sos/cleaner/default_mappingF�)�archive_type�domains�disable_parsers�skip_cleaning_files�jobs�keywords�keyword_file�map_file�	no_update�keep_binary_files�target�	usernamescs�|st�j|||�d|_n�|d|_|d|_|d|_|d|_|d|_d|_t|jd�shd	|j_	d
|j_
tjd�|_
tjd�|_tjtjj|jd
�dd�|j�|j�|_tjd�||_|jj�|_|jjjd
�|_|jj}t|j|�t|j|�t |j|�t!|j|�t"|j|�t#|j|�g|_$x�|jj%D]z}xr|j$D]h}|j&j'�j(ddd�d}	|	j)�}
|j'�j)�|
k�r@|j*d|
���|jj+d|�d��|j$j,|��q@W�q4Wt-t.t/t0t1t2t3g|_4d|_5|j*d|j���dS)NT�options�tmpdir�sys_tmp�policy�manifestFr rr�sosZsos_ui�cleaner)�exist_ok�?�parser�)�maxsplitrzDisabling parser: zDisabling the 'zP' parser. Be aware that this may leave sensitive plain-text data in the archive.z#Cleaner initialized. From cmdline: )6�super�__init__�from_cmdline�optsr)r*r+r,�hasattrr r�loggingZ	getLogger�soslog�ui_log�os�makedirs�path�join�review_parser_values�
load_map_fileZcleaner_mapping�umask�in_placeZget_preferred_hash_name�	hash_nameZ
components�add_section�
cleaner_mdrr	rrrr
r�parsersr�name�lower�split�strip�log_info�warning�removerr
rrrrr�
archive_types�nested_archive)�selfr1�argsZcmdlinerCZhook_commonsr�_parserZ_loaded�_tempZ_loaded_name)�	__class__��/usr/lib/python3.6/__init__.pyr5bs^











zSoSCleaner.__init__cCsd|rd|��nd�d|��S)Nz[cleaner�:rz] rV)rQ�msg�callerrVrVrW�_fmt_log_msg�szSoSCleaner._fmt_log_msgcCs|jj|j||��dS)N)r:�debugr[)rQrYrZrVrVrW�	log_debug�szSoSCleaner.log_debugcCs|jj|j||��dS)N)r:�infor[)rQrYrZrVrVrWrL�szSoSCleaner.log_infocCs|jj|j||��dS)N)r:�errorr[)rQrYrZrVrVrW�	log_error�szSoSCleaner.log_errorcCs|jd�|j|j�dS)NzSoS Cleaner Detailed Help)Z	set_titleZadd_text�__doc__)�clsZsectionrVrVrW�display_help�s
zSoSCleaner.display_helpcCs�i}d}tjj|jj�r,td|jj�d���tjj|jj�s`|jj|kr�|jd|jj�d��n�t|jjddd��p}yt	j
|�}WnZt	jk
r�|jd	�Yn<tk
r�}z |jd
|jj�d|���WYdd}~XnXWdQRX|S)
z�Verifies that the map file exists and has usable content.

        If the provided map file does not exist, or it is empty, we will print
        a warning and continue on with cleaning building a fresh map
        z /etc/sos/cleaner/default_mappingzRequested map file z is a directoryzERROR: map file z6 does not exist, will not load any obfuscation matches�rzutf-8)�encodingzOERROR: Unable to parse map file, json is malformed. Will not load any mappings.zERROR: Could not load 'z': N)r<r>�isdirr7r#�	Exception�existsr`�open�json�loadZJSONDecodeError)rQZ_confZdefault_map�mf�errrVrVrWrA�s 6zSoSCleaner.load_map_filecCs�|jd�}|jjdt�d��|jj|�|jjs�ytd�WnVtk
rh|jjd�|jd�Yn.t	k
r�}z|jd|�WYdd}~XnXdS)	z�When we are directly running `sos clean`, rather than hooking into
        SoSCleaner via report or collect, print a disclaimer banner
        a�This command will attempt to obfuscate information that is generally considered to be potentially sensitive. Such information includes IP addresses, MAC addresses, domain names, and any user-provided keywords.

Note that this utility provides a best-effort approach to data obfuscation, but it does not guarantee that such obfuscation provides complete coverage of all such data in the archive, or that any obfuscation is provided to data that does not fit the description above.

Users should review any resulting data and/or archives generated or processed by this utility for remaining sensitive content before being passed to a third party.
z
sos clean (version z)
z-
Press ENTER to continue, or CTRL-C to quit.
z
Exiting on user cancel�r2N)
Z_fmt_msgr;r^rr7Zbatch�input�KeyboardInterrupt�_exitrg)rQrY�erVrVrW�print_disclaimer�s
zSoSCleaner.print_disclaimerc	Csd|_|jdd�}|jdddd�|jdd	d	d
ddd
dgdd�|jddgdd�|jddgddd�|jdddgddd�|jdddtd d!�|jd"dgd#d$d�|jd%dd&d'd(�|jd)d*d+d,d-�|jd.d/d0d1d2d3�|jd4d0d1d5d6d7�|jd8d9gdd:d3�dS);Nzsos clean|mask TARGET [options]zCleaner/Masking Optionsz7These options control how data obfuscation is performedr&ZTARGETz%The directory or archive to obfuscate)�metavar�helpz--archive-typerZreportZcollectZinsightszdata-dir�tarballz8Specify what kind of archive the target was generated as)�default�choicesruz	--domains�extendz!List of domain names to obfuscate)�actionrwruz--disable-parsersrzCDisable specific parsers, so that those elements are not obfuscated)rzrw�destruz--skip-cleaning-filesz--skip-masking-filesrzBList of files to skip/ignore during cleaning. Globs are supported.z-jz--jobsrz&Number of concurrent archives to clean)rw�typeruz
--keywordsr!zList of keywords to obfuscatez--keyword-filer"z&Provide a file a keywords to obfuscate)rwr{ruz
--map-filer#z /etc/sos/cleaner/default_mappingz;Provide a previously generated mapping file for obfuscation)r{rwruz--no-updater$F�
store_truez<Do not update the --map-file with new mappings from this run)r{rwrzruz--keep-binary-filesr%zGKeep unprocessable binary files in the archive instead of removing them)rwrzr{ruz--usernamesr'zList of usernames to obfuscate)ZusageZadd_argument_group�add_argument�int)rbr1Z	clean_grprVrVrW�add_parser_options�sP



zSoSCleaner.add_parser_optionscCs||j_dS)z�For use by report and collect to set the TARGET option appropriately
        so that execute() can be called just as if we were running `sos clean`
        directly from the cmdline.
        N)r7r&)rQr>rVrVrW�set_target_path%szSoSCleaner.set_target_pathcCs�d}|jjdkrL|jjjdd�}xZ|jD]}|j|kr(||jj|j�}q(Wn0x.|jD]$}|j|jj�rT||jj|j�}PqTW|s�dS|jj	|�|j
r�|jj|j��|jj
|�||_|jr�|jj|j_dS)z�The target path is not a directory, so inspect it for being an
        archive or an archive of archives.

        In the event the target path is not an archive, abort.
        Nr�-�_)r7r�replacerOZ	type_namer&r)Z
check_is_type�report_paths�append�	is_nestedryZget_nested_archivesrNrP�description�ui_name)rQZ_arcZ
check_type�archive�arcrVrVrW�inspect_target_archive,s&
z!SoSCleaner.inspect_target_archivecCsNx2|jjD]&}t|jd��dkr
td|�d���q
Wdd�|jjD�|j_dS)z�Check any values passed to the parsers via the commandline:
        - For the --domains option, ensure that they are valid for the parser
          in question.
        - Convert --skip-cleaning-files from globs to regular expressions.
        �.�zInvalid value 'z0' given: --domains values must be actual domainscSsg|]}tj|��qSrV)�fnmatch�	translate)�.0�prVrVrW�
<listcomp>Usz3SoSCleaner.review_parser_values.<locals>.<listcomp>N)r7r�lenrJrgr)rQZ_domrVrVrWr@IszSoSCleaner.review_parser_valuescCs�|jjjd�djd�d|_|jr,|j�g|_tjj	|jj�sb|j
jd|jj���|jd�|j
�|js�|j
jd�|jd�g|_x"|jD]}|jdkr�|jj�q�W|j�|j�|j�|js�|jr�dS|j
jd	�|jd�|j
jd
t|j��d��|j�}|j|�}|j|�|j�|j�rLdd
�|jD�}||fSd}t|j�dk�rj|j�}n~|jd}|j}|j|j�}|dk	�r�|j |jd�d�d|j!���}	t"tjj#|j$|	�ddd��}
|
j%|�WdQRX|j&�tjj#|j$|j |jd�d��}t'j(||�tj)|�}|j
jd|���|j
jd|�d��|j
jdt*|j+����|j
jdt,|j-�j.�d��|j
jd�|j/�dS)a,SoSCleaner will begin by inspecting the TARGET option to determine
        if it is a directory, archive, or archive of archives.

        In the case of a directory, the default behavior will be to edit the
        data in place. For an archive will we unpack the archive, iterate
        over the contents, and then repack the archive. In the case of an
        archive of archives, such as one from SoSCollector, each archive will
        be unpacked, cleaned, and repacked and the final top-level archive will
        then be repacked as well.
        �/r2z.tarrz*Invalid target: no such file or directory z'No valid archives or directories found
zHostname ParserNz#No reports obfuscated, aborting...
z
Successfully obfuscated z report(s)
cSsg|]
}|j�qSrV)�final_archive_path)r��arVrVrWr��sz&SoSCleaner.execute.<locals>.<listcomp>r��wzutf-8)rez2A mapping of obfuscated elements is available at
	z)
The obfuscated archive is available at
	�
z	Size	z	Owner	zcPlease send the obfuscated archive to your support representative and keep the mapping file private���r�r�)0r7r&rJ�arc_namer6rsr�r<r>rhr;r_rqr��completed_reportsrGrH�mappingZset_initial_counts�preload_all_archives_into_maps�generate_parser_item_regexes�obfuscate_report_pathsrCr^r��compile_mapping_dict�write_map_for_archive�write_map_for_config�write_stats_to_manifest�rebuild_nested_archiver��get_new_checksum�obfuscate_stringrDrir?r*�write�write_cleaner_log�shutil�move�statr�st_sizer�st_uidZpw_nameZcleanup)rQr1�_map�map_pathZ	arc_pathsZ
final_path�arc_pathr��checksumZchksum_nameZcfZarcstatrVrVrW�executeXst










zSoSCleaner.executecCs�|jd}|j|d�xT|jD]J}|jjd�d}|j|j�}|dk	rd|�d|j��}|jj||d�qWxnt	j
|jj�D]\\}}}xP|D]H}	t	j
j||	�}
|
j|jj�d}|jd�}|jj|
|d�t	j|
�q�Wq|W|jd	d
�|jj|jj�S)
z�Handles repacking the nested tarball, now containing only obfuscated
        copies of the reports, log files, manifest, etc...
        z-obfuscated)rHr�r2Nz
checksums/r�)r{T)r�r�r�)r�Z
setup_archiver�r�rJr�rDr�Z
add_stringr<�walkrP�extracted_pathr>r?�lstrip�add_filerNr��finalizer7Zcompression_type)rQr�r�Zarc_destr�ZdnameZdirnr��files�filename�fnamerVrVrWr��s"


z!SoSCleaner.rebuild_nested_archivecCs6i}x,|jD]"}i||j<||jj|j��qW|S)aBuild a dict that contains each parser's map as a key, with the
        contents as that key's value. This will then be written to disk in the
        same directory as the obfuscated report so that sysadmins have a way
        to 'decode' the obfuscation locally
        )rGZmap_file_key�updateZget_map_contents)rQr�r1rVrVrWr��s

zSoSCleaner.compile_mapping_dictcCs2t|ddd��}|jtj|dd��WdQRX|S)zjWrite the mapping to a file on disk that is in the same location as
        the final archive(s).
        r�zutf-8)rer)�indentN)rir�rj�dumps)rQr�r>rlrVrVrW�write_map_to_file�szSoSCleaner.write_map_to_filecCs`y*tjj|j|j|j�d���}|j||�Stk
rZ}z|jd|���dSd}~XnXdS)Nz-private_mapz"Could not write private map file: )	r<r>r?r*r�r�r�rgr`)rQr�r�rmrVrVrWr��sz SoSCleaner.write_map_for_archivecCs�|jjr�|jjr�tjj|jj�}y6tj|dd�|j||jj�|jd|jj���Wn2t	k
r�}z|j
d|���WYdd}~XnXdS)z}Write the mapping to the config file so that subsequent runs are
        able to provide the same consistent mapping
        T)r/zWrote mapping to z&Could not update mapping config file: N)r7r#r$r<r>�dirnamer=r�r]rgr`)rQr�Zcleaner_dirrmrVrVrWr��szSoSCleaner.write_map_for_configcCs~tjj|j|j�d��}t|ddd��0}|jjd�x|jj�D]}|j	|�q@WWdQRX|rz|j
|�|jj|dd�dS)	z�When invoked via the command line, the logging from SoSCleaner will
        not be added to the archive(s) it processes, so we need to write it
        separately to disk
        z-obfuscation.logr�zutf-8)rerNzsos_logs/cleaner.log)r{)
r<r>r?r*r�riZsos_log_file�seek�	readlinesr��obfuscate_filer�r�)rQr�Zlog_nameZlogfile�linerVrVrWr��s
zSoSCleaner.write_cleaner_logcCs�yRd}t|d��:}tj|j�}x|j|�}|s0P|j|�q W|j�dSQRXWn2tk
r�}z|jd|���WYdd}~XnXdS)zvCalculate a new checksum for the obfuscated archive, as the previous
        checksum will no longer be valid
        ir��rbr�Nz!Could not generate new checksum: i)	ri�hashlib�newrD�readr�Z	hexdigestrgr])rQ�archive_pathZ	hash_sizeZ
archive_fpZdigestZhashdatarmrVrVrWr�s
"zSoSCleaner.get_new_checksumcCs�y�dt|j��d|jj�d�}|jj|�|jjr>|jjd�t|jj�}|j	|j
|jdd�|jdd�|jr�|j
�|j
|j�Wn*tk
r�|jjd	�tjd
�YnXdS)z�Perform the obfuscation for each archive or sos directory discovered
        during setup.

        Each archive is handled in a separate thread, up to self.opts.jobs will
        be obfuscated concurrently.
        zFound z. total reports to obfuscate, processing up to z concurrently
zpWARNING: binary files that potentially contain sensitive information will NOT be removed from the final archive
r2)Z	chunksizeT)�waitzExiting on user cancelrnN)r�r�r7r r;r^r%rMr�map�obfuscate_reportZshutdownrP�_replace_obfuscated_archivesrpr<rq)rQrYZpoolrVrVrWr�"sz!SoSCleaner.obfuscate_report_pathscCsZxT|jD]J}tj|j�|jj}|jjd�d}tjj	||�}t
j|j|�||_qWdS)z�When we have a nested archive, we need to rebuild the original
        archive, which entails replacing the existing archives with their
        obfuscated counterparts
        r�r2Nr�)r�r<rNr�rPr�r�rJr>r?r�r�)rQr�r{rvZ	dest_namerVrVrWr�?sz'SoSCleaner._replace_obfuscated_archivescCsx|jD]}|j�qWdS)z�For the parsers that use prebuilt lists of items, generate those
        regexes now since all the parsers should be preloaded by the archive(s)
        as well as being handed cmdline options and mapping file configuration.
        N)rGZgenerate_item_regexes)rQr1rVrVrWr�Lsz'SoSCleaner.generate_parser_item_regexescCs8�x0|jD�]$}|jj�j�dj�}x�|j||�D]�}|j|�}|sHq4|jd|�d|�d|j���x^|j	�D]R}y|j
|�Wqptk
r�}z"|jd|�d|�d|���WYdd}~XqpXqpWq4W|j||�}	|	�r|jd|�d	|j���x|	D]}
|j
j|
�q�Wx |j|D]}|j
j|��qWq
WdS)
a*
        For each archive we've determined we need to operate on, pass it to
        each prepper so that we can extract necessary files and/or items for
        direct regex replacement. Preppers define these methods per parser,
        so it is possible that a single prepper will read the same file for
        different parsers/mappings. This is preferable to the alternative of
        building up monolithic lists of file paths, as we'd still need to
        manipulate these on a per-archive basis.

        :param archive: The archive we are currently using to prepare our
                        mappings with
        :type archive:  ``SoSObfuscationArchive`` subclass

        :param prepper: The individual prepper we're using to source items
        :type prepper:  ``SoSPrepper`` subclass
        rz	Prepping z parser with file z from zFailed to prep z
 map from z: Nz mapping with items from )rGrHrIrJrKZget_parser_file_listZget_file_contentr]r��
splitlines�
parse_linergZget_items_for_mapr��addZregex_itemsZadd_regex_item)rQr��prepperrSZpnameZ_fileZcontentr�rmZ	map_items�itemZritemrVrVrW�_prepare_archive_with_prepperTs(
2
z(SoSCleaner._prepare_archive_with_prepperccsbttjj�}g}x$|j�D]}|jtd|����qWx&t|dd�d�D]}||jd�VqHWdS)a
        Discover all locally available preppers so that we can prepare the
        mappings with obfuscation matches in a controlled manner

        :returns: All preppers that can be leveraged locally
        :rtype:   A generator of `SoSPrepper` items
        zsos.cleaner.preppers.cSs|jS)N)Zpriority)�xrVrVrW�<lambda>�sz)SoSCleaner.get_preppers.<locals>.<lambda>)�key)r(N)	rr-r.ZpreppersZget_modulesryr�sortedr7)rQ�helperZprepsZ_prepr�rVrVrW�get_preppers~szSoSCleaner.get_prepperscCs<|jd�x,|j�D] }x|jD]}|j||�q WqWdS)aBefore doing the actual obfuscation, if we have multiple archives
        to obfuscate then we need to preload each of them into the mappings
        to ensure that node1 is obfuscated in node2 as well as node2 being
        obfuscated in node1's archive.
        z.Pre-loading all archives into obfuscation mapsN)rLr�r�r�)rQr�r�rVrVrWr��s
z)SoSCleaner.preload_all_archives_into_mapsc4Cs��y�|jj|j�}tj�}|jd|�|js4|j�|jd�x�|j	�D]�}|j
|jd�d}|j|�rlqH|jj
r�|j|�r�|j|�qH|jj
r�t|�sH|j|�r�qHy$|j|||j�}|r�|j||�WqHtk
�r}z|jd|�d|���WYdd}~XqHXqHWy|j|�Wn:tk
�rT}z|jd|��|jd	�WYdd}~XnXy|j|�Wn:tk
�r�}z|jd
|��|jd	�WYdd}~XnX|j�s8|j�}|�r,|jd�y |j|j|j��|j|�WnJtk
�r*}z,|jd|j�d
|���|jd|���dSd}~XnX|jj|�tj�}	|jd|	�|jd|	|�|jdt|j ��|jd|j!�d}
|j"�r�d}
|
|j"}
|jd|
���Wn>tk
�r�}z |j#j$d|j�d|���WYdd}~XnXdS)z�Individually handle each archive or directory we've discovered by
        running through each file therein.

        Positional arguments:

            :param report str:      Filepath to the directory or archive
        �
start_timezBeginning obfuscation...r�r2zUnable to parse file z: Nz!Failed to obfuscate directories: )rZzFailed to obfuscate symlinks: zRe-compressing...zArchive z failed to compress: zFailed to re-compress archive: �end_timeZrun_timeZfiles_obfuscatedZtotal_substitutionsrz! [removed %s unprocessable files]zObfuscation completedzException while processing )%rFrE�archive_namerZnow�	add_fieldZis_extracted�extractZ
report_msgZ
get_file_listrJZshould_skip_filer7r%Zshould_remove_fileZremove_filerr�Zupdate_sub_countrgr]�obfuscate_directory_namesrL�obfuscate_symlinksr�Zget_compressionZrename_top_dirr��compressr�r�r�Z
file_sub_listZtotal_sub_countZremoved_file_countr;r^)rQr�Zarc_mdr�r��
short_name�countrm�methodr�ZrmsgrVrVrWr��s|





,

zSoSCleaner.obfuscate_reportc s�|sdSd}�s|jd�d�tjj|��s@�fdd�|jD�}|s^|jd�pP|�d��dS|jd	�pj|��|d
�tjd|jd���}t	|d
ddd��z}xr|D]j}y&|j
||�\}}	||	7}|j|�Wq�tk
�r}
z |jd��d|
��|d
�WYdd}
~
Xq�Xq�WWdQRX|j
d�|�r6tj|j|�WdQRX|j�jd�d�}�j�jd�d|�}|�k�r�|j��d}
tjj|
|�}tjj|��s�tj||�n&|jtj|��}tj|�tj||�|S)a5Obfuscate and individual file, line by line.

        Lines processed, even if no substitutions occur, are then written to a
        temp file without our own tmpdir. Once the file has been completely
        iterated through, if there have been substitutions then the temp file
        overwrites the original file. If there are no substitutions, then the
        original file is left in place.

        Positional arguments:

            :param filename str:        Filename relative to the extracted
                                        archive root
        Nrr�r2cs(g|] }t�fdd�|jD��s|�qS)c3s|]}|j��VqdS)N)�match)r��_skip)r�rVrW�	<genexpr>
sz7SoSCleaner.obfuscate_file.<locals>.<listcomp>.<genexpr>)�any�
skip_patterns)r��_p)r�rVrWr�sz-SoSCleaner.obfuscate_file.<locals>.<listcomp>zSkipping obfuscation of z" due to matching file skip patternzObfuscating )rZr�)�mode�dirrdzutf-8r�)re�errorszUnable to obfuscate rXr�r�r�)rJr<r>�islinkrGr]�tempfileZNamedTemporaryFiler)ri�obfuscate_liner�rgr�r�ZcopyfilerHr�r�r?�rename�readlinkrN�symlink)rQr�r�r�Zsubs�_parsersZtfiler�r�r�rmZ_ob_short_nameZ_ob_filenamer�Z_ob_pathZ
_target_obrV)r�rWr��sP

(


zSoSCleaner.obfuscate_filecs|jd|jd�x�|j�D]�}y�|j|j�djd���fdd�|jD�}|sb|jd��d��w|jd	���|jd�tj	|�}tj
j|j|j���}|j|�}||ks�||kr�tj
|�tj||�Wqtk
�r}z|jd
|�d|���WYdd}~XqXqWdS)
a�Iterate over symlinks in the archive and obfuscate their names.
        The content of the link target will have already been cleaned, and this
        second pass over just the names of the links is to ensure we avoid a
        possible race condition dependent on the order in which the link or the
        target get obfuscated.

        :param archive:     The archive being obfuscated
        :type archive:      ``SoSObfuscationArchive``
        zObfuscating symlink names)rZr2r�cs(g|] }t�fdd�|jD��s|�qS)c3s|]}|j��VqdS)N)r�)r�r�)�_symrVrWr�Psz;SoSCleaner.obfuscate_symlinks.<locals>.<listcomp>.<genexpr>)r�r�)r�r�)r�rVrWr�Osz1SoSCleaner.obfuscate_symlinks.<locals>.<listcomp>z Skipping obfuscation of symlink z due to skip pattern matchzObfuscating symlink zError obfuscating symlink 'z': N)rLr�Zget_symlinksrJr�r�rGr]r<r�r>r?r�rNr�rg)rQr�r�r�Z_targetZ_ob_sym_nameZ
_ob_targetrmrV)r�rWr�=s(





zSoSCleaner.obfuscate_symlinkscCs�|jd|j���x�t|j�dd�D]�}x|tj|�D]n}tjj||�}|j|j	�d}tjj
|�r4|j|�}||kr4|j|�}tjj|j	|j
d�|�}tj||�q4Wq$WdS)z�For all directories that exist within the archive, obfuscate the
        directory name if it contains sensitive strings found during execution
        z'Obfuscating directory names in archive T)�reverser2r�Nr�)rLr�r�Zget_directory_listr<�listdirr>r?rJr�rfr��rstripr�r�)rQr��dirpath�_nameZ_dirnameZ_arc_dirZ_ob_dirnameZ_ob_arc_dirrVrVrWr�ks

z$SoSCleaner.obfuscate_directory_namescCsVxP|jD]F}y|j|�}Wqtk
rL}z|jd|���WYdd}~XqXqW|S)NzError obfuscating string data: )rGZparse_string_for_keysrgrL)rQZstring_datar1rmrVrVrWr��s&zSoSCleaner.obfuscate_stringcCs�d}|j�s||fS|dkr"|j}x^|D]V}y|j|�\}}||7}Wq(tk
r|}z|jd|��|j�WYdd}~Xq(Xq(W||fS)a�Run a line through each of the obfuscation parsers, keeping a
        cumulative total of substitutions done on that particular line.

        Positional arguments:

            :param line str:        The raw line as read from the file being
                                    processed
            :param parsers:         A list of parser objects to obfuscate
                                    with. If None, use all.

        Returns the fully obfuscated line and the number of substitutions made
        rNzfailed to parse line: )rKrGr�rgr]rH)rQr�rGr�r1�_countrmrVrVrWr��s
*zSoSCleaner.obfuscate_linecCsP|jjd�}x>|jD]4}|j|jjdd�j��}|jdt|jj	j
���qWdS)zLWrite some cleaner-level, non-report-specific stats to the manifest
        rG� r�ZentriesN)rFrErGrHr�rIr�r�r�Zdataset�keys)rQZ	parse_secr1Z_secrVrVrWr��sz"SoSCleaner.write_stats_to_manifest)NNNFN)N)N)N)N)F)NN)N))�__name__�
__module__�__qualname__raZdescZarg_defaultsr5r[r]rLr`�classmethodrcrArsr�r�r�r@r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r��
__classcell__rVrV)rUrWr*s`%F



2`


*W
N.
r)-r�rjr9r<r�r�r��concurrent.futuresrr�pwdrZsos.cleaner.preppersr-rZ
sos.componentrZsos.cleaner.parsers.ip_parserrZsos.cleaner.parsers.mac_parserrZ#sos.cleaner.parsers.hostname_parserr	Z"sos.cleaner.parsers.keyword_parserr
Z#sos.cleaner.parsers.username_parserrZsos.cleaner.parsers.ipv6_parserrZsos.cleaner.archives.sosr
rrrZsos.cleaner.archives.genericrrZsos.cleaner.archives.insightsrZ
sos.utilitiesrrrrrrVrVrVrW�<module>s.

NineSec Team - 2022