How is it possible to run a traceroute-like program without needing root privileges?

五迷三道 提交于 2020-02-03 18:05:14

问题


I have seen another program provide traceroute functionality within it but without needing root (superuser) privileges? I've always assumed that raw sockets need to be root, but is there some other way? (I think somebody mentioned "supertrace" or "tracepath"?) Thanks!


回答1:


Ping the target, gradually increasing the TTL and watching where the "TTL exceeded" responses originate.




回答2:


Rather than using raw sockets, some applications use a higher numbered tcp or udp port. By directing that tcp port at port 80 on a known webserver, you could traceroute to that server. The downside is that you need to know what ports are open on a destination device to tcpping it.




回答3:


You don't need to use raw sockets to send and receive ICMP packets. At least not on Windows.




回答4:


ping and traceroute use the ICMP protocol. Like UDP and TCP this is accessible through the normal sockets API. Only UDP and TCP port numbers less than 1024 are protected from use, other than by root. ICMP is freely available to all users.

If you really want to see how ping and traceroute work you can download an example C code implementation for them from CodeProject.

In short, they simple open an ICMP socket, and traceroute alters the increments the TTL using setsockopt until the target is reached.




回答5:


If you have a modern Linux distro you can look at the source for traceroute (or tracepath, which came about before traceroute went no setuid) and tcptraceroute. None of those require RAW sockets -- checked on Fedora 9, they aren't setuid and work with default options for the normal user.

Using the code that tcptraceroute does might be esp. useful, as ICMP packets to an address will not necessarily end up at the same place as a TCP connection to port 80, for example.

Doing an strace of traceroute (as a normal user) shows it doing something like:


int opt_on  = 1;
int opt_off = 0;

fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)
setsockopt(fd, SOL_IP, IP_MTU_DISCOVER, &opt_off, sizeof int)
setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP, &opt_on, sizeof int)
setsockopt(fd, SOL_IP, IP_RECVTTL, &opt_on, sizeof int)

...and then reading the data out of the CMSG results.



来源:https://stackoverflow.com/questions/76988/how-is-it-possible-to-run-a-traceroute-like-program-without-needing-root-privile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!