+
    i  c                   s*   ^ RI t ^ RIt^ RIt^ RIt^ RIt^ RIt^ RIHt ^ RIHt ^ RI	H
t
 ^ RIt^ RIHt ^ RIHt ^ RIHt ^ RIHt ^ R	IHt ^ R
IHt ^ RIHt ^ RIHt ^ RIHtHtHtH t  ^ RI!H"t"H#t# ^ RI$H%t% ^ RI&H't'H(t(H)t)H*t* R t+ ! R R]4      t,R# )i    N)ProcessPoolExecutor)datetime)getpwuid)__version__)SoSComponent)SoSIPParser)SoSMacParser)SoSHostnameParser)SoSKeywordParser)SoSUsernameParser)SoSIPv6Parser)SoSReportArchiveSoSReportDirectorySoSCollectorArchiveSoSCollectorDirectory)DataDirArchiveTarballArchive)InsightsArchive)get_human_readableimport_moduleImporterHelperis_executablec                 s$    V P                  V4      # N)obfuscate_arc_files)arcZflist   &&9/usr/lib/python3.14/site-packages/sos/cleaner/__init__.pyr   r   *   s    ""5))    c                   sX  a a ] tR t^.t oRtRtRRR. R. R. R^R	. R
RRRRRRRRRR. RR/tR7V 3R lltR8R ltR8R lt	R8R lt
R8R lt]R 4       tR tR t]R 4       tR tR  tR! tR" tR# tR$ tR% tR& tR' tR9R( ltR) tR* tR+ tR, tR- tR. t R/ t!R0 t"R1 t#R2 t$R3 t%R4 t&R5 t'R6t(Vt)V ;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 private IP address from 172.17.0.0/22
range 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archive_typeautodomainsdisable_parsersskip_cleaning_filesjobskeywordskeyword_fileNmap_file /etc/sos/cleaner/default_mapping	no_updateFkeep_binary_filestarget 	usernamestreat_certificates	obfuscatec                s  < V'       g   \         SV `  WV4       R V n        EMVR,          V n        VR,          V n        VR,          V n        VR,          V n        VR,          V n        RV n        \        V P                  R4      '       g&   V P                  P                  V P                  n
        RV P                  n        \        P                  ! R	4      V n        \        P                  ! R
4      V n        \         P"                  ! \         P$                  P'                  V P                  R4      R R7       V P)                  4        V P+                  4       V n        \         P.                  ! ^?4       W@n        V P                  P3                  4       V n        V P                  P6                  P9                  R4      V n        V P                  P<                  '       d4   \         P$                  P?                  V P                  P<                  4      MRpV P,                  VV P                  P@                  .p\B        \D        \F        \H        \J        \L        .pV U	u. uF  qPN                  NK  	  p
p	V P                  PP                   F  pRpV
 F  pV^R PS                  4       pVPS                  4       PU                  4       V8X  g   K;  V PW                  RV 24       V P                  PY                  RV R24       V
