403Webshell
Server IP : 172.67.131.151  /  Your IP : 104.23.243.109
Web Server : Apache
System : Linux keen-cori.18-142-40-148.plesk.page 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 06:59:36 UTC 2025 x86_64
User : simottodesign.com_2tntp341vs7 ( 10011)
PHP Version : 8.3.31
Disable Function : opcache_get_status
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : ON  |  Pkexec : ON
Directory :  /lib/python3/dist-packages/fail2ban/tests/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /lib/python3/dist-packages/fail2ban/tests/__pycache__/clientreadertestcase.cpython-38.pyc
U

��!c��צ�@svdZdZdZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZmZm
Z
mZddlmZddlmZmZmZdd	lmZdd
lmZddlmZmZddlmZdd
lmZddl m Z ddl!m"Z"m#Z#ej$�%ej$�&e'�d�Z(iZ)ddl!m*Z*e	j+j,Z-ej$�%ej$�&e'�d�Z.iZ/Gdd�de	j0�Z1Gdd�de"�Z2Gdd�de"�Z3Gdd�de"�Z4Gdd�de"�Z5dS)z!Cyril Jaquier, Yaroslav Halchenkoz>Copyright (c) 2004 Cyril Jaquier, 2011-2013 Yaroslav Halchenko�GPL�N�)�ConfigReader�ConfigReaderUnshared�DefinitionInitConfigReader�NoSectionError)�configparserinc)�
JailReader�extractOptions�splitWithOptions)�FilterReader)�JailsReader)�ActionReader�
CommandAction)�Configurator)�MyTime)�version�)�LogCaptureTestCase�with_tmpdir�files)�
CONFIG_DIR�configcs|eZdZ�fdd�Z�fdd�Zddd�Zdd	�Zddd�Zd
d�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Z�ZS)�ConfigReaderTestcs.tt|���tjdd�|_t|jd�|_dS)zCall before every test case.zf2b-temp)�prefix��basedirN)�superr�setUp�tempfile�mkdtemp�dr�c��self��	__class__��E/usr/lib/python3/dist-packages/fail2ban/tests/clientreadertestcase.pyr7szConfigReaderTest.setUpcst�|j�tt|���dS)zCall after every test case.N)�shutil�rmtreer!rr�tearDownr#r%r'r(r+=szConfigReaderTest.tearDownNcCs�tjj|kr>tj�|�}tj�|j|�}tj�|�s>t�|�td|j|fd�}|dk	rh|�	d|�|dk	rz|�	|�|�
�dS)N�%s/%s�wz
[section]
option = %s
	)�os�path�sep�dirname�joinr!�exists�makedirs�open�write�close)r$�fname�value�contentr!Zd_�fr'r'r(�_writeBs
�
zConfigReaderTest._writecCs*t�d|j|f�|�|j�d��dS)Nr,r")r.�unlinkr!�
assertTruer"�read)r$r8r'r'r(�_removeSszConfigReaderTest._remover"cCs&|�|j�|��|j�ddg�dS)N�section)�int�optionrC)r>r"r?�
getOptions)r$r;r'r'r(�
_getoptionWszConfigReaderTest._getoptioncCs�|j�d�|j�ddd�|j�ddd�|j�ddd�|j�dd�}|�|dd	d
d��|j�dd�}|�|dd	dd��|j�dd
dd
d��}|�|dd	d
d��dS)N�
Definition�a�1�br"�test))rBrGr)�boolrIr)rBr"rrTr)rGrIr"))rBrG)rKrI)rBr")rBr)rKr)r"�add_section�setrD�assertSortedEqual)r$�optsr'r'r(�testConvert[s ��
�zConfigReaderTest.testConvertcCsztj�|jd�}|�dd�|�|�d�d�t�|d�t�|tj	�s\|�
|j�d��nddl
}t�d|�
���dS)Nzd.confrr!z0Skipping on %s -- access rights are not enforced)r.r/r2r!r<�assertEqualrE�chmod�access�R_OK�assertFalser"r?�platform�unittest�SkipTest)r$r;rVr'r'r(�testInaccessibleFilejsz%ConfigReaderTest.testInaccessibleFilecCsL|�|j�d��|�dd�|�|��d�|�dd�|�|��d�|�dd�|�|��d	�|�d
d�|�|��d	�|�dd
�|�|��d�|�dd�|�|��d�|�dd�|�|��d�|�d�|�d�|�|��d�|�d�|�|��d	�|�d�|�|��d�|�d
�|�|��d�dS)Nr"�c.confrHr�2rzc.d/98.confZ998i�zc.d/90.confZ990zc.d/99.confZ999i�zc.local�3�zc.d/1.local�4�i�)rUr"r?r<rQrEr@r#r'r'r(�testOptionalDotDDirws0




