2012-09-01 133 views
5

有什么方法可以在没有root权限的情况下获取当前的无线SSID?linux:如何在没有root权限的情况下获得无线ssid?

iwconfig告诉我ESSID,但前提是我以root身份运行它。

+0

如果以普通用户身份运行它会发生什么?你使用什么Linux发行版?此外,您可能会发现[unix.se]这类问题的更适合的网站。 –

回答

4

如果你看一看的iwconfigwireless_tools)的源代码,你会看到这样一行:

iwconfig.c:639: if(iw_get_ext(skfd, ifname, SIOCGIWESSID, &wrq) < 0) 

这条线负责ESSID的get(wireless.h)。我认为只有root权限(开箱即可)才能执行此操作,因此调用ioctl的函数iw_get_ext(在iwlib.h包中定义)将返回EPERMOperation not permitted)。

/*------------------------------------------------------------------*/ 
/* 
* Wrapper to extract some Wireless Parameter out of the driver 
*/ 
static inline int 
iw_get_ext(int     skfd,   /* Socket to the kernel */ 
      const char *   ifname,   /* Device name */ 
      int     request,  /* WE ID */ 
      struct iwreq *  pwrq)   /* Fixed part of the request */ 
{ 
    /* Set device name */ 
    strncpy(pwrq->ifr_name, ifname, IFNAMSIZ); 
    /* Do the request */ 
    return(ioctl(skfd, request, pwrq)); 
} 

你有2个解决方案:

  1. 使用setuid,以允许用户使用iwconfig命令:

    sudo chmod u+s /sbin/iwconfig

  2. 您也可以尝试做一些黑客CAP_NET_ADMIN功能,它允许特定用户使用某些特定功能。这里是一些关于CAP_NET_ADMIN链接:

http://packetlife.net/blog/2010/mar/19/sniffing-wireshark-non-root-user/

http://peternixon.net/news/2012/01/28/configure-tcpdump-work-non-root-user-opensuse-using-file-system-capabilities/

http://www.lids.org/lids-howto/node48.html

http://lwn.net/Articles/430462/

最后,你可以使用strace跟踪所有系统调用和确认ioctl呼叫那里sponsible此:

root做到这一点:

#strace /sbin/iwconfig your_interface_name > strace_iwconfig_root.log 

与同为普通用户

$strace /sbin/iwconfig your_interface_name > strace_iwconfig_normal.log 

,并比较结果。

+0

太棒了! chmod u + s或者用CAP_NET_ADMIN和CAP_NET_ADMIN做一些工作都很好。非常感谢你! – npcode

+0

@npcode:好的谢谢,不客气。 – TOC

+0

如果系统上安装了“network-manager”,则可以解决此问题。然后你可以(取决于版本)使用'nm-tool'或'nmctl'。我的地理位置库有示例代码,请参阅:https://github.com/privatwolke/geolocation –

相关问题