3 h>`@sddlmZddlmZddlZddlZddlTddlmZddlm Z ddl Z ddl m Z m Z e je dd e dd d d Zy ejaWnek rej aYnXd ddddddddddddddddgZedkre je dd e dd ddd l madd!l m Z ddlTddlTddlTddlTddljZddlTddlZddlTdd"l m!Z!ddl"Z"ddl#Z#d#d$Z$dZ%d%Z&d&Z'e%td'e&td(e'td)iZ(e%d*e&d+e'd,iZ)e%e&e'd-Z*Gd.d d e+Z,Gd/dde-Z.Gd0dde-Z/Gd1dde-Z0iZ1td2e1d3<td4e1d5<td6e1d7<td8e1d9<td:e1d;<td<e1d<<td=e1d=<td>e1d><td?e1d?<td@e1d@<tdAe1dA<tdBe1dB<tdCe1dC<dDdEZ2GdFdGdGe-Z3GdHdde-Z4GdIdde-Z5GdJdde-Z6GdKdde-Z7GdLdde-Z8GdMdde-Z9GdNdde-Z:GdOdde-Z;GdPdde-ZdQdRZ?e9Z@e@jAe?dSe@jBdZCeCjDjEdZFeGeFjHeGdTeCjDeGe@e=jIe=j>dQdkreGdUneGdVe=j>dQe=jJdS)W)absolute_import)print_functionN)*)range) cmp_to_key)parse_config_setting get_configZgeneralZi18n_text_domainZi18n_locale_dirT)domain localedirZfallbackSignatureMatchSEFilterSEFaultSignatureSEFaultSignatureInfoSEFaultSignatureSetSEFaultSignatureUser SEEnvironmentSEDatabasePropertiesSEFaultUserInfoSEFaultUserSetSEPluginSEEmailRecipientSEEmailRecipientSet FILTER_NEVER FILTER_ALWAYSFILTER_AFTER_FIRST filter_text__main__)r r )ngettext)r)TemplatecCs||k||kS)N)xyrr/usr/lib/python3.6/signature.pyQsr#z Never Ignorez Ignore AlwayszIgnore After First Alertneveralways after_first)r&r'r(c@seZdZddZdS)r cCs||_||_dS)N)siginfoscore)selfr)r*rrr"__init__oszSignatureMatch.__init__N)__name__ __module__ __qualname__r,rrrr"r msc seZdZddddddiddiddiddiddiddideddedddiddiddid Zfd d Zd d Zd dZddZZ S)r attributecCsdS)Nz1.0rrrrr"r#vszSEEnvironment.)XMLFormdefaultr1element)r1import_typecast) versionplatformkernel policy_type policy_rpmlocal_policy_rpmenforceselinux_enabledselinux_mls_enabled policyvershostnameunamecstt|j|jdS)N)superrr,update)r+) __class__rr"r,szSEEnvironment.__init__cCsddl}ddl}t\|_|_|jd|_td|j|_|j|_t |j |_ |j }|dkrjd|_ nd|_ t|j|_t|j|_|j|_dj|j|_dS)Nrz/etc/selinux/%s PermissiveZ Enforcing )r6selinuxZget_os_environmentr7Zselinux_getpolicytyper8Zget_package_nvr_by_file_pathr9r:strZsecurity_policyversr>Zsecurity_getenforcer;boolZis_selinux_enabledr<Zis_selinux_mls_enabledr=noder?joinr@)r+r6rGr;rrr"rBs zSEEnvironment.updatecCs |j| S)N)__eq__)r+otherrrr"__ne__szSEEnvironment.__ne__cCs4x.t|jjD]}t||t||krdSqWdS)NFT)list _xml_infokeysgetattr)r+rMnamerrr"rLszSEEnvironment.__eq__) r-r.r/booleanrPr,rBrNrL __classcell__rr)rCr"rts    csBeZdZdeddddeddddZeffdd ZZS) r r3cCstS)N)rrrrr"r#szSEFilter.)r1r4r2cCsdS)Nrrrrrr"r#s) filter_typecountcstt|j||_dS)N)rAr r,rV)r+rV)rCrr"r,szSEFilter.__init__)r-r.r/intrPrr,rUrr)rCr"r scsdeZdZddideddddeddddedddd Zfd d Zd d ZdddZZ S)rr1r0cCsdS)NFrrrrr"r#szSEFaultSignatureUser.)r1r4r2cCsdS)NFrrrrr"r#sr3cCstS)N)r rrrr"r#s)usernameZ seen_flagZ delete_flagfiltercstt|j||_dS)N)rArr,rY)r+rY)rCrr"r,szSEFaultSignatureUser.__init__cCs:||jkrttd||dkr*ttdt|||dS)Nz!item (%s) is not a defined memberrYz changing the username is illegal)Z_names ProgramErrorZERR_NOT_MEMBERZERR_ILLEGAL_USER_CHANGEsetattr)r+itemdatarrr" update_items   z SEFaultSignatureUser.update_itemNcCsXtdtj|d|f|tks0|tks0|tkrHtdt|d|_dStd|dS)Nz%update_filter: filter_type=%s data=%sunknownzupdate_filter: !!!)rVTzBad filter_type (%s)) log_debugmap_filter_value_to_namegetrrrr rZ ValueError)r+rVr^rrr" update_filters z"SEFaultSignatureUser.update_filter)N) r-r.r/rTr rPr,r_rerUrr)rCr"rs  directorydirZ semaphoreZsemz shared memoryZshmz message queueZmsgqmessagemsgfileZsocketprocessprocess2Z filesystemrJ capability capability2cCs|ttjkrt|S|S)N)rO class_dictrQ)tclassrrr"translate_classsrqcs eZdZdZfddZZS)AttributeValueDictionaryZ unstructuredcstt|jdS)N)rArrr,)r+)rCrr"r,sz!AttributeValueDictionary.__init__)r-r.r/rPr,rUrr)rCr"rrsrrc sZeZdZddddddidddded ded ddided d Zfd d ZZS) r r0cCsdS)Nz4.0rrrrr"r#szSEFaultSignature.)r1r2r1r3Z operation)r1rO)r1r4)r5hostaccessscontexttcontextrpportc s8tt|jx$t|jD]\}}t|||qWdS)N)rAr r,rOitemsr\)r+kwdskv)rCrr"r,szSEFaultSignature.__init__)r-r.r/ AvcContextrXrPr,rUrr)rCr"r s cs8eZdZddiddddZfddZdd ZZS) rr1r3arg)r1rO) analysis_idargscstt|j||_||_dS)N)rArr,r~r)r+r~r)rCrr"r, szSEPlugin.__init__cCst|j|jfS)N)rHr~r)r+rrr"__str__szSEPlugin.__str__)r-r.r/rPr,rrUrr)rCr"rs cseZdZddeddedddiddiddidddddddeddedddideddedddiddiddidedde dde ddedd d ddidd e dddiddiddid Z d dddddddddg Z fddZ ddZddZddZdd Zd!d"Zd#d$ZdEd&d'ZdFd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4d5Zd6d7ZdGd9d:Zd;d<Zd=d>ZdHd?d@Z dAdBZ!dIdCdDZ"Z#S)Jrr3Zplugin)r1rOr4)r1r4r1Zrpm)r1rOcCsdS)Nrrrrrr"r#)szSEFaultSignatureInfo.)r1r4r2user) plugin_list audit_eventsourcespathtpath src_rpm_list tgt_rpm_listrurvrprwsigZif_textZ then_textZdo_text environmentfirst_seen_datelast_seen_date report_countlocal_iduserslevelZfixableZ button_textrrrrrurvrprwrrc s|tt|jx$t|jD]\}}t|||qWd|_g|_d}tj dkrTd}yt |j |d|j _ Wn YnXdS)NrDTrF)use_dbus)rArr,rOrxr\rrosgetuidZget_rpm_nvr_by_scontextrurr:)r+ryrzr{r)rCrr"r,7s zSEFaultSignatureInfo.__init__cCsZ|j|jkr"|j|_|jd7_x |jD]}t||t||q*W|jdkrV|j|_dS)NrD)rr merge_includer\rRr)r+r)rSrrr" update_mergeHs   z!SEFaultSignatureInfo.update_mergecCs|jjS)N)rr9)r+rrr"get_policy_rpmTsz#SEFaultSignatureInfo.get_policy_rpmcCs(d|j|jj|jj|jdj|jjfS)Nz%s,%s,%s,%s,%s,)rrutypervrprKrrt)r+rrr" get_hash_strWsz!SEFaultSignatureInfo.get_hash_strcCstj|jjd}|jS)Nzutf-8)hashlibZsha256rencodeZ hexdigest)r+hashrrr"get_hashZszSEFaultSignatureInfo.get_hashcCsBx|jD]}|j|kr|SqWtd|t|}|jj||S)Nznew SEFaultSignatureUser for %s)rrYrarappend)r+rYrrrr" get_user_data^s    z"SEFaultSignatureInfo.get_user_datacCs,td|d}|j|}|dk r(|j}|S)Nzfind_filter_by_username %s)rarrZ)r+rYrZ user_datarrr"find_filter_by_usernamegs   z,SEFaultSignatureInfo.find_filter_by_usernameNcCs|j|}|j||dS)N)rre)r+rYrVr^rrrr"update_user_filterps z'SEFaultSignatureInfo.update_user_filtercCsTd}|j|}td||f|dk rP|dk r4||_|j|}td|||f|S)Ndisplayz5evaluate_filter_for_user: found %s user's filter = %sz4evaluate_filter_for_user: found filter for %s: %s %s)rrarVevaluate_filter)r+rYrVactionfrrr"evaluate_filter_for_userts  z-SEFaultSignatureInfo.evaluate_filter_for_usercCsb|j}d}|tkrd}n8|tkr6|jdkr0d}qPd}n|tkrDd}n td||jd7_|S)Nrrignorezunknown filter_type (%s)rD)rVrrrWrrd)r+rZrVrrrr"rs  z$SEFaultSignatureInfo.evaluate_filtercCs2t|tr&t|dkr dj|SdSntdSdS)NrrF) isinstancerOlenrK default_text)r+Zrpm_listrrr"format_rpm_lists    z$SEFaultSignatureInfo.format_rpm_listcCsd|j|jfS)Nz %s [ %s ])rrp)r+rrr"format_target_objectsz)SEFaultSignatureInfo.format_target_objectcCsTd}|jjd}|dkr<|jddkrzSEFaultSignatureInfo.summaryFc st_g}d}|r>xjD]}||j7}|j|d fqWn\xZjD]P}xJjD]@}|j|jkrR||j7}|jt|j|j|t|jfPqRWqFW|j t j dddddg}d j fd d |D}x.|D]&}||krx|D]\}} d |_ qWPqW||fS)Nr allow_ypbind1)rZmozilla_read_contentZ"mozilla_plugin_can_network_connectZmozilla_plugin_use_bluejeansZ$unconfined_mozilla_plugin_transitionrFcs g|]\}}|jjj|qSr) get_do_textrrecords).0pa)r+rr" sz4SEFaultSignatureInfo.get_plugins..F)rr)Z load_pluginspluginsrrrr~Z init_argstuplersortrrrKZ report_bug) r+allrtotal_priorityrZsolutionZnoreport_booleansZdo_textsbrr)r+r" get_pluginss0         z SEFaultSignatureInfo.get_pluginscCst|j|jS)N)rZsafe_substituter)r+txtrrr" substituteszSEFaultSignatureInfo.substitutecsfdd|DS)Ncsg|]}j|qSr)r)rr)r+rr"r sz9SEFaultSignatureInfo.substitute_array..r)r+rr)r+r"substitute_arraysz%SEFaultSignatureInfo.substitute_arrayc s|j}td}|ttd|jj7}|ttd|jj7}|ttd|j7}|ttdt|j7}|ttdt|j 7}|ttdt|j 7}|r|ttdd 7}n|ttdt|j j 7}|ttd t|j |j7}|ttd t|j |j7}|ttd t|j7}|ttd t|j7}|ttdt|j7}|ttdt|j7}|ttdt|j7}|r|ttdd 7}n|ttdt|j7}|r|jj}d |d<|ttdtdj|7}n|ttdt|j7}|ttdt|j7}d}|ttd|jj|7}|ttd|jj|7}|ttdt|j7}|dtd7}d}xj|jjD]^jdkr|dj d7}n6|djj!f7}|djfdd j"Dd7}qW|d!|j#7}y~d"}t$j%j&|rvd#}t'|gt(t(d$} || j)|d%7}t$j%j*d&rn|d'7}t'|d(gt(t(d$} || j)|d%7}||7}Wn YnX||d7}|S))NzAdditional Information: zSource ContextzTarget ContextzTarget ObjectsZSourcez Source PathZPortZHostz (removed)zSource RPM PackageszTarget RPM PackageszSELinux Policy RPMzLocal Policy RPMzSelinux Enabledz Policy TypezEnforcing Modez Host NamerDZPlatformrFz Alert Countz%Y-%m-%d %H:%M:%S %Zz First Seenz Last SeenzLocal ID zRaw Audit MessagesrZAVCz type=%s msg=%s: csg|]}d|j|fqS)z%s=%s)Zfields)rrz) audit_recordrr"r8sz7SEFaultSignatureInfo.format_details..z Hash: z/usr/bin/audit2allowz audit2allow)stdinstdoutrz /var/lib/sepolgen/interface_infoz audit2allow -Rz-R)+rrZformat_2_column_name_valueruformatrvrrrrrwrrsrrrr9r:r<r8r;r?r@splitrKrrrrrr record_typeZto_textZevent_idZ fields_ordrrrZexistPopenPIPEZ communicateexists) r+replaceenvtextr@Z date_formatZavcbufZ audit2allowZnewbufrr)rr"format_details sl  (  z#SEFaultSignatureInfo.format_detailsc Os2t}t}zddadda|||S|a|aXdS)zdefine.*untranslated\(.*\ncSs|dkr |S|S)NrDr)r r!zrrr"r#Xsz3SEFaultSignatureInfo.untranslated..cSs|S)Nr)r rrr"r#YsN)rr)r+funcrkwargsZsaved_translateP_Zsaved_translate_rrr" untranslatedNs z!SEFaultSignatureInfo.untranslatedc Cs>|j|j}|j|\}}x |D]\}}td|jt|jt|ddf}||7}x"tt|dD]} |td7}qpW|td7}|j |j |j j |} |td| 7}|j |j |j j |} |td| dj| d d7}|j |j|j j |} |td | dj| d d7}q&W|td 7}|S) Nz0 ***** Plugin %s (%.4s confidence) suggests dg?Prrz Then rrDz Do z )rrrrr~floatrrrrZ get_if_textrrZ get_then_textlowerr) r+rrrrrrrtitleirrrr" format_text_s"( $( z SEFaultSignatureInfo.format_text)N)N)F)F)FF)$r-r.r/rZ AuditEventr|rXr rZ TimeStamprrPrr,rrrrrrrrrrrrrrrrrrrrrrUrr)rCr"rsd          / " CcsReZdZddddddideddddd d d Zfd d ZddZZS)rr0cCsdS)Nz1.0rrrrr"r#zszSEFaultUserInfo.)r1r2r1r3cCsdS)NFrrrrr"r#|s)r1r4r2 email_address)r1rO)r5rYZ email_alertemail_address_listcstt|j||_dS)N)rArr,rY)r+rY)rCrr"r,szSEFaultUserInfo.__init__cCs||jkr|jj|dS)N)rr)r+rrrr"add_email_addresss z!SEFaultUserInfo.add_email_address)r-r.r/rTrPr,rrUrr)rCr"rxs   csHeZdZddddddeddZfd d Zd d Zd dZZS)rr0cCsdS)Nz1.0rrrrr"r#szSEFaultUserSet.)r1r2r3r)r1rOr4)r5 user_listcstt|jdS)N)rArr,)r+)rCrr"r,szSEFaultUserSet.__init__cCs"x|jD]}||jkr|SqWdS)N)rrY)r+rYrrrr"get_users  zSEFaultUserSet.get_usercCs*|j|dk rdSt|}|jj||S)N)rrrr)r+rYrrrr"add_users  zSEFaultUserSet.add_user) r-r.r/rrPr,rrrUrr)rCr"rs   cseZdZdddddeddddded d Zfd d Zd dZddZddZ ddZ ddZ ddZ de jfddZZS)rr0cCs dttfS)Nz%d.%d)ZDATABASE_MAJOR_VERSIONZDATABASE_MINOR_VERSIONrrrr"r#szSEFaultSignatureSet.)r1r2r3cCstS)N)rrrrr"r#s)r1r4r2r))r1rOr4)r5rsignature_listcstt|jdS)N)rArr,)r+)rCrr"r,szSEFaultSignatureSet.__init__ccsx|jD] }|VqWdS)N)r)r+r)rrr"siginfoss zSEFaultSignatureSet.siginfoscCs|jj||S)N)rr)r+r)rrr" add_siginfos zSEFaultSignatureSet.add_siginfocCs|jj|dS)N)rremove)r+r)rrr"remove_siginfosz"SEFaultSignatureSet.remove_siginfocCs g|_dS)N)r)r+rrr"clearszSEFaultSignatureSet.clearcCs ttjS)N)rHuuidZuuid4)r+rrr"generate_local_idsz%SEFaultSignatureSet.generate_local_idcCs.|dkr dSx|jD]}|j|kr|SqWdS)N)rr)r+rr)rrr"lookup_local_ids   z#SEFaultSignatureSet.lookup_local_idexactc Cst|j}d}|dkrd}n(t|tr:t|}d|}n td|g}x|jD]} d} | j} x>|D]6} t|| t| | kr|rd} q| |7} qf|rfd} PqfW|r| dkr|j t | | qR| |krR|j t | | qRW|j t ddd |S) NFrTg?zunknown criteria = %sgcSst|j|jS)N)rr*)rrrrr"r#sz6SEFaultSignatureSet.match_signatures..)r) rOrQrrrrdrrrRrr rr) r+ZpatZcriteriaZxml_infoZ match_targetsrZnum_match_targetsZscore_per_match_targetZmatchesr)r*rrSrrr"match_signaturess6       z$SEFaultSignatureSet.match_signatures)r-r.r/rrrPr,rrrrrrr rrUrr)rCr"rs   cs6eZdZddiddiddidZdfdd ZZS)rr1r3)rS friendly_namefilepathNcs<tt|j|dk r||_|dk r*||_|dk r8||_dS)N)rArr,rSrr)r+rSrr)rCrr"r,szSEDatabaseProperties.__init__)NNN)r-r.r/rPr,rUrr)rCr"rs cs@eZdZddideddddZd fdd Zd d ZZS) rr1r3cCstS)N)rrrrr"r#szSEEmailRecipient.)r1r4r2)addressrVNcs&tt|j||_|dk r"||_dS)N)rArr,rrV)r+rrV)rCrr"r,szSEEmailRecipient.__init__cCsd|jtj|jdfS)Nz%s:%sr`)rrbrcrV)r+rrr"rszSEEmailRecipient.__str__)N)r-r.r/rXrPr,rrUrr)rCr"rscsneZdZddddddeddZdfd d Zd d ZddZefddZ ddZ ddZ ddZ Z S)rr0cCsdS)Nrrrrrr"r# szSEEmailRecipientSet.)r1r2r3 recipient)r1rOr4)r5recipient_listNcs tt|j|dk r||_dS)N)rArr,r)r+r)rCrr"r,szSEEmailRecipientSet.__init__cCsdjdd|jDS)NrcSsg|] }t|qSr)rH)rr rrr"rsz/SEEmailRecipientSet.__str__..)rKr)r+rrr"rszSEEmailRecipientSet.__str__cCs*|j}x|jD]}||jkr|SqWdS)N)striprr)r+rrrrr" find_addresss   z SEEmailRecipientSet.find_addresscCsP|j}t|s$ttd|ddS|j|}|dk r:dS|jjt||dS)Nz address='%s')detail)rZvalid_email_addressr[ERR_INVALID_EMAIL_ADDRrrrr)r+rrVrrrr" add_addresss zSEEmailRecipientSet.add_addresscCs g|_dS)N)r)r+rrr"clear_recipient_list)sz(SEEmailRecipientSet.clear_recipient_listcCsddl}|jd}|jd}|jd}ddddddddd}y t|}Wn8tk r}zttd||jfd WYdd}~XnX|jx|jD]} |j d | } | j } | r|j | } | r| j d } | j d } d} | rNxl|j | D]^} | j d }| j d }|dkr:tj|jd} | dkrJtd|| fqtd|| fqWy|j| | Wqtk r}z"|jtkrt|jn|WYdd}~XqXqW|jdS)Nrz#.*z(\S+)(\s+(.+))?z(\w+)\s*=\s*(\S+)TF)ZenabledtruerZonZdisabledZfalsenoZoffz%s, %s)rrrDrVz(unknown email filter (%s) for address %sz(unknown email option (%s) for address %s)rcompileopenIOErrorr[ ERR_FILE_OPENstrerrorr  readlinesrrsearchgroupfinditermap_filter_name_to_valuercrrar errnorclose)r+rrZ comment_reZentry_reZ key_value_reZ map_booleanrelinematchrZoptionsrVZoptionrrrr"parse_recipient_file,sT    (          z(SEEmailRecipientSet.parse_recipient_filecCsyt|d}Wn8tk rF}zttd||jfdWYdd}~XnXx,|jD]"}t|j}|jd|j |fqPW|j dS)Nwz%s, %s)rz%-40s filter_type=%s ) rrr[rrrrbrVwriterr)r+rrrrrVrrr"write_recipient_fileds(  z(SEEmailRecipientSet.write_recipient_file)N)r-r.r/rrPr,rrrr r rr!rUrr)rCr"r s  8rDzaudit_listener_database.xmlsigszsiginfo.audit_event=%sz Memory OKzMemory leak %d bytes)KZ __future__rrZsixZsyslog subprocessZ six.movesr functoolsrgettextZsetroubleshoot.configrrZ translationZugettextrAttributeError__all__r-ZinstallrrZsetroubleshoot.errcodeZsetroubleshoot.utilZsetroubleshoot.xml_serializeZsetroubleshoot.html_utilZsetroubleshoot.uuidrZsetroubleshoot.audit_datartypesstringrrrrrrrrrbrobjectr Z XmlSerializerr rrorqrrr rrrrrrrrZlibxml2Z debugMemoryZxml_filer"Z read_xml_filerr)rrrecordprintrZ cleanupParserZ dumpMemoryrrrr"s             4 !             gHk