Security Research: Network listening Technology in Linux (2)

Source: Internet
Author: User
Article Title: Security Research: Network listening Technology in Linux (2 ). 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.
   Analysis on the monitoring of data frames over Ethernet
  
Data frames over Ethernet mainly involve TCP/IP protocols. The following protocols are analyzed: ip, ARP, RARP, and IPX, with emphasis on IP and arp, these two protocols are the basis of most network protocols. Therefore, the principle and characteristics of most protocols are clearly understood by thoroughly studying them. Since the data frames of different protocols are different, the format analysis of many data frame headers will be described one by one.
  
To listen to the network in linux, you must first set the NIC status so that it is in the mixed mode to listen to all data frames on the network. Then, use Linux socket to intercept data frames. By setting the socket () function parameter value, the socket can intercept unprocessed network data frames. The key is the function parameter settings, the following is the relevant program section:
  
  
If (fd = socket (AF_INET, SOCK_PACKET, htons (0x0003) <0)
  
{Perror ("can get SOCK_PACKET socket
  
");
  
Exit (0 );
  
}
  
AF_INET = 2 indicates internet ip protocol
  
SOCK_PACKET = 10 indicates that the layer of the captured data frame is in the physical layer and is not processed.
  
Htons (0x0003) indicates that the type of the captured data frame is uncertain and all packets are accepted.
  
The general setting is to intercept all data frames on the NIC. In this way, we can intercept the underlying data frame, because the returned data will be a pointer to the data. For convenience of analysis, I have set a basic data frame header structure.
  
Struct etherpacket
  
{Struct ethhdr eth;
  
Struct iphdr ip address;
  
Struct tcphdr tcp;
  
Char buff [8192];
  
} Ep;
  
Assign the returned pointer to the pointer pointing to the data frame header structure and analyze it. The following is the protocol header: ethhdr is the mac header of the Ethernet data frame:
  
--------------------------------------------------------
| 48-bit destination physical address | 48-bit source physical address | 16-bit Protocol address |
--------------------------------------------------------
  
   The corresponding data structure is as follows:
  
Struct ethhdr
  
{
  
Unsigned char h_dest [ETH_ALEN];
  
Unsigned char h_source [ETH_ALEN];
  
Unsigned short h_proto;
  
}
  
H_dest [6] is the physical IP address of the 48-bit target IP address, and h_source [6] is the physical IP address of the 48-bit source IP address. H_proto is a 16-bit Ethernet protocol with 0x0800 ip addresses and 0x80 35 ip addresses. x25, 0x8137 ipx, 0x8863-0x8864 pppoe (this is the Linux ppp), 0x0600 ether _ loop_back, 0x0200-0x0201 pup, etc. Iphdr: This is the IP protocol header:
  
   The structure can be defined as follows:
  
Struct iphdr
  
{
  
# Elif defined (_ LITTLE_ENDIAN_BITFIELD)
  
_ U8 version: 4,
  
# Elif defined (_ BIG_ENDIAN_BITFIELD)
  
_ U8 version: 4,
  
Ihl: 4;
  
# Else
  
# Error "Please fix"
  
# Endif
  
_ U8 tos;
  
_ 16 tot_len;
  
_ B2id;
  
_ 2010frag_off;
  
_ U8 ttl;
  
_ U8 protocol;
  
_ 2010check;
  
_ U32 saddr;
  
_ U32 daddr;
  
};
  
This is the ip protocol header of Linux. Different versions can have different definitions. We generally use BIG definitions in China, where version is the ip version, protocol is an ip protocol classification mainly including 0x06 tcp.0x11 udp, 0x01 icmp, 0x02 igmp, etc. saddr is a 32-bit source ip address, daddr is a 32-bit destination IP address.
  
   Corresponding data structure:
  
Struct arphdr
  
{
  
Unsigned short int ar_hrd;
  
Unsigned short int ar_pro;
  
Unsigned char ar_hln;
  
Unsigned char ar_pln;
  
Unsigned short int ar_op;
  
# If 0
  
Unsigned char _ ar_sha [ETH_ALEN];
  
Unsigned char _ ar_sip [4];
  
Unsigned char _ ar_tha [ETH_ALEN];
  
Unsigned char _ ar_tip [4];
  
# End if
  
};
  
This is the arp protocol header of linux, where ar_hrd is the hardware address format, ar_pro is the Protocol address format, ar_hln is the hardware address length, and ar_pln is the Protocol address length, ar_op is the arp Protocol. 0x001 is arp echo 0x0002 is arp reply. the following are the physical addresses of the source address, the source IP address, the physical address of the target IP address, and the target IP address.
  
   Tcp Header of Tcphdr IP protocol
  
The following figure shows the data structure:
  
Struct tcphdr
  
{
  
U_int16_t source;
  
U_int16_t dest;
  
U_int32_t seq;
  
U_int32_t ack_seq;
  
# If _ BYTE_ORDER = _ LITTLE _ ENDIAN
  
U_int16_t resl: 4;
  
U_int16_t doff: 4;
  
U_int16_t fin: 1;
  
U_int16_t syn: 1;
  
U_int16_t rst: 1;
  
U_int16_t psh: 1;
  
U_int16_t ack: 1;
  
U_int16_t urg: 1;
  
U_int16_t res2: 2;
  
# Elif _ BYTE _ ORDER = _ BIG _ ENDIAN
  
U_int16_t doff: 4;
  
U_int16_t res1: 4;
  
U_int16_t res2: 2;
  
U_int16_t urg: 1;
  
U_int16_t ack: 1;
  
U_int16_t psh: 1;
  
U_int16_t rst: 1;
  
U_int16_t syn: 1;
  
U_int16_t fin: 1;
  
# Else
  
# Error "Adjust your defines"
  
# Endif
  
U_int16_t window;
  
U_int16_t check;
  
U_int16_t urg_ptr;
  
};
  
This is part of the tcp protocol in Linux. It is the same as the IP protocol. The source protocol is the source port, the dest protocol is the destination port, the seq protocol is the s sequence, and the ack_seq protocol is the sequence number, the other are tcp connection flags, which contain Six Flags: syn indicates connection request, urg indicates emergency information, fin indicates connection end, ack indicates connection response, and psh indicates push stack flag, rst indicates that the connection is interrupted. Window indicates the size of the received data window, check indicates the verification code, and urg ptr indicates the emergency pointer.
  
   Udphdr. This is the udp protocol header.
  
Struct udphdr {
  
U_int16_t source;
  
U_int16_t dest;
  
U_int16_t len;
  
U_int16_t check;
  
}
  
This is part of the udp protocol in Linux. The structure is obvious: source Port, dest destination port, len udp length, and check is the verification code.
  
   Icmphdr this is the IP protocol's icmp protocol header
  
Struct icmphdr
  
{
  
U_int8_t type;
  
U_int8_t code;
  
U_int16_t checksum;
  
Union
  
{
  
Struct
  
{
  
U_int16_t id;
  
U_int16_t sequence;
  
} Echo;
  
U_int32_t gateway;
  
Struct
  
{
  
U_int16_t_unused;
  
U_int16_t mtu;
  
} Frag;
  
} Un;
  
};

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.