question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support root-less ICMP pings on MacOS and Linux

See original GitHub issue

Provided the Linux kernel is configured to allow non-raw ICMP for your group ID (on macOS it is available by default, you can use a SOCK_DGRAM socket to ping without being root.

I can demonstrate this on my macOS system:

>>> import os
>>> os.getuid()  # not root
501
>>> os.uname()   # on MacOS
posix.uname_result(sysname='Darwin', nodename='Martijns-MacBook-Pro-2.local', release='17.7.0', version='Darwin Kernel Version 17.7.0: Thu Dec 20 21:47:19 PST 2018; root:xnu-4570.71.22~1/RELEASE_X86_64', machine='x86_64')
>>> import socket, threading, ping3
>>> ping3.DEBUG = True
>>> sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_ICMP)
>>> sock.setsockopt(socket.SOL_IP, socket.IP_TTL, 64)
>>> icmp_id = threading.current_thread().ident % 0xFFFF
>>> if True:
...     ping3.send_one_ping(sock=sock, dest_addr='192.168.1.160', icmp_id=icmp_id, seq=0, size=56)
...     res = ping3.receive_one_ping(sock=sock, icmp_id=icmp_id, seq=0, timeout=4)
...
[DEBUG] IP HEADER: {'version': 69, 'tos': 0, 'len': 14336, 'id': 63036, 'flags': 0, 'ttl': 64, 'protocol': 1, 'checksum': 65435, 'src_addr': 3232235936, 'dest_addr': 3232236008}
[DEBUG] ICMP HEADER: {'type': 0, 'code': 0, 'checksum': 8346, 'id': 61668, 'seq': 0}
>>> res
0.0025510787963867188

Linux enforces a suitable ICMP ID and checksum for you; there is no point in generating your own checksum (it is ignored and re-calculated) and you should not check for the ICMP ID when receiving packets as the kernel routes the packets to your socket based on their internal bookkeeping, instead.

I haven’t found any proof that macOS does this however, so some platform-specific code would be needed to handle this difference. This can be as simple as ignoring the ICMP id when on Linux and not root.

Also see the Linux man 7 icmp documentation.

Could support for this be implemented in ping3?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:8
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
kyan001commented, Aug 25, 2019

Thanks for the information. I will look into this issue and see what I can do with it.

In another hand, I’d love to keep ping3 as an universal solution on all platforms by using python standard libraries. I had to investigate carefully before implementing any platform-specified codes.

1reaction
mjpieterscommented, Mar 31, 2020

@clockzhong please read my post carefully and verify that your kernel and groups have been configured correctly:

Provided the Linux kernel is configured to allow non-raw ICMP for your group ID

Read more comments on GitHub >

github_iconTop Results From Across the Web

How does macOS allow Standard users to ping - Ask Different
In macOS, regular users can ping as well as root. Normally, root access is required to open the raw socket necessary to run...
Read more >
python non-privileged ICMP - ping - Stack Overflow
The ping program is installed setuid root. This allows any user to use the program, and still be able to open a raw...
Read more >
nping(1) - Linux manual page - man7.org
While Nping can be used as a simple ping utility to detect active hosts, it can also be used as a raw packet...
Read more >
Configuring container networking with Podman - Red Hat
This action would allow any process within these groups to send ping packets. To communicate amongst two or more rootless containers, there are ......
Read more >
What allows ping to work for Standard users?
I've noticed on Linux that this usually accomplished by adding extended attributes to the ping program file to allow root-level access for ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found