Tcpdump

1366阅读 0评论2011-10-16 ulovko
分类:LINUX

Cool Intro:

Some people say that “wireshark” is good, and bla bla bla , & bla bla bla…
Yes , wireshark is a great project but when it comes to firewalls, or to real work stuff nothing is like tcpdump.

When you log in to a remote firewall, and want to check out what the hack is going on, tcpdump is your buddy.
It is old and wise, it is the Adam of sniffers, so lets see what we can do with it.


First, we wan to see what interfaces can use for capturing packets
  1. # tcpdump -D
  2. 1.em0
  3. 2.pflog0
  4. 3.lo0
Ok, lets continue our tcpdump trip !
  • -i any : Listen on all interfaces just to see if you're seeing any traffic.
  • -n : Don't resolve hostnames.
  • -nn : Don't resolve hostnames or port names.
  • -X : Show the packet's contents in both hex and ASCII.
  • -XX : Same as -X, but also shows the ethernet header.
  • -v, -vv, -vvv : Increase the amount of packet information you get back.
  • -c : Only get x number of packets and then stop, e.g. 'tcpdump -c 3'
  • -s : The amount of data that is being captured in bytes 
  • (Use -s 1514 to get full coverage, The default snaplength as of tcpdump 4.0 is 96 bytes
  •  PS: Use -s 0 for a snaplength, which gets everything!)
  • -S : Print absolute sequence numbers.
  • -e : Get the ethernet header as well.
  • -q : Show less protocol information.
  • -E : Decrypt IPSEC traffic by providing an encryption key.

Basic Usage
  1. # tcpdump -nS
  2. # tcpdump -nnvvS
  3. # tcpdump -nnvvXS
  4. # tcpdump -nnvvXSs 1514
  5. # tcpdump -nnvvXSs 0 -c2 icmp (Use -s 0 for a snaplength, which gets everything!)

  6. HOST SRC DST NET ETHER
  7. # tcpdump host 1.2.3.4
  8. # tcpdump src 2.3.4.5
  9. # tcpdump dst 3.4.5.6
  10. # tcpdump net 1.2.3.0/24
  11. # tcpdump ether src host 0:a0:3b:3:e1:1d

  12. PROTO PORT
  13. # tcpdump icmp
  14. # tcpdump port 80
  15. # tcpdump src port 1024
  16. # tcpdump sdt port 1024
  17. # tcpdump tcp and src port 80

  18. # tcpdump proto 1
  19. or:
  20. tcpdump icmp
  21. or:
  22. SYNTAX ==> tcpdump '{protocol}[bypass n bytes]={number}'
  23. The available protocols are: ip, tcp, udp, icmp, ether, arp, rarp, and fddi ..
  24. # tcpdump 'ip[9]=1' (IP Header 第10 字节是:协议号ip[9] , icmp协议号为:1 grep icmp /etc/protocols)

  25. PORT RANGES
  26. # tcpdump portrange 21-23

  27. PACKET SIZE FILTER
  28. # tcpdump less 32
  29. # tcpdump greater 128
  30. # tcpdump > 32
  31. # tcpdump <= 128

  32. WRITING TO A FILE / READ
  33. # tcpdump -s 1514 port 80 -w capture_file.pcap
  34. # tcpdump -r capture_file.pcap

  35. GETTING CREATIVE
  36. 1.AND and or &&
  37. 2.OR or or ||
  38. 3.EXCEPT not or !

  39. MORE EXAMPLES
  40. # tcpdump -nnvvS and src 10.5.2.3 and dst port 80
  41. # tcpdump -nvX src net 192.168.0.0/16 and dst net 10.0.0.0/8 or 172.16.0.0/16

  42. # tcpdump -nvvXSs 1514 dst 192.168.0.2 and src net 192.168.1.0/24 and not icmp
  43. # tcpdump -vv src mars and not dst port 22

  44. # tcpdump -ttttnvX not '(port 22 and host spider and host ant)'

  45. GROUPING
  46. # tcpdump 'src 192.168.1.11 and (dst port 80 or 22)'

  47. SPECIALIZED TRAFFIC
  48. # tcpdump ip6 (IPV6 Traffic)
  49. # tcpdump 'tcp[13] = 6' (Packets with both the RST and SYN flags set,WHY?)
  50. # tcpdump 'ip[6] & 128 != 0' (Traffic with the 'Evil Bit' Set!)

  51. # tcpdump 'icmp[0] == 8 or icmp[0] == 0'
  52. or:
  53. # tcpdump 'icmp[icmptype] == icmp-echo or icmp[icmptype] == icmp-echoreply'
  54. Keyword Value
  55. icmp-echoreply 0
  56. icmp-unreach 3
  57. icmp-sourcequench 4
  58. icmp-redirect 5
  59. icmp-echo 8
  60. icmp-routeradvert 9
  61. icmp-routersolicit 10
  62. icmp-timxceed 11
  63. icmp-paramprob 12
  64. icmp-tstamp 13
  65. icmp-tstampreply 14
  66. icmp-ireq 15
  67. icmp-ireqreply 16
  68. icmp-maskreq 17
  69. icmp-maskreply 18

  70. what bastards are bombarding my firewall with junk ?
  71. # tcpdump -i em0 -nq \
  72. not '(port 22 and host spider)' \
  73. and not '(port 53 or 80 or 110 or 119 or 443)' \ (排除对外开放的port)
  74. and dst host 216.209.50.188

  75. what my firewall is actually sending out onto the internet ?
  76. # tcpdump -i em0 -nq \
  77. and not port '(20 or 21 or 25 or 53 or 80 or 110 or 119 or 123 or 443)' \
  78. and not icmp \ (排除对外开放的port)
  79. and src host 216.209.50.188
  1. So as you read the SYN capture tcpdump 'tcp[13] & 2 != 0', you're saying find the 13th 
  2. offset in the TCP header, and only grab packets where the flag in the 2nd bit is not zero.

  3. Capture TCP Flags Using the tcpflags Option...
  4. # tcpdump 'tcp[tcpflags] && tcp-syn != 0'
