CasperSecurity

Current Path : /usr/lib/python3/dist-packages/ufw/__pycache__/
Upload File :
Current File : //usr/lib/python3/dist-packages/ufw/__pycache__/common.cpython-310.pyc

o

MH�dX�@sjdZddlZddlZddlZddlmZdZdZdZeZ	dZ
dZd	Zd
Z
Gdd�de�ZGd
d�d�ZdS)z!common.py: common classes for ufw�N)�debug�ufwz/lib/ufwz/usr/share/ufwz/etcz/usrz	/usr/sbinTc@s eZdZdZdd�Zdd�ZdS)�UFWErrorz$This class represents ufw exceptionscCs
||_dS�N)�value)�selfr�r�,/usr/lib/python3/dist-packages/ufw/common.py�__init__#�
zUFWError.__init__cCs
t|j�Sr)�reprr�rrrr	�__str__&rzUFWError.__str__N)�__name__�
__module__�__qualname__�__doc__r
rrrrr	r!src@s�eZdZdZ			d9dd�Zd	d
�Zdd�Zd
d�Zdd�Zdd�Z	d:dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�Zd4d5�Zd6d7�Zd8S);�UFWRulez$This class represents firewall rules�any�	0.0.0.0/0�inF�c

Cs�d|_d|_d|_d|_d|_d|_d|_d|_d|_d|_	d|_
d|_d|_d|_
d|_d|_d|_||_d|_z,|�|�|�|�|�|�|�|d�|�|�|�|�|�|�|�|	�WdStym�w)NFrr�src)�remove�updated�v6�dstr�dport�sport�protocol�multi�dapp�sapp�action�position�logtype�interface_in�
interface_out�	direction�forward�comment�
set_action�set_protocol�set_port�set_src�set_dst�
set_direction�set_commentr)
rr#rrrrrr(r)r*rrr	r
,s>





�zUFWRule.__init__cCs|��Sr)�format_ruler
rrr	rOszUFWRule.__str__cCs>d|}t|j�}|��|D]
}|d||j|f7}q|S)zPrint rule to stdoutz'%s'z, %s=%s)�list�__dict__�sort)r�res�keys�krrr	�_get_attribRs
zUFWRule._get_attribcCs�t|j|j�}|j|_|j|_|j|_|j|_|j|_|j|_|j	|_	|j
|_
|j|_|j|_|j
|_
|j|_|j|_|j|_|j|_|j|_|j|_|S)zReturn a duplicate of a rule)rr#rrrrrrrrr r!r"r$r%r&r'r(r)r*)r�rulerrr	�dup_rule[s&zUFWRule.dup_rulecCsrd}|jdkr|d|j7}|jdkr|d|j7}|jdkr$|d7}nD|d|j7}|jrh|d7}|jdkrO|jdkrO|d|j7}|d7}|d	|j7}n|jdkr\|d|j7}n|jdkrh|d	|j7}|jd
kry|jdkry|d|j7}|js�|jdkr�|d
|j7}|jd
kr�|jdkr�|d|j7}|js�|jdkr�|d|j7}d}|jdkr�d|j}|j	dkr�|d|7}n'|j	dkr�|d|7}|jdkr�|d7}n|j	dkr�|d|7}n|d|7}|j
dks�|jdk�r5d}t�
d�}|j
dk�r
|d|�d|j
�7}|j
dk�r|jdk�r|d7}|jdk�r+|d|�d|j�7}|d 7}|d|7}|��S)!zFormat rule for later parsingrz -i %sz -o %srz -p allz -p z
 -m multiportz
 --dports z
 --sports r�::/0z -d z	 --dport z -s z	 --sport �_�allowz -j ACCEPT%s�rejectz -j REJECT%s�tcpz --reject-with tcp-reset�limitz -j LIMIT%sz
 -j DROP%sz-m comment --comment '� �dapp_z%20�,�sapp_�')r&r'rr rrrrr%r#r!r"�re�compile�sub�strip)r�rule_str�lstrr*�	pat_spacerrr	r2rsf










�

zUFWRule.format_rulecCsj|���d�}|ddks|ddks|ddkr|d|_nd|_d}t|�dkr.|d}|�|�d	S)
zSets action of the ruler=rr>r?rA�denyr�N)�lower�splitr#�len�set_logtype)rr#�tmpr%rrr	r+�s$zUFWRule.set_actionrc		Cs�td�|}|dkrn�|dkr|jrn�|dkr|jrn�t�d|�s't�d|�r+t|��|�d�|�d�d	kr;t|��|�d�}t|�d
krId|_	d}|D]y}t�d
|�r�d|_	|�d�}|D]}t
|�d
ksmt
|�dkrqt|��q_t
|d�t
|d
�kr�t|��n4t�d|�r�t
|�d
ks�t
|�dkr�t|��nt�d|�r�zt�|�}Wnt
y�t|��wt|��|r�|dt|�7}qMt|�}qM|}|dkr�t|�|_dSt|�|_dS)z:Sets port and location (destination or source) of the rulez
Bad port '%s'rrrz^[,:]z[,:]$rD�:�rOTrz	^\d+:\d+$i��rz^\d+$z
^\w[\w\-]+N)r=r!r"rG�matchr�countrQrRr �int�socket�
getservbyname�	Exception�strrr)	r�port�loc�err_msg�portsrT�p�ran�qrrr	r-�sX

