基于python模擬TCP3次握手連接及發(fā)送數(shù)據(jù)
源碼如下
from scapy.all import *import logginglogging.getLogger(’scapy.runtime’).setLevel(logging.ERROR)target_ip = ’192.168.1.1’target_port = 80data = ’GET / HTTP/1.0 rnrn’def start_tcp(target_ip,target_port): global sport,s_seq,d_seq #主要是用于TCP3此握手建立連接后繼續(xù)發(fā)送數(shù)據(jù) try: #第一次握手,發(fā)送SYN包 ans = sr1(IP(dst=target_ip)/TCP(dport=target_port,sport=RandShort(),seq=RandInt(),flags=’S’),verbose=False) sport = ans[TCP].dport #源隨機(jī)端口 s_seq = ans[TCP].ack #源序列號(hào)(其實(shí)初始值已經(jīng)被服務(wù)端加1) d_seq = ans[TCP].seq + 1 #確認(rèn)號(hào),需要把服務(wù)端的序列號(hào)加1 #第三次握手,發(fā)送ACK確認(rèn)包 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 ’[-]有錯(cuò)誤,請(qǐng)注意檢查!’ print edef trans_data(target_ip,target_port,data): #先建立TCP連接 start_tcp(target_ip=target_ip,target_port=target_port) #print sport,s_seq,d_seq #發(fā)起GET請(qǐng)求 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() #讀取服務(wù)端發(fā)來(lái)的數(shù)據(jù) rcv = ans[Raw] print rcvif __name__ == ’__main__’: #start_tcp(target_ip,target_port) trans_data(target_ip,target_port,data)
運(yùn)行結(jié)果如下
# 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>�������������������
wireshark抓包截圖如下:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ajax請(qǐng)求添加自定義header參數(shù)代碼2. 基于javascript處理二進(jìn)制圖片流過(guò)程詳解3. Gitlab CI-CD自動(dòng)化部署SpringBoot項(xiàng)目的方法步驟4. ASP基礎(chǔ)知識(shí)VBScript基本元素講解5. 解決android studio引用遠(yuǎn)程倉(cāng)庫(kù)下載慢(JCenter下載慢)6. idea刪除項(xiàng)目的操作方法7. 使用python 計(jì)算百分位數(shù)實(shí)現(xiàn)數(shù)據(jù)分箱代碼8. Kotlin + Flow 實(shí)現(xiàn)Android 應(yīng)用初始化任務(wù)啟動(dòng)庫(kù)9. 詳談ajax返回?cái)?shù)據(jù)成功 卻進(jìn)入error的方法10. html5手機(jī)觸屏touch事件介紹