P[                  V4       R p M	  V'       d   K  V P                  PY                  RV R24       K  	  V
 Uu. uF  p\]        4       V,          ! V!  NK  	  upV n/        \`        \b        \d        \f        \h        \j        \l        .V n7        RV n8        V PW                  RV P                   24       R# u up	i u upi )Toptionstmpdirsys_tmppolicymanifestFr"   r   sosZsos_uicleanerZexist_okz/etc/sos/cleanerzDisabling parser: zDisabling the 'zP' parser. Be aware that this may leave sensitive plain-text data in the archive.z&Failing to disable an unknown parser 'z'.Nz#Cleaner initialized. From cmdline: i)9Zsuper__init__from_cmdlineoptsr/   r0   r1   r2   ZhasattrZthreadsr"   r   loggingZ	getLoggersoslogui_logosmakedirspathjoinreview_parser_valuesload_map_filecleaner_mappingZumaskin_placeZget_preferred_hash_name	hash_nameZ
componentsadd_section
cleaner_mdr%   dirnamer!   r   r   r
   r   r   r	   __name__r    lowerstriplog_infowarningremoveZglobalsparsersr   r   r   r   r   r   r   archive_typesnested_archive)selfparserZargsZcmdlinerC   Zhook_commonscleaner_dirZparser_argsZparser_classesclsZparser_names_parserZfoundpnameZparser_nameZcls_name	__class__s   &&&&&&          r   r6   ZSoSCleaner.__init__h   s   GV73 $D
 %Y/DI&x0DK'	2DL&x0DK(4DM %D499f--!%!2!2		%+DII"!++E2DK!++H5DK KKT[[)<tL!!##113
 <<>--22>>yI yy!!! ggoodii&8&89'9 	   II))
 
 %3
$2SLLN 	 
 yy00GE%#Abk//1==?((*k9MM$6{m"DEKK'')' 3O O !''. E & 5##<WIRH 1( )
( Ih-(
 !	
 #1$2C2C1DE	GO
,
s   N:N?c                s,    R V'       d   RV 2MR RV 2# )z[cleanerZ:r*   z]  rQ   msgcaller   &&&r   _fmt_log_msgZSoSCleaner._fmt_log_msg   s     &AfX,b9C5AAr   c                Z    V P                   P                  V P                  W4      4       R # r   )r:   Zdebugr]   rY   r\   r   	log_debugZSoSCleaner.log_debug       $++C89r   c                r^   r   )r:   infor]   rY   r\   r   rK   ZSoSCleaner.log_info   s    **378r   c                r^   r   )r:   errorr]   rY   r\   r   	log_errorZSoSCleaner.log_error   r`   r   c                s^    VP                  R 4       VP                  V P                  4       R# )zSoS Cleaner Detailed HelpN)Z	set_titleZadd_text__doc__)rT   Zsectionr   r   display_helpZSoSCleaner.display_help   s#    56%r   c           	     sT   / pRp\         P                  P                  V P                  P                  4      '       d$   \        RV P                  P                   R24      h\         P                  P                  V P                  P                  4      '       gG   V P                  P                  V8w  d*   V P                  RV P                  P                   R24       V# \        V P                  P                  RRR7      ;_uu_ 4       p \        P                  ! V4      pRRR4       V#   \        P                   d    T P                  R	4        L2\
         d6   pT P                  R
T P                  P                   RT 24        Rp?LlRp?ii ; i  + '       g   i     T# ; i)zVerifies 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 z is a directoryzERROR: map file z6 does not exist, will not load any obfuscation matchesZrutf-8ZencodingzOERROR: Unable to parse map file, json is malformed. Will not load any mappings.zERROR: Could not load '': N)r<   r>   isdirr8   r%   	Exceptionexistsrc   openjsonZloadZJSONDecodeError)rQ   Z_confZdefault_mapmferr   &    r   rA   ZSoSCleaner.load_map_file   sk    877==++,,1$))2D2D1E F( ( ) )ww~~dii0011yy!![0&tyy'9'9&: ;< <=  dii((#@@BE IIbME A  ++ MNN $L M  ENN $''+yy'9'9&:#cU$D E EE A@ sB   
FD,,&FFFF+F	FFFF'	c                s   V P                  R4      pV P                  P                  R\         R24       V P                  P                  V4       V P                  P
                  '       g    \        R4       R# R#   \         d1    T P                  P                  R4       T P                  ^4        R# \         d   pT P                  ^T4        Rp?R# Rp?ii ; i)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 cancelN)
Z_fmt_msgr;   ra   r   r8   ZbatchZinputKeyboardInterrupt_exitrj   )rQ   rZ   Ze   &  r   print_disclaimerZSoSCleaner.print_disclaimer   s     mm   	0SAByy!GH  %    !;<

3 !

