TcpMatch¶
The TcpMatch class provides access to the tcp match extension.
It supports the following iptables(8) options:
--sport, --dport, --tcp-flags, --sync, --tcp-options
Example:
Type "help", "copyright", "credits" or "license" for more information.
>>> from linuxnet.iptables import TcpMatch
>>> m = TcpMatch()
>>> m.dest_port().equals(22).syn().bit_set()
<linuxnet.iptables.matches.tcpmatch.TcpMatch object at 0x7fa49c330dc0>
>>> m.to_iptables_args()
['-m', 'tcp', '--syn', '--dport', '22']
- class TcpMatch[source]¶
Match against the fields of the TCP header
- static get_match_name() str[source]¶
Returns the iptables(8) match extension name, in this case,
tcp
- get_criteria() Iterable[Criterion][source]¶
Returns the TCP match criteria: flags, source-port, dest-port, tcp-option
- syn() TcpFlagsCriterion[source]¶
Criterion for matching against a SYN packet
- tcp_flags() TcpFlagsCriterion[source]¶
Compare with TCP flags
- source_port() SourcePortCriterion[source]¶
Matching against the source port
- dest_port() DestPortCriterion[source]¶
Match against the destination port
- tcp_option() TcpOptionCriterion[source]¶
Match against a TCP option
TcpFlagsCriterion¶
- class TcpFlagsCriterion(match: Match, syn_only=False)[source]¶
A criterion for comparing against packets with an arbitrary set of TCP flags set, or for comparing against
SYNpackets. This is an either-or use, determined at the time of object instantiation.The value is the tuple (flags-checked, flags-set); both flags-checked and flags-set are comma-separated lists of TCP flag names as defined in
TcpFlag- get_value() Tuple[Set[TcpFlag], Set[TcpFlag]][source]¶
Returns the value that the criterion is comparing against
- is_syn_only() bool[source]¶
Returns
Trueif the criterion is only meant to check for the SYN flag (but note that it may not be set yet)
- bit_set() Match[source]¶
This method can be used if this criterion implements a SYN-only comparison to check if the packet flags include only the SYN bit.
- bit_not_set() Match[source]¶
This method can be used if this criterion implements a SYN-only comparison to check for the non-existence of the SYN bit
- equals(flags_checked: Optional[Set[TcpFlag]] = None, flags_set: Optional[List[TcpFlag]] = None) Match[source]¶
Perform flags comparison
- any() Match¶
Match any value.
This method is used when creating a
Criterionin order to search an existing chain for rules that try to match against certain packet properties (e.g. input interface) without being particular about the specific property value (e.g.eth0).
- compare(is_equal: bool, *args, **kwargs) Match¶
Alternative method used for comparisons. It invokes
equals()(ornot_equals()) withargsandkwargsifis_equalisTrue(orFalse).
- is_positive() bool¶
Returns the ‘polarity’ of the criterion:
Trueforequals()orFalsefornot_equals()Raises
IptablesErrorif the criterion is not set
- is_set() bool¶
Returns
Trueif the criterion has been set
- class TcpFlag(value)[source]¶
Names and values for the TCP flags.
- FIN = 1¶
FINbit
- SYN = 2¶
SYNbit
- RST = 4¶
RSTbit
- PSH = 8¶
PSHbit
- ACK = 16¶
ACKbit
- URG = 32¶
URGbit
SourcePortCriterion¶
- class SourcePortCriterion(match: Match)[source]¶
Compare with a source port or check for inclusion in port-range
The value is the tuple (port, last_port) where last_port may be
None- any() Match¶
Match any value.
This method is used when creating a
Criterionin order to search an existing chain for rules that try to match against certain packet properties (e.g. input interface) without being particular about the specific property value (e.g.eth0).
- compare(is_equal: bool, *args, **kwargs) Match¶
Alternative method used for comparisons. It invokes
equals()(ornot_equals()) withargsandkwargsifis_equalisTrue(orFalse).
- equals(first: int, last: Optional[int] = None) Match¶
Compare with a number (or inclusion in number-range if
lastis present)
- get_value() Tuple[int, Optional[int]]¶
Returns the value that the criterion is comparing against
- Return type:
a tuple of (int, int|None)
- is_positive() bool¶
Returns the ‘polarity’ of the criterion:
Trueforequals()orFalsefornot_equals()Raises
IptablesErrorif the criterion is not set
- is_set() bool¶
Returns
Trueif the criterion has been set
DestPortCriterion¶
- class DestPortCriterion(match: Match)[source]¶
Compare against a destination port or check for inclusion in port-range
The value is the tuple (port, last_port) where last_port may be
None- any() Match¶
Match any value.
This method is used when creating a
Criterionin order to search an existing chain for rules that try to match against certain packet properties (e.g. input interface) without being particular about the specific property value (e.g.eth0).
- compare(is_equal: bool, *args, **kwargs) Match¶
Alternative method used for comparisons. It invokes
equals()(ornot_equals()) withargsandkwargsifis_equalisTrue(orFalse).
- equals(first: int, last: Optional[int] = None) Match¶
Compare with a number (or inclusion in number-range if
lastis present)
- get_value() Tuple[int, Optional[int]]¶
Returns the value that the criterion is comparing against
- Return type:
a tuple of (int, int|None)
- is_positive() bool¶
Returns the ‘polarity’ of the criterion:
Trueforequals()orFalsefornot_equals()Raises
IptablesErrorif the criterion is not set
- is_set() bool¶
Returns
Trueif the criterion has been set
TcpOptionCriterion¶
- class TcpOptionCriterion(match: Match)[source]¶
Compare against a TCP option number.
The value is an integer.
- any() Match¶
Match any value.
This method is used when creating a
Criterionin order to search an existing chain for rules that try to match against certain packet properties (e.g. input interface) without being particular about the specific property value (e.g.eth0).
- compare(is_equal: bool, *args, **kwargs) Match¶
Alternative method used for comparisons. It invokes
equals()(ornot_equals()) withargsandkwargsifis_equalisTrue(orFalse).
- get_iptables_option() str¶
Returns the iptables(8) option
- get_value() Any¶
Returns the criterion value
- is_positive() bool¶
Returns the ‘polarity’ of the criterion:
Trueforequals()orFalsefornot_equals()Raises
IptablesErrorif the criterion is not set
- is_set() bool¶
Returns
Trueif the criterion has been set