How to use tcpdump to capture packets in Linux

Source: Internet
Author: User
Next we will introduce tcpdump, an excellent sniffer in linux. (our operations are all tested in the redhat6.2linux2.2.14 environment. The installation of Tcpdump in linux is very simple. Generally, there are two installation methods. An rpm Package is used for installation. Another method is to introduce an excellent sniffer in linux as follows -- Tcpdump. (The following operations are all tested in the redhat 6.2 linux2.2.14 environment.

I. Tcpdump installation
In linux, the installation of tcpdump is very simple. Generally, there are two installation methods. An rpm Package is used for installation. In addition, it is installed in the form of a source program.
1. rpm Package installation
This form of installation is the simplest installation method. The rpm package is to compile the software and package it into a binary format. you can directly install the rpm command without modifying anything. Use the following command to log on as a super user:
In this way, tcpdump is successfully installed on your linux system. It's easy.
2. installation of the source program
Since the rpm Package is easy to install, why should we use a complex source program for installation? In fact, the biggest attraction of linux is that there are a lot of software on it that provides the source program. people can modify the source program to meet their special needs. Therefore, I especially recommend that you install the source program.
The first step is to obtain the source program. in the installation method of the source program, we must first obtain the tcpdump source program distribution package. There are two types of distribution packages:
In form, one is the tarcompressed package (tcpdump-3_4a5.tar. Z), and the other is the rpm distribution package (tcpdump-3_4a5.src.rpm ). These two types
The content in the form is the same. The difference is the compression method. Run the following command to uncompress the tar package:
Run the following command to install the rpm Package:
In this way, extract the source code of tcpdump to the/usr/src/redhat/SOURCES Directory.
Step 2 Prepare the source program for compilation
Before compiling the source program, it is best to confirm that the library file libpcap has been installed. this library file is the library file required by tcpdump software.
. Similarly, you also need a standard C language compiler. In linux, the standard c language compiler is usually gcc.
In the source program directory. One file is Makefile. in. the configure command automatically generates Makefile files from the Makefile. in file.
. In the Makefile. in file, you can modify the BINDEST and MANDEST macro definitions according to the system configuration. the default value is
The first macro value indicates the path name of the binary file where tcpdump is installed, and the second macro value indicates the path name of the tcpdump man help page. you can repair
Modify them to meet system requirements.
Step 3 compile the source program
Use the configure script in the source program directory to read various required attributes from the system. And automatically generated according to the Makefile. in file
Makefile for compilation and use. The make command compiles the tcpdump source program according to the rules in the Makefile file. Use
Install the compiled binary file tcpdump.
To sum up

II. Use of Tcpdump
Tcpdump adopts the command line method. its command format is:
Tcpdump [-adeflnNOpqStvx] [-c quantity] [-F file name
[-I network interface] [-r file name
[-T type] [-w file name] [expression
1. Introduction to tcpdump options
Converts a network address and broadcast address into a name;
The code that matches the information package is given in a compilation format that people can understand;
The code that matches the information package is given in the format of the C language program segment;
The matching information package code is given in decimal format;
Print the header information of the data link layer in the output line;
Print the Internet address in numbers;
Changes the standard output to the buffer row format;
Do not convert the network address into a name;
No timestamp is printed on each output line;
Output a slightly detailed information. for example, the IP package can contain ttl and service type information;
Outputs detailed message information;
After receiving the specified number of packages, tcpdump stops;
Reads expressions from the specified file and ignores other expressions;
Network interface of the listener;
Reads packages from a specified file (these packages are generally generated using the-w option );
Directly write the package into the file, without analysis and printing;
The packets that are listened to are directly interpreted as the specified type of packets. Common types include rpc (remote process call) and snmp (simple
Network Management Protocol ;)
2. Introduction to tcpdump expressions
The expression is a regular expression. tcpdump uses it as a condition for filtering packets. if a packet meets the expression conditions, the packet will be captured. If no conditions are provided, all information packets on the network will be intercepted.
In an expression, the following types of keywords are generally used. one is about the type of keywords, including host, net, and port. for example, host210.27.48.2 indicates that 210.27.48.2 is a host, 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 orsrc, 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 a specific network protocol on FDDI (distributed optical fiber data interface network). In fact, it is an alias of "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 the three types of keywords, other important keywords are as follows: gateway, broadcast, less, greater, and there are three logical operations. The non-operation 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.
(1) all packets received and sent by all hosts 210.27.48.1 are to be intercepted:
(2) to intercept the communication between the host 210.27.48.1 and the host 210.27.48.2 or 210.27.48.3, run the following command: (applicable in the command line)
Must be in parentheses
(3) to obtain an IP packet for all hosts except 210.27.48.1 and 210.27.48.2, run the following command:
(4) to obtain the telnet packet received or sent by the host 210.27.48.1, run the following command:
3. Introduction to output results of tcpdump
Below we will introduce the output information of several typical tcpdump commands.
(1) data link layer header information
Use commands
Ice is a linux host. her MAC address is 0: 90: 27: 58: AF:
H219 is a SUN workstation with SOLARIC. its MAC address is 8: 0: 20: 79: 5B: 46. the output result of the previous command is as follows:
Analysis: 21: 50: 12 indicates the display time, 847509 indicates the ID number, and eth0 indicates the display time. <表示从网络接口eth0 接受该数据包,eth0> The packet sent from the network interface device. 8: 0: 20: 79: 5b: 46 is the MAC address of the host H219. it indicates the packet sent from the source address H219. 0: 90: 27: 58: af: 1a is the MAC address of the host ICE, indicating that the destination address of the data packet is ICE. ip indicates that the data packet is an IP data packet, and 60 indicates the length of the data packet, h219.33357> ice. telnet indicates that the packet is the TELNET (23) Port sent from Port 33357 of host H219 to host ICE. Ack 22535 indicates to respond to a packet whose serial number is 222535. Win 8760 indicates that the size of the sending window is
(2) TCPDUMP output information of ARP packets
Use commands
The output result is:
Analysis: 22:32:42 is the timestamp, 802509 is the ID number, eth0> indicates that the packet is sent from the host, arp indicates that the packet is an ARP Request packet, who-has route tell ice indicates that it is the MAC address of the host ROUTE requested by the host ICE. 0: 90: 27: 58: af: 1a is the MAC address of the host ICE.
(3) TCP packet output information
The common output information of TCP packets captured with TCPDUMP is:
Src> dst: Indicates from the source address to the destination address. flags indicates the flag information in the TCP packet, S indicates the SYN mark, F (FIN), P (PUSH), R (RST )". "(not marked); data-seqno is the sequence number of the data in the data packet, and ack is expected next time
View arp spoofing virus
Because the intranet cannot connect to the Gateway normally, remote login to the gateway is no longer possible. Therefore, all operations are performed locally on the gateway server.
Capture packets on the local port of the server
Capture the packet through the eth1 port and save it as the error. network file.
Intercept ARP packets
'Arp'
Truncate all ARP packets and save them as files
Analyze ARP packets
When the problem arises, we can see that the above IP addresses, including the Gateway IP addresses, all claim that they are located at the MAC address.
Nic for "00: 14: 78: 80; d9: e4"
View the MAC address of the Gateway (192.168.2.11)
The MAC address of the Gateway is 00: 0A: EB: 55: D1: 72, which is inconsistent with the arp-reply package above.
Here, the MAC address of the infected machine can be analyzed as "00: 14: 78: 80: d9: e4"
Accurate identification of viruses
Knowing the MAC address of a computer that may be infected should not be difficult. For small networks, we can query the MAC addresses of each machine, but for large networks, the environment with more than 200 machines is not the most efficient way. Because DHCP is used, I think of DHCP lease records.
Find the IP record corresponding to the MAC address in the/var/lib/dhcp/dhcpd. leases record
Omitted
Omitted
In the dhcpd. leases file, I found the above record
We can see that the MAC address is "00: 14: 78: 80: d9: e4" and the IP address is
The machine name is "ABEAAF6E64884EB"
Here, the machine name and IP address of the infected machine have been detected.

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.