1a  !s$   -A< <7C6C?C CCc           	     sz   R Vn         VP                  RR4      pVP                  RRRR7       VP                  RR. R:OR	R
7       VP                  RR. RR7       VP                  RR. RRR7       VP                  RRR. RRR7       VP                  RR^\        RR7       VP                  RR. RRR7       VP                  RRR R!R"7       VP                  R#R$R%R&R'7       VP                  R(R)R*R+R,R-7       VP                  R.R*R+R/R0R17       VP                  R2R3. RR4R-7       VP                  R5R6. R;OR7R8R97       R# )<zsos clean|mask TARGET [options]zCleaner/Masking Optionsz7These options control how data obfuscation is performedr)   ZTARGETz%The directory or archive to obfuscate)Zmetavarhelpz--archive-typer   z8Specify what kind of archive the target was generated as)defaultchoicesru   z	--domainsextendz!List of domain names to obfuscate)actionrv   ru   z--disable-parsersr    zCDisable specific parsers, so that those elements are not obfuscated)ry   rv   destru   z--skip-cleaning-filesz--skip-masking-filesr!   zBList of files to skip/ignore during cleaning. Globs are supported.z-jz--jobsz&Number of concurrent archives to clean)rv   Ztyperu   z
--keywordsr#   zList of keywords to obfuscatez--keyword-fileNr$   z&Provide a file a keywords to obfuscate)rv   rz   ru   z
--map-filer%   r&   z;Provide a previously generated mapping file for obfuscation)rz   rv   ru   z--no-updater'   FZ
store_truez<Do not update the --map-file with new mappings from this run)rz   rv   ry   ru   z--keep-binary-filesr(   zGKeep unprocessable binary files in the archive instead of removing them)rv   ry   rz   ru   z--usernamesr+   zList of usernames to obfuscatez--treat-certificatesr-   r,   zHow to treat certificate files [.csr .crt .pem]. Defaults to "obfuscate" after convert the file to text. "Key" certificate files are always removed.)rv   rw   rz   ru   )r   ZreportZcollectZinsightszdata-dirtarball)r-   keeprM   )ZusageZadd_argument_groupZadd_argumentZint)rT   rR   Z	clean_grps   && r   add_parser_optionsZSoSCleaner.add_parser_options  s   8--%E
	 	x$K 	 	M/(K&8 	 	:
 	{8R$G 	 	I28')0A&C 	 	E 	68N&.$9&F 	 	H
 	tXqs$L 	 	N|Hb$.$C 	 	E 	/$2$L 	 	N 	|*'I&< 	 	> 	};&2%= 	 	> 	4e&2$7%G 	 	H
 	};&.$D 	 	F 	5{'F$8$.	 	 	0r   c                s&    WP                   n        R# )zFor 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)r8   r)   )rQ   r>   r   r   set_target_pathZSoSCleaner.set_target_pathJ  s    
  		r   c                s   RpV P                   P                  R8w  d   V P                   P                  P                  RR4      pV P                   Fg  pVP                  V8X  g   K  V! V P                   P
                  V P                  V P                   P                  V P                   P                  4      pKi  	  MV P                   F  pVP                  V P                   P
                  4      '       g   K0  V! V P                   P
                  V P                  V P                   P                  V P                   P                  4      p M	  V'       g   R# Wn
        V P                  P                  V4       VP                  '       dK   V P                  P                  VP                  4       4       V P                  P!                  V4       Wn        V P"                  '       d(   V P"                  P$                  V P"                  n        R# R# )zThe 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   Z-_)r8   r   replacerO   Z	type_namer)   r/   r(   r,   Zcheck_is_typemain_archivereport_pathsappend	is_nestedrx   Zget_nested_archivesrM   rP   Zdescriptionui_name)rQ   Z_arcZ
check_typearchiver   rp   r   inspect_target_archiveZ!SoSCleaner.inspect_target_archiveQ  s    99!!V+//77SAJ--$$
2"499#3#3T[[#'99#>#>#'99#?#?AD . ))$$TYY%5%566tyy//#yy::#yy;;=D  *    &>>>$$T%=%=%?@ $$T*"&*.*=*=*I*ID' r   c                s8   V P                   P                   F1  p\        VP                  R4      4      ^8  g   K$  \	        RV R24      h	  V P                   P
                   Uu. uF  p\        P                  ! V4      NK  	  upV P                   n        R# u upi )zCheck 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.
.zInvalid value 'z0' given: --domains values must be actual domainsN)r8   r   lensplitrj   r!   fnmatchZ	translate)rQ   Z_domZprs   r   r@   ZSoSCleaner.review_parser_valuess  s     II%%D4::c?#a'%dV ,% %  & *.)F)F)H)F CD):):1)=)F)H		% )Hs   $Bc                s
   V P                   P                  P                  R4      V P                   n        V P                   P                  P                  R4      R,          P                  R4      ^ ,          V n        V P
                  '       d   V P                  4        . V n        \        P                  P                  V P                   P                  4      '       gD   V P                  P                  RV P                   P                   24       V P                  ^4       V P                  4        V P                  '       g-   V P                  P                  R4       V P                  ^4       . V n        V P                    F0  pVP"                  R8X  g   K  VP$                  P'                  4        K2  	  V P)                  4        V P+                  4        V P-                  4        V P                  '       gA   V P.                  '       d   R# V P                  P1                  R4       V P                  ^4       V P                  P1                  R\3        V P                  4       R	24       V P5                  4       pV P7                  V4      pV P9                  V4       V P;                  4        V P.                  '       d(   V P                   Uu. uF  qDP<                  NK  	  ppW53# Rp\3        V P                  4      ^8  d   V P?                  4       pMV P                  ^ ,          pVP<                  pV PA                  VP<                  4      p	V	e   V PC                  VP                  R4      R,           R