z$ConfigReaderTest.testOptionalDotDDircCs�|jdddd�|jdddd�|jdddd�|jddd	d�|jd
ddd�Gdd
�d
t�}|ddi�|_|j�|j�|�|j���|jjidd�|j��}|�	|�
d�d�|�	|�
d�d�|�	|�
d�d�dS)NrZzS
[INCLUDES]
before = ib.conf
after  = ia.conf
[Definition]
test = %(default/test)s
�r9r:zib.confz,
[DEFAULT]
test = A
[Definition]
option = 1
zib.localz,
[DEFAULT]
test = B
[Definition]
option = 2
zia.confz,
[DEFAULT]
test = C
[Definition]
oafter = 3
zia.localz,
[DEFAULT]
test = D
[Definition]
oafter = 4
c@s$eZdZddgddgddgd�ZdS)z?ConfigReaderTest.testLocalInIncludes.<locals>.TestDefConfReaderrBN�string)rC�oafterrJ)�__name__�
__module__�__qualname__Z_configOptsr'r'r'r(�TestDefConfReader�s�rgr"rCT)�allrrcr_rJ�D)r<rr"�
setBaseDirr!r>r?rD�getCombinedrQ�get)r$rg�or'r'r(�testLocalInIncludes�s
z$ConfigReaderTest.testLocalInIncludescCs�|�|j�d��|jdddd�|�|j�d��|�|j��ddg�|�|j�dd�d�|�|j�dd	�d
�|�|j�dd�d�|�|j�dd�d
�|�|j�dd�d�dS)N�izi.confzu
[DEFAULT]
b = a
zz = the%(__name__)s

[section]
y = 4%(b)s
e = 5${b}
z = %(__name__)s

[section2]
z = 3%(__name__)s
rarAZsection2�yZ4a�ez5${b}�z�zzZ
thesectionZ	3section2)rUr"r?r<r>rQ�sectionsrlr#r'r'r(�testInterpolations�s
z#ConfigReaderTest.testInterpolationscCsd|�|j�d��|jdddd�|�|j�d��|�|j�dd�d�|�|j�dd�d	�dS)
N�g�g.confz4
[DEFAULT]
# A comment
b = a
c = d ;in line comment
ra�DEFAULTrIrGr"r!)rUr"r?r<r>rQrlr#r'r'r(�testComments�s
zConfigReaderTest.testCommentscCs<|�|j�d��|jdddd�|�|j�d��|�|j�dd�d�|�|j�dd�d	�|�|j�d
d�d�|�|j�d
d�d�|�|j�d
d
�d�|�|j�d
d�d�|�|j�dd
�d�|�|j�dd�d�|�|j�dd
�d�|�|j�dd�d�|�t|jjdd�|�t|jjd
d�dS)Nrvrwz�
[DEFAULT]
a = def-a
b = def-b,a:`%(a)s`
c = def-c,b:"%(b)s"
d = def-d-b:"%(known/b)s"

[jail]
a = jail-a-%(test/a)s
b = jail-b-%(test/b)s
y = %(test/y)s

