Simulate TCP3 handshake connection and send data with Python

Source: Internet
Author: User
Tags ack

The source code is as follows:

1  fromScapy.allImport*2 3 4 ImportLogging5Logging.getlogger ('Scapy.runtime'). SetLevel (Logging. ERROR)6 7TARGET_IP ='192.168.1.1'8Target_port = 809data ='get/http/1.0 \r\n\r\n'Ten  One defstart_tcp (target_ip,target_port): A     GlobalSport,s_seq,d_seq#used primarily for TCP3 this handshake to continue sending data after the connection is established -     Try: -         #first handshake, send SYN packet theAns = SR1 (IP (DST=TARGET_IP)/tcp (Dport=target_port,sport=randshort (), Seq=randint (), flags='S'), verbose=False) -Sport = Ans[tcp].dport#Source Random Port -S_seq = Ans[tcp].ack#Source serial Number (in fact the initial value has been added to the server 1) -D_seq = ans[tcp].seq + 1#Confirmation number, you need to add the serial number of the server 1 +         #third handshake, send ACK Confirmation package -Send (IP (DST=TARGET_IP)/tcp (dport=target_port,sport=sport,ack=d_seq,seq=s_seq,flags='A'), verbose=False) +     exceptexception,e: A         Print '[-] There are errors, please pay attention to check! ' at         Printe -  - defTrans_data (target_ip,target_port,data): -     #establish a TCP connection first -START_TCP (target_ip=target_ip,target_port=target_port) -     #Print Sport,s_seq,d_seq in     #initiating a GET request -Ans = SR1 (IP (DST=TARGET_IP)/tcp (dport=target_port,sport=sport,seq=s_seq,ack=d_seq,flags=24)/data,verbose=False) to     #ans.show () +     #read data sent from the service side -RCV =Ans[raw] the     PrintRCV *  $ if __name__=='__main__':Panax Notoginseng     #start_tcp (Target_ip,target_port) -Trans_data (Target_ip,target_port,data)

The results of the operation are as follows:

1 #python exp3.py2<meta http-equiv="Pragma"Content="No-cache">3<meta http-equiv="Expires"Content="Wed, 1997 08:21:57 GMT">4 notsupported</title> notSupported

Wireshark grab bag as follows:

Simulate TCP3 handshake connection and send data with Python

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.