V PD                   24      p
\G        \        P                  PI                  V PJ                  V
4      RRR7      ;_uu_ 4       pVPM                  V	4       RRR4       V PO                  4        \        P                  PI                  V PJ                  V PC                  VP                  R4      R,          4      4      p\P        PR                  ! Wv4       \        PT                  ! V4      pV P                  P1                  RV 24       V P                  P1                  RV R24       V P                  P1                  R\W        VPX                  4       24       V P                  P1                  R\[        VP\                  4      P^                   R24       V P                  P1                  R4       V Pa                  4        R# u upi   + '       g   i     ELx; i)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.
/z.tarz*Invalid target: no such file or directory z'No valid archives or directories found
zHostname ParserNz#No reports obfuscated, aborting...
z
Successfully obfuscated z report(s)
r   wrf   rg   z2A 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)1r8   r)   rstripr   arc_namer7   rt   r   r<   r>   rk   r;   rb   rr   r   completed_reportsrN   namemappingZset_initial_countspreload_all_archives_into_mapsgenerate_parser_item_regexesobfuscate_report_pathsrC   ra   r   compile_mapping_dictwrite_map_for_archivewrite_map_for_configwrite_stats_to_manifestfinal_archive_pathrebuild_nested_archiveget_new_checksumobfuscate_stringrD   rl   r?   r0   writewrite_cleaner_logshutilmoveZstatr   Zst_sizer   Zst_uidZpw_nameZcleanup)rQ   rR   _mapmap_pathZaZ	arc_pathsZ
final_pathZarc_pathr   checksumZchksum_nameZcfZarcstats   &            r   executeZSoSCleaner.execute  s$     99++2237				((..s3B7==fEaH!!#ww~~dii..//KKJ!%!1!1 24 5JJqM##%   KKHIJJqM "$ llF{{//113 # 	++-))+##%%%%}}}KKCDJJqM5 6 678F 	G ((*--d3!!$'$$&===7;7M7MN7M!--7MIN&&
t%%&*224H((+C--H,,S-C-CDH#"33~~c*2./q0@A "'',,t||[A3#*, ,/1HHX&, ""$WW\\LL!!(.."5b"9:

 	H)''*%
 	B8*M	
 	::,bI	
 	8$6w$G#HIJ9Xgnn%=%E%E$FbIJ L 	M 	U O, , ,s   U UU	c                sl   V P                   R,           pV P                  VR7       V P                   Ft  pVP                  P	                  R4      R	,          pV P                  VP                  4      pVf   KF  RV RV P                   2pV P                  P                  WER7       Kv  	  \        P                  ! V P                  P                  4       F  w  rgpV F  p	\        P                  P                  Wi4      p
V
P	                  V P                  P                  4      R	,          pVP                  R4      pV P                  P!                  WR7       \        P"                  ! V
4       K  	  K  	  V P%                  RR7       V P                  P'                  V P(                  P*                  4      # )
zxHandles repacking the nested tarball, now containing only obfuscated
copies of the reports, log files, manifest, etc...
z-obfuscated)r   r   z
checksums/r   rz   T)r   r   )r   Zsetup_archiver   r   r   r   rD   r   Z
add_stringr<   ZwalkrP   extracted_pathr>   r?   lstripadd_filerM   r   Zfinalizer8   Zcompression_type)rQ   r   r   Zarc_destr   ZdnameZdirnr   ZfilesfilenameZfnames   &          r   r   Z!SoSCleaner.rebuild_nested_archive  sX   
 ===0)--G1177<R@H,,W-G-GHH#$XJa/?@'''= . !ggd&9&9&H&HINDU!T4D$7$7$F$FGKS)%%e%8		%  " J 	t,||$$TYY%?%?@@r   c                s    / pV P                    F@  p/ WP                  &   WP                  ,          P                  VP                  4       4       KB  	  V# )zBuild 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
)rN   Zmap_file_keyupdateZget_map_contents)rQ   r   rR   rs   r   r   ZSoSCleaner.compile_mapping_dict  sL     llF(*D$$%$$%,,V-D-D-FG # r   c           	     s    \        VRRR7      ;_uu_ 4       pVP                  \        P                  ! V^R7      4       RRR4       V#   + '       g   i     T# ; i)zZWrite the mapping to a file on disk that is in the same location as
the final archive(s).
r   rf   rg   )ZindentN)rl   r   rm   Zdumps)rQ   r   r>   rn   s   &&& r   write_map_to_fileZSoSCleaner.write_map_to_file  sD     $g.."HHTZZQ/0 / /.s   (A		A	c                s    \         P                  P                  V P                  V P	                  V P
                   R 24      4      pV P                  W4      #   \         d    pT P                  RT 24        Rp?R# Rp?ii ; i)z-private_mapz"Could not write private map file: N)	r<   r>   r?   r0   r   r   r   rj   rc   )rQ   r   r   ro      &&  r   r   Z SoSCleaner.write_map_for_archive  ss    	ww||%%|&DEH ))$99 	NN?uEF	s   AA B$A>>Bc                s
   V P                   P                  '       d   V P                   P                  '       g   \        P                  P                  V P                   P                  4      p \        P                  ! VRR7       V P                  WP                   P                  4       V P                  RV P                   P                   24       R# R# R#   \         d    pT P                  RT 24        Rp?R# Rp?ii ; i)zmWrite the mapping to the config file so that subsequent runs are