[test]
a = test-a-%(default/a)s
b = test-b-%(known/b)s
x = %(test/x)s
y = %(jail/y)s
rarJrGztest-a-def-arIztest-b-def-b,a:`test-a-def-a`�jailzjail-a-test-a-def-az+jail-b-test-b-def-b,a:`jail-a-test-a-def-a`r"z5def-c,b:"jail-b-test-b-def-b,a:`jail-a-test-a-def-a`"r!z'def-d-b:"def-b,a:`jail-a-test-a-def-a`"z'def-c,b:"test-b-def-b,a:`test-a-def-a`"z def-d-b:"def-b,a:`test-a-def-a`"rxzdef-c,b:"def-b,a:`def-a`"zdef-d-b:"def-b,a:`def-a`"�xrp)	rUr"r?r<r>rQrl�assertRaises�	Exceptionr#r'r'r(�testTargetedSectionOptions�sz+ConfigReaderTest.testTargetedSectionOptions)NN)r")rdrerfrr+r<r@rErPrYr`rnruryr~�
__classcell__r'r'r%r(r5s


0rcs�eZdZ�fdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zedd��Zdd �Z�ZS)!�JailReaderTestcstt|�j||�dS�N)rr��__init__�r$�args�kwargsr%r'r(r�	szJailReaderTest.__init__cCs�dD]�}|�td|f�ddg�|�td|f�ddg�|�td|f�ddg�|�td	|f�d
dg�|�td|f�ddg�|�td
|f�ddg�|�td|f�ddg�qdS)N)�
�	� za%sbrGrIz	a[x=y]%sbza[x=y]za[x=y][z=z]%sbza[x=y][z=z]za[x="y][z"]%sbza[x="y][z"]z
a[x="y z"]%sbz
a[x="y z"]z
a[x="y	z"]%sbz
a[x="y	z"]z
a[x="y
z"]%sbz
a[x="y
z"])rQr)r$r0r'r'r(�testSplitWithOptionssz#JailReaderTest.testSplitWithOptionscCs tdttd�}|�t|j�dS)NZXXXABSENTXXX�r�share_config)r	r�CONFIG_DIR_SHARE_CFGr|�
ValueErrorr?�r$rzr'r'r(�testIncorrectJailsz JailReaderTest.testIncorrectJailcCsPtdttd�}|�|���|�|���|�|���|�d�|�d�dS)N�emptyactionr�z"No filter set for jail emptyactionz'No actions were defined for emptyaction)r	�IMPERFECT_CONFIG�IMPERFECT_CONFIG_SHARE_CFGr>r?rD�	isEnabled�assertLoggedr�r'r'r(�testJailActionEmptys
z"JailReaderTest.testJailActionEmptycCsTtdttd�}|�|���|�|���|�|���|�dt�|�d�dS)NZmissingbitsjailr�zJFound no accessible config files for 'filter.d/catchallthebadies' under %szUnable to read the filter�	r	r�r�r>r?rUrDr�r�r�r'r'r(�testJailActionFilterMissing#sz*JailReaderTest.testJailActionFilterMissingcCsFtdttd�}|�|���|�|���|�|���|�d�dS)NZbrokenactiondefr�z$Invalid action definition 'joho[foo'r�r�r'r'r(�testJailActionBrokenDef+s�z&JailReaderTest.testJailActionBrokenDefcCsNtdttd�}|�|���|�|���|�|���|�|jdd�dS)N�
tz_correctr��logtimezone�UTC+0200)	r	r�r�r>r?rDr�rQ�optionsr�r'r'r(�testJailLogTimeZone3s�z"JailReaderTest.testJailLogTimeZonecCsFtdttd�}|�|���|�|���|�|���|�d�dS)NZbrokenfilterdefr�z$Invalid filter definition 'flt[test'r�r�r'r'r(�testJailFilterBrokenDef;s�z&JailReaderTest.testJailFilterBrokenDefcCsttjjdd�tdttd�}|�|���|�|���|�	|�
��|�|��d�|�
d�|�|��d�dS)NT�Zstock�sshdr�zssh-funky-blocker)rW�F2B�SkipIfCfgMissingr	rr�r>r?rDrUr�rQ�getName�setNamer�r'r'r(�testStockSSHJailCs
zJailReaderTest.testStockSSHJailcCs
tjjdd�tdttdd�}|�|���|�|���|�|�	��|�
�}|�ddddggdd	�|D��|�ddd
dggdd	�|D��|�ddd
dggdd	�|D��|�ddddg�}d}|D]:}|�
t|�dko�|d�d��|d7}||kr��qq�dS)NTr�zsshd-override-flt-opts�rr��force_enablerM�	prefregexz^TestcSs(g|] }t|�dkr|ddkr|�qS)rr���len��.0rmr'r'r(�
<listcomp>Xsz>JailReaderTest.testOverrideFilterOptInJail.<locals>.<listcomp>�addjournalmatchz
_COMM=testcSs(g|] }t|�dkr|ddkr|�qS)rr�r�r�r'r'r(r�[s�maxlinesrcSs(g|] }t|�dkr|ddkr|�qS)rr�r�r�r'r'r(r�^s�usedns�nor�regexr)rWr�r�r	r�r�r>r?rDr��convertrQ�indexrUr��endswith)r$rz�streamZ	usednsidxrormr'r'r(�testOverrideFilterOptInJailMs2���� z*JailReaderTest.testOverrideFilterOptInJailc	Cs�tjjdd�dD]r}dD]h}td|��ttdd�}|�|���|�|�	��|�
�}|�d|��dd	|ggd
d�|D��qqdS)NTr�)rr)ZJRNLZFILE�TESTZINITZ
checklogtype_r�rM�addfailregexz^%s failure from <HOST>$cSs(g|] }t|�dkr|ddkr|�qS)rr�r�r�r'r'r(r�tsz=JailReaderTest.testLogTypeOfBackendInJail.<locals>.<listcomp>)
rWr�r�r	�lowerr�r�r>r?rDr�rQr�)r$roZpreflinerzr�r'r'r(�testLogTypeOfBackendInJailgs��z)JailReaderTest.testLogTypeOfBackendInJailc
Csvd}dddif}t|�}|�||�|�diftd��|�dddd�ftd	��|�d
iftd
��|�ddd
iftd��|�dddiftd��|�ttd�|�ttd�|�ttd�|�ttd�|�ttd�d}dif}t|�}|�||�d}ddddddddd d!d"d"d#�f}t|�}|�||�t|�d
d$��}|d%td&d'�|d(��D��f}|�||�dS))Nzmail-whois[name=SSH]z
mail-whois�nameZSSHzmail.who_is�catZdog)rGrIzmail.who_is[a=cat,b=dog]zmail--ho_is�mailrG�,zmail[a=',']rIzmail[a=b, ]z	mail-how[z-mail[a="test with interim (wrong) "" quotes"]z-mail[a='test with interim (wrong) '' quotes']zmail[a='x, y, z', b=x, y, z]z	mail['s']zabc[]�abcz�option[opt01=abc,opt02="123",opt03="with=okay?",opt04="andwith,okay...",opt05="how about spaces",opt06="single'in'double",opt07='double"in"single',  opt08= leave some space, opt09=one for luck, opt10=, opt11=]rCZ123z
with=okay?zandwith,okay...zhow about spaceszsingle'in'doublezdouble"in"singlezleave some spacezone for luck�)Zopt01Zopt02Zopt03Zopt04Zopt05Zopt06Zopt07Zopt08Zopt09Zopt10Zopt11�][rcss"|]\}}||�dd�fVqdS)r�r�N)�replace)r��k�vr'r'r(�	<genexpr>�sz1JailReaderTest.testSplitOption.<locals>.<genexpr>r)r
rQr|r�r��dict�items)r$rCZexpected�resultZ	expected2r'r'r(�testSplitOptionvsN�
�zJailReaderTest.testSplitOptioncCs�tddttd�}|�|���|�|���|�|jdd�|�|jdd�|�dd	�|j	D�d
dddgd
dddddgddgddggggd
dddgd
dddddgddgddgddggggd
dddgd
dddddgddgddggggg�dS)Nz	multi-logT)r�rr�Zlogpathza.log
b.log
c.log�actionzeaction[actname='ban']
action[actname='log', logpath="a.log
b.log
c.log
d.log"]
action[actname='test']cSsg|]}|���qSr')r��r�rGr'r'r(r��sz6JailReaderTest.testMultiLineOption.<locals>.<listcomp>rM�	addactionZban�	multi-set�	actionbanz4echo "name: ban, ban: <ip>, logs: a.log
b.log
c.log"�actnamer��logz:echo "name: log, ban: <ip>, logs: a.log
b.log
c.log
d.log"za.log
b.log
c.log
d.logrJz5echo "name: test, ban: <ip>, logs: a.log
b.log
c.log")
r	r�r�r>r?rDrQr�rN�_JailReader__actionsr�r'r'r(�testMultiLineOption�s.����z"JailReaderTest.testMultiLineOptionc	stjjdd�tddtd�}t�|d�|j��}t	d�|d<|�
|���|��}g}|D]^�t
��dkrnq\�dd	kr��dd
kr�|���q\�ddkr\|��fdd
��dD��q\dt}|�t
|�d�|�|dd	dddd
|g�|�|dd	dddd
|g�dS)NTr��
blocklisttest)r�rrz))rdr�)�filterr�)�	failregexz
^test <HOST>$)Zsenderz[email protected])Zblocklist_de_apikeyztest-key)r�zX%(action_blocklist_de)s
mynetwatchman[port=1234,protocol=udp,agent="%(fail2ban_agent)s"]r_rrM�agentr�cs.g|]&}|ddkrdg�dd�|�qS)rr�rMrr_r'r���cmdr'r(r��sz3JailReaderTest.testVersionAgent.<locals>.<listcomp>zFail2Ban/%srr�Zblocklist_derZ
mynetwatchman)rWr�r�r	rrr?Z_cfgZget_sectionsr�r>rDr�r��append�extendrrQ)r$rzrtr��actZ	useragentr'r�r(�testVersionAgent�s&
	zJailReaderTest.testVersionAgentcCs�tj�|d�}t|d���tj�|d�}t�d|�|�t�tj�|d��|g�|�t�|�g�|�	d|�|�t�tj�|d��g�dS)N�f1r-�f2Znonexisting�*z4File %s is a dangling link, thus cannot be monitored)
