tcpdump:out.pcap:权限被拒绝


15
[root@localhost ~]# cat /etc/issue
Fedora release 17 (Beefy Miracle)
Kernel \r on an \m (\l)
[root@localhost ~]# uname -a
Linux localhost.localdomain 3.6.10-2.fc17.i686 #1 SMP Tue Dec 11 18:33:15 UTC 2012 i686 i686 i386 GNU/Linux
[root@localhost ~]# tcpdump -i p3p1 -n -w out.pcap -C 16
tcpdump: out.pcap: Permission denied

为什么我会出错?

我该怎么办?

Answers:


21

我在Centos 5上尝试过,即使在tmp或根文件夹上也一样。从tcpdump手册页中,与-Z选项(默认启用)一起使用时,特权会在打开第一个保存文件之前被丢弃。因为您指定了“ -C 1”,所以由于文件大小已经达到1而导致拒绝权限发生,并且在创建新文件时,它将引发一个拒绝权限错误。所以只需指定-Z用户

# strace tcpdump -i eth0 -n -w out.pcap -C 1
fstat(4, {st_mode=S_IFREG|0644, st_size=903, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2aea31934000
lseek(4, 0, SEEK_CUR)                   = 0
read(4, "root:x:0:root\nbin:x:1:root,bin,d"..., 4096) = 903
read(4, "", 4096)                       = 0
close(4)                                = 0
munmap(0x2aea31934000, 4096)            = 0
setgroups(1, [77])                      = 0
setgid(77)                              = 0
setuid(77)                              = 0
setsockopt(3, SOL_SOCKET, SO_ATTACH_FILTER, "\1\0\0\0\0\0\0\0\310\357k\0\0\0\0\0", 16) = 0
fcntl(3, F_GETFL)                       = 0x2 (flags O_RDWR)
fcntl(3, F_SETFL, O_RDWR|O_NONBLOCK)    = 0

recvfrom(3, 0x7fff9563d35f, 1, 32, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
fcntl(3, F_SETFL, O_RDWR)               = 0
setsockopt(3, SOL_SOCKET, SO_ATTACH_FILTER, "\1\0\17\0\0\0\0\0P\327\233\7\0\0\0\0", 16) = 0
open("out.pcap", O_WRONLY|O_CREAT|O_TRUNC, 0666) = -1 EACCES (Permission denied)
write(2, "tcpdump: ", 9tcpdump: )                = 9
write(2, "out.pcap: Permission denied", 27out.pcap: Permission denied) = 27
write(2, "\n", 1
)                       = 1
exit_group(1)                           = ?

您可以在上面看到strace结果,tcpdump将特权放到用户和组pcap中(77)。

# grep 77 /etc/group
pcap:x:77:
# grep 77 /etc/passwd
pcap:x:77:77::/var/arpwatch:/sbin/nologin

从tcpdump手册页中-C

# man tcpdump
       -C     Before writing a raw packet to a savefile, check whether the file is currently larger than file_size and, if so,
              close the current savefile and open a new one.  Savefiles after the first savefile will have the name  specified
              with  the -w flag, with a number after it, starting at 1 and continuing upward.  The units of file_size are mil-
              lions of bytes (1,000,000 bytes, not 1,048,576 bytes).

              **Note that when used with -Z option (enabled by default), privileges are dropped before opening first savefile.**


# tcpdump --help
tcpdump version 3.9.4
libpcap version 0.9.4
Usage: tcpdump [-aAdDeflLnNOpqRStuUvxX] [-c count] [ -C file_size ]
                [ -E algo:secret ] [ -F file ] [ -i interface ] [ -M secret ]
                [ -r file ] [ -s snaplen ] [ -T type ] [ -w file ]
                [ -W filecount ] [ -y datalinktype ] [ -Z user ]
                [ expression ]

用-Z用户指定特定用户

# tcpdump -i eth0 -n -w out.pcap -C 1 -Z root
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
35 packets captured
35 packets received by filter
0 packets dropped by kernel     

8

尝试从/tmp或任何其他可写目录下运行命令。我记得在世界不可写的目录中有tcpdump的问题,我不知道为什么-:)

         cd /tmp
         tcpdump -i p3p1 -n -w out.pcap -C 16 

5

您的tcpdump将特权授予“ tcpdump”用户,检查手册页(“ -Z tcpdump”为默认值,并且tcpdump用户无权写入root的homedir)。正如Daniel T.告诉您的那样,在/ tmp这样的世界可写目录中运行捕获,或者至少在您授予用户或组“ tcpdump”写许可权的目录中运行捕获。


5

当我遇到此Permission denied问题时,事实证明是我.cap在文件上添加了扩展名而不是.pcap。正如RichL在评论中指出的那样,Ubuntu上的AppArmor配置文件/etc/apparmor.d/usr.sbin.tcpdump导致了这种情况。

  # uname -a ; lsb_release -a
  Linux bidder-lb4 3.2.0-76-virtual #111-Ubuntu SMP Tue Jan 13 22:33:42 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
  No LSB modules are available.
  Distributor ID: Ubuntu
  Description:    Ubuntu 12.04.5 LTS
  Release:        12.04
  Codename:       precise

5
事实证明,Ubuntu的AppArmor配置文件(/etc/apparmor.d/usr.sbin.tcpdump)仅允许tcpdump二进制文件写入扩展名为.pcap的文件。默认情况下,AppArmor在Ubuntu中处于启用状态,这使我感到震惊。
Rich L

3

SELinux正在运行吗?通过键入检查终端:

/usr/sbin/getenforce

如果显示,则Enforcing可以尝试禁用SELinux,然后再次尝试tcpdump,以查看SE是否正在停止它。


@Hauke Laging从他的bash输出中,似乎他正在从root的主目录运行命令。
mako_reactor

1

该错误消息对我而言意义不大。SELinux是一个可能的解释。您可以通过strace启动tcpdump来仔细看看发生了什么:

strace tcpdump -i p3p1 -n -w out.pcap -C 16

1

您应该更改运行tcpdump的目录模式。

chmod 777

现在运行命令tcpdump -vv -i any -s0 -w file_name.pcap

它应该工作...!


1
对于“工作”的某些价值。现在,您有一个目录,其中包含数据包捕获,其中可能完全包含暴露的敏感数据,任何有权访问主机的人都可以读取。“嘿医生,我有一个指甲”无论如何完全解决了指甲问题,都不应截肢来回答。
DopeGhoti
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.