able to provide the same consistent mapping
Tr5   zWrote mapping to z&Could not update mapping config file: N)r8   r%   r'   r<   r>   rG   r=   r   r_   rj   rc   )rQ   r   rS   ro   r   r   r   ZSoSCleaner.write_map_for_config  s     99dii&9&9&9''//$))*<*<=KOK$7&&tYY-?-?@!24993E3E2FGH ':  O!GuMNNOs   -A%C D#C==Dc                s   \         P                  P                  V P                  V P                   R24      p\        VRRR7      ;_uu_ 4       pV P                  P                  ^ 4       V P                  P                  4        F  pVP                  V4       K  	  RRR4       V'       d1   V P                  V4       V P                  P                  VRR7       R# R#   + '       g   i     LJ; i)zWhen 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   rf   rg   Nzsos_logs/cleaner.logr   )r<   r>   r?   r0   r   rl   Zsos_log_fileZseekZ	readlinesr   obfuscate_filer   r   )rQ   r   Zlog_nameZlogfilelines   &&   r   r   ZSoSCleaner.write_cleaner_log*  s    
 77<<LLT]]O+;<
 (C'22g""1%))335d# 6 3
 )LL!!(1G!H  32s   ACC.	c                s    Rp\        VR4      ;_uu_ 4       p\        P                  ! V P                  4      p VP	                  V4      pV'       g   MVP                  V4       K.  VP                  4       R,           uuRRR4       #   + '       g   i     R# ; i  \         d    pT P                  RT 24        Rp?R# Rp?ii ; i)zfCalculate a new checksum for the obfuscated archive, as the previous
checksum will no longer be valid
Zrbr   Nz!Could not generate new checksum: i   )	rl   hashlibZnewrD   Zreadr   Z	hexdigestrj   r_   )rQ   archive_pathZ	hash_sizeZ
archive_fpZdigestZhashdataro   s   &&     r   r   ZSoSCleaner.get_new_checksum;  s    	FIlD))Z T^^4)y9H#MM(+'')D0 *)))   	FNN>seDEE	Fs:   B :B*B=
B B	B B C'CCc                s    R\        V P                  4       RV P                  P                   R2pV P                  P                  V4       V P                  P                  '       d   V P                  P                  R4       V P                  P                  R8X  d>   \        R4      '       g-   RV P                  n        V P                  P                  R4       V P                  P                  R8X  d   V P                  P                  R	4       MmV P                  P                  R
8X  d   V P                  P                  R4       M6V P                  P                  R8X  d   V P                  P                  R4       V P                   F<  pV P                  P                  RVP                   24       V P                  V4       K>  	  V P                  '       d.   V P                  4        V P                  V P                  4       R# R#   \         d6    T P                  P                  R4       \        P                   ! ^4        R# i ; i)zPerform 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.
zFound z. total reports to obfuscate, processing up to z! concurrently within one archive
zpWARNING: binary files that potentially contain sensitive information will NOT be removed from the final archive
r-   ZopensslrM   zgWARNING: No `openssl` command available. Replacing `--treat-certificates` from `obfuscate` to `remove`.zWARNING: certificate files that potentially contain sensitive information will be CONVERTED to text and OBFUSCATED in the final archive.
r|   zsWARNING: certificate files that potentially contain sensitive information will be KEPT in the final archive as is.
zpWARNING: certificate files that potentially contain sensitive information will be REMOVED in the final archive.
zObfuscating zExiting on user cancelN)r   r   r8   r"   r;   ra   r(   rL   r,   r   r   obfuscate_reportrP   _replace_obfuscated_archivesrq   r<   rr   )rQ   rZ   Zreport_pathrs   r   r   Z!SoSCleaner.obfuscate_report_pathsM  s   -	T../0 1$$(IINN#3 4 
 KKS!yy***##O 		,,;))44/7		,##K yy++{:##9
 --7##'
 --9##!"  $00  <0H0H/I!JK%%k2  1 """113%%d&9&9: # ! 	KK56HHSM	s   GH +H <IIc                sh   V P                    F  p\        P                  ! VP                  4       V P                  P
                  pVP                  P                  R4      R,          p\        P                  P                  W#4      p\        P                  ! VP                  V4       WAn        K  	  R# )zWhen we have a nested archive, we need to rebuild the original