r.r/r2r5r7�symlinkrQr	Z_globr�)r$r!r�r�r'r'r(�testGlob�szJailReaderTest.testGlobcCsttid�}|�|��g�|�|�d��|�t|jdi�|�t|jd�|�t|j	dd�|�t|j
di�dS)N�r�rJ�any)rrQrtrU�has_sectionr|rZ
merge_sectionr�rlrD)r$r"r'r'r(�testCommonFunction�s
z!JailReaderTest.testCommonFunction)rdrerfr�r�r�r�r�r�r�r�r�r�r�r�r�r�rr�r�rr'r'r%r(r�s"
5!
r�c@sTeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�ZdS)�FilterReaderTestc
Cs�ddddgdddddd	ggddd
dgdddd
dddgddddddgddddgg}tddi�}|�t�|��|�d�|�|��|�tddddittd�}|��|�d�d|dd<|�|��|�dS)NrM�
testcase01r�rr�r�z�^\s*(?:\S+ )?(?:kernel: \[\d+\.\d+\] )?(?:@vserver_\S+ )?(?:(?:\[\d+\])?:\s+[\[\(]?sshd(?:\(\S+\))?[\]\)]?:?|[\[\(]?sshd(?:\(\S+\))?[\]\)]?:?(?:\[\d+\])?:)?\s*(?:error: PAM: )?Authentication failure for .* from <HOST>\s*$z�^\s*(?:\S+ )?(?:kernel: \[\d+\.\d+\] )?(?:@vserver_\S+ )?(?:(?:\[\d+\])?:\s+[\[\(]?sshd(?:\(\S+\))?[\]\)]?:?|[\[\(]?sshd(?:\(\S+\))?[\]\)]?:?(?:\[\d+\])?:)?\s*(?:error: PAM: )?User not known to the underlying authentication module for .* from <HOST>\s*$a^\s*(?:\S+ )?(?:kernel: \[\d+\.\d+\] )?(?:@vserver_\S+ )?(?:(?:\[\d+\])?:\s+[\[\(]?sshd(?:\(\S+\))?[\]\)]?:?|[\[\(]?sshd(?:\(\S+\))?[\]\)]?:?(?:\[\d+\])?:)?\s*(?:error: PAM: )?User not known to the\nunderlying authentication.+$<SKIPLINES>^.+ module for .* from <HOST>\s*$Zaddignoreregexz"^.+ john from host 192.168.1.1\s*$r�z
_COMM=sshd�+z_SYSTEMD_UNIT=sshd.servicez_UID=0zFIELD= with spaces zAFIELD= with + char and spacesZdatepatternz%Y %m %d %H:%M:%S�5�r�r�r���)rrj�TEST_FILES_DIRr?rDrNr��TEST_FILES_DIR_SHARE_CFG)r$�output�filterReaderr'r'r(rPsF
����
�

�
zFilterReaderTest.testConvertcCsPtddddd�ttd�}|��|�d�|��}|�|dd�|�d�dS)Nr�z<test>�X)r�rJr�r�z6Wrong int value 'X' for 'maxlines'. Using default one:)rr�r�r?rDrkZassertNotEqualr��r$r�rOr'r'r(�testConvertOptions.s�
z#FilterReaderTest.testConvertOptionscCsJddddgg}tddittd�}|��|�d�|��}|�||�dS)NrM�jailnamer�z [email protected] fromip=<IP>�substitutionr��rr�r�r?rDr�rN�r$r�r�r"r'r'r(�!testFilterReaderSubstitionDefault7s�
z2FilterReaderTest.testFilterReaderSubstitionDefaultcCsBtddittd�}|��|�d�|��}|�d|dk�dS)NZ
testcase02r�r�r�r�)rr�r�r?rDrkr>r�r'r'r(�testFilterReaderSubstKnown@s�
z+FilterReaderTest.testFilterReaderSubstKnowncCsNddddgg}tddddittd�}|��|�d�|��}|�||�dS)	NrMr�r�z[email protected] fromip=<IP>r��honeypotz[email protected]�r�r�r'r'r(�testFilterReaderSubstitionSetKs�
z.FilterReaderTest.testFilterReaderSubstitionSetcCsVddddgg}td�\}}tdd|ttd�}|��|�d�|��}|�||�dS)NrMr�r�z?^to=test,[email protected],test2,[email protected] fromip=<IP>$zwsubstitution[failregex="^<known/failregex>$", honeypot="<sweet>,<known/honeypot>", sweet="test,<known/honeypot>,test2"]r�r��r
rr�r�r?rDr�rN�r$r��
filterName�	filterOptr�r"r'r'r(�testFilterReaderSubstitionKnownTs��
z0FilterReaderTest.testFilterReaderSubstitionKnowncCsVddddgg}td�\}}tdd|ttd�}|��|�d�|��}|�||�dS)NrMr�r�z)^\s*to=fail2ban@localhost fromip=<IP>\s*$zWsubstitution[failregex="^\s*<Definition/failregex>\s*$", honeypot="<default/honeypot>"]r�r�r�r�r'r'r(�!testFilterReaderSubstitionSection_s��
z2FilterReaderTest.testFilterReaderSubstitionSectioncCsvtddddittd�}|��|�d�|�ttj|�tddddd�ttd�}|��|�d�|�ttj|�dS)Nr�r�r�z
<honeypot>r�z<sweet>)r�Zsweet)rr�r�r?rDr|r�r�)r$r�r'r'r(�testFilterReaderSubstitionFailjs�
�
z/FilterReaderTest.testFilterReaderSubstitionFailc
Cs�tj�tj�td��}ttj�|d�di�}|�|��tj�|d�tj�|d�g�z2|�d�|�	dd�|�	dd�|�	dd�Wn0t
k
r�}z|�d	|�W5d}~XYnXdS)
N�filter.dztestcase01.confr�ztestcase-common.confrFZ
__prefix_liner�Zignoreregexz)unexpected options after readexplicit: %s)r.r/�abspathr2r�rrQZreadexplicitrDrlr}�fail)r$Zpath_r�rqr'r'r(�testFilterReaderExplicitxs
�
z)FilterReaderTest.testFilterReaderExplicitN)rdrerfrPr�r�r�r�rrrrr'r'r'r(r�s,			r�c@s*eZdZd	dd�Zdd�Zedd��ZdS)
�JailsReaderTestCacheFNcCsBt||d�}|�|�|��|��|��|�|�d��dS)N�r�r�)rrj�	readEarly�getEarlyOptions�readAllr>rD)r$rr�r��configuratorr'r'r(�_readWholeConf�s
z#JailsReaderTestCache._readWholeConfcCs4d}|���d�D]}t�d||�r|d7}q|S)Nrr�z^\s*Reading files?: .*/r)ZgetLog�rsplit�re�match)r$Z	filematch�cnt�sr'r'r(�_getLoggedReadCount�s

z(JailsReaderTestCache._getLoggedReadCountcCstj��tj}tjt_z�t�|�t�	t
|�t�t
d|d�t�t
d|d�t�}|j
||d�|�d�}|�|dkd|�|j
|d	|d
�|�d�}|�|dkd|�|�d
�}|�|dkd|�|�d�}|�|dkd|�W5|t_XdS)Nz
/jail.confz/jail.localz/fail2ban.confz/fail2ban.localr�z
jail.localrz3Unexpected count by reading of jail files, cnt = %sTr	zjail\.localz:Unexpected count by second reading of jail files, cnt = %szfilter\.d/common\.confz5Unexpected count by reading of filter files, cnt = %szaction\.d/iptables\.confz5Unexpected count by reading of action files, cnt = %s)rWr�Z
SkipIfFastrZlogLevel�logging�DEBUGr)r*�copytreer�copyr�rrr>)r$rZsaved_llZ	share_cfgrr'r'r(�testTestJailConfCache�s(





z*JailsReaderTestCache.testTestJailConfCache)FN)rdrerfrrrrr'r'r'r(r�s

