https://t.me/RX1948
Server : Apache
System : Linux iad1-shared-b8-43 6.6.49-grsec-jammy+ #10 SMP Thu Sep 12 23:23:08 UTC 2024 x86_64
User : dh_edsupp ( 6597262)
PHP Version : 8.2.26
Disable Function : NONE
Directory :  /lib/python3/dist-packages/fail2ban/tests/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //lib/python3/dist-packages/fail2ban/tests/__pycache__/actiontestcase.cpython-310.pyc
o

;s*b�]�@s�dZdZdZddlZddlZddlZddlZddlmZm	Z	m
Z
ddlmZm
Z
ddlmZd	d
lmZd	dlmZmZmZGdd
�d
e�ZdS)z
Cyril Jaquierz Copyright (c) 2004 Cyril Jaquier�GPL�N�)�
CommandAction�
CallingMap�substituteRecursiveTags)�OrderedDict�Actions)�Utils�)�	DummyJail)�
pid_exists�with_tmpdir�LogCaptureTestCasec@s�eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
edd��Zdd�Z
edd��Zedd��Zedd��Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�Zd5S)6�CommandActionTestcs>t���tdd��_d�_�jj���fdd�}|�j_dS)zCall before every test case.N�TestFcsd�_��S)NT)�"_CommandActionTest__action_started���orgstart�selfr�?/usr/lib/python3/dist-packages/fail2ban/tests/actiontestcase.py�
_action_start1sz.CommandActionTest.setUp.<locals>._action_start)r�setUpr�_CommandActionTest__actionr�start)rrrrrr*s
zCommandActionTest.setUpcCs|jr|j��t�|�dS)zCall after every test case.N)rr�stopr�tearDown�rrrrr6s
zCommandActionTest.tearDownc
Cs�dddd�}|�tdd��|�tdd��|�tdd��|�td	d��|�td
d��trl|�ttd��dd
ddd��|�ttd��dddd
ddd��|�ttd��td��|�tdd��|�tdd��|�tddi�ddi�|�tddd��d dd��|�td!d"d#��d$d"d#��|�td%d"d#��d&d"d#��|�td'd(d)d*��d+d(d)d*��|�t|�dd,d-d��|�td.d/d0��d1d/d0��|�td.d/d2d3��d2d/d2d3��|�td4d5d2d3��d6d5d2d3��dS)7N�	192.0.2.0z
123 <HOST>z	890 <ABC>��HOST�ABC�xyzcSstddi�S)N�A�<A>�rrrrr�<lambda>D�z?CommandActionTest.testSubstituteRecursiveTags.<locals>.<lambda>cSstddd��S)N�<B>r$�r#�Br%rrrrr&F�cSstdddd��S)Nr(�<C>r$)r#r*�Cr%rrrrr&H�cS�tddddd��S)Nzto=<B> fromip=<IP>r(r,�)r#r-r*�Dr%rrrrr&K�cSr/)Nzto=<honeypot> fromip=<IP>z
<honeypot>z<sweet>r0)�	failregex�sweet�honeypot�ignoreregexr%rrrrr&Mr2))�Xzx=x<T>��T�1)�Zz<X> <T> <Y>��Yzy=y<T>zx=x1r:zy=y1zx=x1 1 y=y1)r7r9r=r;))r7zx=x<T> <Z> <<R1>> <<R2>>)�R1r;)�R2r=r8)r;z<T> <Y>r<zx=x1 1 y=y1 1 y=y1 y=y1r;r=z1 y=y1)r7r>r?r9r;r=)
)�actionstartzgipset create <ipmset> hash:ip timeout <bantime> family <ipsetfamily>
<iptables> -I <chain> <actiontype>)�ipmsetz
f2b-<name>��name�any��bantime�600��ipsetfamily�inet)�iptablesziptables <lockingopt>��
lockingoptz-w��chain�INPUT)�
actiontypez<multiport>)�	multiportzY-p <protocol> -m multiport --dports <port> -m set --match-set <ipmset> src -j <blocktype>��protocol�tcp��port�ssh��	blocktype�REJECT)
)r@z�ipset create f2b-any hash:ip timeout 600 family inet
iptables -w -I INPUT -p tcp -m multiport --dports ssh -m set --match-set f2b-any src -j REJECT)rAzf2b-anyrBrErH)rKziptables -wrLrN)rQ�I-p tcp -m multiport --dports ssh -m set --match-set f2b-any src -j REJECT)rRr\rSrVrYcS�ttd��S)N)�r#z<<B><C>>�r*r1�r-�E��DEz	cycle <A>�rrrrrrr&{r'cSr])N)rbr^r_r`rdrrrrr&�r'r#r,z<C> <D> <X>�fun)r#r7z<C> <D> funz<C> <B>�coolr)z<C> coolz
<matches> <B>z<matches> coolz/to=<honeypot> fromip=<IP> evilperson=<honeypot>�pokier0)r3r5r6z%to=pokie fromip=<IP> evilperson=pokiez
123 192.0.2.0z890 123 192.0.2.0z<<PREF>HOST>�IPV4)r#�PREFz
<IPV4HOST>z1.2.3.4)r#ri�IPV4HOSTzA <IP<PREF>HOST> B IP<PREF> C�V4zA 1.2.3.4 B IPV4 C)�assertRaises�
ValueErrorr�assertEqualr�r�aInforrr�testSubstituteRecursiveTags<sx����������� ��
���
�
�z-CommandActionTest.testSubstituteRecursiveTagscs�tddd�ddd���t��ddd��d<��t�fdd��t��d	�d
<��t�fdd�����j�d��d
����j�d��d���t��fdd�����j�d��d	�dS)NrcS�dS)Nz<A><A>rrrrrr&��zHCommandActionTest.testSubstRec_DontTouchUnusedCallable.<locals>.<lambda>r0)r#r*r-r1cSsdt|d�S)N�r#��int)r�irrrr&�r.r-cs�dS)Nr-rr��cmrrr&���test=<C>r1cst��S�Nr%rrxrrr&�rzztest=<A>ztest=0ztest=<A>--<B>--<A>ztest=0--<A><A>--0cs�j�d��S)Nr{)r�
replaceTagr�ryrrrr&�r+z<D>)r)rrrl�ZeroDivisionErrorrnrr}rrr~r�$testSubstRec_DontTouchUnusedCallable�s �z6CommandActionTest.testSubstRec_DontTouchUnusedCallablecCs�dddd�}|�|j�d|�d�|�|j�d|�d�|�|j�d	|�d
�|�|j�ddd
i�d�|�|j�ddd
i�d�|�|j�dddi�d�d|d<|�|j�d	|�d�|�|j�dtdd�d��d�dS)Nr�123�890rzText<br>textz	Text
textzText <HOST> textzText 192.0.2.0 textzText <xyz> text <ABC> ABCzText 890 text 123 ABCz	<matches>�matchesz$some >char< should \< be[ escap}ed&
z,some \>char\< should \\\< be\[ escap\}ed\&\nz<ipmatches>�	ipmatchesz<ipjailmatches>�
ipjailmatchesz%some >char< should \< be[ escap}ed&
z.some \>char\< should \\\< be\[ escap\}ed\&\r\nz<xyz>r!zText 890 text 890 ABCz09 <matches> 11cS�td�S�N�
��strrrrrr&�rzz2CommandActionTest.testReplaceTag.<locals>.<lambda>�r�z09 10 11�rnrr}rrorrr�testReplaceTag�sZ�������������z CommandActionTest.testReplaceTagcCs$|�|j�dtdd�d��d�dS)N�abccSr��N�arurrrrr&�rzz4CommandActionTest.testReplaceNoTag.<locals>.<lambda>r�r�rrrr�testReplaceNoTag�s��z"CommandActionTest.testReplaceNoTagcs�t�jdd�t�jdd�t�jdd�t�jdd�t�jd	d
�t�jdd���td
�fdd��t�jd���td�fdd��dS)Nr�z<a�bzc>zb?family=inet6zb>�ac�<a><b>�abz<ac>zx?family=inet6r0z/properties contain self referencing definitionsc��jjd�jjdd�S)Nr��family=inet4��conditional�rr}�_propertiesrrrrr&���z?CommandActionTest.testReplaceTagSelfRecursion.<locals>.<lambda>z.possible self referencing definitions in querycr�)NzZ<x<x<x<x<x<x<x<x<x<x<x<x<x<x<x<x<x<x<x<x<x<x<x<x<x<x<x<x<x<x>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>�family=inet6r�r�rrrrr&�r�)�setattrr�assertRaisesRegexrm�delattrrrrr�testReplaceTagSelfRecursion�s
�
�z-CommandActionTest.testReplaceTagSelfRecursionc	Cspt|jdd�t|jdd�t|jdd�t|jdd�t|jd	d
�|jj}td�D]2}|�|jjd|jjd
|d�d�|�|jjd|jjd|d�d�|�|jjd|jjd|d�d�q+|�t|�dk�t|jdd�|�t|�d�td�D]2}|�|jjd|jjd
|d�d�|�|jjd|jjd|d�d�|�|jjd|jjd|d�d�qz|�t|�dk�dS)Nr�r�zabc?family=inet4�345zabc?family=inet6�567r"z	890-<abc>�	banactionzText <xyz> text <abc>rz<banaction> '<abc>'r0)r��cachezText 890-123 text 123 '123'r�zText 890-345 text 345 '345'r�zText 890-567 text 567 '567'�z	000-<abc>rzText 000-123 text 123 '123'zText 000-345 text 345 '345'zText 000-567 text 567 '567')	r�r�_substCache�rangernr}r��
assertTrue�len)rr�rwrrr�testReplaceTagConditionalCacheds`������������z1CommandActionTest.testReplaceTagConditionalCachedcCs�|d7}d||j_|jj|j_|�|jjd|�d||j_|�|jjd|�d|j_|�|jjd�d||j_|�|jjd|�d|j_|�|jjd�|��|�	d�|j�
ddi�|�d	�|�d
�|j��|�|jj�dS)N�/fail2ban.test�
touch '%s'�
rm -f '%s'zecho -n�[ -e '%s' ]�true�returned�ip�Invariant check failedzreturned successfully)
rr@�actionrepairrn�
actionstop�	actionban�actioncheck�actionunban�pruneLog�assertNotLogged�ban�assertLoggedr�r�tmprrr�testExecuteActionBan,s&



z&CommandActionTest.testExecuteActionBancCs�d|j_d|j_d|j_d|j_|j��|j�i�|��|j�i�|j	ddd�|j�i�|�d�|j�
�|j�i�|j��|j	ddd�|�d�dS)	Nr0zecho -n 'flush'zecho -n 'stop'�
Nothing to doT��wait�	[phase 2]r)
rr�r��actionflushr�rr�r��unbanr��flushrr�rrrr�testExecuteActionEmptyUnbanEs 



z-CommandActionTest.testExecuteActionEmptyUnbancCsL|d7}d|j_d||j_d||j_d||j_|j��|j��dS)Nr�rztouch '%s.<HOST>'zrm -f '%s.<HOST>'z[ -e '%s.192.0.2.0' ])rr r@r�r�r�consistencyCheckr�rrr�testExecuteActionStartCtagsYs
z-CommandActionTest.testExecuteActionStartCtagscCs�|d7}d|j_d||j_d||j_d||j_|�t|jjddi�|jddd	d
�|�	d�d||j_d||j_d
||j_d||j_|j�ddi�|�d�|�
d�dS)Nr�r0r��rm '%s'r�r�r��Unable to restore environmentT��allr�r�zprintf "%%%%b
" <ip> >> '%s')rr@r�r�r�rl�RuntimeErrorr�r�r�r�r�rrr�(testExecuteActionCheckRestoreEnvironmentcs

z:CommandActionTest.testExecuteActionCheckRestoreEnvironmentcCs�|d7}d|j_d|j_d||j_d||j_d||j_|j�ddi�|jddd	d
�|��d|j_|�	t
|jjddi�|jdddd	d
�dS)Nr�r0r�r�zecho 'repair ...'; touch '%s'r�zInvariant check failed. Tryingzecho 'repair ...'Tr�r�)rr@r�r�r�r�r�r�r�rlr�r�rrr�'testExecuteActionCheckRepairEnvironmentvs 
�z9CommandActionTest.testExecuteActionCheckRepairEnvironmentcCs.|�tt|jd�d|j_|�|jjd�dS)N�ROSTr)rl�AttributeError�getattrrr�rnrrrr�testExecuteActionChangeCtags�sz.CommandActionTest.testExecuteActionChangeCtagscCsPtdddd�d��}d|j_d|j_|j�|�|j�|�|jdd	d
d�dS)Nr��	192.0.2.1cSsdddd�S)N�o���tester)�fid�fport�userrrrrrr&�s�z?CommandActionTest.testExecuteActionUnbanAinfo.<locals>.<lambda>)r!r�zF-*zFecho '<ABC>, failure <F-ID> of <F-USER> -<F-TEST>- from <ip>:<F-PORT>'z$echo '<ABC>, user <F-USER> unbanned'z> -- stdout: '123, failure 111 of tester -- from 192.0.2.1:222'z' -- stdout: '123, user tester unbanned'Tr�)rrr�r�r�r�r�rorrr�testExecuteActionUnbanAinfo�s�	
�z-CommandActionTest.testExecuteActionUnbanAinfocCs^d|j_|j��|�|j�d��|�d�|��|�|j�d��|�d�|��dS)Nr0r�)rr@rr��
executeCmdr�r��_processCmdrrrr�testExecuteActionStartEmpty�s


z-CommandActionTest.testExecuteActionStartEmptycCs6|�|jjddddd�d��|jddd	d
dd�dS)
NzUprintf %b "foreign input:\n -- $f2bV_A --\n -- $f2bV_B --\n -- $(echo -n $f2bV_C) --"z I'm a hacker; && $(echo $f2bV_B)zI"m very bad hackerz#`Very | very
$(bad & worst hacker)`)�f2bV_A�f2bV_B�f2bV_C)�varsDictzforeign input:z' -- I'm a hacker; && $(echo $f2bV_B) --z -- I"m very bad hacker --z* -- `Very | very $(bad & worst hacker)` --Tr�)r�rr�r�rrrr�testExecuteWithVars�s
��
�z%CommandActionTest.testExecuteWithVarscCs�d|j_d|j_d|j_gd�}ddd�|�d�}|��|j�|�|jd	|d
|dg|�Rdd
i�|jd|d
dd
d�|��|j�	|�|j�
�|jd|d
dd
d�dS)Nz3echo "** ban <ip>, reason: <reason> ...\n<matches>"zecho "** unban <ip>"zecho "** stop monitoring")z
<actionunban>z" Hooray! #z`I'm cool script kiddyz7`I`m very cool > /here-is-the-path/to/bin/.x-attempt.shz<actionstop>r�zAhacking attempt ( he thought he knows how f2b internally works ;)�
)r��reasonr�z	** ban %sr�r�r�Tz** unban %sz** stop monitoringr�)rr�r�r��joinr�r�r�r�r�r)rr�rprrr� testExecuteReplaceEscapeWithVars�s4�����

�z2CommandActionTest.testExecuteReplaceEscapeWithVarscCst�d�|�d�dS)Nz+/bin/ls >/dev/null
bogusXXX now 2>/dev/nullz HINT on 127: "Command not found"�rr�r�rrrr�testExecuteIncorrectCmd�s
z)CommandActionTest.testExecuteIncorrectCmdcCsvt��}tjjs
dnd}|�tjd|d��|�t��||ko)t��||dk�|jdddd�|�dd	�dS)
Nr
g{�G�z�?zsleep 30��timeoutz -- timed out afterTr�� -- killed with SIGTERM� -- killed with SIGKILL)	�time�unittest�F2B�fast�assertFalserr�r�r�)r�stimer�rrr�testExecuteTimeout�s*�z$CommandActionTest.testExecuteTimeoutcsPt�dd��t�d��}|�d��Wd�n1swYd���fdd�}�fdd	��t���|�tjd
�|d�����|�t	�
�fdd
�d��|�dd�|�d�|�dd�t�
�d�t���|�tjd�|d�����|�t	�
�fdd
�d��|�dd�|�d�|�dd�t�
��t�
�d�dS)Nz.sh�	fail2ban_�wzo#!/bin/bash
		trap : HUP EXIT TERM

		echo "$$" > %s.pid
		echo "my pid $$ . sleeping lo-o-o-ong"
		sleep 30
		rcs��dupt���dkS�Nrt)r�r)�getnastypidr�rr�
getnasty_tout�s
�zLCommandActionTest.testExecuteTimeoutWithNastyChildren.<locals>.getnasty_toutc	szd}tj��d�r;t�d��#}zt|���}Wn	ty"Yn	wWd�|SWd�|S1s6wY|S)N�.pid)�os�path�isfile�openrv�readrm)�cpid�f)�tmpFilenamerrr��s ��
��
��zJCommandActionTest.testExecuteTimeoutWithNastyChildren.<locals>.getnastypidzbash %sr�c�
t��Sr|�rr�rrrr&�
zGCommandActionTest.testExecuteTimeoutWithNastyChildren.<locals>.<lambda>r�zmy pid z Resource temporarily unavailablez	timed outzkilled with SIGTERMzkilled with SIGKILLr�zout=`bash %s`; echo ALRIGHTcrr|rrrrrr&rz
 -- timed outr�r�)�tempfile�mktempr��writer�r�rr�r�r	�wait_forr�r��unlink)rrr�r)rr�r�rr�#testExecuteTimeoutWithNastyChildren�sD���
��
�
z5CommandActionTest.testExecuteTimeoutWithNastyChildrencCs,t�d�|�d�t�d�|�d�dS)Nzecho "How now brown cow"zstdout: 'How now brown cow'
z7echo "The rain in Spain stays mainly in the plain" 1>&2z6stderr: 'The rain in Spain stays mainly in the plain'
r�rrrr�testCaptureStdOutErr&s

