File manager - Edit - /usr/lib/python3/dist-packages/sos/cleaner/__pycache__/__init__.cpython-310.pyc
Back
o i��d�� � @ s$ d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlm Z d dl m Z d dlmZ d dlm Z d dlmZ d dlmZ d dlmZ d d lmZ d d lmZ d dlmZ d dlmZ d d lmZmZmZm Z d dl!m"Z"m#Z# d dl$m%Z% d dl&m'Z'm(Z(m)Z) d dl*m+Z+ G dd� 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)�fillc sd e Zd ZdZdZdg g dg dddddg d �Z dN� fd d� ZdOdd �ZdOdd�ZdOdd�Z dOdd�Z dd� Zedd� �Z dd� Zdd� Zedd� �Zdd� Zd d!� Zd"d#� Zd$d%� Zd&d'� Zd(d)� Zd*d+� Zd,d-� Zd.d/� ZdPd0d1�Zd2d3� Zd4d5� Zd6d7� Zd8d9� Zd:d;� Zd<d=� Z d>d?� Z!d@dA� Z"dQdBdC�Z#dDdE� Z$dFdG� Z%dHdI� Z&dOdJdK�Z'dLdM� Z(� Z)S )R� 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� N� /etc/sos/cleaner/default_mappingF� )�archive_type�domains�disable_parsers�jobs�keywords�keyword_file�map_file� no_update�keep_binary_files�target� usernamesc s� |st t| ��|||� d| _nC|d | _|d | _|d | _|d | _|d | _d| _t | jd�s6d | j_ d | j_t� d�| _t� d�| _tjtj�| jd �dd� | �� | �� | _t�d� || _| j�� | _| jj�d �| _t| j�t| j�t | j�t!| j�t"| j�t#| j�g| _$| jj%D ]1}| j$D ]+}|j&�'� �(d�d �)� }|�'� �)� |kr�| �*d| � | j�+d| � | j$�,|� q�q�t-t.t/t0t1t2t3g| _4d | _5| �*d| j � d S )NT�options�tmpdir�sys_tmp�policy�manifestFr r r �sos�sos_ui�cleaner��exist_ok�? �parserr zDisabling parser: %szaDisabling the '%s' parser. Be aware that this may leave sensitive plain-text data in the archive.z%Cleaner initialized. From cmdline: %s)6�superr �__init__�from_cmdline�optsr) r* r+ r, �hasattrr r �logging� getLogger�soslog�ui_log�os�makedirs�path�join�validate_parser_values� load_map_file�cleaner_mapping�umask�in_place�get_preferred_hash_name� hash_name� components�add_section� cleaner_mdr r r r r r �parsersr �name�lower�split�strip�log_info�warning�remover r r r r r r � archive_types�nested_archive) �selfr3 �args�cmdlinerE �hook_commons�_parser�_loaded�_loaded_name�� __class__� �6/usr/lib/python3/dist-packages/sos/cleaner/__init__.pyr5 ^ sj � ����� �zSoSCleaner.__init__c C s d|r d| |f S d|f S )Nz[cleaner%s] %sz:%sr r^ �rU �msg�callerr^ r^ r_ �_fmt_log_msg� s zSoSCleaner._fmt_log_msgc C � | j �| �||�� d S �N)r; �debugrc r` r^ r^ r_ � log_debug� � zSoSCleaner.log_debugc C rd re )r; �inforc r` r^ r^ r_ rP � rh zSoSCleaner.log_infoc C rd re )r; �errorrc r` r^ r^ r_ � log_error� rh zSoSCleaner.log_errorc C s0 d}d}|� � D ] }|t||dd� d }q|S )N�P r F)�replace_whitespace� )� splitlinesr )rU ra �width�_fmt�liner^ r^ r_ �_fmt_msg� s zSoSCleaner._fmt_msgc C s |� d� |�| j� d S )NzSoS Cleaner Detailed Help)� set_title�add_text�__doc__)�cls�sectionr^ r^ r_ �display_help� s zSoSCleaner.display_helpc C s i }d}t j�| jj�rtd| jj ��t j�| jj�s-| jj|kr+| �d| jj � |S t| jjd��L}zt � |�}W n+ t jyK | �d� Y n% tyg } z| �d| jj|f � W Y d}~nd}~ww W d � |S W d � |S W d � |S 1 s�w Y |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 r z$Requested map file %s is a directoryzHERROR: map file %s does not exist, will not load any obfuscation matches�rzOERROR: Unable to parse map file, json is malformed. Will not load any mappings.zERROR: Could not load '%s': %sN)r= r? �isdirr7 r# � Exception�existsrk �open�json�load�JSONDecodeError)rU �_conf�default_map�mf�errr^ r^ r_ rB � sH ��� � ���� � � � � � � zSoSCleaner.load_map_filec C s� | � d�}| j�dt � | j�|� | jjsLztd� W dS ty3 | j�d� | �d� Y dS t yK } z | �d|� W Y d}~dS d}~ww dS )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 %s) z- Press ENTER to continue, or CTRL-C to quit. z Exiting on user cancel� � N) rs r<