rcs|eZdZ�fdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
edd��Zdd�Z
dd�Zedd��Z�ZS)�JailsReaderTestcstt|�j||�dSr�)rrr�r�r%r'r(r��szJailsReaderTest.__init__cCs(tj�d�s$tdd�}|�t|j�dS)Nz/XXXr)r.r/r3r
r|r�r?)r$�readerr'r'r(�testProvidingBadBasedir�s
z'JailsReaderTest.testProvidingBadBasedircCsPtttd�}|�|���|�|jdd��|�t|j	�|j	dd�}d|_
|�|dddgdd	dgd
d	ddd
dggdd	gdddgddddgdddgddddgddddgd
dddddgddgddgggdddgddddgddddgddddgddgddgddgddgdddgddgddgdd gdd!gdd"gg�|�d#�|�
d$�|�d%�dS)&Nr�F)ZignoreWrongT�Zallow_no_files�addr��autoztest-known-interpr�r�z*failure test 1 (filter.d/test.conf) <HOST>z+failure test 2 (filter.d/test.local) <HOST>z"failure test 3 (jail.local) <HOST>�startZmissinglogfilesrMz<IP>Zbrokenactionr�r�r�zhit with big stick <ip>r�r�zparse_to_end_of_jail.confr�r�r�zconfig-errorz~Jail 'brokenactiondef' skipped, because of wrong configuration: Invalid action definition 'joho[foo': unexpected option syntaxz~Jail 'brokenfilterdef' skipped, because of wrong configuration: Invalid filter definition 'flt[test': unexpected option syntaxzoJail 'missingaction' skipped, because of wrong configuration: Unable to read action 'noactionfileforthisaction'zmJail 'missingbitsjail' skipped, because of wrong configuration: Unable to read the filter 'catchallthebadies'z!Errors in jail 'missingbitsjail'.zSkipping...z6No file(s) found for glob /weapons/of/mass/destruction)r
r�r�r>r?rUrDr|r�r��maxDiffrNr�ZassertNotLogged)r$�jails�
comm_commandsr'r'r(�testReadTestJailConf�sh�


�


������&

z$JailsReaderTest.testReadTestJailConfcCsDtjjdd�t�tj�tdd��D�]}tj�|��	dd�}t
|ditd�}|�|���z|�
i�Wn<tk
r�}z|�d	|t|�j|f�W5d}~XYnX|�d
�s$|jd|��d|d
�|j|j�dd���d|d
�|jtjtd�Bd�}|j|�d�dd|d
�|dkr$|jd|�dd�d|d
�q$dS)NTr��action.d�*.confz.confr�r�rzaction %r
%s: %sz-commonrFz.Action file %r is lacking [Definition] section��msgr�z#Action file %r is lacking actionban)�timeout�bantime)�ignorer�z5Action file %r does not contains jail-name 'f2b-TEST')�pfziptables-allports�iptables-multiportzf2b-TESTZactionstartzSAction file %r: interpolation of actionstart does not contains jail-name 'f2b-TEST')rWr�r��globr.r/r2r�basenamer�rr>r?rDr}r�typerdr��assertInrt�_optsrl�striprkrZ_escapedTagsrMrQ)r$ZactionConfigZ
actionName�actionReaderrqrOr'r'r(�testReadStockActionConf�s4,
�����z'JailsReaderTest.testReadStockActionConfc	Cs�tjjdd�tttd�}|�|���|�|���|�	�}|�
|g�t�}|��D�]L}|dkrhqX|�
|d�}t|�\}}|�|�|�t|��t|||ttd�}|�|��d|�|�i�|�|j�
dd	����|�
|d
�}|�t|����t|�D]�}	t|	�\}
}|�t|
��|�t|t��|
dk�rJ|�d|�t|
|ittd�}|�|���|�i�|�	�}
|�t|
��|�|j�
d
d	�����qqXdS)NTr�r�ZINCLUDESr�r�zFailed to read filter:r�r�r�r-�portr�)rWr�r�r
rr�r>r?rDr�rQrMrtrlr
rr�rr2r3r�
isinstancer�r1r)r$r"r#Z
allFiltersrzr�rr��actionsr�ZactNameZactOptr4Zcmdsr'r'r(�testReadStockJailConfsL
�

�
z%JailsReaderTest.testReadStockJailConfc	Cs�tjjdd�ttdtd�}|�|���|�|���t	dd�t
�
tj�
ddd��D��}t	d	d�|jD��}d|_|�|�|�d
|�|��|�|�|�d|�|��dS)NTr��rr�r�css>|]6}|�d�s|�d�stj�tj�|�d�dVqdS)zcommon.confz-aggressive.confrrN)r�r.r/�splitext�splitr�r'r'r(r�_s

�zBJailsReaderTest.testReadStockJailFilterComplete.<locals>.<genexpr>rrr&css |]}t|jd�dVqdS)r�rN)r
r�)r�rzr'r'r(r�csz=More filters exists than are referenced in stock jail.conf %rz2Stock jail.conf references non-existent filters %r)rWr�r�r
rr�r>r?rDrMr.r.r/r2r"r!�issubset�
difference)r$r"�filtersZfilters_jailr'r'r(�testReadStockJailFilterCompleteYs"����z/JailsReaderTest.testReadStockJailFilterCompletecCs(tjjdd�ttdtd�}|�|���|�|���|j	dd�}|�t
|��|D]@}t
|�dkrV|d|dgdd	gkrV|�t�|d�dk�qVd
D]B}|�
d|gdd
�|D��|�
d|ddg|�|�
d|g|�q�|�|ddd�|jD�]&}|j}|��}|jt
|�d|d�|D]�}|�	�}|��}	dt|�k�r(|�
d|j�d}
|d|	g}|D]�}t
|�dk�r�|ddk�r�|dd�|k�r�ddd�|dD�k}
n@t
|�dk�r�|ddk�r�|dd�|k�r�|ddk�r�d}
|
�rj�q�qj|j|
d|t|�fd��q(q�dS)NTr�r:rr]rrrMr*)r�Zrecidivercss2|]*}t|�dkr|ddkr|dd�VqdS)r]rrNrr��r�r�r'r'r(r��szDJailsReaderTest.testReadStockJailConfForceEnabled.<locals>.<genexpr>r��warnr r�zNo actions found for jail %sr'z<blocktype>Z	blocktypeFr�r_r�rcSsg|]}|d�qS)rr'rAr'r'r(r��szEJailsReaderTest.testReadStockJailConfForceEnabled.<locals>.<listcomp>r�zFound no %s command among %s)rWr�r�r
rr�r>r?rDr�r�rZstr2secondsr1rQZ_JailsReader__jailsr�r��strZ	_initOpts)r$r"r#�command�jr8Z	jail_namer��commandsZaction_nameZblocktype_presentZtarget_commandr'r'r(�!testReadStockJailConfForceEnabledls`$
�
�
��
�

��z1JailsReaderTest.testReadStockJailConfForceEnabledcsVtjjdd�t�}|�t�|�|��t�|��|�	�}|�|dd�|�|dd�|�
�|��|��|�
���fdd�}|�|d	�|d
�ko�|d�kn�|�|d�|d
�k�|�|d�|d
�k�|��dd	dgdd
dgdddgdddgdd
dgdddgdddgg�|j�d�|�|j��d�|�|��t�dS)NTr��socketz/var/run/fail2ban/fail2ban.sockZpidfilez/var/run/fail2ban/fail2ban.pidcsFt��D](\}}|ddkr|d|kr|Sqtd|�f��dS)NrrMrz/Did not find command 'set %s' among commands %s)�	enumerater�)rCrorq�rFr'r(�find_set�s
�z7JailsReaderTest.testStockConfigurator.<locals>.find_setZsyslogsocketZloglevelZ	logtargetZ
dbpurgeageZdbfileZdbmaxmatchesrMr�INFOz/var/log/fail2ban.logZ	allowipv6z"/var/lib/fail2ban/fail2ban.sqlite3�
Z1dz/tmp)rWr�r�rrjrrQZ
getBaseDirr
rrrDZconvertToProtocolZgetConfigStreamr>rNZ_Configurator__jails)r$r
rOrKr'rJr(�testStockConfigurator�s<
"��
z%JailsReaderTest.testStockConfiguratorcCs�t�tj�|d��t�tj�|d��ttj�|dd�d���ttj�|dd�d���ttj�|d�d�}|�d�|��t|id�}|�|�	��|�|�
��|jd	d
�}dd�|D�}|�t
td
d�|D���d�|�|ddd�dS)Nrr%ztestaction1.confr-ztestfilter1.conf�	jail.confz�
[testjail1]
enabled = true
action = testaction1[actname=test1]
         testaction1[actname=test2]
         testaction.py
         testaction.py[actname=test3]
