Introduction to Network data collection and analysis tool TCPDUMP

Source: Internet
Author: User
Article title: Introduction to TCPDUMP, a network data collection and analysis tool. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Among today's many hacking technologies, sniffer is the most common and important technology. Anyone who has used the sniffer tool on windows (for example, netxray and sniffer pro) may know that in a shared Lan, using the sniffer tool can provide a clear view of all the traffic in the network! The Sniffer tool is actually a packet capture tool on the network. It can also analyze captured packets. In a shared network, information packets are the network interfaces of all hosts in the network, but before the sniffer tool is used, the network device of the host determines whether the information packet should be received, so that it will discard the information packet that should not be received. the sniffer tool allows the network device of the host to receive all the information packets that have arrived, this achieves the effect of network listening. In fact, the sniffer tool can be used by hackers and is also beneficial to network administrators and network programmers. Network administrators can keep abreast of the actual network conditions by using the sniffer. when network performance drops sharply, you can use the sniffer tool to analyze the causes and find out the source of network congestion. For network programmers, the sniffer tool is used to debug programs.
  
Data collection and analysis is essential when Linux is a network server, especially a router or gateway. So, let's take a look at TcpDump, a powerful network data collection and analysis tool in Linux.
  
Tcpdump is defined as simple as possible: dump the traffice on a network. it is a packet analysis tool that intercepts packets on the network according to the user's definition.
  
As an essential tool for system administrators on the Internet, tcpdump, with its powerful functions and flexible interception policies, becomes one of the essential tools for every senior system administrator to analyze the network and troubleshoot problems.
  
As the name suggests, TcpDump can completely intercept the "header" of the packets transmitted in the network for analysis. It supports filtering network layer, protocol, host, network or port, and provides logical statements such as and, or, not to help you remove useless information.
  
Tcpdump provides source code and open interfaces, so it has high scalability and is a very useful tool for network maintenance and intruders. Tcpdump exists in the basic FreeBSD System. because it needs to set the network interface to the mixed mode, normal users cannot execute normally, however, users with root permissions can directly execute the command to obtain information on the network. Therefore, the network analysis tools in the system are not a threat to the security of the local machine, but a threat to the security of other computers on the network.
  
Under normal circumstances, directly starting tcpdump will monitor all the data packets flowing through the first network interface.
-----------------------
Bash-2.02 # tcpdump
Tcpdump: listening on eth0
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
^ C
------------------------
  
First, we should pay attention to the following output: the output format of tcpdump is: System time source host. Port> target host. Port data packet parameter.
  
   Parameter support for TcpDump
Tcpdump supports many different parameters. for example, you can 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, and use the-w parameter to specify to write the data packets to the file for storage, and so on.
  
However, the more complex tcpdump parameter is used for filtering, because the traffic in the network is large. If no difference is added, all data packets are intercepted, and the data volume is too large, instead, it is difficult to find the required data packets. Filter rules defined by these parameters can be used to intercept specific data packets to narrow down the target so as to better analyze problems in the network. Tcpdump uses parameters to specify the type, address, and port of the data packet to be monitored. based on specific network problems, making full use of these filtering rules can quickly locate faults. Use man tcpdump to view the specific usage of these filter rules.
  
Obviously, this type of network analysis software should not be run on computers that are not used for network management. to shield them, we can shield bpfilter pseudo devices in the kernel. Generally, network hardware and TCP/IP stacks do not support receiving or sending data packets unrelated to the computer. to receive these data packets, you must use the network adapter's hybrid mode, and bypass the standard TCP/IP stack. In FreeBSD, the kernel must support bpfilter, a pseudo-device. Therefore, network analysis tools such as tcpdump can be shielded by canceling bpfilter in the kernel.
  
When the NIC is set to the hybrid mode, the system will leave a record in the console and log files, reminding the administrator to check whether the system is used as a springboard to attack other computers on the same network.
  
May 15 16:27:20 host1/kernel: fxp0: promiscuous mode enabled
  
