Tag: MTU buffer usr DDoS attack ack result add force HTTPS
Write it in front.
has been trying to say that the TCP/IP stack, combined with the network programming in Python, then use the tcpdump command to grab packets to analyze three handshake, connection establishment and four disconnects.
There are too many concepts involved, so this article may be a little bit longer, and trying to tell a simple story about this complex protocol is a small sum.
TCP/IP protocol
Everyone says the internet, so what is the purpose of the Internet? is to view certain information to browse the Web and other content, in other words, to view the resources shared online, which is commonly known as Web resources, then the Web resources are divided into two categories, one is static resources, one is a dynamic resource .
Static resources, such as some HTML pages, JS scripts, CSS styles or some pictures, or some video, these are called static resources, immutable resources.
Dynamic resources, usually the suffix is. jsp,.php,.py files, these files must be compiled by the interpreter before they can be returned to the client, and dynamically generated resources are called dynamic resources.
Access to these resources, we generally through a URL address to access, the only address is to identify the path of the resource , that is, the various domain name address, and when accessing resources, we generally do not access local resources, That is, these resources are stored on someone else's computer or on a single host on the Internet.
so the question is, how do the two hosts communicate and interact with each other?
In the implementation of the program, the general is a reference to the protocol, and then related to the implementation , in the OSI seven layer model is a reference protocol, divided into seven levels, and in the specific implementation of the TCP network model, divided into four layers, The upper three layers of the user space correspond to the application layer of the TCP stack, the transport layer and the network layer are the same, and the data link layer and the physical layer correspond to the driver and the physical hardware of the TCP protocol stack.
A reference model, a specific implementation, the equivalent of the connection server, we use the SSH program, where the protocol used is the SSH protocol, and the specific open source implementation of the software program is OpenSSH, in the data transfer, that is, the The specific implementation of the OSI seven-layer Reference model is the TCP network model .
The application layer, mainly focus on the application layer of details, that is, user space, in the communication details, that is, the kernel of the Linux implementation, that is, kernel space. The application layer is completely user-controlled, can write a variety of applications, the equivalent of the various URLs we visit, all kinds of mobile phone apps, and for the kernel level of things, because it is necessary to communicate with each other, then directly in the kernel implementation, so as to achieve common use.
How does the host communicate between hosts? When accessing a resource, the transport layer is used primarily to provide the port number, which is equivalent to the address of a process, the port number that the application listens to, to distinguish between different applications , and at the network layer, which provides the IP address, which determines which host and which host to communicate , while the physical layer, mainly provides the MAC address, thus identifies the device to the device communication, the device to the device communication and the host to the host communication is different, the host and the host must be two servers, but the device to the device, may be the server, may also be the router, There may be other devices as well.
How is it transmitted at the time of transmission? In the application layer, if the HTTP protocol used, then the application layer to send the data will include the HTTP header and related data information, to the transport layer, the TCP header will be added, where the data is called a segment , segment, to the network layer, the IP header will be added , The data here is called the packet package , to the physical layer, will add the Mac header, that is, the Ethernet header, where the data calledframe, frame, in fact ... Why are so many types, mainly easy to distinguish, in addition to each layer in the different layers of the header information has relevant annotations, which will be divided into different types.
host communication between the host, in essence, is the process and process communication between , socket is an implementation of the IPC mechanism, (Inter process communication) in the implementation of TCP, using sockets, That is, Socket,socket is using a four-tuple, which is the source IP, the source port, the destination IP, the destination port, respectively.
The IP address of the network layer is divided into several types, which are now commonly used as IPV4 addresses, using 32bit for storage, and an IP IPV6 address. IP address is divided into V type, non-Abcde,a class address range is 1-127, where the private address is the 10.0.0.0,b class address of 128-191, where the private address is 172.16.0.0./16-172.31.0.0/ The 16,c class address is 192-223, where the private address is the 192.168.0.0/24-192.168.255.0/24,d class address is 224-239, the multicast address, and the E-class address is 240-254. In the network layer, Python uses the af_inet in the socket module to represent IPv4, uses Af_inet6 to represent IPv6, and uses Af_unix to represent communication between different processes on the same host.
The transport layer is divided into three types, one for TCP, one for UDP, and one for bare , which is represented in Python, Sock_stream, SOCK_DGRAM, packet socket, SOCK_RAW, bare socket, This type of communication is mainly expressed for the network layer directly and the application layer.
The transport layer mainly provides the address of the process, that is, the port number of the process, in the Linux system, the port number can be divided into three, one is a privileged port, that is, 0-1023 ports, these ports are used by specific services, such as SSH service using port 22, The port used by HTTP for 80,telnet uses a port of 21 for 23,ftp, and the boot of these port services must be started with root, and the other port number is 1024-41952, which is the ports that represent some specific services. Can be used by the application, such as Port 3306 for the service of MySQL, 6379 for the Redis service, the last type of port is 4193-65535 for the random port, mainly for the client port , This is the port that this host uses when connecting to the remote host as a client, and in Linux the default range is as follows:
[Email protected] ~]# Cat/proc/sys/net/ipv4/ip_local_port_range
32768 61000
[[email protected] ~]# echo "1025 65535" >/proc/sys/net/ipv4/ip_local_port_range (this port range can be modified)
[Email protected] ~]# Cat/proc/sys/net/ipv4/ip_local_port_range
1025 65535
When using the port, the port number is limited, each client connects in, then always assign a random port to respond to the client's connection, so when the port is allocated, then the host will not be able to provide services, so when the TCP connection, the server can be DDoS attacks, This makes the service status unusable.
In the transport layer, divided into TCP and UDP, for the two protocols, the port number is the same number, are 65535, and in the port allocation, you will also receive the above privileged port restrictions, which is to view some services, while listening to the same port number, but the use of different protocols, For example, FTP will listen for both UDP 21 ports and TCP 21 ports.
The TCP Protocol, Transport Control Protocol, the transmission protocol, is a connection-oriented protocol , that is, in communication, you must first establish a connection, that is, the three-time handshake process, A communication pipeline can then be established to transmit data; a reliable connection , after each sending of the packet, will be confirmed, that is, receive a response message, which contains the identity of the ACK, no boundary protocol, when the TCP transmission message, is a kind of streaming message transmission, as in the telephone, is always said, without labeling I began to speak, to end the speech.
UDP protocol, User Datagram Protocol, Subscriber Packet protocol, UDP protocol for TCP protocol,UDP is a non-connected protocol , that is, in the communication time does not need to establish a connection, unreliable connection , That is, there is no confirmation, the message has been sent out of the tube; it is a boundary agreement that identifies the beginning and end of each message, like when the envelope is mailed, where it will begin and where it ends.
TCP and UDP use of different scenarios, TCP is mainly for the reliability of the birth of data, and UDP is for the birth of speed, TCP in the establishment of the connection and disconnection time, it takes time, but can guarantee the full arrival of data, and UDP is directly sent and accepted, so faster, But it's not that reliable.
In the use of TCP, in order to ensure the reliable arrival of data packets, using a variety of algorithms, such as sliding window algorithm, mainly to ensure that the receiver can receive the transmission of the message, the algorithm can be flow control, such as congestion control algorithm, which is mainly to prevent the network transmission situation, the use of slow-start mode, That is, to start sending a package, when the network is good, each time you send multiple packets to get a response, when the network becomes congested, then send a small number of packets, so as to prevent the network congestion, and when the packet is sent timeout, will be confirmed, and then re-transmitted, so as to ensure the effective arrival of the message Each time a message is sent, there is a checksum to ensure the integrity of the message, and when the packet is unpacked, the header contains the sequence number of the package, which effectively guarantees the order of the packets.
tcpdump Grab Bag
In a Linux system, we can use the command-line tool tcpdump to grab packets, analyze the flow of data, transfer data, and to understand the relevant protocol.
The results of the tcpdump using the grab pack are as follows:
[[email protected] ~]# tcpdump-i eth1 TCP port 8888 (crawl NIC interface eth1, and protocol is TCP and port 8888 data packets)
18:24:55.476709 IP 192.168.249.10.32806 > 192.168.249.236.ddi-tcp-1: Flags [S], seq 3268496798, Win 14600, options [MSS 1460,sackok,ts Val 354009 ECR 0,nop,wscale 6], length 0 (first handshake, the client sends a SYN packet to the service segment, the initial sequence number of the package is 3268496798, the window size is 14600, The maximum packet size is 1460 bytes, the packet length is 0, only in order to establish, client state modification Syn_sent, service side state modified to syn_recive)
18:24:55.476768 IP 192.168.249.236.ddi-tcp-1 > 192.168.249.10.32806:flags [S.], seq 587382068, Ack 3268496799, win 144 The options [MSS 1460,sackok,ts val 101739285 ECR 354009,nop,wscale 5], length 0 (the server sends a SYN packet to the client with an initialization serial number, and a confirmation number is included, Indicates that the first handshake has received a package with a window size of 14480 and a maximum packet size of 1460 bytes, at which time the client's state is modified to established)
18:24:55.477676 IP 192.168.249.10.32806 > 192.168.249.236.ddi-tcp-1: Flags [.], ACK 1, Win 229, options [Nop,nop,ts Val 354010 ECR 101739285], length 0 (the client wants to send a confirmation packet to the server, that is, to send an ACK packet, so that the status of the server is modified to establised)
18:24:55.478488 IP 192.168.249.236.ddi-tcp-1 > 192.168.249.10.32806:flags [P.], seq 1:6, ACK 1, Win 453, options [NOP, Nop,ts Val 101739286 ECR 354010], length 5 (server sends messages to client)
18:24:55.478955 IP 192.168.249.10.32806 > 192.168.249.236.ddi-tcp-1: Flags [.], ACK 6, Win 229, options [Nop,nop,ts Val 354011 ECR 101739286], length 0 (client confirms receipt of message)
18:24:55.479288 IP 192.168.249.10.32806 > 192.168.249.236.ddi-tcp-1: Flags [P.], seq 1:4, ack 6, Win 229, options [NOP, Nop,ts Val 354012 ECR 101739286], length 3 (client sends message to server, length 3)
18:24:55.479508 IP 192.168.249.10.32806 > 192.168.249.236.ddi-tcp-1: Flags [F.], seq 4, Ack 6, Win 229, options [Nop,no P,ts Val 354012 ECR 101739286], length 0 (four disconnects for the first time, client sends fin message to service segment indicating this end, client status modified to FIN_WAIT1)
18:24:55.479909 IP 192.168.249.236.ddi-tcp-1 > 192.168.249.10.32806:flags [.], ACK 4, Win 453, options [Nop,nop,ts Val 101739288 ECR 354012], length 0 (the second time a four-time disconnection, the server sends a confirmation message to the client, indicating a disconnect, and the server state is modified to close_wait)
18:24:55.480162 IP 192.168.249.236.ddi-tcp-1 > 192.168.249.10.32806:flags [F.], seq 6, ACK 5, Win 453, options [Nop,no P,ts Val 101739288 ECR 354012], length 0 (third time four disconnects, service side sends fin message to client, indicates disconnection, client status modified to Fin_wait2)
18:24:55.480455 IP 192.168.249.10.32806 > 192.168.249.236.ddi-tcp-1: Flags [.], ACK 7, Win 229, options [Nop,nop,ts Val 354013 ECR 101739288], length 0 (fourth time of four disconnects, client confirmed disconnect, client, server state modified to time_wait)
The TCP status change graph looks like this:
When the state changes, the main attention to several places:
When the client sends a request to the server and then the server confirms it, the time-out is about a minute or so, which means a DDoS attack (a loss of CPU time and memory) is possible.
In the server, often can see a lot of time_wait state, this is a normal state, about a minute or so.
There are many options available when using tcpdump, and the common options are as follows:
-I: Represents the interface, also the name of the NIC, such as ETH0,ETH1, etc.
TCP/UDP: Represents the Protocol type
Host: The IP that represents the hosts
Port: Indicates port number
NN: Indicates the first IP address and port
-W FileName: Indicates that the result is saved in a file
-SS0: Indicates that information is displayed for all packages
And or not: combination condition
SRC represents source, DST indicates target
For more information, you can view it directly using man.
using Netstat to view TCP status
In the use of sockets, there are two types, one is a listening socket , that is, listen port, a connection socket , that is, a variety of TCP state ports, you can use the command netstat to view, as follows:
[[email protected] ~]# Netstat-tnlp|grep python (listening socket, mainly using the parameter L, which represents the port of listen, where the port listening is 8888)
TCP 0 0 192.168.249.236:8888 0.0.0.0:* LISTEN 1960/python
[[email protected] ~]# netstat-anp|grep 8888 (TCP status of the service end socket)
TCP 0 0 192.168.249.236:8888 192.168.249.10:32807 time_wait
[[email protected] ~]# netstat-anp|grep 8888 (TCP status of client sockets)
TCP 0 0 192.168.249.10:32807 192.168.249.236:8888 time_wait
Python network Programming
The client program for Python is as follows:
[email protected] ~]# cat cli.py
#!/usr/bin/env python
Import socket
Import commands
HOST = "192.168.249.236"
PORT = 8888
BUFSIZE = 1024
Clisock = Socket.socket (socket.af_inet,socket. SOCK_STREAM)
Clisock.connect ((Host,port))
Print Clisock.recv (BUFSIZE)
Clisock.send ("End")
Clisock.close ()
The client's process is as follows: Establish a TCP socket, connect to the server side, receive data sent by the service side, send data to the service side, and close the connection.
The service-side program for Python is as follows:
[email protected] ~]# cat ser.py
#!/usr/bin/env python
Import socket
HOST = "192.168.249.236"
PORT = 8888
BUFSIZE = 1024
Sersock = Socket.socket (socket.af_inet,socket. SOCK_STREAM)
Sersock.bind ((Host,port))
Sersock.listen (5)
While True:
CONN,ADDR = Sersock.accept ()
Conn.send ("Hello")
Print Conn.recv (BUFSIZE)
Conn.close ()
Sersock.close ()
The process of the server is as follows: Establish a TCP socket, register the port used by the binding to the kernel, listen, receive the client connection, send the data to the client, accept the client data, and close the connection.
There are several issues to be aware of when using the socket:
-
A simple socket connection is a single process mode, that is, only one client request can be accepted, cannot support concurrency, and other client requests may be established, but cannot send data, in a blocked state
-
When sending data to the server and the client, it must be one or the other, or it will be blocked.
-
When the application's bufsize is set, if the settings are too small, the data in the buffer may not be fully received, and the loop can be used to receive
-
The party sending the data is too fast, the party receiving the data is too slow, may lead to data confusion, in some cases, to wait for sleep for a period of time
-
The size setting of the buffer is independent of the MTU and is only relevant to the application's acceptability
-
When using the socket, you can simulate the execution of the command, such as executing the associated shell, and can impersonate FTP to transfer files
-
There may be several errors: broken pipe, which indicates that the buffer setting is too small, the data cannot be received; connetion reset by peer means that one party forces an interrupt connection; address already use, the listening port is not released, wait about a minute.
- Public Number:
-
Python Network programming