filter = testfilter1
r�TrcSs.g|]&}|dd�dddgkr|dd��qS)Nr]rMZ	testjail1r�r')r�Zcommr'r'r(r��s�z:JailsReaderTest.testMultipleSameAction.<locals>.<listcomp>css|]}|dVqdS)rNr')r�r�r'r'r(r��sz9JailsReaderTest.testMultipleSameAction.<locals>.<genexpr>r_r�z{})r.�mkdirr/r2r5r7r6r
r>r?rDr�rQr�rM)r$r�jailfdr"r#Zadd_actionsr'r'r(�testMultipleSameAction�s
	z&JailsReaderTest.testMultipleSameActioncCs|jtd|jdd�dS)Nz'Have not found any log file for .* jailZpolling��backend)�assertRaisesRegexr��_testLogPathr#r'r'r(�testLogPathFileFilterBackend�s�z,JailsReaderTest.testLogPathFileFilterBackendc
CsZzddlm}Wn,tk
r<}zt�d��W5d}~XYnX|jdd�|jdd�dS)Nr)�
FilterSystemdz&systemd python interface not availableZsystemdrSzsystemd[journalflags=2])Zserver.filtersystemdrXr}rWrXrV)r$rXrqr'r'r(�testLogPathSystemdBackendsz)JailsReaderTest.testLogPathSystemdBackendcCs`ttj�|d�d�}|�d||f�|��t|d�}|�|���|�|�	��|�
�dS)NrOr-z�
[testjail1]
enabled = true
backend = %s
logpath = %s/not/exist.log
          /this/path/should/not/exist.log
action = 
filter = 
failregex = test <HOST>
r)r5r.r/r2r6r7r
r>r?rDr�)r$rrTrQr"r'r'r(rV	s	�

zJailsReaderTest._testLogPath)rdrerfr�rr$r5r9r@rGrNrrRrWrYrVrr'r'r%r(r�s1??4
r)6�
__author__�
__copyright__�__license__r.rr.rr)rrWZclient.configreaderrrrr�clientrZclient.jailreaderr	r
rZclient.filterreaderrZclient.jailsreaderr
Zclient.actionreaderrrZclient.configuratorrZ
server.mytimerr�utilsrrr/r2r1�__file__r�r�rr�r�r�r�r�ZTestCaserr�r�rrr'r'r'r(�<module>sBSz:

Youez - 2016 - github.com/yon3zu
LinuXploit