��z&CommandActionTest.testCaptureStdOutErrcCs>tdd�dd�ddd�}|�d|d�|�td	d�|�dS)
NcSr�r�r�rrrrr&/rzz2CommandActionTest.testCallingMap.<locals>.<lambda>cSr�r�rurrrrr&/rz�string�)�callme�error�
dontcallme�numberz)%(callme)s okay %(dontcallme)s %(number)iz10 okay string 17cSsd|S)Nz	%(error)ir)�xrrrr&7rz)rrnrlrm)r�mymaprrr�testCallingMap.s��z CommandActionTest.testCallingMapcCsTtdd�dd�dd��}|��d|d<|d=|�t|�d	�|�d|�|�|d|d
fd�|��t|�}|�t|�d�|�d|�|�|d|d
|dfd
�d|d<|��}dd�|d<d|d<|d
=|d=|�d
|v�|�d|v�|�	d
|v�|�	d|v�|�|d|d
|d|dfd�|�|d|dfd�dS)NcSrrr�rrrrrr&;rsz8CommandActionTest.testCallingMapModify.<locals>.<lambda>cS�|ddS�Nr��rrrrrr&<r'�test�r�r��c�r�rrr�)rr�r�)rt�r�dddd�dcSr)Nr��rrrrrr&Qr'r
)rtrrr )r
�)
r�resetrnr��assertNotIn�repr�assertIn�copyr�r�)r�m�s�m2rrr�testCallingMapModify9s8�$z&CommandActionTest.testCallingMapModifycCs�tdd�dd�dd��}t|�}|�d|�|�d|�|�d|�|�d	�}|�d
|�|�d|�|�d|�dd�|d
<|�d	�}|�d
|�|�d|�|�d|�|�d|�dS)NcSrrr�rrrrrr&^rsz5CommandActionTest.testCallingMapRep.<locals>.<lambda>cSrrrrrrrr&_r'r0rz'a': z'b': z'c': ''Tz'a': 5z'b': 11cSr)N�xxxr"rrrrrr&lr'rz'c': )rr&r%r'�_asrepr)rr)r*rrr�testCallingMapRep\s&�

z#CommandActionTest.testCallingMapRepcCsRtt��}d|_d|_|��|jddd�d|_|jddd�d|_|��dS)Ng-C��6?TzActions: enter idle moder�r�FzActions: leave idle mode)rr�	sleeptime�idlerr��activer�)rr�rrr�testActionsIdleModess
z%CommandActionTest.testActionsIdleModeN)�__name__�
__module__�__qualname__rrrqr�r�r�r�r�r
r�r�r�r�r�r�r�r�r�r�r�r�r
rrr,r/r3rrrrr(s>e()

	


=#r)�
__author__�
__copyright__�__license__r�rr�r��
server.actionrrr�server.actionsrr�server.utilsr	�	dummyjailr�utilsrr
rrrrrr�<module>s

https://t.me/RX1948 - 2025