archive, which entails replacing the existing archives with their
obfuscated counterparts
r   Nr   )r   r<   rM   r   rP   r   r   r   r>   r?   r   r   )rQ   r   rz   r{   Z	dest_namerp   r   r   Z'SoSCleaner._replace_obfuscated_archives  s~    
 --GIIg**+&&55D0066s;B?GT3IKK22D9)2& .r   c                sL    V P                    F  pVP                  4        K  	  R# )zFor 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)rN   Zgenerate_item_regexes)rQ   rR   s   & r   r   Z'SoSCleaner.generate_parser_item_regexes  s    
 llF((* #r   c                sl   V P                    EFT  pVP                  P                  4       P                  4       ^ ,          P	                  4       pVP                  WA4       Fk  pVP                  V4      pV'       g   K  V P                  RV RV RVP                   24       VP                  4        F  p VP                  V4       K  	  Km  	  VP                  WA4      p	V	'       dF   V P                  RV RVP                   24       V	 F  p
VP                  P                  V
4       K   	  VP                  V,           F  pVP                  P!                  V4       K   	  EKW  	  VP#                  V P                   4       R#   \         d&   pT P                  RT RT RT 24        Rp?K  Rp?ii ; i)	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
z	Prepping z parser with file z from zFailed to prep z
 map from : Nz mapping with items from )rN   r   rI   r   rJ   Zget_parser_file_listZget_file_contentr_   r   Z
splitlinesZ
parse_linerj   Zget_items_for_mapr   ZaddZregex_itemsZadd_regex_itemset_parsers)rQ   r   prepperrU   rV   Z_fileZcontentr   ro   Z	map_itemsZitemZritems   &&&         r   _prepare_archive_with_prepperZ(SoSCleaner._prepare_archive_with_prepper  s   " ||GLL&&(..0399;E 55eE!22595'1CE7 K''.&7 9 :#..0D**40 1 F  11%AI5'1J")//!2 4 5%DOO''- & !,,U33..u5 4- $2 	DLL) % -eWJugRuM s   5FF3	F.	.F3	c              #  s  "   \        \        P                  P                  4      p. pVP	                  4        F   pVP                  \        RV 24      4       K"  	  \        VR R7       F  pV! V P                  R7      x  K  	  R# 5i)z
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
zsos.cleaner.preppers.c                 s    V P                   # r   )Zpriority)Zxs   &r   Z<lambda>Z)SoSCleaner.get_preppers.<locals>.<lambda>  s    1::r   )Zkey)r.   N)	r   r3   r4   ZpreppersZget_modulesrx   r   sortedr8   )rQ   ZhelperZprepsZ_prepr   rp   r   get_preppersZSoSCleaner.get_preppers  sm        4 45'')ELL)>ug'FGH *e)=>G$)),, ?s   BBc                s    V P                  R4       V P                  4        F'  pV P                   F  pV P                  W!4       K  	  K)  	  V P                  P                  V P                  4       R# )zBefore 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)rK   r   r   r   r   r   rN   )rQ   r   r   rs   r   r   Z)SoSCleaner.preload_all_archives_into_maps  s\     	FG((*G,,227D - + 	%%dll3r   c                s    V P                   P                  VP                  4      p\        P                  ! 4       pVP                  RV4       VP                  '       g   VP                  4        VP                  R4       \        VP                  4       4      p^ ;p;rg\        V P                  P                  4       Uu. uF  qNK  	  p	p\        V P                  P                  VP                  R7      ;_uu_ 4       p
T
P!                  \"        T	\        V P                  P                  4       Uu. uF"  qVRV P                  P                  1,          NK$  	  up4      pV F  w  rpW\,          pWm,          pW~,          pK   	  RRR4       VP                  4         V P%                  V4        V P+                  V4       VP,                  '       g   VP/                  4       pV'       dN   VP                  R4        VP1                  V P3                  VP                  4      4       VP5                  V4       V P8                  P;                  V4       \        P                  ! 4       pVP                  RV4       VP                  RVV,
          4       VP                  RV4       VP                  RV4       RpV'       d   RpVV,          pVP                  RV 24       R# u upi u upi   + '       g   i     ELp; i  \&         d,   pT P)                  RT 2TP                  R7        Rp?ELRp?ii ; i  \&         d,   pT P)                  RT 2TP                  R7        Rp?ELRp?ii ; i  \&         dA   pT P7                  R	TP                   R
