Changeset View
Changeset View
Standalone View
Standalone View
sbin/ping/tests/test_ping.py
Show First 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | if special == "warp": | ||||
# Build a package with a timestamp of INT_MAX | # Build a package with a timestamp of INT_MAX | ||||
# (time-warped package) | # (time-warped package) | ||||
payload_no_timestamp = sc.bytes_hex(load)[16:] | payload_no_timestamp = sc.bytes_hex(load)[16:] | ||||
load = (b"\xff" * 8) + sc.hex_bytes(payload_no_timestamp) | load = (b"\xff" * 8) + sc.hex_bytes(payload_no_timestamp) | ||||
if special == "wrong": | if special == "wrong": | ||||
# Build a package with a wrong last byte | # Build a package with a wrong last byte | ||||
payload_no_last_byte = sc.bytes_hex(load)[:-2] | payload_no_last_byte = sc.bytes_hex(load)[:-2] | ||||
load = (sc.hex_bytes(payload_no_last_byte)) + b"\x00" | load = (sc.hex_bytes(payload_no_last_byte)) + b"\x00" | ||||
if special == "not-mine": | |||||
# Modify the ICMP Identifier field | |||||
oicmp.id += 1 | |||||
if icmp.type in icmp_id_seq_types: | if icmp.type in icmp_id_seq_types: | ||||
pkt = ip / icmp / load | pkt = ip / icmp / load | ||||
else: | else: | ||||
ip.options = "" | ip.options = "" | ||||
pkt = ip / icmp / oip / oicmp / load | pkt = ip / icmp / oip / oicmp / load | ||||
return pkt | return pkt | ||||
▲ Show 20 Lines • Show All 76 Lines • ▼ Show 20 Lines | def pinger( | ||||
icmp_otime: sc.scapy.layers.inet.ICMPTimeStampField = 0, | icmp_otime: sc.scapy.layers.inet.ICMPTimeStampField = 0, | ||||
icmp_rtime: sc.scapy.layers.inet.ICMPTimeStampField = 0, | icmp_rtime: sc.scapy.layers.inet.ICMPTimeStampField = 0, | ||||
icmp_ttime: sc.scapy.layers.inet.ICMPTimeStampField = 0, | icmp_ttime: sc.scapy.layers.inet.ICMPTimeStampField = 0, | ||||
icmp_mask: sc.scapy.fields.IPField = "0.0.0.0", | icmp_mask: sc.scapy.fields.IPField = "0.0.0.0", | ||||
request: Optional[str] = None, | request: Optional[str] = None, | ||||
# Miscellaneous arguments | # Miscellaneous arguments | ||||
count: int = 1, | count: int = 1, | ||||
dup: bool = False, | dup: bool = False, | ||||
verbose: bool = True, | |||||
) -> subprocess.CompletedProcess: | ) -> subprocess.CompletedProcess: | ||||
"""P I N G E R | """P I N G E R | ||||
Echo reply faker | Echo reply faker | ||||
:param str iface: Interface to send packet to | :param str iface: Interface to send packet to | ||||
:keyword src: Source packet IP | :keyword src: Source packet IP | ||||
:type src: class:`scapy.fields.SourceIPField` | :type src: class:`scapy.fields.SourceIPField` | ||||
Show All 9 Lines | ) -> subprocess.CompletedProcess: | ||||
:keyword flags: IP flags - one of `DF`, `MF` or `evil`, defaults to None | :keyword flags: IP flags - one of `DF`, `MF` or `evil`, defaults to None | ||||
:type flags: class:`scapy.fields.FlagsField`, optional | :type flags: class:`scapy.fields.FlagsField`, optional | ||||
:keyword opts: Include IP options - one of `EOL`, `NOP`, `NOP-40`, `unk`, | :keyword opts: Include IP options - one of `EOL`, `NOP`, `NOP-40`, `unk`, | ||||
`unk-40`, `RR`, `RR-same`, `RR-trunc`, `LSRR`, `LSRR-trunc`, `SSRR` or | `unk-40`, `RR`, `RR-same`, `RR-trunc`, `LSRR`, `LSRR-trunc`, `SSRR` or | ||||
`SSRR-trunc`, defaults to None | `SSRR-trunc`, defaults to None | ||||
:type opts: str, optional | :type opts: str, optional | ||||
:keyword oip_ihl: Inner packet's Internet Header Length, defaults to None | :keyword oip_ihl: Inner packet's Internet Header Length, defaults to None | ||||
:type oip_ihl: class:`scapy.fields.BitField`, optional | :type oip_ihl: class:`scapy.fields.BitField`, optional | ||||
:keyword special: Send a special packet - one of `no-payload`, `tcp`, | :keyword special: Send a special packet - one of `no-payload`, `not-mine`, | ||||
`udp`, `wrong` or `warp`, defaults to None | `tcp`, `udp`, `wrong` or `warp`, defaults to None | ||||
:type special: str, optional | :type special: str, optional | ||||
:keyword icmp_pptr: ICMP pointer, defaults to 0 | :keyword icmp_pptr: ICMP pointer, defaults to 0 | ||||
:type icmp_pptr: class:`scapy.fields.ByteField` | :type icmp_pptr: class:`scapy.fields.ByteField` | ||||
:keyword icmp_gwaddr: ICMP gateway IP address, defaults to "0.0.0.0" | :keyword icmp_gwaddr: ICMP gateway IP address, defaults to "0.0.0.0" | ||||
:type icmp_gwaddr: class:`scapy.fields.IPField` | :type icmp_gwaddr: class:`scapy.fields.IPField` | ||||
:keyword icmp_nextmtu: ICMP next MTU, defaults to 0 | :keyword icmp_nextmtu: ICMP next MTU, defaults to 0 | ||||
:type icmp_nextmtu: class:`scapy.fields.ShortField` | :type icmp_nextmtu: class:`scapy.fields.ShortField` | ||||
:keyword icmp_otime: ICMP originate timestamp, defaults to 0 | :keyword icmp_otime: ICMP originate timestamp, defaults to 0 | ||||
:type icmp_otime: class:`scapy.layers.inet.ICMPTimeStampField` | :type icmp_otime: class:`scapy.layers.inet.ICMPTimeStampField` | ||||
:keyword icmp_rtime: ICMP receive timestamp, defaults to 0 | :keyword icmp_rtime: ICMP receive timestamp, defaults to 0 | ||||
:type icmp_rtime: class:`scapy.layers.inet.ICMPTimeStampField` | :type icmp_rtime: class:`scapy.layers.inet.ICMPTimeStampField` | ||||
:keyword icmp_ttime: ICMP transmit timestamp, defaults to 0 | :keyword icmp_ttime: ICMP transmit timestamp, defaults to 0 | ||||
:type icmp_ttime: class:`scapy.layers.inet.ICMPTimeStampField` | :type icmp_ttime: class:`scapy.layers.inet.ICMPTimeStampField` | ||||
:keyword icmp_mask: ICMP address mask, defaults to "0.0.0.0" | :keyword icmp_mask: ICMP address mask, defaults to "0.0.0.0" | ||||
:type icmp_mask: class:`scapy.fields.IPField` | :type icmp_mask: class:`scapy.fields.IPField` | ||||
:keyword request: Request type - one of `mask` or `timestamp`, | :keyword request: Request type - one of `mask` or `timestamp`, | ||||
defaults to None | defaults to None | ||||
:type request: str, optional | :type request: str, optional | ||||
:keyword count: Number of packets to send, defaults to 1 | :keyword count: Number of packets to send, defaults to 1 | ||||
:type count: int | :type count: int | ||||
:keyword dup: Duplicate packets, defaults to `False` | :keyword dup: Duplicate packets, defaults to `False` | ||||
:type dup: bool | :type dup: bool | ||||
:keyword verbose: Turn on/off verbosity, defaults to `True` | |||||
:type verbose: bool | |||||
:return: A class:`subprocess.CompletedProcess` with the output from the | :return: A class:`subprocess.CompletedProcess` with the output from the | ||||
ping utility | ping utility | ||||
:rtype: class:`subprocess.CompletedProcess` | :rtype: class:`subprocess.CompletedProcess` | ||||
""" | """ | ||||
tun = sc.TunTapInterface(iface) | tun = sc.TunTapInterface(iface) | ||||
subprocess.run(["ifconfig", tun.iface, "up"], check=True) | subprocess.run(["ifconfig", tun.iface, "up"], check=True) | ||||
subprocess.run(["ifconfig", tun.iface, src, dst], check=True) | subprocess.run(["ifconfig", tun.iface, src, dst], check=True) | ||||
ip_opts = generate_ip_options(opts) | ip_opts = generate_ip_options(opts) | ||||
ip = sc.IP(ihl=ihl, flags=flags, src=dst, dst=src, options=ip_opts) | ip = sc.IP(ihl=ihl, flags=flags, src=dst, dst=src, options=ip_opts) | ||||
command = [ | command = [ | ||||
"/sbin/ping", | "/sbin/ping", | ||||
"-c", | "-c", | ||||
str(count), | str(count), | ||||
"-t", | "-t", | ||||
str(count), | str(count), | ||||
"-v", | |||||
] | ] | ||||
if verbose: | |||||
command += ["-v"] | |||||
if request == "mask": | if request == "mask": | ||||
command += ["-Mm"] | command += ["-Mm"] | ||||
if request == "timestamp": | if request == "timestamp": | ||||
command += ["-Mt"] | command += ["-Mt"] | ||||
if special: | if special: | ||||
command += ["-p1"] | command += ["-p1"] | ||||
if opts in [ | if opts in [ | ||||
"RR", | "RR", | ||||
▲ Show 20 Lines • Show All 978 Lines • ▼ Show 20 Lines | |||||
1 packets transmitted, 0 packets received, 100.0% packet loss | 1 packets transmitted, 0 packets received, 100.0% packet loss | ||||
""", | """, | ||||
"stderr": """\ | "stderr": """\ | ||||
ping: quoted data too short (28 bytes) from 192.0.2.2 | ping: quoted data too short (28 bytes) from 192.0.2.2 | ||||
""", | """, | ||||
"redacted": False, | "redacted": False, | ||||
}, | }, | ||||
id="_3_1_special_udp", | id="_3_1_special_udp", | ||||
), | |||||
pytest.param( | |||||
{ | |||||
"src": "192.0.2.1", | |||||
"dst": "192.0.2.2", | |||||
"icmp_type": 3, | |||||
"icmp_code": 1, | |||||
"verbose": False, | |||||
}, | |||||
{ | |||||
"returncode": 2, | |||||
"stdout": """\ | |||||
PING 192.0.2.2 (192.0.2.2): 56 data bytes | |||||
92 bytes from 192.0.2.2: Destination Host Unreachable | |||||
Vr HL TOS Len ID Flg off TTL Pro cks Src Dst | |||||
4 5 00 0054 0001 0 0000 40 01 f6a4 192.0.2.1 192.0.2.2 | |||||
cy: Minor nit: git apply complains about trailing space here. | |||||
Not Done Inline Actionscy: slippy$ git apply /tmp/D39244.diff
/tmp/D39244.diff:71: trailing whitespace.
4 5 00 0054… | |||||
Not Done Inline ActionsThe trailing space is needed, otherwise the test will fail. I have a number of cosmetic changes planned for ping, which remove the trailing space, among other fixes. jlduran_gmail.com: The trailing space is needed, otherwise the test will fail. I have a number of cosmetic changes… | |||||
--- 192.0.2.2 ping statistics --- | |||||
1 packets transmitted, 0 packets received, 100.0% packet loss | |||||
""", | |||||
"stderr": "", | |||||
"redacted": False, | |||||
}, | |||||
id="_3_1_verbose_false", | |||||
), | |||||
pytest.param( | |||||
{ | |||||
"src": "192.0.2.1", | |||||
"dst": "192.0.2.2", | |||||
"icmp_type": 3, | |||||
"icmp_code": 1, | |||||
"special": "not-mine", | |||||
"verbose": False, | |||||
}, | |||||
{ | |||||
"returncode": 2, | |||||
"stdout": """\ | |||||
PATTERN: 0x01 | |||||
PING 192.0.2.2 (192.0.2.2): 56 data bytes | |||||
--- 192.0.2.2 ping statistics --- | |||||
1 packets transmitted, 0 packets received, 100.0% packet loss | |||||
""", | |||||
"stderr": "", | |||||
"redacted": False, | |||||
}, | |||||
id="_3_1_special_not_mine_verbose_false", | |||||
), | ), | ||||
pytest.param( | pytest.param( | ||||
{ | { | ||||
"src": "192.0.2.1", | "src": "192.0.2.1", | ||||
"dst": "192.0.2.2", | "dst": "192.0.2.2", | ||||
"icmp_type": 0, | "icmp_type": 0, | ||||
"icmp_code": 0, | "icmp_code": 0, | ||||
"special": "warp", | "special": "warp", | ||||
Show All 35 Lines |
Minor nit: git apply complains about trailing space here.