天天看点

以普通用户权限运行tshark

 wireshark的命令行版本tshark是一个很好用的工具,对抓取的数据包有很强的解释分析能力,还有基于协议、时间和会话的统计功能。

tshark抓取数据包需要root权限,但分析保存的数据包则没有必要。更好的解决方法是使用dumpcap(也可以用tcpdump)将要分析的数据包dump出来,使tshark能以普通用户权限载入这些数据包。这样在抓取和分析数据包时,只有dumpcap(或tcpdump)这个程序需要root权限,相对要安全一些。

在linux kernel 2.6.24及以后的版本中,file POSIX capabilities功能已经完全可用,让进程执行中的权限得到更有效的管理,其目标是使进程只拥有能正确执行的必要权限。下面我们会用到这一功能。

如果使用sudo执行tshark,会出现以下提示:

  1. test@localhost:~/test$ sudo tshark -i eth0 
  2. [sudo] password for test:  
  3. Running as user "root" and group "root". This could be dangerous. 
  4. Capturing on eth0 

解决办法如下

1. 确认内核版本和参数支持file POSIX capabilities功能

  1. test@localhost:~/test$ uname -r 
  2. 2.6.32-30-generic 
  3. test@localhost:~/test$ grep FILE_CAPA /boot/config-2.6.32-30-generic  
  4. CONFIG_SECURITY_FILE_CAPABILITIES=y 

2. 安装dumpcap软件

  1. test@localhost:~/test$ sudo apt-get install pcaputils 

3. 创建wireshark用户

  1. test@localhost:~/test$ sudo useradd -s /usr/sbin/nologin wireshark 
  2. test@localhost:~/test$ grep wireshark /etc/passwd 
  3. wireshark:x:1002:1002::/home/wireshark:/usr/sbin/nologin 

4. 更改dumpcap的用户组

  1. test@localhost:~/test$ which dumpcap 
  2. /usr/bin/dumpcap 
  3. test@localhost:~/test$ sudo chgrp wireshark /usr/bin/dumpcap 

5. 更改dumpcap的权限

  1. test@localhost:~/test$ sudo chmod 754 /usr/bin/dumpcap 

6. 设置POSIX capabilities

  1. test@localhost:~/test$ sudo setcap 'CAP_NET_RAW+eip CAP_NET_ADMIN+eip' /usr/bin/dumpcap 

7. 尝试以普通用户权限运行tshark

  1. test@localhost:~/test$ tshark -i eth0 
  2. Capturing on eth0 

wireshark设置的文章来源

 http://wiki.wireshark.org/CaptureSetup/CapturePrivileges

file POSIX capabilities的参考资料

http://www.friedhoff.org/posixfilecaps.html

继续阅读