Timestamps
By default, all output lines are preceded by a timestamp. The timestamp is the current clock time
in the form e.g.,

11:16:02.911079 is the timestamp, in hh:mm:ss:fraction format.

HH :MM :SS :Fraction (分数)

To denote "14 hours, 30 and one half minutes", do not include a seconds figure. Represent it as "14:30,5", "1430,5", "14:30.5", or "1430.5".

14:30.5    --  0.5=1/2 分钟
Protocol Specification
I want only ICMP traffic:
  1. # tcpdump -tttt -nvi em0 icmp (-n:numeric -v:verbose -tttt:readable timestamp)
I want only tcp traffic:
  1. # tcpdump -nnvvi em0 tcp (-nn : Don't resolve hostnames or port names.)
Tcpdump Recipes
Host Names and Addresses: host  net  port
src  dst  dst or src  dst and src
host 192.168.1.1 src net 192.168.100.0/24 udp dst port 53
src 192.168.1.1 dst host 192.168.255.255 tcp src port http
Hardware Addresses: ether dst host ff:ff:ff:ff:ff:ff ether src host 00:f9:06:aa:01:03
Protocols: ip ip6 igmp icmp arp rarp tcp udp
Logical Operations: not is equivalent to ! and is equivalent to && or is equivalent to ||
Other Keywords: gateway broadcast less greater

Using tcpdump:
  1. # tcpdump -Annvvi em0 src net 192.168.1.0/24 and dst host or dst host (检测网站登入情况 -A: ASCII)
Display Captured Packets in HEX and ASCII using tcpdump -XX
  1. # tcpdump -i em0 -XX 
Capture only N number of packets using tcpdump -c
  1. # tcpdump -i em0 -c 2 
Capture the packets and write into a file using tcpdump -w
  1. # tcpdump -i em0 -w 20110101.pcap
Reading the packets from a saved file using tcpdump -r
  1. # tcpdump -tttt -r 20110101.pcap
Read packets longer/lesser than N bytes
  1. # tcpdump -w l_1024.pcap less 1024
  2. # tcpdump -w g_1024.pcap greater 1024

Feel free to contact me if you have a question, a comment, or if you just want to say hello. Until then, I hope you find something here worth your time.

上一篇:FreeBSD Monitoring
下一篇:Basic Wordpress Setup