T 24       TP                  RT 24        Rp?R# Rp?ii ; i  \&         d7   pT P<                  P?                  RTP                   RT 24        Rp?R# Rp?ii ; i)zIndividually handle each archive or directory we've discovered by
running through each file therein.

Positional arguments:

    :param archive str:      Filepath to the directory or archive

start_timezBeginning obfuscation...)Zmax_workersZinitializerNz!Failed to obfuscate directories: r[   zFailed to obfuscate symlinks: zRe-compressing...zArchive z failed to compress: zFailed to re-compress archive: end_timeZrun_timeZfiles_obfuscatedZtotal_substitutionsr*   z! [removed %s unprocessable files]zObfuscation completedzException while processing r   ) rF   rE   archive_namer   Znow	add_fieldZis_extractedZextractZ
report_msgZlistZ	get_filesZranger8   r"   r    Zload_parser_entriesZmapr   obfuscate_directory_namesrj   rK   obfuscate_symlinksr   Zget_compressionZrename_top_dirr   Zcompressr_   r   r   r;   ra   )rQ   r   Zarc_mdr   Z	file_listZfiles_obfuscated_countZtotal_sub_countZremoved_file_countZiZarchive_listZexecutorZfuturesZfocZtscZrfcro   Zmethodr   Zrmsgs   &&                 r   r   ZSoSCleaner.obfuscate_report  s   ^	?__001E1EFF!J\:6'''!9:W..01I* MNM"M_-2499>>-BC-BG-BLC$ $		 ' ; ;= =@H",,':L(-diinn(=(?(= JK!2CTYY^^2C(D(D(=(?@ (/OSs*1*#*O&-& (/=" '');..w7
;''0 $$$ 002&&':;
.. 11'2F2FG  ((0 &&--g6||~HZ2ZJ)>?/1GH2ODD!:00!6tf=>w D
(?	= = =*  ; A#G%,%9%9  ; ;;  ; >seD%,%9%9  ; ;; % '2F2F1G H88;u(> ?**=cUCE&  	?KK: ' 4 45Ru> ? ?	?s   AO   A O   
K"
5O  ?3K,2(K'+K,O  L  0L9 O  O  +O  =;M2 8BO  O  "O  'K,,K=	7	O   L6 L1+O  1L66O  9M/ M*$O  *M//O  2N==5N82O  8N==O   P+O<<Pc                s>    V P                   P                  V.4       R # r   )r   r   )rQ   r   r   r   r   ZSoSCleaner.obfuscate_fileH  s    --xj9r   c                s  a	 V P                  RVP                  R7       VP                  4        EF  p VP                  VP                  4      ^,          P                  R4      o	V P                   Uu. uF[  p\        ;QJ d)    V	3R lVP                   4       F  '       g   K   RM	  RM! V	3R lVP                   4       4      '       d   KY  VNK]  	  ppV'       g   V P                  RS	 R24       K  V P                  R	S	 2VP                  R7       \        P                  ! V4      p\        P                  P                  VP                  V P                  S	4      4      pV P                  V4      pWb8w  g   Wu8w  d0   \        P                  ! V4       \        P                   ! Wv4       EK  EK  	  R# u upi   \"         d$   pT P                  R
T RT 24        Rp?EK  Rp?ii ; i)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``
zObfuscating symlink namesr   r   c              3   sD   <"   T F  qP                  S4      x  K  	  R # 5ir   )Zmatch)Z.0Z_skip_syms   & r   Z	<genexpr>Z0SoSCleaner.obfuscate_symlinks.<locals>.<genexpr>^  s     H7GeD))7Gs    TFz Skipping obfuscation of symlink z due to skip pattern matchzObfuscating symlink zError obfuscating symlink 'rh   N)rK   r   Zget_symlinksr   r   r   rN   ZanyZskip_patternsr_   r<   Zreadlinkr>   r?   r   rM   symlinkrj   )
rQ   r   r   Z_pZ_parsersZ_targetZ_ob_sym_nameZ
_ob_targetro   r   s
   &&       @r   r   ZSoSCleaner.obfuscate_symlinksK  s    	1':N:NO++-G O}}W%;%;<Q?FFsK "&!-2CHr7G7GHCCCHr7G7GHH B    NN:4& A( ) !5dV<&-&:&:  < ++g.  "ww||G,B,B,0,A,A$,G I "227;
 !+1FIIg&JJz8 2G; .4  O ;G9CuMNNOsB   ?F<3F7F7$%F7F7F<5B8F<7F<<G*G%%G*c                s   V P                  RVP                   24       \        VP                  4       RR7       EF  p\        P
                  ! V4       F  p\        P                  P                  W#4      pVP                  VP                  4      R,          p\        P                  P                  V4      '       g   Kk  V P                  V4      pWc8w  g   K  VP                  V4      p\        P                  P                  VP                  VP                  R4      V4      p\        P                  ! WG4       K  	  EK  	  R# )zFor 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)Zreverser   Nr   )rK   r   r   Zget_directory_listr<   Zlistdirr>   r?   r   r   ri   r   r   r   Zrename)rQ   r   ZdirpathZ_nameZ_dirnameZ_arc_dirZ_ob_dirnameZ_ob_arc_dirs   &&      r   r   Z$SoSCleaner.obfuscate_directory_namesy  s     	? --.0 	1g88:DIIGG,77<<7#>>'*@*@A"E77==**"&"7"7">K"+&.ooe&<&(ggll#22'..s3''
 		(8 - Jr   c                s    V P                    F  p VP                  V4      pK  	  V#   \         d    pT P                  R T 24        Rp?K@  Rp?ii ; i)zError obfuscating string data: N)rN   Zparse_string_for_keysrj   rK   )rQ   Zstring_datarR   ro   r   r   r   ZSoSCleaner.obfuscate_string  sY    llFG$::;G #
   G ?uEFFGs   (AAAc                sN   V P                   P                  R4      pV P                   Fy  pVP                  VP                  P	                  RR4      P                  4       4      pVP                  R\        VP                  P                  P                  4       4      4       K{  	  R# )zLWrite some cleaner-level, non-report-specific stats to the manifest
        rN   Z r   ZentriesN)rF   rE   rN   r   r   rI   r   r   r   ZdatasetZkeys)rQ   Z	parse_secrR   Z_secs   &   r   r   Z"SoSCleaner.write_stats_to_manifest  sv     OO//	:	llF(()<)<S#)F)L)L)NODNN9c&..*@*@*E*E*G&HI #r   )r   rO   rB   rF   r   r7   rD   rC   r   r2   rP   r8   rN   r1   r   r:   r0   r/   r;   )NNNFNr   )F)+rH   Z
__module__Z__qualname__Z__firstlineno__rd   ZdescZarg_defaultsr6   r]   r_   rK   rc   Zclassmethodre   rA   rt   r}   r~   r   r@   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Z__static_attributes__Z__classdictcell__Z__classcell__)rW   Z__classdict__s   @@r   r   r   .   s6    %N DD 	22rB6UU"RkL [GzB:9: & &8!: 80 80t  JDH_BA0	OI"$4l3+**X-
4g?R:,O\9.J Jr   r   )-r   rm   r9   r<   r   r   Zconcurrent.futuresr    r   Zpwdr   Zsos.cleaner.preppersr3   r   Zsos.componentr   Zsos.cleaner.parsers.ip_parserr   Zsos.cleaner.parsers.mac_parserr   Z#sos.cleaner.parsers.hostname_parserr   Z"sos.cleaner.parsers.keyword_parserr   Z#sos.cleaner.parsers.username_parserr	   Zsos.cleaner.parsers.ipv6_parserr
   Zsos.cleaner.archives.sosr   r   r   r   Zsos.cleaner.archives.genericr   r   Zsos.cleaner.archives.insightsr   Zsos.utilitiesr   r   r   r   r   r   rX   r   r   <module>r      so       	   2     & 5 7 A ? A 9= = H 9: :
*pJ pJr   