linux:如何在没有root权限的情况下获得无线ssid?

npc*_*ode 5 linux ssid iwconfig

有没有办法在没有root权限的情况下获得当前的无线SSID?

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

TOC*_*TOC 5

如果你看一下iwconfig(wireless_tools)的源代码,你会看到这一行:

iwconfig.c:639: if(iw_get_ext(skfd, ifname, SIOCGIWESSID, &wrq) < 0)
Run Code Online (Sandbox Code Playgroud)

该行负责获取ESSID(wireless.h).而且我认为,只有root权限(开箱)要做到这一点,因此函数iw_get_ext(定义在iwlib.hwireless_tools封装),它调用ioctl将返回EPERM(Operation 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));
}
Run Code Online (Sandbox Code Playgroud)

你有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调用是否对此负责:

root做到这一点:

#strace /sbin/iwconfig your_interface_name > strace_iwconfig_root.log
Run Code Online (Sandbox Code Playgroud)

普通用户一样:

$strace /sbin/iwconfig your_interface_name > strace_iwconfig_normal.log
Run Code Online (Sandbox Code Playgroud)

并比较结果.