Detailed usage of tcpdump

Source: Internet
Author: User

I. Concise usage of tcpdump

Usage: TCPDUMP [-adeflnnopqrstuvxx] [-c count] [-C file_size]
[-F file] [-I interface] [-r file] [-s snaplen]
[-T type] [-u user] [-W file] [-e algo: Secret] [expression]

-C capture a specified number of packets-f use a file as the source of the filter expression-I use an optional Network Interface to capture packets-P prohibit capture in the garbage collection mode,-r read the capture file rather network Interface-W saves the original packet to the file

Tcpdump is a powerful network security analysis tool that saves captured packets on the network to a file for analysis. You can define a filter rule to only intercept data packets of interest to reduce the size of the output file and the loading and processing time of data packet analysis.

This article only covers the basic usage of tcpdump. Remember that tcpdump is much more powerful than what I described.

Network interfaces, ports, and protocols. Suppose you want to intercept the TCP packet of the network interface eth1, port number 6881. Save the data file as test. pcap.

Tcpdump-W test. pcap-I eth1 TCP port 6881

Is it easy? What if we want to intercept packets of UDP port numbers 33210 and 33220 at the same time?

Tcpdump-W test. pcap-I eth1 TCP port 6881 or UDP/(33210 or 33220 /)

'/' Is an escape character, and the logical symbol or is used to add (+. Other expressions are used to intercept TCP packets of port 6881 and UDP packets of port 33210 and 33220. The and operator of the tcpdump filter expression indicates the intersection. Therefore, use or instead of and to intercept UDP packets of port numbers 33210 and 33220. The usage of the and operator is described below.

How to save the file read data packets?

Tcpdump-NNR test. pcap

Option-nn does not convert the network IP address and port number to the name. R (read) reads the package.

You can add the-tttt option to make the timestamp format more readable.

Tcpdump-ttttttnnr test. pcap

How to intercept data for IP addresses?

Do you need to specify the IP type, destination IP address or source IP address to tcpdump? For example, the destination IP address for sniffing is 10.168.28.22 and the TCP port number is 22.

Tcpdump-W test. pcap DST 10.168.28.22 and TCP port 22

The intersection of the destination IP address and port (intersection), using the and operator.

The default value of the sniffing packet is 96 bytes. You can specify-s to change the default value.

Tcpdump-W test. pcap-s 1550 DST 10.168.28.22 and TCP port 22

In some versions, tcpdump allows you to specify the port range. The following command intercepts data for a certain port range.

Tcpdump TCP portrange 20-24

Note that the preceding command does not specify-W to save the captured data packet to a file but directly output it to the screen.

Do not know the port number using tcpdump

The data traffic on the Internet is too large. You can use lsof to search for a specified port. For lsof examples, see monitor who runs what, listen to what ports, and established what connections.

Ii. detailed usage of tcpdump

The first type keyword mainly includes host, net, port, for example host 210.27.48.2. It indicates that 210.27.48.2 is a host, and net 202.0.0.0 indicates that 202.0.0.0 is a network address, port 23 indicates that the port number is 23. If no type is specified, the default type is host.

The second type is the key words for determining the transmission direction, including SRC, DST, DST or SRC, DST and SRC, which indicate the transmission direction. For example, Src 210.27.48.2 indicates that the source address in the IP package is 210.27.48.2, and DST net 202.0.0.0 indicates that the destination network address is 202.0.0.0. If no direction keyword is specified, the SRC or DST keyword is used by default.

The third type is the protocol keyword, which mainly includes FDDI, IP, ARP, RARP, TCP, UDP, and other types. FDDI indicates that it is in FDDI (Distributed Optical Fiber Data Interface Network ?? The specific network protocol is actually an alias for "Ether". FDDI and ether have similar source and destination addresses, therefore, the FDDI protocol package can be processed and analyzed as the ether package. The other keywords indicate the Protocol content of the listener package. If no protocol is specified, tcpdump listens to the information packages of all protocols.
In addition to these three types of keywords, other important keywords include gateway, broadcast, less, greater, and three logical operations. The non-operation type is 'not ''! ', And the operation is 'and',' & '; or the operation is 'or',' │ '; these keywords can be combined to form a powerful combination condition to meet people's needs. The following are several examples.
Under normal circumstances, directly starting tcpdump will monitor all the data packets flowing through the first network interface.
# Tcpdump
Tcpdump: listening on fxp0
11:58:47. 873028 202.102.245.40.netbios-ns> 202.102.245.127.netbios-ns: UDP 50
11:58:47. 974331 0: 10: 7b: 8: 3A: 56> 1: 80: C2: 0: 0: 0 802.1d UI/C Len = 43
0000 0000 0080 0000 1007 cf08 0900 0000
0e80 0000 902b 4695 0980 8701 0014
000f 0000 902b 4695 0008 00
11:58:48. 373134 0: 0: E8: 5b: 6d: 85> broadcast sap E0 UI/C Len = 97
FFFF 0060 0004 FFFF
0452 FFFF 0000 e85b 6d85 4008 0002
0640 4d41 5354 4552 5f57 4542 0000 0000
00, 0000
Use the-I parameter to specify the network interface of the tcpdump listener, which is useful when the computer has multiple network interfaces,
Use the-C parameter to specify the number of data packets to be monitored,
Use the-W parameter to specify to write the listening data packet to the file and save it

A wants to intercept all packets received and sent by all 210.27.48.1 hosts:
# Tcpdump host 210.27.48.1

B. to intercept the communication between host 210.27.48.1 and host 210.27.48.2 or 210.27.48.3, run the following command: (when parentheses are applied in the command line, be sure
# Tcpdump host 210.27.48.1 and (210.27.48.2 or 210.27.48.3)

C If You Want To obtain the IP package for all hosts except 210.27.48.1 and 210.27.48.2, run the following command:
# Tcpdump IP host 210.27.48.1 and! 210.27.48.2

D. To obtain the Telnet packet received or sent by the host 210.27.48.1, run the following command:
# Tcpdump TCP port 23 host 210.27.48.1

E. Monitor the UDP port 123 of the Local Machine. Port 123 is the NTP service port.
# Tcpdump UDP port 123

The F system only monitors the communication data packets of the host named hostname. The host name can be a local host or any computer on the network. The following command reads all data sent by the host hostname:
# Tcpdump-I eth0 SRC host hostname

The command below g can monitor all data packets sent to the host hostname:
# Tcpdump-I eth0 DST host hostname

H We can also monitor the data packets through the specified Gateway:
# Tcpdump-I eth0 gateway gatewayname

If you want to monitor the TCP or UDP data packets destined for the specified port, run the following command:
# Tcpdump-I eth0 host hostname and port 80

J. If you want to obtain an IP packet for all hosts except 210.27.48.1 and 210.27.48.2
, Run the following command:
# Tcpdump IP host 210.27.48.1 and! 210.27.48.2

K. to intercept the communication between host 210.27.48.1 and host 210.27.48.2 or 210.27.48.3, run the following command:
(When using parentheses in the command line, be sure
# Tcpdump host 210.27.48.1 and (210.27.48.2 or 210.27.48.3)

L if you want to obtain an IP packet for all hosts except 210.27.48.1 and 210.27.48.2, run the following command:
# Tcpdump IP host 210.27.48.1 and! 210.27.48.2

M to obtain the Telnet packet received or sent by the host 210.27.48.1, run the following command:
# Tcpdump TCP port 23 host 210.27.48.1

The third type is the protocol keyword, which mainly includes FDDI, IP, ARP, RARP, TCP, UDP, and other types.
In addition to the three types of keywords, other important keywords are as follows: Gateway, broadcast, less,
Greater, there are three logical operations. The non-operation is 'not ''! ', And the operation is 'and',' & '; or the operation is 'o
R', '| ';
The second type is the key words for determining the transmission direction, including SRC, DST, DST or SRC, DST and SRC,
If we only need to list the data packets sent to port 80, use DST port; if we only want to see the data packets returned to port 80, use SRC port.
# Tcpdump-I eth0 host hostname and DST port 80 the destination port is 80
Or
# Tcpdump-I eth0 host hostname and SRC port 80 the source port is 80. Generally, it is the host that provides HTTP Services.
If there are many conditions, add and or not before the conditions.
# Tcpdump-I eth0 host! 211.161.223.70 and! 211.161.223.71 and DST port 80

Logs in the Ethernet mixed mode system are recorded.

May 7 20:03:46 localhost kernel: eth0: promiscuous mode enabled.
May 7 20:03:46 localhost kernel: Device eth0 entered promiscuous mode
May 7 20:03:57 localhost kernel: Device eth0 left promiscuous mode

Tcpdump does not thoroughly decode the intercepted data. Most of the content in the data packet is printed in hexadecimal format. Obviously, this is not conducive to the analysis of network faults. The common solution is to first use tcpdump with the-W parameter to capture data and save it to the file, and then use other programs for decoding and analysis. Of course, filter rules should also be defined to prevent the captured data packets from filling the entire hard disk.

In addition to the filter statement, there is also a very important parameter. That is to say, if this parameter is not set correctly, packet data will be lost!

It is the-S parameter, snaplen, that is, the interception length of the data packet. Man will understand it carefully! The intercepted length is 60 bytes by default, but generally the Ethernet MTU is 1500 bytes. Therefore, when you want to capture packets larger than 60 bytes, using the default parameters will cause packet data loss!

You only need to use-S 0 to capture data by the package length!
 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.