����
zUFWRule.set_portcCs0|tjjdgvr||_dStd�|}t|��)zSets protocol of the rulerzUnsupported protocol '%s'N)r�util�supported_protocolsrr=r)rrr`rrr	r,�s
zUFWRule.set_protocolcCs�|jr)|jr|jdks|jdkrd|_|jr%|jdks |jdkr'd|_dSdSdS|jr9|jdks6|jdkr9d|_|jrK|jdksF|jdkrMd|_dSdSdS)zAdjusts src and dst based on v6rrr<N)rrrr
rrr	�
_fix_anywhere�s
�
�zUFWRule._fix_anywherecCs||_|��dS)zXSets whether this is ipv6 rule, and adjusts src and dst
           accordingly.
        N)rrg)rrrrr	�set_v6szUFWRule.set_v6cC�@|��}|dkrtj�|d�std�}t|��||_|��dS)zSets source address of rulerzBad source addressN)rPrre�
valid_addressr=rrrg�r�addrrTr`rrr	r.�zUFWRule.set_srccCri)z Sets destination address of rulerzBad destination addressN)rPrrerjr=rrrgrkrrr	r/rmzUFWRule.set_dstcCs�|dkr|dkrtd�}t|��dt|�vrtd�}t|��dt|�vr,td�}t|��t|�dks8t|�d	kr@td
�}t|��tt|��dkrPtd�}t|��tt|��d
kr`td�}t|��t�dt|��sptd�}t|��|dkry||_dS||_dS)zSets an interface for ruler�outzBad interface type�!z+Bad interface name: reserved character: '!'rUz/Bad interface name: can't use interface aliases�.z..z)Bad interface name: can't use '.' or '..'rz+Bad interface name: interface name is empty�z+Bad interface name: interface name too longz^[a-zA-Z0-9_\-\.\+,=%@]+$zBad interface nameN)r=rr]rRrGrWr&r')r�if_type�namer`rrr	�
set_interface's0


zUFWRule.set_interfacecCs>t|�dkrt�dt|��std�|}t|��t|�|_dS)zSets the position of the rulez-1z^[0-9]+z,Insert position '%s' is not a valid positionN)r]rGrWr=rrYr$)r�numr`rrr	�set_positionWszUFWRule.set_positioncCsB|��dks|��dks|dkr|��|_dStd�|}t|��)zSets logtype of the rule�logzlog-allrzInvalid log type '%s'N)rPr%r=r)rr%r`rrr	rSas
zUFWRule.set_logtypecCs.|dks|dkr
||_dStd�|}t|��)zSets direction of the rulerrnzUnsupported direction '%s'N)r(r=r)rr(r`rrr	r0js
zUFWRule.set_directioncCstj�|j�S)zGet decoded comment of the rule)rre�
hex_decoder*r
rrr	�get_commentrszUFWRule.get_commentcCs
||_dS)zSets comment of the ruleN)r*)rr*rrr	r1vs
zUFWRule.set_commentcCs�d}|jr(ztj�|j|j�\|_}Wnty"td�}t|��w|r(||_|j	rNztj�|j	|j�\|_	}WntyHtd�}t|��w|rN||_|j
rc|j
�d�}tj�|�d�
|�|_
|jrz|j�d�}tj�|�d�
|�|_dSdS)z&Normalize src and dst to standard formFz"Could not normalize source addressz'Could not normalize destination addressrDN)rrre�normalize_addressrr\r=rrrrrQ�
human_sort�joinr)r�changedr`rarrr	�	normalizezs@
��
���zUFWRule.normalizecCs�|r|st��d||f}|j|jkrt|�dS|j|jkr%t|�dS|j|jkr1t|�dS|j|jkr=t|�dS|j|jkrIt|�dS|j|jkrUt|�dS|j|jkrat|�dS|j	|j	krmt|�dS|j
|j
kryt|�dS|j|jkr�t|�dS|j|jkr�t|�dS|j
|j
kr�t|�dS|j|jkr�|j|jkr�|j|jkr�td�}t|�dS|j|jkr�|j|jkr�|j|jkr�td�}t|�dStd�|j|j|j|j|j|jd�}t|�d	S)
z�Check if rules match
        Return codes:
          0  match
          1  no match
         -1  match all but action, log-type and/or comment
         -2  match all but comment
        zNo match '%s' '%s'rOzFound exact matchrz$Found exact match, excepting comment���zZFound non-action/non-logtype/comment match (%(xa)s/%(ya)s/'%(xc)s' %(xl)s/%(yl)s/'%(yc)s'))�xa�ya�xl�yl�xc�yc���)�
ValueErrorrrrrrrrr!r"r&r'r(r)r#r%r*r=)�x�y�dbg_msgrrr	rW�sr��z
UFWRule.matchcCs�dd�}|r|st��|�|�dkrdSd||j||jf}|jdkr-td|d�dS|j|jkr;t|d	�dS|j|jkrN|jd
krNtd|�dS|jd
krb||j|j�sbtd|�dS|jd
kr�|jd
krs|�	|j
�rsn�|j
|j
kr�d|j
vr�td|�dS|j
|j
kr�d|j
vr�|j|jkr�tj�
|j
|j
|j�s�td|d|j
|j
f�dSn�|jd
kr�|j|jkr�td|d|j|jf�dSztj�|j|j�}Wnty�td|d|j�YdSw|j
|k�r
d|j
v�r
td|d|j
|f�dS|j
|k�r7d|j
v�r7|j|jk�r7tj�
||j
|j��s7td|d||j
f�dS|j|jk�rNtd|d|j
|j
f�dStd||j||jf�dS)a�This will match if x is more specific than y. Eg, for protocol if x
           is tcp and y is all or for address if y is a network and x is a
           subset of y (where x is either an address or network). Returns:

            0  match
            1  no match
           -1  fuzzy match

           This is a fuzzy destination match, so source ports or addresses
           are not considered, and (currently) only incoming.
        cSs~d|vsd|vr||krdSdS|�d�D]'}||krdSd|vr<|�d�\}}t|�t|�kr<t|�t|�kr<dSqdS)z:Returns True if p is an exact match or within a multi rulerDrUTF)rQrY)�test_p�to_matchr^�low�highrrr	�_match_ports�s �z-UFWRule.fuzzy_dst_match.<locals>._match_portsrz(No fuzzy match '%s (v6=%s)' '%s (v6=%s)'rz(direction) z (not incoming)rOz (forward does not match)rz(protocol) z(dport) r�/z(dst) z ('%s' not in network '%s')z(interface) z (%s != %s)z %s does not existz(v6) z'(fuzzy match) '%s (v6=%s)' '%s (v6=%s)'r�)r�rWrr(rr)rrr&�_is_anywhererrre�
in_network�get_ip_from_if�IOError)r�r�r�r��if_iprrr	�fuzzy_dst_match�s��

"�

��

�
��
�&��zUFWRule.fuzzy_dst_matchcCs|dks|dkr
dSdS)zCheck if address is anywherer<rTFr)rrlrrr	r�NszUFWRule._is_anywherecCs�d}|jdks|jdkred|j|j|j|jf}|jdkr)d|j|j|j|jf}|jdkr:d|j|j|j|jf}|jdkrM|jdkrM|d|j7}|S|jdkrY|d|j7}|jdkre|d|j7}|S)a�Returns a tuple to identify an app rule. Tuple is:
             dapp dst sapp src direction_iface|direction
           or
             dport dst sapp src direction_iface|direction
           or
             dapp dst sport src direction_iface|direction

           where direction_iface is of form 'in_eth0', 'out_eth0' or
           'in_eth0 out_eth0' (ie, both interfaces used). If no interfaces are
           specified, then tuple ends with the direction instead.
        rz%s %s %s %sz %sz in_%sz out_%s)	r!r"rrrrr&r'r()r�tuplrrr	�
get_app_tupleTs&
�
�
�
zUFWRule.get_app_tuplecCs�|jdkr|jdks|jdkrtd�|j}t|��|jtjjvr0|dkr0td�|j}t|��|jtjjvrL|j	dksA|j
dkrNtd�|j}t|��dSdS)zVerify rulerrz3Improper rule syntax ('%s' specified with app rule)rz'Invalid IPv6 address with protocol '%s'zInvalid port with protocol '%s'N)rr"r!r=rrre�ipv4_only_protocols�portless_protocolsrr)r�rule_iptyper`rrr	�verifyvs(
����zUFWRule.verifyN)rrrrrFr)r)rrrrr
rr9r;r2r+r-r,rgrhr.r/rtrvrSr0ryr1r~rWr�r�r�r�rrrr	r*s:
�#	C

5


0
	#Cn"r)rrGrZ�ufw.utilrr�programName�	state_dir�	share_dir�	trans_dir�
config_dir�
prefix_dir�iptables_dir�	do_checksr\rrrrrr	�<module>s	
Hacker Blog, Shell İndir, Sql İnjection, XSS Attacks, LFI Attacks, Social Hacking, Exploit Bot, Proxy Tools, Web Shell, PHP Shell, Alfa Shell İndir, Hacking Training Set, DDoS Script, Denial Of Service, Botnet, RFI Attacks, Encryption
Telegram @BIBIL_0DAY