2012-09-01 134 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 –

相關問題