Although the network analysis tool can record the data transmitted over the network, the data traffic in the network is quite large, how to analyze, classify, and collect statistics on the data, and discover and report errors is more critical. Data packets in the network belong to different protocols, and the formats of data packets in different protocols are also different. Therefore, decoding captured data and displaying packet information as much as possible is more important for protocol analysis tools. The advantage of expensive commercial analysis tools is that they support many types of application layer protocols, not only tcp, udp and other low-layer protocols.
  
According to the output of tcpdump, 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.
  
   TCP functions
   Data Filtering
Without any parameters, TcpDump searches all network interfaces in the system and displays all the data it intercepts. this data is not necessarily all required for us, and too much data is not conducive to analysis. Therefore, we should first think about what data is needed. TcpDump provides the following parameters for us to select data:
  
-B selects the protocol on the data-link layer, including ip, arp, rarp, and ipx.
  
For example, tcpdump-B arp only displays arp in the network, that is, address translation protocol information.
  
-I: Select the network interface to be filtered. if a router has at least two network interfaces, you can use this option to filter only the data passed through the specified interface. For example:
  
Tcpdump-I eth0 only displays all headers on the eth0 interface.
  
The options src, dst, port, host, net, ether, and gateway include additional options such as src, dst, port, host, net, and ehost. They are used to identify the source and destination of data packets. src host 192.168.0.1 specifies that the source host IP address is 192.168.0.1, and dst net 192.168.0.0/24 specifies that the destination is 192.168.0.0. Similarly, the host is related to the specified host, whether it is the source or the target, and the net is related to the specified network. The ether is followed by a physical address instead of an IP address, while the gateway is used for the gateway host. It may be a bit complicated. let's see the following example:
  
Tcpdump src host 192.168.0.1 and dst net 192.168.0.0/24
  
Filters the headers whose source host is 192.168.0.1 and whose destination network is 192.168.0.0.
  
Tcpdump ether src 00: 50: 04: BA: 9B and dst ......
  
Filter the header with the physical address of the source host being XXX (why is there no host or net behind ether src? Of course, the physical address cannot have a network ).
  
Tcpdump src host 192.168.0.1 and dst port not telnet
  
Filter the source host 192.168.0.1 and the target Port are not the telnet header.
  
Ip icmp arp rarp, tcp, udp, icmp, and other options must be placed at the first parameter to filter the data type.
For example:
  
Tcpdump ip src ......
  
Only filter data-IP headers on the link layer.
  
Tcpdump udp and src host 192.168.0.1
  
Only filter all udp headers of the source host 192.168.0.1.
  
Data Display/input/output
  
TcpDump provides enough parameters for us to choose how to process the data, as shown below:
  
-L can redirect data.
  
For example, tcpdump-l> tcpcap.txt stores the obtained data in the tcpcap.txt file.
  
-N does not convert the IP address to the host name.
  
If you do not use this option, TcpDump will convert the IP address to the host name display when a host's host name exists in the system, just like this: eth0 <ntc9.1165> router.domain.net. telnet: eth0 <192.168.0.9.1165> 192.168.0.1.telnet.
  
-Nn does not convert the port name.
  
After using-nn, the above information becomes: eth0 <ntc9.1165> router.domain.net. 23.
  
-N does not print the default domain name.
  
After N, it is eth0 <ntc9.1165> router. telnet.
  
-O does not optimize the matching code.
-T does not print the UNIX timestamp, that is, the time is not displayed.
-Tt prints the original, unformatted time.
-V has a more detailed output than a common TTL and service type.
  
   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
# Rpm-ivh tcpdump-3_4a5.rpm
In this way, tcpdump is successfully installed on your linux system. It's easy.
2. installation of the source program
# Tar xvfz tcpdump-3_4a5.tar.Z
Run the following command to install the rpm Package:
# Rpm-ivh tcpdump-3_4a5.src.rpm
In this way, extract the SOURCE code of tcpdump to/usr/src/redhat/SOURCE.

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.