基于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. 詳解Android studio 動(dòng)態(tài)fragment的用法2. 解決Android studio xml界面無(wú)法預(yù)覽問(wèn)題3. 圖文詳解vue中proto文件的函數(shù)調(diào)用4. Spring Boot和Thymeleaf整合結(jié)合JPA實(shí)現(xiàn)分頁(yè)效果(實(shí)例代碼)5. php模擬實(shí)現(xiàn)斗地主發(fā)牌6. 什么是python的自省7. Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)8. vue 使用localstorage實(shí)現(xiàn)面包屑的操作9. .Net Core使用Coravel實(shí)現(xiàn)任務(wù)調(diào)度的完整步驟10. Vuex localStorage的具體使用

網(wǎng)公網(wǎng)安備