基于python模拟TCP3次握手连接及发送数据
程序员文章站
2022-08-07 18:16:45
源码如下from scapy.all import *import logginglogging.getlogger('scapy.runtime').setlevel(logging.error)t...
源码如下
from scapy.all import * import logging logging.getlogger('scapy.runtime').setlevel(logging.error) target_ip = '192.168.1.1' target_port = 80 data = 'get / http/1.0 \r\n\r\n' def start_tcp(target_ip,target_port): global sport,s_seq,d_seq #主要是用于tcp3此握手建立连接后继续发送数据 try: #第一次握手,发送syn包 ans = sr1(ip(dst=target_ip)/tcp(dport=target_port,sport=randshort(),seq=randint(),flags='s'),verbose=false) sport = ans[tcp].dport #源随机端口 s_seq = ans[tcp].ack #源序列号(其实初始值已经被服务端加1) d_seq = ans[tcp].seq + 1 #确认号,需要把服务端的序列号加1 #第三次握手,发送ack确认包 send(ip(dst=target_ip)/tcp(dport=target_port,sport=sport,ack=d_seq,seq=s_seq,flags='a'),verbose=false) except exception,e: print '[-]有错误,请注意检查!' print e def trans_data(target_ip,target_port,data): #先建立tcp连接 start_tcp(target_ip=target_ip,target_port=target_port) #print sport,s_seq,d_seq #发起get请求 ans = sr1(ip(dst=target_ip)/tcp(dport=target_port,sport=sport,seq=s_seq,ack=d_seq,flags=24)/data,verbose=false) #ans.show() #读取服务端发来的数据 rcv = ans[raw] print rcv if __name__ == '__main__': #start_tcp(target_ip,target_port) trans_data(target_ip,target_port,data)
运行结果如下
# python exp3.py <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="expires" content="wed, 26 feb 1997 08:21:57 gmt"> <html><head><title>505 http version not supported</title></head><body><center><h1>505 http version not supported</h1></center></body></html>�p�-1���-1��2��2��d��o�p�-1��`��d
wireshark抓包截图如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。